KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > throwable > v1 > IChainableExceptionUTestI


1 /*
2  * @(#)IChainableExceptionUTestI.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.throwable.v1;
28
29 import java.io.PrintStream JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import org.easymock.EasyMock;
34 import org.easymock.MockControl;
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38 import net.sourceforge.groboutils.junit.v1.iftc.*;
39
40
41 /**
42  * Tests the IChainableException interface.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2003/05/06 05:35:02 $
46  * @since March 17, 2002
47  */

48 public class IChainableExceptionUTestI extends InterfaceTestCase
49 {
50     
51     //-------------------------------------------------------------------------
52
// Due to the way exceptions work, most of the logic is involved in the
53
// constructor. Hence, we'll use an alternative pattern to the standard
54
// ImplFactory, where it will return a factory (below) that will create
55
// exceptions using the correct constructor.
56

57     public static interface IChainableExceptionFactory
58     {
59         public IChainableException createException();
60         public IChainableException createException( String JavaDoc message );
61         public IChainableException createException( Throwable JavaDoc cause );
62         public IChainableException createException( String JavaDoc message,
63             Throwable JavaDoc cause );
64         public IChainableException createException( Throwable JavaDoc cause,
65             String JavaDoc message );
66     }
67     
68     
69     
70     
71     //-------------------------------------------------------------------------
72
// Standard JUnit Class-specific declarations
73

74     private static final Class JavaDoc THIS_CLASS =
75         IChainableExceptionUTestI.class;
76     
77     public IChainableExceptionUTestI( String JavaDoc name, ImplFactory f )
78     {
79         super( name, IChainableExceptionFactory.class, f );
80     }
81
82     
83     //-------------------------------------------------------------------------
84
// setup
85

86     /**
87      *
88      * @exception Exception thrown under any exceptional condition.
89      */

90     protected void setUp() throws Exception JavaDoc
91     {
92         super.setUp();
93         
94         // set ourself up
95
}
96     
97     
98     private IChainableExceptionFactory createChainableExceptionFactory()
99     {
100         return (IChainableExceptionFactory)createImplObject();
101     }
102     
103     
104     protected IChainableException createException()
105     {
106         IChainableException ce = createChainableExceptionFactory().
107             createException();
108         assertIsRealChainableException( ce );
109         return ce;
110     }
111     
112     
113     protected IChainableException createException( String JavaDoc message )
114     {
115         IChainableException ce = createChainableExceptionFactory().
116             createException( message );
117         assertIsRealChainableException( ce );
118         return ce;
119     }
120     
121     
122     protected IChainableException createException( Throwable JavaDoc cause )
123     {
124         IChainableException ce = createChainableExceptionFactory().
125             createException( cause );
126         assertIsRealChainableException( ce );
127         return ce;
128     }
129     
130     
131     protected IChainableException createException( String JavaDoc message,
132             Throwable JavaDoc cause )
133     {
134         IChainableException ce = createChainableExceptionFactory().
135             createException( message, cause );
136         assertIsRealChainableException( ce );
137         return ce;
138     }
139     
140     
141     protected IChainableException createException( Throwable JavaDoc cause,
142             String JavaDoc message )
143     {
144         IChainableException ce = createChainableExceptionFactory().
145             createException( cause, message );
146         assertIsRealChainableException( ce );
147         return ce;
148     }
149     
150     //-------------------------------------------------------------------------
151
// Tests
152

153     
154     public void testEmptyConstructor1()
155     {
156         IChainableException ce = createException();
157         assertNull(
158             "Cause is not null.",
159             ce.getCause() );
160     }
161     
162     
163     public void testEmptyConstructor2()
164     {
165         IChainableException ce = createException();
166         Throwable JavaDoc t = new Throwable JavaDoc();
167         ce.initCause( t );
168         assertEquals(
169             "Cause is not right.",
170             t,
171             ce.getCause() );
172     }
173     
174     
175     public void testEmptyConstructor3()
176     {
177         IChainableException ce = createException();
178         try
179         {
180             ce.initCause( (Throwable JavaDoc)ce );
181             fail( "Did not throw IllegalArgumentException." );
182         }
183         catch (IllegalArgumentException JavaDoc iae)
184         {
185             // check exception
186
}
187     }
188     
189     
190     public void testCauseConstructor1()
191     {
192         Throwable JavaDoc t = null;
193         IChainableException ce = createException( t );
194         assertNull(
195             "Cause is not null.",
196             ce.getCause() );
197     }
198     
199     
200     public void testCauseConstructor2()
201     {
202         Throwable JavaDoc t = new Throwable JavaDoc();
203         IChainableException ce = createException( t );
204         assertEquals(
205             "Cause is not right.",
206             t,
207             ce.getCause() );
208     }
209     
210     
211     public void testCauseConstructor3()
212     {
213         Throwable JavaDoc t = new Throwable JavaDoc();
214         IChainableException ce = createException( t );
215         try
216         {
217             ce.initCause( t );
218             fail( "Did not throw IllegalStateException." );
219         }
220         catch (IllegalStateException JavaDoc ise)
221         {
222             // check exception
223
}
224     }
225     
226     
227     public void testCauseConstructor4()
228     {
229         IChainableException ce = createException( (Throwable JavaDoc)null );
230         try
231         {
232             ce.initCause( null );
233             fail( "Did not throw IllegalStateException." );
234         }
235         catch (IllegalStateException JavaDoc ise)
236         {
237             // check exception
238
}
239     }
240     
241     
242     public void testCauseConstructor5()
243     {
244         IChainableException ce = createException( (Throwable JavaDoc)null );
245         try
246         {
247             ce.initCause( new Throwable JavaDoc() );
248             fail( "Did not throw IllegalStateException." );
249         }
250         catch (IllegalStateException JavaDoc ise)
251         {
252             // check exception
253
}
254     }
255     
256     
257     public void testCauseConstructor6()
258     {
259         IChainableException ce = createException( new Throwable JavaDoc() );
260         try
261         {
262             ce.initCause( null );
263             fail( "Did not throw IllegalStateException." );
264         }
265         catch (IllegalStateException JavaDoc ise)
266         {
267             // check exception
268
}
269     }
270     
271     
272     public void testCauseConstructor7()
273     {
274         IChainableException ce = createException( new Throwable JavaDoc() );
275         try
276         {
277             // order is important here - already set the throwable, so that
278
// will be thrown first.
279
ce.initCause( (Throwable JavaDoc)ce );
280             fail( "Did not throw IllegalStateException." );
281         }
282         catch (IllegalStateException JavaDoc iae)
283         {
284             // check exception
285
}
286     }
287     
288     
289     
290     
291     
292     
293     //-------------------------------------------------------------------------
294
// Helpers
295

296     
297     private void assertIsRealChainableException( IChainableException ce )
298     {
299         assertNotNull( ce );
300         assertTrue(
301             "Class under test ("+ce.getClass()+") is not an exception.",
302             ce instanceof Throwable JavaDoc );
303     }
304     
305     
306     
307     //-------------------------------------------------------------------------
308
// Standard JUnit declarations
309

310     
311     public static InterfaceTestSuite suite()
312     {
313         InterfaceTestSuite suite = new InterfaceTestSuite( THIS_CLASS );
314         
315         return suite;
316     }
317     
318     public static void main( String JavaDoc[] args )
319     {
320         String JavaDoc[] name = { THIS_CLASS.getName() };
321         
322         // junit.textui.TestRunner.main( name );
323
// junit.swingui.TestRunner.main( name );
324

325         junit.textui.TestRunner.main( name );
326     }
327     
328     
329     /**
330      *
331      * @exception Exception thrown under any exceptional condition.
332      */

333     protected void tearDown() throws Exception JavaDoc
334     {
335         // tear ourself down
336

337         
338         super.tearDown();
339     }
340 }
341
342
Popular Tags