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