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.commons.lang.exception;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.EOFException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.PrintStream;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: /**
031: * Tests the org.apache.commons.lang.exception.NestableException class.
032: *
033: * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
034: * @version $Id: NestableExceptionTestCase.java 437554 2006-08-28 06:21:41Z bayard $
035: */
036: public class NestableExceptionTestCase extends AbstractNestableTestCase {
037:
038: /**
039: * Construct a new instance of
040: * <code>NestableExceptionTestCase</code>.
041: *
042: * @param name test case name
043: */
044: public NestableExceptionTestCase(String name) {
045: super (name);
046: }
047:
048: /**
049: * Sets up instance variables required by this test case.
050: */
051: public void setUp() {
052: }
053:
054: /**
055: * Returns the test suite
056: *
057: * @return the test suite
058: */
059: public static Test suite() {
060: return new TestSuite(NestableExceptionTestCase.class);
061: }
062:
063: /**
064: * Tears down instance variables required by this test case.
065: */
066: public void tearDown() {
067: }
068:
069: /**
070: * Command line entry point for running the test suite.
071: *
072: * @param args array of command line arguments
073: */
074: public static void main(String args[]) {
075: TestRunner.run(suite());
076: }
077:
078: /**
079: * @see AbstractNestableTestCase#getNestable()
080: */
081: public Nestable getNestable() {
082: return new NestableException();
083: }
084:
085: /**
086: * @see AbstractNestableTestCase#getNestable(Nestable)
087: */
088: public Nestable getNestable(Nestable n) {
089: return new NestableException((Throwable) n);
090: }
091:
092: /**
093: * @see AbstractNestableTestCase#getNestable(String)
094: */
095: public Nestable getNestable(String msg) {
096: return new NestableException(msg);
097: }
098:
099: /**
100: * @see AbstractNestableTestCase#getNestable(Throwable)
101: */
102: public Nestable getNestable(Throwable t) {
103: return new NestableException(t);
104: }
105:
106: /**
107: * @see AbstractNestableTestCase#getNestable(String, Throwable)
108: */
109: public Nestable getNestable(String msg, Throwable t) {
110: return new NestableException(msg, t);
111: }
112:
113: /**
114: * @see AbstractNestableTestCase#getNestable(String, Nestable)
115: */
116: public Nestable getNestable(String msg, Nestable n) {
117: return new NestableException(msg, (Throwable) n);
118: }
119:
120: /**
121: * @see AbstractNestableTestCase#getTester1(Throwable)
122: */
123: public Nestable getTester1(Throwable t) {
124: return new NestableExceptionTester1(t);
125: }
126:
127: /**
128: * @see AbstractNestableTestCase#getTester1(Nestable)
129: */
130: public Nestable getTester1(Nestable n) {
131: return new NestableExceptionTester1((Throwable) n);
132: }
133:
134: /**
135: * @see AbstractNestableTestCase#getTester1(String, Throwable)
136: */
137: public Nestable getTester1(String msg, Throwable t) {
138: return new NestableExceptionTester1(msg, t);
139: }
140:
141: /**
142: * @see AbstractNestableTestCase#getTester1(String, Nestable)
143: */
144: public Nestable getTester1(String msg, Nestable n) {
145: return new NestableExceptionTester1(msg, (Throwable) n);
146: }
147:
148: /**
149: * @see AbstractNestableTestCase#getTester1Class()
150: */
151: public Class getTester1Class() {
152: return NestableExceptionTester1.class;
153: }
154:
155: /**
156: * @see AbstractNestableTestCase#getTester2(String, Throwable)
157: */
158: public Nestable getTester2(String msg, Throwable t) {
159: return new NestableExceptionTester2(msg, t);
160: }
161:
162: /**
163: * @see AbstractNestableTestCase#getTester2(String, Nestable)
164: */
165: public Nestable getTester2(String msg, Nestable n) {
166: return new NestableExceptionTester2(msg, (Throwable) n);
167: }
168:
169: /**
170: * @see AbstractNestableTestCase#getTester2Class()
171: */
172: public Class getTester2Class() {
173: return NestableExceptionTester2.class;
174: }
175:
176: /**
177: * @see AbstractNestableTestCase#getThrowable(String)
178: */
179: public Throwable getThrowable(String msg) {
180: return new EOFException(msg);
181: }
182:
183: /**
184: * @see AbstractNestableTestCase#getThrowableClass()
185: */
186: public Class getThrowableClass() {
187: return EOFException.class;
188: }
189:
190: /**
191: * @see AbstractNestableTestCase#getBaseThrowableClass()
192: */
193: public Class getBaseThrowableClass() {
194: return Exception.class;
195: }
196:
197: public void testSpecificPrintStackTrace() {
198: ByteArrayOutputStream baos = new ByteArrayOutputStream();
199: PrintStream ps = new PrintStream(baos);
200: NestableException ne = new NestableException("outer",
201: new NestableException("inner", new Exception(
202: "another exception")));
203: for (int i = 0; i < 2; i++) {
204: if (i == 0) {
205: // Test printStackTrac()
206: // Replace System.err with our own PrintStream so that we can
207: // obtain and check the printStrackTrace output
208: PrintStream err = System.err;
209: System.setErr(ps);
210: ne.printStackTrace();
211: // Restore the System.err
212: System.setErr(err);
213: } else {
214: // Test printStackTrace(PrintStream)
215: ne.printStackTrace(ps);
216: }
217: }
218: String msg = baos.toString();
219: assertTrue(
220: "printStackTrace() starts with outer message",
221: msg
222: .startsWith("org.apache.commons.lang.exception.NestableException: outer"));
223: assertTrue(
224: "printStackTrace() contains 1st nested message",
225: msg
226: .indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") >= 0);
227: assertTrue(
228: "printStackTrace() contains 2nd nested message",
229: msg
230: .indexOf("Caused by: java.lang.Exception: another exception") >= 0);
231: assertTrue(
232: "printStackTrace() inner message after outer message",
233: msg
234: .indexOf("org.apache.commons.lang.exception.NestableException: outer") < msg
235: .indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner"));
236: assertTrue(
237: "printStackTrace() cause message after inner message",
238: msg
239: .indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") < msg
240: .indexOf("Caused by: java.lang.Exception: another exception"));
241: }
242:
243: public void testSerialization() throws java.io.IOException,
244: ClassNotFoundException {
245: RuntimeException nestedEx = new RuntimeException(
246: "nested exception message");
247: NestableExceptionTester1 ex = new NestableExceptionTester1(
248: "serialization test", nestedEx);
249:
250: assertTrue("implements java.io.Serializable",
251: nestedEx instanceof java.io.Serializable);
252:
253: assertTrue("implements java.io.Serializable",
254: ex instanceof java.io.Serializable);
255:
256: ByteArrayOutputStream baos = new ByteArrayOutputStream();
257: ByteArrayInputStream bais = null;
258: ObjectOutputStream oos = null;
259: ObjectInputStream ois = null;
260:
261: try {
262: oos = new ObjectOutputStream(baos);
263: oos.writeObject(ex);
264: oos.flush();
265: bais = new ByteArrayInputStream(baos.toByteArray());
266: ois = new ObjectInputStream(bais);
267: NestableExceptionTester1 deserializedEx = (NestableExceptionTester1) ois
268: .readObject();
269: assertEquals("getThrowableCount() return value", ex
270: .getThrowableCount(), deserializedEx
271: .getThrowableCount());
272:
273: for (int i = 0; i < ex.getThrowableCount(); i++) {
274: Throwable t = ex.getThrowable(i);
275: Throwable deserializedThrowable = deserializedEx
276: .getThrowable(i);
277:
278: assertEquals(t.getClass(), deserializedThrowable
279: .getClass());
280:
281: assertEquals(t.getMessage(), deserializedThrowable
282: .getMessage());
283: }
284: } finally {
285: if (null != oos) {
286: try {
287: oos.close();
288: } catch (Exception ignored) {
289: // intentionally empty
290: }
291: }
292: }
293:
294: }
295: }
296:
297: /**
298: * First nestable tester implementation for use in test cases.
299: */
300: class NestableExceptionTester1 extends NestableException {
301: public NestableExceptionTester1() {
302: super ();
303: }
304:
305: public NestableExceptionTester1(String reason, Throwable cause) {
306: super (reason, cause);
307: }
308:
309: public NestableExceptionTester1(String reason) {
310: super (reason);
311: }
312:
313: public NestableExceptionTester1(Throwable cause) {
314: super (cause);
315: }
316:
317: }
318:
319: /**
320: * Second nestable tester implementation for use in test cases.
321: */
322: class NestableExceptionTester2 extends NestableException {
323: public NestableExceptionTester2() {
324: super ();
325: }
326:
327: public NestableExceptionTester2(String reason, Throwable cause) {
328: super (reason, cause);
329: }
330:
331: public NestableExceptionTester2(String reason) {
332: super (reason);
333: }
334:
335: public NestableExceptionTester2(Throwable cause) {
336: super(cause);
337: }
338:
339: }
|