001: /*
002: * TestThrowableList.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.java.lang;
045:
046: //-----------------------------------------------------------------------------
047: // Imports
048: //
049: import java.text.MessageFormat;
050:
051: import junit.framework.Test;
052: import junit.framework.TestCase;
053: import junit.framework.TestSuite;
054: import junit.framework.Assert;
055:
056: import de.susebox.TestUtilities;
057:
058: //-----------------------------------------------------------------------------
059: // Class TestThrowableList
060: //
061:
062: /**<p>
063: * This class tests the functionality of the {@link ThrowableList} interface
064: * through various implementing classes.
065: *</p>
066: *
067: * @see ThrowableList
068: * @see ExtRuntimeException
069: * @author Heiko Blau
070: */
071: public class TestThrowableList extends TestCase {
072:
073: //---------------------------------------------------------------------------
074: // properties
075: //
076:
077: //---------------------------------------------------------------------------
078: // main method
079: //
080:
081: /**
082: * call this method to invoke the tests
083: */
084: public static void main(String[] args) {
085: String[] tests = { TestThrowableList.class.getName() };
086:
087: TestUtilities.run(tests, args);
088: }
089:
090: //---------------------------------------------------------------------------
091: // suite method
092: //
093:
094: /**
095: * Implementation of the JUnit method <code>suite</code>. For each set of test
096: * properties one or more tests are instantiated.
097: *
098: * @return a test suite
099: */
100: public static Test suite() {
101: TestSuite suite = new TestSuite(TestThrowableList.class
102: .getName());
103:
104: suite.addTest(new TestThrowableList("testWrappedThrowable"));
105: suite.addTest(new TestThrowableList("testThrowableList"));
106: suite.addTest(new TestThrowableList("testMessageFormatting"));
107: return suite;
108: }
109:
110: //---------------------------------------------------------------------------
111: // Constructor
112: //
113:
114: /**
115: * Default constructor. Standard input {@link java.lang.System#in} is used
116: * to construct the input stream reader.
117: */
118: public TestThrowableList(String test) {
119: super (test);
120: }
121:
122: //---------------------------------------------------------------------------
123: // Fixture setup and release
124: //
125:
126: /**
127: * Sets up the fixture, for example, open a network connection.
128: * This method is called before a test is executed.
129: */
130: protected void setUp() throws Exception {
131: }
132:
133: /**
134: * Tears down the fixture, for example, close a network connection.
135: * This method is called after a test is executed.
136: */
137: protected void tearDown() throws Exception {
138: }
139:
140: //---------------------------------------------------------------------------
141: // test cases
142: //
143:
144: /**
145: * Test wrapped exceptions. The wrapping exception is only a thin layer around
146: * the real one.
147: */
148: public void testWrappedThrowable() throws Throwable {
149: String msg = "This is an illegal argument.";
150: IllegalArgumentException argEx = new IllegalArgumentException(
151: msg);
152: ExtRuntimeException rtEx = new ExtRuntimeException(argEx);
153: ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(
154: argEx);
155: ExtIndexOutOfBoundsException bigEx = new ExtIndexOutOfBoundsException(
156: rtEx);
157:
158: assertTrue("rtEx: Wrapper exception not recognized.", rtEx
159: .isWrapper());
160: assertTrue("rtEx: Didn't retrieve the wrapped exception.", rtEx
161: .getCause() == argEx);
162: assertTrue("rtEx: Messages not equal.", rtEx.getMessage()
163: .equals(argEx.getMessage()));
164:
165: assertTrue("indexEx: Wrapper exception not recognized.",
166: indexEx.isWrapper());
167: assertTrue("indexEx: Didn't retrieve the wrapped exception.",
168: indexEx.getCause() == argEx);
169: assertTrue("indexEx: Messages not equal.", indexEx.getMessage()
170: .equals(argEx.getMessage()));
171:
172: assertTrue("bigEx: Wrapper exception not recognized.", bigEx
173: .isWrapper());
174: assertTrue(
175: "bigEx: Didn't retrieve the first wrapped exception.",
176: bigEx.getCause() == rtEx);
177: assertTrue(
178: "bigEx: Didn't retrieve the second wrapped exception.",
179: ((ThrowableList) bigEx.getCause()).getCause() == argEx);
180: assertTrue("bigEx: Messages not equal.", bigEx.getMessage()
181: .equals(argEx.getMessage()));
182: }
183:
184: /**
185: * Test nested exceptions.
186: */
187: public void testThrowableList() throws Throwable {
188: String format = "This is exception no {0} of class {1}.";
189: String msg = "Message without format parameters.";
190: ExtRuntimeException rtEx1 = new ExtRuntimeException(format,
191: new Object[] { new Integer(1),
192: ExtRuntimeException.class });
193: ExtRuntimeException rtEx2 = new ExtRuntimeException(rtEx1,
194: format, new Object[] { new Integer(2),
195: ExtRuntimeException.class });
196: ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(
197: rtEx2, msg);
198:
199: assertTrue("rtEx1: False wrapper exception.", !rtEx1
200: .isWrapper());
201: assertTrue("rtEx2: False wrapper exception.", !rtEx2
202: .isWrapper());
203: assertTrue("indexEx: False wrapper exception.", !indexEx
204: .isWrapper());
205: assertTrue("rtEx2: Didn't retrieve the nested exception.",
206: rtEx2.getCause() == rtEx1);
207: assertTrue(
208: "indexEx: Didn't retrieve the first nested exception.",
209: indexEx.getCause() == rtEx2);
210: assertTrue(
211: "indexEx: Didn't retrieve the second nested exception.",
212: ((ThrowableList) indexEx.getCause()).getCause() == rtEx1);
213: assertTrue("rtEx1: Format not found.",
214: rtEx1.getFormat() == format);
215: assertTrue("rtEx2: Format not found.",
216: rtEx2.getFormat() == format);
217: assertTrue("indexEx: Format not found.",
218: indexEx.getFormat() == msg);
219: }
220:
221: /**
222: * Test the message formatting
223: */
224: public void testMessageFormatting() throws Throwable {
225: String format = "Class {0}, reason \"{1}\", user {2}.";
226: Object[] paras = new Object[] { ExtRuntimeException.class,
227: "bad weather", "myself" };
228: ExtRuntimeException rtEx = new ExtRuntimeException(format,
229: paras);
230:
231: assertTrue("Format not found.", rtEx.getFormat() == format);
232:
233: String str1 = MessageFormat.format(format, paras);
234: String str2 = rtEx.getMessage();
235: assertTrue("Formating failed. Expected \"" + str1
236: + "\", got \"" + str2 + "\".", str1.equals(str2));
237: }
238: }
|