KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > test > CompositeExceptionTestCase


1 /*
2  * Copyright 2003-2004 The 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.fortress.util.test;
19
20 import junit.framework.TestCase;
21 import org.apache.avalon.fortress.util.CompositeException;
22
23 /**
24  * CompositeExceptionTestCase does XYZ
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  * @version CVS $ Revision: 1.1 $
28  */

29 public class CompositeExceptionTestCase extends TestCase
30 {
31     private Exception JavaDoc[] m_exceptions;
32
33     public CompositeExceptionTestCase( String JavaDoc name )
34     {
35         super( name );
36     }
37
38     public void setUp()
39     {
40         m_exceptions = new Exception JavaDoc[2];
41         m_exceptions[0] = new RuntimeException JavaDoc( "Test1" );
42         m_exceptions[1] = new RuntimeException JavaDoc( "Test2" );
43     }
44
45     public void testRegularCreation()
46     {
47         CompositeException exc = new CompositeException( m_exceptions );
48         assertNotNull( exc );
49         assertNotNull( exc.getMessage() );
50         assertTrue( null == exc.getCause() );
51         assertNotNull( exc.getExceptions() );
52
53         final StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
54         for ( int i = 0; i < m_exceptions.length; i++ )
55         {
56             if ( i > 0 ) msg.append( '\n' );
57             msg.append( m_exceptions[i].getMessage() );
58         }
59         final String JavaDoc message = msg.toString();
60
61         assertEquals( message, exc.getMessage() );
62
63         Exception JavaDoc[] exceptions = exc.getExceptions();
64         assertEquals( m_exceptions.length, exceptions.length );
65
66         for ( int i = 0; i < exceptions.length; i++ )
67         {
68             assertEquals( m_exceptions[i], exceptions[i] );
69         }
70     }
71
72     public void testNestedCreation()
73     {
74         final String JavaDoc message = "Message";
75         CompositeException exc = new CompositeException( m_exceptions, message );
76         assertNotNull( exc );
77         assertNotNull( exc.getMessage() );
78         assertTrue( null == exc.getCause() );
79         assertNotNull( exc.getExceptions() );
80
81         assertEquals( message, exc.getMessage() );
82
83         Exception JavaDoc[] exceptions = exc.getExceptions();
84         assertEquals( m_exceptions.length, exceptions.length );
85
86         for ( int i = 0; i < exceptions.length; i++ )
87         {
88             assertEquals( m_exceptions[i], exceptions[i] );
89         }
90     }
91
92     public void testIllegalArgument()
93     {
94         try
95         {
96             new CompositeException( null );
97             fail( "Did not throw an IllegalArgumentException" );
98         }
99         catch ( IllegalArgumentException JavaDoc iae )
100         {
101             // SUCCESS!!
102
}
103         catch ( Exception JavaDoc e )
104         {
105             fail( "Threw the wrong exception: " + e.getClass().getName() );
106         }
107
108         try
109         {
110             new CompositeException( new Exception JavaDoc[]{} );
111             fail( "Did not throw an IllegalArgumentException" );
112         }
113         catch ( IllegalArgumentException JavaDoc iae )
114         {
115             // SUCCESS!!
116
}
117         catch ( Exception JavaDoc e )
118         {
119             fail( "Threw the wrong exception: " + e.getClass().getName() );
120         }
121     }
122 }
123
Popular Tags