KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > TestExceptions


1 /*
2  * TestExceptions.java: JUnit test for ThrowableList implementations
3  *
4  * Copyright (C) 2002 Heiko Blau
5  *
6  * This file belongs to the Susebox Java core test suite.
7  * The Susebox Java core test suite is free software; you can redistribute it
8  * and/or modify it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of the License,
10  * or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with the Susebox Java core test suite. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * The Susebox Java core test suite uses the test framework JUnit by Kent Beck
28  * and Erich Gamma. You should have received a copy of their JUnit licence
29  * agreement along with the Susebox Java test suite.
30  *
31  * We do NOT provide the JUnit archive junit.jar nessecary to compile and run
32  * our tests, since we assume, that You either have it already or would like
33  * to get the current release Yourself.
34  * Please visit either:
35  * http://sourceforge.net/projects/junit
36  * or
37  * http://junit.org
38  * to obtain JUnit.
39  *
40  * Contact:
41  * email: heiko@susebox.de
42  */

43
44 package de.susebox;
45
46 //-----------------------------------------------------------------------------
47
// Imports
48
//
49
import java.lang.reflect.Constructor JavaDoc;
50 import java.text.MessageFormat JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55 import junit.framework.Assert;
56
57 import de.susebox.java.lang.ThrowableList;
58
59
60 //-----------------------------------------------------------------------------
61
// Class TestExceptions
62
//
63

64 /**<p>
65  * This class is a generic test for implementations of the {@link ThrowableList}
66  * interface. Instead of testing each implementation by its own, the common
67  * pattern of all these exceptions is used to define tests on all of them.
68  *</p>
69  *
70  * @see ThrowableList
71  * @author Heiko Blau
72  */

73 public class TestExceptions extends TestCase {
74   
75   //---------------------------------------------------------------------------
76
// properties
77
//
78

79   /**
80    * The class paths of the exceptions that are tested with this unit test.
81    */

82   public static final String JavaDoc[] EXCEPTIONS_TO_TEST = {
83     "de.susebox.java.lang.ExtRuntimeException",
84     "de.susebox.java.lang.ExtIndexOutOfBoundsException",
85     "de.susebox.java.lang.ExtIllegalArgumentException",
86     "de.susebox.java.lang.ExtNoSuchMethodException",
87     "de.susebox.java.lang.ExtNullPointerException",
88     "de.susebox.java.lang.ExtUnsupportedOperationException",
89     "de.susebox.java.io.ExtIOException",
90     "de.susebox.jtopas.TokenizerException",
91   };
92
93   
94   //---------------------------------------------------------------------------
95
// main method
96
//
97

98   /**
99    * Call this method to run this test class.
100    */

101   public static void main(String JavaDoc[] args) {
102     String JavaDoc[] tests = { TestExceptions.class.getName() };
103
104     TestUtilities.run(tests, args);
105   }
106   
107
108   //---------------------------------------------------------------------------
109
// suite method
110
//
111

112   /**
113    * Implementation of the JUnit method <code>suite</code>. For each of the
114    * {@link de.susebox.java.lang.ThrowableList} implementatios one set of test
115    * cases is added to the suite.
116    *
117    * @return a test suite
118    */

119   public static Test suite() {
120     TestSuite suite = new TestSuite(TestExceptions.class.getName());
121     
122     for (int index = 0; index < EXCEPTIONS_TO_TEST.length; ++index) {
123       suite.addTest(new TestExceptions("testWrappedException", EXCEPTIONS_TO_TEST[index]));
124       suite.addTest(new TestExceptions("testNestedExceptions", EXCEPTIONS_TO_TEST[index]));
125       suite.addTest(new TestExceptions("testMessageFormatting", EXCEPTIONS_TO_TEST[index]));
126     }
127     return suite;
128   }
129   
130   
131   //---------------------------------------------------------------------------
132
// Constructor
133
//
134

135   /**
136    * Constructs a test case instance that has a name and is responsible to test
137    * one implementation of the {@link de.susebox.java.lang.ThrowableList}
138    * interface.
139    *
140    * @param test name of the test
141    * @param exToTest class path of the {@link de.susebox.java.lang.ThrowableList} implementation to test
142    */

143   public TestExceptions(String JavaDoc test, String JavaDoc exToTest) {
144     super(test);
145     _classToTest = exToTest;
146   }
147
148   
149   //---------------------------------------------------------------------------
150
// Fixture setup and release
151
//
152

153   /**
154    * Sets up the fixture, for example, open a network connection.
155    * This method is called before a test is executed.
156    *
157    * @throws Exception for anything that might go wrong
158    */

159   protected void setUp() throws Exception JavaDoc {}
160
161   
162   /**
163    * Tears down the fixture, for example, close a network connection.
164    * This method is called after a test is executed.
165    *
166    * @throws Exception for anything that might go wrong
167    */

168   protected void tearDown() throws Exception JavaDoc {}
169   
170   
171   //---------------------------------------------------------------------------
172
// test cases
173
//
174

175   /**
176    * Test wrapped exceptions. The wrapping exception is only a thin layer around
177    * the real one.
178    *
179    * @throws Throwable for anything that might go wrong
180    */

181   public void testWrappedException() throws Throwable JavaDoc {
182     // prerequisitories
183
Class JavaDoc[] paraTypes = new Class JavaDoc[] { new Throwable JavaDoc().getClass() };
184     String JavaDoc msg = "This is an illegal argument.";
185     IllegalArgumentException JavaDoc argEx = new IllegalArgumentException JavaDoc(msg);
186     
187     // construct the exception to test
188
Constructor JavaDoc constr = Class.forName(_classToTest).getConstructor(paraTypes);
189     ThrowableList ex = (ThrowableList)constr.newInstance(new Object JavaDoc[] { argEx } );
190
191     // do the checks
192
assertTrue("rtEx: Wrapper exception not recognized.", ex.isWrapper());
193     assertTrue("rtEx: Didn't retrieve the wrapped exception.", ex.getCause() == argEx);
194     assertTrue("rtEx: Messages not equal.", ((Exception JavaDoc)ex).getMessage().equals(argEx.getMessage()));
195   }
196
197   /**
198    * Test nested exceptions.
199    */

200   public void testNestedExceptions() throws Throwable JavaDoc {
201     // prerequisitories
202
Object JavaDoc[] objArray = new Object JavaDoc[1];
203     String JavaDoc format = "This is exception no {0} of class {1}.";
204     String JavaDoc msg = "Message without format parameters.";
205     IllegalArgumentException JavaDoc argEx = new IllegalArgumentException JavaDoc(msg);
206     Class JavaDoc[] paraTypes = new Class JavaDoc[] { new Throwable JavaDoc().getClass(), msg.getClass(), objArray.getClass() };
207     
208     // construct the exception to test
209
Constructor JavaDoc constr = Class.forName(_classToTest).getConstructor(paraTypes);
210     ThrowableList ex1 = (ThrowableList)constr.newInstance(new Object JavaDoc[] { argEx, format, new Object JavaDoc[] { new Integer JavaDoc(1), _classToTest } } );
211     ThrowableList ex2 = (ThrowableList)constr.newInstance(new Object JavaDoc[] { ex1, format, new Object JavaDoc[] { new Integer JavaDoc(2), _classToTest } } );
212
213     // do the checks
214
assertTrue("ex1: False wrapper exception.", ! ex1.isWrapper());
215     assertTrue("ex2: False wrapper exception.", ! ex2.isWrapper());
216     assertTrue("ex1: Didn't retrieve the nested exception.", ex1.getCause() == argEx);
217     assertTrue("ex2: Didn't retrieve the first nested exception.", ex2.getCause() == ex1);
218     assertTrue("ex2: Didn't retrieve the second nested exception.", ((ThrowableList)ex2.getCause()).getCause() == argEx);
219     assertTrue("ex1: Format not found.", ex1.getFormat() == format);
220     assertTrue("ex2: Format not found.", ex2.getFormat() == format);
221   }
222   
223   /**
224    * Test the message formatting
225    */

226   public void testMessageFormatting() throws Throwable JavaDoc {
227     // prerequisitories
228
String JavaDoc format = "Class {0}, reason \"{1}\", user {2}.";
229     Object JavaDoc[] paras = new Object JavaDoc[] { _classToTest, "bad weather", "myself" };
230     Class JavaDoc[] paraTypes = new Class JavaDoc[] { format.getClass(), paras.getClass() };
231     
232     // construct the exception to test
233
Constructor JavaDoc constr = Class.forName(_classToTest).getConstructor(paraTypes);
234     ThrowableList ex = (ThrowableList)constr.newInstance(new Object JavaDoc[] { format, paras } );
235
236     // do the checks
237
assertTrue("Format not found.", ex.getFormat() == format);
238     
239     String JavaDoc str1 = MessageFormat.format(format, paras);
240     String JavaDoc str2 = ((Exception JavaDoc)ex).getMessage();
241     assertTrue("Formating failed. Expected \"" + str1 + "\", got \"" + str2 + "\".",
242               str1.equals(str2));
243
244   }
245   
246   //---------------------------------------------------------------------------
247
// members
248
//
249
private String JavaDoc _classToTest = null;
250 }
251
Popular Tags