KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ChainableExceptionHelperUTest.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
39
40 /**
41  * Tests the ChainableExceptionHelper class.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2003/02/10 22:52:42 $
45  * @since March 17, 2002
46  */

47 public class ChainableExceptionHelperUTest extends TestCase
48 {
49     //-------------------------------------------------------------------------
50
// Standard JUnit Class-specific declarations
51

52     private static final Class JavaDoc THIS_CLASS =
53         ChainableExceptionHelperUTest.class;
54     
55     public ChainableExceptionHelperUTest( String JavaDoc name )
56     {
57         super( name );
58     }
59
60     
61     //-------------------------------------------------------------------------
62
// setup
63

64     /**
65      *
66      * @exception Exception thrown under any exceptional condition.
67      */

68     protected void setUp() throws Exception JavaDoc
69     {
70         super.setUp();
71         
72         // set ourself up
73
}
74
75
76     //-------------------------------------------------------------------------
77
// Tests
78

79     
80     public void testConstructor1a()
81     {
82         Throwable JavaDoc source = new Throwable JavaDoc();
83         new ChainableExceptionHelper( source );
84     }
85     
86     
87     public void testConstructor1b()
88     {
89         try
90         {
91             new ChainableExceptionHelper( null );
92             fail("Did not throw IllegalArgumentException");
93         }
94         catch (IllegalArgumentException JavaDoc iae)
95         {
96             // test exception?
97
}
98     }
99     
100     
101     public void testConstructor2a()
102     {
103         Throwable JavaDoc source = new Throwable JavaDoc();
104         Throwable JavaDoc cause = new Throwable JavaDoc();
105         new ChainableExceptionHelper( source, null );
106     }
107     
108     
109     public void testConstructor2b()
110     {
111         Throwable JavaDoc source = new Throwable JavaDoc();
112         Throwable JavaDoc cause = new Throwable JavaDoc();
113         new ChainableExceptionHelper( source, cause );
114     }
115     
116     
117     public void testConstructor2c()
118     {
119         Throwable JavaDoc source = new Throwable JavaDoc();
120         Throwable JavaDoc cause = new Throwable JavaDoc();
121         try
122         {
123             new ChainableExceptionHelper( null, null );
124             fail("Did not throw IllegalArgumentException");
125         }
126         catch (IllegalArgumentException JavaDoc iae)
127         {
128             // test exception?
129
}
130     }
131     
132     
133     public void testConstructor2d()
134     {
135         Throwable JavaDoc source = new Throwable JavaDoc();
136         Throwable JavaDoc cause = new Throwable JavaDoc();
137         try
138         {
139             new ChainableExceptionHelper( null, cause );
140             fail("Did not throw IllegalArgumentException");
141         }
142         catch (IllegalArgumentException JavaDoc iae)
143         {
144             // test exception?
145
}
146     }
147     
148     
149     public void testConstructor2e()
150     {
151         Throwable JavaDoc source = new Throwable JavaDoc();
152         Throwable JavaDoc cause = new Throwable JavaDoc();
153         try
154         {
155             new ChainableExceptionHelper( source, source );
156             fail("Did not throw IllegalArgumentException");
157         }
158         catch (IllegalArgumentException JavaDoc iae)
159         {
160             // test exception?
161
}
162     }
163     
164     
165     public void testGetCause1()
166     {
167         Throwable JavaDoc source = new Throwable JavaDoc();
168         Throwable JavaDoc cause = new Throwable JavaDoc();
169         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
170             source );
171         
172         assertNull(
173             "Does not have a null cause.",
174             ceh.getCause() );
175     }
176     
177     
178     public void testGetCause2()
179     {
180         Throwable JavaDoc source = new Throwable JavaDoc();
181         Throwable JavaDoc cause = new Throwable JavaDoc();
182         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
183             source, null );
184         
185         assertNull(
186             "Does not have a null cause.",
187             ceh.getCause() );
188     }
189     
190     
191     public void testGetCause3()
192     {
193         Throwable JavaDoc source = new Throwable JavaDoc();
194         Throwable JavaDoc cause = new Throwable JavaDoc();
195         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
196             source, cause );
197         
198         assertEquals(
199             "Does not have right cause.",
200             ceh.getCause(),
201             cause );
202     }
203     
204     
205     public void testInitCause1()
206     {
207         Throwable JavaDoc source = new Throwable JavaDoc();
208         Throwable JavaDoc cause = new Throwable JavaDoc();
209         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
210             source );
211         
212         assertEquals(
213             "Did not return right exception.",
214             ceh.initCause( cause ),
215             source );
216     }
217     
218     
219     public void testInitCause2()
220     {
221         Throwable JavaDoc source = new Throwable JavaDoc();
222         Throwable JavaDoc cause = new Throwable JavaDoc();
223         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
224             source );
225         
226         assertEquals(
227             "Did not return right exception.",
228             ceh.initCause( null ),
229             source );
230     }
231     
232     
233     public void testInitCause3()
234     {
235         Throwable JavaDoc source = new Throwable JavaDoc();
236         Throwable JavaDoc cause = new Throwable JavaDoc();
237         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
238             source, cause );
239         
240         try
241         {
242             ceh.initCause( cause );
243             fail("Did not throw IllegalStateException");
244         }
245         catch (IllegalStateException JavaDoc ise)
246         {
247             // test exception?
248
}
249     }
250     
251     
252     public void testInitGetCause1()
253     {
254         Throwable JavaDoc source = new Throwable JavaDoc();
255         Throwable JavaDoc cause = new Throwable JavaDoc();
256         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
257             source );
258         
259         ceh.initCause( cause );
260         assertEquals(
261             "Did not return right cause.",
262             ceh.getCause(),
263             cause );
264     }
265     
266     
267     public void testInitGetCause2()
268     {
269         Throwable JavaDoc source = new Throwable JavaDoc();
270         Throwable JavaDoc cause = new Throwable JavaDoc();
271         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
272             source );
273         
274         ceh.initCause( null );
275         assertNull(
276             "Did not return right cause.",
277             ceh.getCause() );
278     }
279     
280     
281     public void testInitGetCause3()
282     {
283         Throwable JavaDoc source = new Throwable JavaDoc();
284         Throwable JavaDoc cause = new Throwable JavaDoc();
285         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
286             source, cause );
287         
288         assertEquals(
289             "Did not return right cause.",
290             ceh.getCause(),
291             cause );
292     }
293     
294     
295     public void testInitGetCause4()
296     {
297         Throwable JavaDoc source = new Throwable JavaDoc();
298         Throwable JavaDoc cause = new Throwable JavaDoc();
299         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
300             source, null );
301         
302         assertNull(
303             "Did not return right cause.",
304             ceh.getCause() );
305     }
306     
307     
308     public void testPrintStackTrace1()
309     {
310         Throwable JavaDoc source = new Throwable JavaDoc();
311         Throwable JavaDoc cause = new Throwable JavaDoc();
312         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
313             source );
314         
315         StringWriter JavaDoc sw = new StringWriter JavaDoc();
316         ceh.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
317         
318         // check sw result.
319
assertTrue(
320             "Not a valid length",
321             sw.toString().length() > 0 );
322     }
323     
324     
325     public void testPrintStackTrace2()
326     {
327         Throwable JavaDoc source = new Throwable JavaDoc();
328         Throwable JavaDoc cause = new Throwable JavaDoc();
329         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
330             source );
331         
332         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
333         ceh.printStackTrace( new PrintStream JavaDoc( baos, true ) );
334         
335         // check sw result.
336
assertTrue(
337             "Not a valid length",
338             baos.toString().length() > 0 );
339     }
340     
341     
342     public void testPrintStackTrace3()
343     {
344         Throwable JavaDoc source = new Throwable JavaDoc();
345         Throwable JavaDoc cause = new Throwable JavaDoc();
346         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
347             source );
348         
349         ceh.initCause( cause );
350         StringWriter JavaDoc sw = new StringWriter JavaDoc();
351         ceh.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
352         
353         // check sw result.
354
assertTrue(
355             "Not a valid length",
356             sw.toString().length() > 0 );
357     }
358     
359     
360     public void testPrintStackTrace4()
361     {
362         Throwable JavaDoc source = new Throwable JavaDoc();
363         Throwable JavaDoc cause = new Throwable JavaDoc();
364         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
365             source );
366         
367         ceh.initCause( cause );
368         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
369         ceh.printStackTrace( new PrintStream JavaDoc( baos, true ) );
370         
371         // check sw result.
372
assertTrue(
373             "Not a valid length",
374             baos.toString().length() > 0 );
375     }
376     
377     
378     public void testInitGetCause5()
379     {
380         Throwable JavaDoc source = new Throwable JavaDoc();
381         Throwable JavaDoc cause = new Throwable JavaDoc();
382         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
383             source );
384         
385         ceh.initCause( null );
386         StringWriter JavaDoc sw = new StringWriter JavaDoc();
387         ceh.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
388         
389         // check sw result.
390
assertTrue(
391             "Not a valid length",
392             sw.toString().length() > 0 );
393     }
394     
395     
396     public void testInitGetCause6()
397     {
398         Throwable JavaDoc source = new Throwable JavaDoc();
399         Throwable JavaDoc cause = new Throwable JavaDoc();
400         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
401             source );
402         
403         ceh.initCause( null );
404         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
405         ceh.printStackTrace( new PrintStream JavaDoc( baos, true ) );
406         
407         // check sw result.
408
assertTrue(
409             "Not a valid length",
410             baos.toString().length() > 0 );
411     }
412     
413     
414     public void testInitGetCause7()
415     {
416         Throwable JavaDoc source = new Throwable JavaDoc();
417         Throwable JavaDoc cause = new Throwable JavaDoc();
418         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
419             source, cause );
420         
421         StringWriter JavaDoc sw = new StringWriter JavaDoc();
422         ceh.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
423         
424         // check sw result.
425
assertTrue(
426             "Not a valid length",
427             sw.toString().length() > 0 );
428     }
429     
430     
431     public void testInitGetCause8()
432     {
433         Throwable JavaDoc source = new Throwable JavaDoc();
434         Throwable JavaDoc cause = new Throwable JavaDoc();
435         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
436             source, cause );
437         
438         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
439         ceh.printStackTrace( new PrintStream JavaDoc( baos, true ) );
440         
441         // check sw result.
442
assertTrue(
443             "Not a valid length",
444             baos.toString().length() > 0 );
445     }
446     
447     
448     public void testInitGetCause9()
449     {
450         Throwable JavaDoc source = new Throwable JavaDoc();
451         Throwable JavaDoc cause = new Throwable JavaDoc();
452         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
453             source, null );
454         
455         StringWriter JavaDoc sw = new StringWriter JavaDoc();
456         ceh.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
457         
458         // check sw result.
459
assertTrue(
460             "Not a valid length",
461             sw.toString().length() > 0 );
462     }
463     
464     
465     public void testInitGetCause10()
466     {
467         Throwable JavaDoc source = new Throwable JavaDoc();
468         Throwable JavaDoc cause = new Throwable JavaDoc();
469         ChainableExceptionHelper ceh = new ChainableExceptionHelper(
470             source, null );
471         
472         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
473         ceh.printStackTrace( new PrintStream JavaDoc( baos, true ) );
474         
475         // check sw result.
476
assertTrue(
477             "Not a valid length",
478             baos.toString().length() > 0 );
479     }
480     
481     
482     //-------------------------------------------------------------------------
483
// Helpers
484

485     
486     //-------------------------------------------------------------------------
487
// Standard JUnit declarations
488

489     
490     public static Test suite()
491     {
492         TestSuite suite = new TestSuite( THIS_CLASS );
493         
494         // Test the implementation's interface conformity.
495
/*
496         suite.addTest( IxUTestI.suite(
497             new ImplementationCreator[] {
498                 new ImplementationCreator() {
499                     public Object createImplementedObject() {
500                         // XXXXXXXXXXXXXXXXXXXXXXXX
501                     }
502                 },
503             } ) );
504         */

505         
506         return suite;
507     }
508     
509     public static void main( String JavaDoc[] args )
510     {
511         String JavaDoc[] name = { THIS_CLASS.getName() };
512         
513         // junit.textui.TestRunner.main( name );
514
// junit.swingui.TestRunner.main( name );
515

516         junit.textui.TestRunner.main( name );
517     }
518     
519     
520     /**
521      *
522      * @exception Exception thrown under any exceptional condition.
523      */

524     protected void tearDown() throws Exception JavaDoc
525     {
526         // tear ourself down
527

528         
529         super.tearDown();
530     }
531 }
532
533
Popular Tags