1 /*
2 * Copyright 2002-2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.commons.lang.exception;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.EOFException;
20 import java.io.PrintStream;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25
26 /**
27 * Tests the org.apache.commons.lang.exception.NestableRuntimeException class.
28 *
29 * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
30 * @version $Id: NestableRuntimeExceptionTestCase.java 161244 2005-04-14 06:16:36Z ggregory $
31 */
32 public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase {
33
34 /**
35 * Construct a new instance of
36 * <code>NestableRuntimeExceptionTestCase</code>.
37 *
38 * @param name test case name
39 */
40 public NestableRuntimeExceptionTestCase(String name)
41 {
42 super(name);
43 }
44
45 /**
46 * Sets up instance variables required by this test case.
47 */
48 public void setUp()
49 {
50 }
51
52 /**
53 * Returns the test suite
54 *
55 * @return the test suite
56 */
57 public static Test suite()
58 {
59 return new TestSuite(NestableRuntimeExceptionTestCase.class);
60 }
61
62 /**
63 * Tears down instance variables required by this test case.
64 */
65 public void tearDown()
66 {
67 }
68
69 /**
70 * Command line entry point for running the test suite.
71 *
72 * @param args array of command line arguments
73 */
74 public static void main(String args[])
75 {
76 TestRunner.run(suite());
77 }
78
79 /**
80 * @see AbstractNestableTestCase#getNestable()
81 */
82 public Nestable getNestable()
83 {
84 return new NestableRuntimeException();
85 }
86
87 /**
88 * @see AbstractNestableTestCase#getNestable(Nestable)
89 */
90 public Nestable getNestable(Nestable n)
91 {
92 return new NestableRuntimeException((Throwable) n);
93 }
94
95 /**
96 * @see AbstractNestableTestCase#getNestable(String)
97 */
98 public Nestable getNestable(String msg)
99 {
100 return new NestableRuntimeException(msg);
101 }
102
103 /**
104 * @see AbstractNestableTestCase#getNestable(Throwable)
105 */
106 public Nestable getNestable(Throwable t)
107 {
108 return new NestableRuntimeException(t);
109 }
110
111 /**
112 * @see AbstractNestableTestCase#getNestable(String, Throwable)
113 */
114 public Nestable getNestable(String msg, Throwable t)
115 {
116 return new NestableRuntimeException(msg, t);
117 }
118
119 /**
120 * @see AbstractNestableTestCase#getNestable(String, Nestable)
121 */
122 public Nestable getNestable(String msg, Nestable n)
123 {
124 return new NestableRuntimeException(msg, (Throwable) n);
125 }
126
127 /**
128 * @see AbstractNestableTestCase#getTester1(Throwable)
129 */
130 public Nestable getTester1(Throwable t)
131 {
132 return new NestableRuntimeExceptionTester1(t);
133 }
134
135 /**
136 * @see AbstractNestableTestCase#getTester1(Nestable)
137 */
138 public Nestable getTester1(Nestable n)
139 {
140 return new NestableRuntimeExceptionTester1((Throwable) n);
141 }
142
143 /**
144 * @see AbstractNestableTestCase#getTester1(String, Throwable)
145 */
146 public Nestable getTester1(String msg, Throwable t)
147 {
148 return new NestableRuntimeExceptionTester1(msg, t);
149 }
150
151 /**
152 * @see AbstractNestableTestCase#getTester1(String, Nestable)
153 */
154 public Nestable getTester1(String msg, Nestable n)
155 {
156 return new NestableRuntimeExceptionTester1(msg, (Throwable) n);
157 }
158
159 /**
160 * @see AbstractNestableTestCase#getTester1Class()
161 */
162 public Class getTester1Class()
163 {
164 return NestableRuntimeExceptionTester1.class;
165 }
166
167 /**
168 * @see AbstractNestableTestCase#getTester2(String, Throwable)
169 */
170 public Nestable getTester2(String msg, Throwable t)
171 {
172 return new NestableRuntimeExceptionTester2(msg, t);
173 }
174
175 /**
176 * @see AbstractNestableTestCase#getTester2(String, Nestable)
177 */
178 public Nestable getTester2(String msg, Nestable n)
179 {
180 return new NestableRuntimeExceptionTester2(msg, (Throwable) n);
181 }
182
183 /**
184 * @see AbstractNestableTestCase#getTester2Class()
185 */
186 public Class getTester2Class()
187 {
188 return NestableRuntimeExceptionTester2.class;
189 }
190
191 /**
192 * @see AbstractNestableTestCase#getThrowable(String)
193 */
194 public Throwable getThrowable(String msg)
195 {
196 return new EOFException(msg);
197 }
198
199 /**
200 * @see AbstractNestableTestCase#getThrowableClass()
201 */
202 public Class getThrowableClass()
203 {
204 return EOFException.class;
205 }
206
207 /**
208 * @see AbstractNestableTestCase#getBaseThrowableClass()
209 */
210 public Class getBaseThrowableClass()
211 {
212 return RuntimeException.class;
213 }
214
215 public void testSpecificPrintStackTrace()
216 {
217 // Test printStackTrac()
218 // Replace System.err with our own PrintStream so that we can obtain
219 // and check the printStrackTrace output
220 ByteArrayOutputStream baos = new ByteArrayOutputStream();
221 PrintStream ps = new PrintStream(baos);
222 NestableRuntimeException ne = new NestableRuntimeException("outer", new NestableRuntimeException("inner", new Exception("another exception")));
223 for(int i = 0; i < 2; i++)
224 {
225 if(i == 0)
226 {
227 // Test printStackTrac()
228 // Replace System.err with our own PrintStream so that we can
229 // obtain and check the printStrackTrace output
230 PrintStream err = System.err;
231 System.setErr(ps);
232 ne.printStackTrace();
233 // Restore the System.err
234 System.setErr(err);
235 }
236 else
237 {
238 // Test printStackTrace(PrintStream)
239 ne.printStackTrace(ps);
240 }
241 }
242 String msg = baos.toString();
243 assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableRuntimeException: outer"));
244 assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") >= 0);
245 assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0);
246 assertTrue( "printStackTrace() inner message after outer message",
247 msg.indexOf("org.apache.commons.lang.exception.NestableRuntimeException: outer") <
248 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner"));
249 assertTrue( "printStackTrace() cause message after inner message",
250 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") <
251 msg.indexOf("Caused by: java.lang.Exception: another exception"));
252 }
253
254 }
255
256 /**
257 * First nestable tester implementation for use in test cases.
258 */
259 class NestableRuntimeExceptionTester1 extends NestableRuntimeException
260 {
261 public NestableRuntimeExceptionTester1()
262 {
263 super();
264 }
265
266 public NestableRuntimeExceptionTester1(String reason, Throwable cause)
267 {
268 super(reason, cause);
269 }
270
271 public NestableRuntimeExceptionTester1(String reason)
272 {
273 super(reason);
274 }
275
276 public NestableRuntimeExceptionTester1(Throwable cause)
277 {
278 super(cause);
279 }
280
281 }
282
283 /**
284 * Second nestable tester implementation.
285 */
286 class NestableRuntimeExceptionTester2 extends NestableRuntimeException
287 {
288 public NestableRuntimeExceptionTester2()
289 {
290 super();
291 }
292
293 public NestableRuntimeExceptionTester2(String reason, Throwable cause)
294 {
295 super(reason, cause);
296 }
297
298 public NestableRuntimeExceptionTester2(String reason)
299 {
300 super(reason);
301 }
302
303 public NestableRuntimeExceptionTester2(Throwable cause)
304 {
305 super(cause);
306 }
307
308 }
309