001: /*
002: * TestExceptions.java: JUnit test for ThrowableList implementations
003: *
004: * Copyright (C) 2002 Heiko Blau
005: *
006: * This file belongs to the Susebox Java core test suite.
007: * The Susebox Java core test suite is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of the License,
010: * or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License along
018: * with the Susebox Java core test suite. If not, write to the
019: *
020: * Free Software Foundation, Inc.
021: * 59 Temple Place, Suite 330,
022: * Boston, MA 02111-1307
023: * USA
024: *
025: * or check the Internet: http://www.fsf.org
026: *
027: * The Susebox Java core test suite uses the test framework JUnit by Kent Beck
028: * and Erich Gamma. You should have received a copy of their JUnit licence
029: * agreement along with the Susebox Java test suite.
030: *
031: * We do NOT provide the JUnit archive junit.jar nessecary to compile and run
032: * our tests, since we assume, that You either have it already or would like
033: * to get the current release Yourself.
034: * Please visit either:
035: * http://sourceforge.net/projects/junit
036: * or
037: * http://junit.org
038: * to obtain JUnit.
039: *
040: * Contact:
041: * email: heiko@susebox.de
042: */
043:
044: package de.susebox;
045:
046: //-----------------------------------------------------------------------------
047: // Imports
048: //
049: import java.lang.reflect.Constructor;
050: import java.text.MessageFormat;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055: import junit.framework.Assert;
056:
057: import de.susebox.java.lang.ThrowableList;
058:
059: //-----------------------------------------------------------------------------
060: // Class TestExceptions
061: //
062:
063: /**<p>
064: * This class is a generic test for implementations of the {@link ThrowableList}
065: * interface. Instead of testing each implementation by its own, the common
066: * pattern of all these exceptions is used to define tests on all of them.
067: *</p>
068: *
069: * @see ThrowableList
070: * @author Heiko Blau
071: */
072: public class TestExceptions extends TestCase {
073:
074: //---------------------------------------------------------------------------
075: // properties
076: //
077:
078: /**
079: * The class paths of the exceptions that are tested with this unit test.
080: */
081: public static final String[] EXCEPTIONS_TO_TEST = {
082: "de.susebox.java.lang.ExtRuntimeException",
083: "de.susebox.java.lang.ExtIndexOutOfBoundsException",
084: "de.susebox.java.lang.ExtIllegalArgumentException",
085: "de.susebox.java.lang.ExtNoSuchMethodException",
086: "de.susebox.java.lang.ExtNullPointerException",
087: "de.susebox.java.lang.ExtUnsupportedOperationException",
088: "de.susebox.java.io.ExtIOException",
089: "de.susebox.jtopas.TokenizerException", };
090:
091: //---------------------------------------------------------------------------
092: // main method
093: //
094:
095: /**
096: * Call this method to run this test class.
097: */
098: public static void main(String[] args) {
099: String[] tests = { TestExceptions.class.getName() };
100:
101: TestUtilities.run(tests, args);
102: }
103:
104: //---------------------------------------------------------------------------
105: // suite method
106: //
107:
108: /**
109: * Implementation of the JUnit method <code>suite</code>. For each of the
110: * {@link de.susebox.java.lang.ThrowableList} implementatios one set of test
111: * cases is added to the suite.
112: *
113: * @return a test suite
114: */
115: public static Test suite() {
116: TestSuite suite = new TestSuite(TestExceptions.class.getName());
117:
118: for (int index = 0; index < EXCEPTIONS_TO_TEST.length; ++index) {
119: suite.addTest(new TestExceptions("testWrappedException",
120: EXCEPTIONS_TO_TEST[index]));
121: suite.addTest(new TestExceptions("testNestedExceptions",
122: EXCEPTIONS_TO_TEST[index]));
123: suite.addTest(new TestExceptions("testMessageFormatting",
124: EXCEPTIONS_TO_TEST[index]));
125: }
126: return suite;
127: }
128:
129: //---------------------------------------------------------------------------
130: // Constructor
131: //
132:
133: /**
134: * Constructs a test case instance that has a name and is responsible to test
135: * one implementation of the {@link de.susebox.java.lang.ThrowableList}
136: * interface.
137: *
138: * @param test name of the test
139: * @param exToTest class path of the {@link de.susebox.java.lang.ThrowableList} implementation to test
140: */
141: public TestExceptions(String test, String exToTest) {
142: super (test);
143: _classToTest = exToTest;
144: }
145:
146: //---------------------------------------------------------------------------
147: // Fixture setup and release
148: //
149:
150: /**
151: * Sets up the fixture, for example, open a network connection.
152: * This method is called before a test is executed.
153: *
154: * @throws Exception for anything that might go wrong
155: */
156: protected void setUp() throws Exception {
157: }
158:
159: /**
160: * Tears down the fixture, for example, close a network connection.
161: * This method is called after a test is executed.
162: *
163: * @throws Exception for anything that might go wrong
164: */
165: protected void tearDown() throws Exception {
166: }
167:
168: //---------------------------------------------------------------------------
169: // test cases
170: //
171:
172: /**
173: * Test wrapped exceptions. The wrapping exception is only a thin layer around
174: * the real one.
175: *
176: * @throws Throwable for anything that might go wrong
177: */
178: public void testWrappedException() throws Throwable {
179: // prerequisitories
180: Class[] paraTypes = new Class[] { new Throwable().getClass() };
181: String msg = "This is an illegal argument.";
182: IllegalArgumentException argEx = new IllegalArgumentException(
183: msg);
184:
185: // construct the exception to test
186: Constructor constr = Class.forName(_classToTest)
187: .getConstructor(paraTypes);
188: ThrowableList ex = (ThrowableList) constr
189: .newInstance(new Object[] { argEx });
190:
191: // do the checks
192: assertTrue("rtEx: Wrapper exception not recognized.", ex
193: .isWrapper());
194: assertTrue("rtEx: Didn't retrieve the wrapped exception.", ex
195: .getCause() == argEx);
196: assertTrue("rtEx: Messages not equal.", ((Exception) ex)
197: .getMessage().equals(argEx.getMessage()));
198: }
199:
200: /**
201: * Test nested exceptions.
202: */
203: public void testNestedExceptions() throws Throwable {
204: // prerequisitories
205: Object[] objArray = new Object[1];
206: String format = "This is exception no {0} of class {1}.";
207: String msg = "Message without format parameters.";
208: IllegalArgumentException argEx = new IllegalArgumentException(
209: msg);
210: Class[] paraTypes = new Class[] { new Throwable().getClass(),
211: msg.getClass(), objArray.getClass() };
212:
213: // construct the exception to test
214: Constructor constr = Class.forName(_classToTest)
215: .getConstructor(paraTypes);
216: ThrowableList ex1 = (ThrowableList) constr
217: .newInstance(new Object[] { argEx, format,
218: new Object[] { new Integer(1), _classToTest } });
219: ThrowableList ex2 = (ThrowableList) constr
220: .newInstance(new Object[] { ex1, format,
221: new Object[] { new Integer(2), _classToTest } });
222:
223: // do the checks
224: assertTrue("ex1: False wrapper exception.", !ex1.isWrapper());
225: assertTrue("ex2: False wrapper exception.", !ex2.isWrapper());
226: assertTrue("ex1: Didn't retrieve the nested exception.", ex1
227: .getCause() == argEx);
228: assertTrue("ex2: Didn't retrieve the first nested exception.",
229: ex2.getCause() == ex1);
230: assertTrue("ex2: Didn't retrieve the second nested exception.",
231: ((ThrowableList) ex2.getCause()).getCause() == argEx);
232: assertTrue("ex1: Format not found.", ex1.getFormat() == format);
233: assertTrue("ex2: Format not found.", ex2.getFormat() == format);
234: }
235:
236: /**
237: * Test the message formatting
238: */
239: public void testMessageFormatting() throws Throwable {
240: // prerequisitories
241: String format = "Class {0}, reason \"{1}\", user {2}.";
242: Object[] paras = new Object[] { _classToTest, "bad weather",
243: "myself" };
244: Class[] paraTypes = new Class[] { format.getClass(),
245: paras.getClass() };
246:
247: // construct the exception to test
248: Constructor constr = Class.forName(_classToTest)
249: .getConstructor(paraTypes);
250: ThrowableList ex = (ThrowableList) constr
251: .newInstance(new Object[] { format, paras });
252:
253: // do the checks
254: assertTrue("Format not found.", ex.getFormat() == format);
255:
256: String str1 = MessageFormat.format(format, paras);
257: String str2 = ((Exception) ex).getMessage();
258: assertTrue("Formating failed. Expected \"" + str1
259: + "\", got \"" + str2 + "\".", str1.equals(str2));
260:
261: }
262:
263: //---------------------------------------------------------------------------
264: // members
265: //
266: private String _classToTest = null;
267: }
|