001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.tests.javax.naming;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.PrintStream;
023: import java.io.PrintWriter;
024:
025: import javax.naming.CompositeName;
026: import javax.naming.InvalidNameException;
027: import javax.naming.Name;
028: import javax.naming.NamingException;
029:
030: import junit.framework.TestCase;
031: import org.apache.harmony.jndi.tests.javax.naming.util.Log;
032:
033: /**
034: * Tests for NamingException
035: */
036: public class NamingExceptionTest extends TestCase {
037:
038: NamingException ex;
039:
040: static Log log = new Log(NamingExceptionTest.class);
041:
042: /**
043: * Constructor for NamingExceptionTest.
044: *
045: * @param arg0
046: */
047: public NamingExceptionTest(String arg0) {
048: super (arg0);
049: }
050:
051: /*
052: * @see TestCase#setUp()
053: */
054: @Override
055: protected void setUp() throws Exception {
056: super .setUp();
057: ex = new NamingException("test message");
058: }
059:
060: /*
061: * @see TestCase#tearDown()
062: */
063: @Override
064: protected void tearDown() throws Exception {
065: super .tearDown();
066: }
067:
068: public void testAppendRemainingComponent()
069: throws InvalidNameException {
070: log.setMethod("testAppendRemainingComponent");
071: ex.appendRemainingComponent("element");
072: assertEquals(new CompositeName("element"), ex
073: .getRemainingName());
074:
075: ex.setRemainingName(new CompositeName("harmony/apache/org"));
076: ex.appendRemainingComponent("element");
077: assertEquals("harmony/apache/org/element", ex
078: .getRemainingName().toString());
079: }
080:
081: public void testAppendRemainingName() throws InvalidNameException {
082: log.setMethod("testAppendRemainingName");
083: Name name = new CompositeName("element/parent");
084: ex.appendRemainingName(name);
085: assertEquals(name, ex.getRemainingName());
086: assertNotSame(name, ex.getRemainingName());
087: ex.setRemainingName(new CompositeName("harmony/apache/org"));
088: ex.appendRemainingName(new CompositeName("element/parent"));
089: assertEquals(new CompositeName(
090: "harmony/apache/org/element/parent"), ex
091: .getRemainingName());
092: }
093:
094: public void testSetResolvedName() throws InvalidNameException {
095: log.setMethod("testSetResolvedName");
096: Name name = new CompositeName("a/b/c");
097: ex.setResolvedName(name);
098: assertEquals(name, ex.getResolvedName());
099: assertNotSame(name, ex.getResolvedName());
100: }
101:
102: public void testSetResolvedObj() {
103: log.setMethod("testSetResolvedObj");
104: Object o = new Object();
105: ex.setResolvedObj(o);
106: assertEquals(o, ex.getResolvedObj());
107: assertSame(o, ex.getResolvedObj());
108: }
109:
110: /*
111: * Test for String toString()
112: */
113: public void testToString() throws InvalidNameException {
114: String str1 = "test message";
115: String remainName = "harmony/apache/org";
116: String rootCause = "root cause1";
117: String resolvedName = "element/parent";
118: String resovledObj = "this is resolved object";
119: log.setMethod("testToString");
120:
121: assertTrue(ex.toString().indexOf(str1) > 0);
122:
123: ex.setRemainingName(new CompositeName(remainName));
124: String str = ex.toString();
125: assertTrue(0 < str.indexOf(str1)
126: && str.indexOf(str1) < str.indexOf(remainName));
127:
128: ex.setRootCause(new InvalidNameException(rootCause));
129: str = ex.toString();
130: assertTrue(0 < str.indexOf(str1)
131: && str.indexOf(str1) < str.indexOf(rootCause)
132: && str.indexOf(rootCause) < str.indexOf(remainName));
133:
134: ex.setResolvedName(new CompositeName(resolvedName));
135: str = ex.toString();
136: assertTrue(0 < str.indexOf(str1)
137: && str.indexOf(str1) < str.indexOf(rootCause)
138: && str.indexOf(rootCause) < str.indexOf(remainName));
139: assertTrue(0 > str.indexOf(resolvedName));
140:
141: ex.setResolvedObj(resovledObj);
142: str = ex.toString();
143: assertTrue(0 < str.indexOf(str1)
144: && str.indexOf(str1) < str.indexOf(rootCause)
145: && str.indexOf(rootCause) < str.indexOf(remainName));
146: assertTrue(0 > str.indexOf(resovledObj));
147:
148: }
149:
150: /*
151: * Test for String toString(boolean)
152: */
153: public void testToStringboolean() throws InvalidNameException {
154: String str1 = "test message";
155: String remainName = "harmony/apache/org";
156: String rootCause = "root cause1";
157: String resolvedName = "element/parent";
158: String resovledObj = "this is resolved object";
159: log.setMethod("testToStringboolean");
160:
161: String str = ex.toString(true);
162: assertTrue(str.indexOf(str1) > 0);
163:
164: ex.setRemainingName(new CompositeName(remainName));
165: str = ex.toString(true);
166: assertTrue(0 < str.indexOf(str1)
167: && str.indexOf(str1) < str.indexOf(remainName));
168:
169: ex.setRootCause(new InvalidNameException(rootCause));
170: str = ex.toString(true);
171: assertTrue(0 < str.indexOf(str1)
172: && str.indexOf(str1) < str.indexOf(rootCause)
173: && str.indexOf(rootCause) < str.indexOf(remainName));
174:
175: ex.setResolvedName(new CompositeName(resolvedName));
176: str = ex.toString(true);
177: assertTrue(0 < str.indexOf(str1)
178: && str.indexOf(str1) < str.indexOf(rootCause)
179: && str.indexOf(rootCause) < str.indexOf(remainName));
180: assertTrue(0 > str.indexOf(resolvedName));
181:
182: ex.setResolvedObj(resovledObj);
183: str = ex.toString(true);
184: assertTrue(0 < str.indexOf(str1)
185: && str.indexOf(str1) < str.indexOf(rootCause)
186: && str.indexOf(rootCause) < str.indexOf(remainName)
187: && str.indexOf(remainName) < str.indexOf(resovledObj));
188: }
189:
190: public void testSerializationCompatibility() throws Exception {
191: log.setMethod("testSerializationCompatibility()");
192:
193: try {
194: ObjectInputStream in = new ObjectInputStream(
195: getClass()
196: .getResourceAsStream(
197: "/serialization/javax/naming/NamingException.ser"));
198: NamingException ex = (NamingException) in.readObject();
199: assertEquals("test purpsoe", ex.getMessage());
200: assertEquals(new CompositeName("RemainingName"), ex
201: .getRemainingName());
202: assertEquals(new CompositeName("RemainingName"), ex
203: .getResolvedName());
204: assertEquals(new Integer(1), ex.getResolvedObj());
205: assertEquals("root exception", ex.getRootCause()
206: .getMessage());
207: in.close();
208: } catch (Exception e) {
209: log.log(e);
210: throw e;
211: }
212: }
213:
214: public void testPrintStackTrace() throws InvalidNameException,
215: IOException {
216: log.setMethod("testPrintStackTrace");
217: ex.setRemainingName(new CompositeName("element/parent"));
218: ex.setResolvedName(new CompositeName("remained name"));
219: ex.setResolvedObj("resolved obj");
220: PrintStream stdErr = System.err;
221: ByteArrayOutputStream bStream = new ByteArrayOutputStream();
222: PrintStream stream = new PrintStream(bStream);
223: System.setErr(stream);
224:
225: ex.printStackTrace();
226: String trace1 = "element/parent";
227: String trace2 = "root cause1";
228: String str = new String(bStream.toByteArray());
229: assertTrue(str.indexOf(trace1) > 0);
230:
231: bStream.reset();
232:
233: ex.printStackTrace(stream);
234: str = new String(bStream.toByteArray());
235: assertTrue(str.indexOf(trace1) > 0);
236: bStream.reset();
237:
238: ex.printStackTrace(new PrintWriter(stream, true));
239: str = new String(bStream.toByteArray());
240: assertTrue(str.indexOf(trace1) > 0);
241: bStream.reset();
242:
243: ex.setRootCause(new Exception(trace2));
244: ex.printStackTrace();
245: str = new String(bStream.toByteArray());
246: assertTrue(str.indexOf(trace1) > 0);
247: assertTrue(str.indexOf(trace2) > 0);
248: bStream.reset();
249:
250: ex.printStackTrace(stream);
251: str = new String(bStream.toByteArray());
252: assertTrue(str.indexOf(trace1) > 0);
253: assertTrue(str.indexOf(trace2) > 0);
254: bStream.reset();
255:
256: ex.printStackTrace(new PrintWriter(stream, true));
257: str = new String(bStream.toByteArray());
258: assertTrue(str.indexOf(trace1) > 0);
259: assertTrue(str.indexOf(trace2) > 0);
260: bStream.reset();
261:
262: System.setErr(stdErr);
263: stream.close();
264: }
265:
266: public void testSetRemainingName() {
267: NamingException exception = new NamingException("Test");
268: exception.setRemainingName(null);
269: assertNull(exception.getRemainingName());
270: }
271:
272: public void testSetResolvedName2() {
273: NamingException exception = new NamingException("Test");
274: exception.setResolvedName(null);
275: assertNull(exception.getResolvedName());
276: }
277: }
|