KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > exception > NestableExceptionTestCase


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.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.EOFException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 /**
30  * Tests the org.apache.commons.lang.exception.NestableException class.
31  *
32  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
33  * @version $Id: NestableExceptionTestCase.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35 public class NestableExceptionTestCase extends AbstractNestableTestCase {
36     
37     /**
38      * Construct a new instance of
39      * <code>NestableExceptionTestCase</code>.
40      *
41      * @param name test case name
42      */

43     public NestableExceptionTestCase(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      * Sets up instance variables required by this test case.
50      */

51     public void setUp()
52     {
53     }
54     
55     /**
56      * Returns the test suite
57      *
58      * @return the test suite
59      */

60     public static Test suite()
61     {
62         return new TestSuite(NestableExceptionTestCase.class);
63     }
64     
65     /**
66      * Tears down instance variables required by this test case.
67      */

68     public void tearDown()
69     {
70     }
71     
72     /**
73      * Command line entry point for running the test suite.
74      *
75      * @param args array of command line arguments
76      */

77     public static void main(String JavaDoc args[])
78     {
79         TestRunner.run(suite());
80     }
81     
82     /**
83      * @see AbstractNestableTestCase#getNestable()
84      */

85     public Nestable getNestable()
86     {
87         return new NestableException();
88     }
89     
90     /**
91      * @see AbstractNestableTestCase#getNestable(Nestable)
92      */

93     public Nestable getNestable(Nestable n)
94     {
95         return new NestableException((Throwable JavaDoc) n);
96     }
97     
98     /**
99      * @see AbstractNestableTestCase#getNestable(String)
100      */

101     public Nestable getNestable(String JavaDoc msg)
102     {
103         return new NestableException(msg);
104     }
105     
106     /**
107      * @see AbstractNestableTestCase#getNestable(Throwable)
108      */

109     public Nestable getNestable(Throwable JavaDoc t)
110     {
111         return new NestableException(t);
112     }
113     
114     /**
115      * @see AbstractNestableTestCase#getNestable(String, Throwable)
116      */

117     public Nestable getNestable(String JavaDoc msg, Throwable JavaDoc t)
118     {
119         return new NestableException(msg, t);
120     }
121     
122     /**
123      * @see AbstractNestableTestCase#getNestable(String, Nestable)
124      */

125     public Nestable getNestable(String JavaDoc msg, Nestable n)
126     {
127         return new NestableException(msg, (Throwable JavaDoc) n);
128     }
129     
130     /**
131      * @see AbstractNestableTestCase#getTester1(Throwable)
132      */

133     public Nestable getTester1(Throwable JavaDoc t)
134     {
135         return new NestableExceptionTester1(t);
136     }
137     
138     /**
139      * @see AbstractNestableTestCase#getTester1(Nestable)
140      */

141     public Nestable getTester1(Nestable n)
142     {
143         return new NestableExceptionTester1((Throwable JavaDoc) n);
144     }
145     
146     /**
147      * @see AbstractNestableTestCase#getTester1(String, Throwable)
148      */

149     public Nestable getTester1(String JavaDoc msg, Throwable JavaDoc t)
150     {
151         return new NestableExceptionTester1(msg, t);
152     }
153     
154     /**
155      * @see AbstractNestableTestCase#getTester1(String, Nestable)
156      */

157     public Nestable getTester1(String JavaDoc msg, Nestable n)
158     {
159         return new NestableExceptionTester1(msg, (Throwable JavaDoc) n);
160     }
161     
162     /**
163      * @see AbstractNestableTestCase#getTester1Class()
164      */

165     public Class JavaDoc getTester1Class()
166     {
167         return NestableExceptionTester1.class;
168     }
169     
170     /**
171      * @see AbstractNestableTestCase#getTester2(String, Throwable)
172      */

173     public Nestable getTester2(String JavaDoc msg, Throwable JavaDoc t)
174     {
175         return new NestableExceptionTester2(msg, t);
176     }
177     
178     /**
179      * @see AbstractNestableTestCase#getTester2(String, Nestable)
180      */

181     public Nestable getTester2(String JavaDoc msg, Nestable n)
182     {
183         return new NestableExceptionTester2(msg, (Throwable JavaDoc) n);
184     }
185     
186     /**
187      * @see AbstractNestableTestCase#getTester2Class()
188      */

189     public Class JavaDoc getTester2Class()
190     {
191         return NestableExceptionTester2.class;
192     }
193     
194     /**
195      * @see AbstractNestableTestCase#getThrowable(String)
196      */

197     public Throwable JavaDoc getThrowable(String JavaDoc msg)
198     {
199         return new EOFException JavaDoc(msg);
200     }
201     
202     /**
203      * @see AbstractNestableTestCase#getThrowableClass()
204      */

205     public Class JavaDoc getThrowableClass()
206     {
207         return EOFException JavaDoc.class;
208     }
209
210     /**
211      * @see AbstractNestableTestCase#getBaseThrowableClass()
212      */

213     public Class JavaDoc getBaseThrowableClass()
214     {
215         return Exception JavaDoc.class;
216     }
217     
218     public void testSpecificPrintStackTrace()
219     {
220         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
221         PrintStream JavaDoc ps = new PrintStream JavaDoc(baos);
222         NestableException ne = new NestableException("outer", new NestableException("inner", new Exception JavaDoc("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 JavaDoc 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 JavaDoc msg = baos.toString();
243         assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableException: outer"));
244         assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: 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.NestableException: outer") <
248             msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner"));
249         assertTrue( "printStackTrace() cause message after inner message",
250             msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") <
251             msg.indexOf("Caused by: java.lang.Exception: another exception"));
252     }
253     
254     public void testSerialization()
255         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
256     {
257         RuntimeException JavaDoc nestedEx = new RuntimeException JavaDoc("nested exception message");
258         NestableExceptionTester1 ex = new NestableExceptionTester1("serialization test", nestedEx);
259
260         assertTrue( "implements java.io.Serializable", nestedEx instanceof java.io.Serializable JavaDoc);
261         
262         assertTrue( "implements java.io.Serializable", ex instanceof java.io.Serializable JavaDoc);
263         
264         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
265         ByteArrayInputStream JavaDoc bais = null;
266         ObjectOutputStream JavaDoc oos = null;
267         ObjectInputStream JavaDoc ois = null;
268
269         try
270         {
271             oos = new ObjectOutputStream JavaDoc(baos);
272             oos.writeObject(ex);
273             oos.flush();
274             bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
275             ois = new ObjectInputStream JavaDoc(bais);
276             NestableExceptionTester1 deserializedEx = (NestableExceptionTester1) ois.readObject();
277             assertEquals(
278                     "getThrowableCount() return value",
279                         ex.getThrowableCount(),
280                         deserializedEx.getThrowableCount());
281             
282             for (int i = 0; i < ex.getThrowableCount(); i++)
283             {
284                 Throwable JavaDoc t = ex.getThrowable(i);
285                 Throwable JavaDoc deserializedThrowable = deserializedEx.getThrowable(i);
286                 
287                 assertEquals( t.getClass(),
288                         deserializedThrowable.getClass());
289                         
290                 assertEquals(
291                     t.getMessage(),
292                     deserializedThrowable.getMessage());
293             }
294         }
295         finally
296         {
297             if (null != oos)
298             {
299                 try
300                 {
301                     oos.close();
302                 }
303                 catch (Exception JavaDoc ignored)
304                 {
305                     // intentionally empty
306
}
307             }
308         }
309         
310     }
311 }
312
313 /**
314  * First nestable tester implementation for use in test cases.
315  */

316 class NestableExceptionTester1 extends NestableException
317 {
318     public NestableExceptionTester1()
319     {
320         super();
321     }
322
323     public NestableExceptionTester1(String JavaDoc reason, Throwable JavaDoc cause)
324     {
325         super(reason, cause);
326     }
327     
328     public NestableExceptionTester1(String JavaDoc reason)
329     {
330         super(reason);
331     }
332     
333     public NestableExceptionTester1(Throwable JavaDoc cause)
334     {
335         super(cause);
336     }
337     
338 }
339
340 /**
341  * Second nestable tester implementation for use in test cases.
342  */

343 class NestableExceptionTester2 extends NestableException
344 {
345     public NestableExceptionTester2()
346     {
347         super();
348     }
349     
350     public NestableExceptionTester2(String JavaDoc reason, Throwable JavaDoc cause)
351     {
352         super(reason, cause);
353     }
354     
355     public NestableExceptionTester2(String JavaDoc reason)
356     {
357         super(reason);
358     }
359     
360     public NestableExceptionTester2(Throwable JavaDoc cause)
361     {
362         super(cause);
363     }
364     
365 }
366
367
Popular Tags