KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > java > lang > TestThrowableList


1 /*
2  * TestThrowableList.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.java.lang;
45
46 //-----------------------------------------------------------------------------
47
// Imports
48
//
49
import java.text.MessageFormat JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54 import junit.framework.Assert;
55
56 import de.susebox.TestUtilities;
57
58
59 //-----------------------------------------------------------------------------
60
// Class TestThrowableList
61
//
62

63 /**<p>
64  * This class tests the functionality of the {@link ThrowableList} interface
65  * through various implementing classes.
66  *</p>
67  *
68  * @see ThrowableList
69  * @see ExtRuntimeException
70  * @author Heiko Blau
71  */

72 public class TestThrowableList extends TestCase {
73   
74   //---------------------------------------------------------------------------
75
// properties
76
//
77

78   
79   //---------------------------------------------------------------------------
80
// main method
81
//
82

83   /**
84    * call this method to invoke the tests
85    */

86   public static void main(String JavaDoc[] args) {
87     String JavaDoc[] tests = { TestThrowableList.class.getName() };
88
89     TestUtilities.run(tests, args);
90   }
91   
92
93   //---------------------------------------------------------------------------
94
// suite method
95
//
96

97   /**
98    * Implementation of the JUnit method <code>suite</code>. For each set of test
99    * properties one or more tests are instantiated.
100    *
101    * @return a test suite
102    */

103   public static Test suite() {
104     TestSuite suite = new TestSuite(TestThrowableList.class.getName());
105     
106     suite.addTest(new TestThrowableList("testWrappedThrowable"));
107     suite.addTest(new TestThrowableList("testThrowableList"));
108     suite.addTest(new TestThrowableList("testMessageFormatting"));
109     return suite;
110   }
111   
112   
113   //---------------------------------------------------------------------------
114
// Constructor
115
//
116

117   /**
118    * Default constructor. Standard input {@link java.lang.System#in} is used
119    * to construct the input stream reader.
120    */

121   public TestThrowableList(String JavaDoc test) {
122     super(test);
123   }
124
125   
126   //---------------------------------------------------------------------------
127
// Fixture setup and release
128
//
129

130   /**
131    * Sets up the fixture, for example, open a network connection.
132    * This method is called before a test is executed.
133    */

134   protected void setUp() throws Exception JavaDoc {}
135
136   
137   /**
138    * Tears down the fixture, for example, close a network connection.
139    * This method is called after a test is executed.
140    */

141   protected void tearDown() throws Exception JavaDoc {}
142   
143   
144   //---------------------------------------------------------------------------
145
// test cases
146
//
147

148   /**
149    * Test wrapped exceptions. The wrapping exception is only a thin layer around
150    * the real one.
151    */

152   public void testWrappedThrowable() throws Throwable JavaDoc {
153     String JavaDoc msg = "This is an illegal argument.";
154     IllegalArgumentException JavaDoc argEx = new IllegalArgumentException JavaDoc(msg);
155     ExtRuntimeException rtEx = new ExtRuntimeException(argEx);
156     ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(argEx);
157     ExtIndexOutOfBoundsException bigEx = new ExtIndexOutOfBoundsException(rtEx);
158     
159     assertTrue("rtEx: Wrapper exception not recognized.", rtEx.isWrapper());
160     assertTrue("rtEx: Didn't retrieve the wrapped exception.", rtEx.getCause() == argEx);
161     assertTrue("rtEx: Messages not equal.", rtEx.getMessage().equals(argEx.getMessage()));
162     
163     assertTrue("indexEx: Wrapper exception not recognized.", indexEx.isWrapper());
164     assertTrue("indexEx: Didn't retrieve the wrapped exception.", indexEx.getCause() == argEx);
165     assertTrue("indexEx: Messages not equal.", indexEx.getMessage().equals(argEx.getMessage()));
166
167     assertTrue("bigEx: Wrapper exception not recognized.", bigEx.isWrapper());
168     assertTrue("bigEx: Didn't retrieve the first wrapped exception.", bigEx.getCause() == rtEx);
169     assertTrue("bigEx: Didn't retrieve the second wrapped exception.", ((ThrowableList)bigEx.getCause()).getCause() == argEx);
170     assertTrue("bigEx: Messages not equal.", bigEx.getMessage().equals(argEx.getMessage()));
171   }
172
173   /**
174    * Test nested exceptions.
175    */

176   public void testThrowableList() throws Throwable JavaDoc {
177     String JavaDoc format = "This is exception no {0} of class {1}.";
178     String JavaDoc msg = "Message without format parameters.";
179     ExtRuntimeException rtEx1 = new ExtRuntimeException( format,
180                                                           new Object JavaDoc[] { new Integer JavaDoc(1), ExtRuntimeException.class } );
181     ExtRuntimeException rtEx2 = new ExtRuntimeException( rtEx1,
182                                                           format,
183                                                           new Object JavaDoc[] { new Integer JavaDoc(2), ExtRuntimeException.class } );
184     ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(rtEx2, msg);
185     
186     assertTrue("rtEx1: False wrapper exception.", ! rtEx1.isWrapper());
187     assertTrue("rtEx2: False wrapper exception.", ! rtEx2.isWrapper());
188     assertTrue("indexEx: False wrapper exception.", ! indexEx.isWrapper());
189     assertTrue("rtEx2: Didn't retrieve the nested exception.", rtEx2.getCause() == rtEx1);
190     assertTrue("indexEx: Didn't retrieve the first nested exception.", indexEx.getCause() == rtEx2);
191     assertTrue("indexEx: Didn't retrieve the second nested exception.", ((ThrowableList)indexEx.getCause()).getCause() == rtEx1);
192     assertTrue("rtEx1: Format not found.", rtEx1.getFormat() == format);
193     assertTrue("rtEx2: Format not found.", rtEx2.getFormat() == format);
194     assertTrue("indexEx: Format not found.", indexEx.getFormat() == msg);
195   }
196   
197   /**
198    * Test the message formatting
199    */

200   public void testMessageFormatting() throws Throwable JavaDoc {
201     String JavaDoc format = "Class {0}, reason \"{1}\", user {2}.";
202     Object JavaDoc[] paras = new Object JavaDoc[] { ExtRuntimeException.class, "bad weather", "myself" };
203     ExtRuntimeException rtEx = new ExtRuntimeException(format, paras);
204                                         
205     assertTrue("Format not found.", rtEx.getFormat() == format);
206     
207     String JavaDoc str1 = MessageFormat.format(format, paras);
208     String JavaDoc str2 = rtEx.getMessage();
209     assertTrue("Formating failed. Expected \"" + str1 + "\", got \"" + str2 + "\".",
210               str1.equals(str2));
211   }
212 }
213
Popular Tags