KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > NestedExceptionTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.core;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * @author Rod Johnson
28  * @author Juergen Hoeller
29  */

30 public class NestedExceptionTests extends TestCase {
31
32     public void testNestedRuntimeExceptionWithNoRootCause() {
33         String JavaDoc mesg = "mesg of mine";
34         // Making a class abstract doesn't _really_ prevent instantiation :-)
35
NestedRuntimeException nex = new NestedRuntimeException(mesg) {};
36         assertNull(nex.getCause());
37         assertEquals(nex.getMessage(), mesg);
38
39         // Check printStackTrace
40
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
41         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(baos);
42         nex.printStackTrace(pw);
43         pw.flush();
44         String JavaDoc stackTrace = new String JavaDoc(baos.toByteArray());
45         assertFalse(stackTrace.indexOf(mesg) == -1);
46     }
47
48     public void testNestedRuntimeExceptionWithRootCause() {
49         String JavaDoc myMessage = "mesg for this exception";
50         String JavaDoc rootCauseMesg = "this is the obscure message of the root cause";
51         ServletException JavaDoc rootCause = new ServletException JavaDoc(rootCauseMesg);
52         // Making a class abstract doesn't _really_ prevent instantiation :-)
53
NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {};
54         assertEquals(nex.getCause(), rootCause);
55         assertTrue(nex.getMessage().indexOf(myMessage) != -1);
56         assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);
57
58         // check PrintStackTrace
59
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
60         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(baos);
61         nex.printStackTrace(pw);
62         pw.flush();
63         String JavaDoc stackTrace = new String JavaDoc(baos.toByteArray());
64         assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1);
65         assertFalse(stackTrace.indexOf(rootCauseMesg) == -1);
66     }
67
68     public void testNestedCheckedExceptionWithNoRootCause() {
69         String JavaDoc mesg = "mesg of mine";
70         // Making a class abstract doesn't _really_ prevent instantiation :-)
71
NestedCheckedException nex = new NestedCheckedException(mesg) {};
72         assertNull(nex.getCause());
73         assertEquals(nex.getMessage(), mesg);
74         
75         // Check printStackTrace
76
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
77         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(baos);
78         nex.printStackTrace(pw);
79         pw.flush();
80         String JavaDoc stackTrace = new String JavaDoc(baos.toByteArray());
81         assertFalse(stackTrace.indexOf(mesg) == -1);
82     }
83     
84     public void testNestedCheckedExceptionWithRootCause() {
85         String JavaDoc myMessage = "mesg for this exception";
86         String JavaDoc rootCauseMesg = "this is the obscure message of the root cause";
87         ServletException JavaDoc rootCause = new ServletException JavaDoc(rootCauseMesg);
88         // Making a class abstract doesn't _really_ prevent instantiation :-)
89
NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {};
90         assertEquals(nex.getCause(), rootCause);
91         assertTrue(nex.getMessage().indexOf(myMessage) != -1);
92         assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);
93         
94         // check PrintStackTrace
95
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
96         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(baos);
97         nex.printStackTrace(pw);
98         pw.flush();
99         String JavaDoc stackTrace = new String JavaDoc(baos.toByteArray());
100         assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1);
101         assertFalse(stackTrace.indexOf(rootCauseMesg) == -1);
102     }
103
104 }
105
Popular Tags