KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > exception > ExceptionHelperTest


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.util.exception;
19
20 import junit.framework.TestCase ;
21
22 /**
23  * ExceptionHelper tests.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  * @version $Revision: 1.3 $
27  */

28 public class ExceptionHelperTest extends TestCase
29 {
30     /**
31      * Constructor for ExceptionHelperTest.
32      * @param name
33      */

34     public ExceptionHelperTest( String JavaDoc name )
35     {
36         super( name );
37     }
38
39     final public void testCompositeExceptionReport()
40         throws Exception JavaDoc
41     {
42         Throwable JavaDoc e1 = null;
43         Throwable JavaDoc e2 = null;
44         try
45         {
46             doSomething();
47         }
48         catch( Throwable JavaDoc e )
49         {
50             e1 = e;
51         }
52         try
53         {
54             doSomething();
55         }
56         catch( Throwable JavaDoc e )
57         {
58             e2 = e;
59         }
60         final String JavaDoc message =
61            ExceptionHelper.packException(
62              "Composite exception report",
63              new Throwable JavaDoc[]{ e1, e2 }, false );
64         assertNotNull( message );
65         //System.out.println( message );
66
}
67
68     final public void testExceptionWithMessageReport()
69         throws Exception JavaDoc
70     {
71         try
72         {
73             doSomething();
74         }
75         catch( Throwable JavaDoc e )
76         {
77             final String JavaDoc message =
78               ExceptionHelper.packException( "An error occured.", e );
79             //System.out.println( message );
80
assertNotNull( message );
81         }
82     }
83
84     final public void testExceptionWithStackTrace()
85         throws Exception JavaDoc
86     {
87         try
88         {
89             doSomething();
90         }
91         catch( Throwable JavaDoc e )
92         {
93             final String JavaDoc message =
94               ExceptionHelper.packException( e, true );
95             assertNotNull( message );
96         }
97     }
98
99     final public void testExceptionWithMessageAndStackTrace()
100         throws Exception JavaDoc
101     {
102         try
103         {
104             doSomething();
105         }
106         catch( Throwable JavaDoc e )
107         {
108             final String JavaDoc message =
109               ExceptionHelper.packException( "An error occured.", e, true );
110             assertNotNull( message );
111         }
112     }
113
114     private void doSomething() throws StandardException
115     {
116         try
117         {
118             doSomethingElse();
119         }
120         catch( Throwable JavaDoc e )
121         {
122             final String JavaDoc error =
123               "Unable to do something due to a error condition.";
124             throw new StandardException( error, e );
125         }
126     }
127
128     private void doSomethingElse()
129     {
130         try
131         {
132             causeSomeError();
133         }
134         catch( Throwable JavaDoc e )
135         {
136             final String JavaDoc error =
137               "Unable to do something else due to a error condition.";
138             throw new StandardRuntimeException( error, e );
139         }
140     }
141
142     private void causeSomeError()
143     {
144         final String JavaDoc error =
145           "Raising exception because that's what I'm programmed to do.";
146         throw new StandardError( error );
147     }
148 }
149
Popular Tags