KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > iftc > InterfaceTestCaseUTest


1 /*
2  * @(#)InterfaceTestCaseUTest.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.junit.v1.iftc;
28
29 import org.easymock.EasyMock;
30 import org.easymock.MockControl;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 import junit.framework.AssertionFailedError;
35
36
37 /**
38  * Tests the InterfaceTestCase class.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @since March 1, 2002
42  * @version $Date: 2003/05/29 13:05:54 $
43  */

44 public class InterfaceTestCaseUTest extends TestCase
45 {
46     
47     //-------------------------------------------------------------------------
48
// Standard JUnit Class-specific declarations
49

50     private static final Class JavaDoc THIS_CLASS = InterfaceTestCaseUTest.class;
51     private static final org.apache.log4j.Logger LOG =
52         org.apache.log4j.Logger.getLogger( InterfaceTestCaseUTest.class );
53     
54     public InterfaceTestCaseUTest( String JavaDoc name )
55     {
56         super( name );
57     }
58
59     
60
61
62     //-------------------------------------------------------------------------
63
// Tests
64

65     public static class MyInterfaceTestCase extends InterfaceTestCase
66     {
67         public MyInterfaceTestCase( String JavaDoc name, Class JavaDoc interfaceClass,
68                 ImplFactory f )
69         {
70             super( name, interfaceClass, f );
71         }
72     }
73     
74     public void testConstructor1()
75     {
76         LOG.debug( "Entering testConstructor1" );
77         ImplFactory f = new ImplFactory() {
78             public Object JavaDoc createImplObject() {
79                 return new Runnable JavaDoc() { public void run() {} };
80             }
81         };
82         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
83             Runnable JavaDoc.class, f );
84         assertEquals(
85             "Did not store the interface class correctly.",
86             Runnable JavaDoc.class,
87             itc.getInterfaceClass() );
88         LOG.debug( "Leaving testConstructor1" );
89     }
90     
91     
92     public void testConstructor2()
93     {
94         LOG.debug( "Entering testConstructor2" );
95         try
96         {
97             new MyInterfaceTestCase( "test", null, null );
98             fail("Did not throw IllegalArgumentException.");
99         }
100         catch (IllegalArgumentException JavaDoc e)
101         {
102             // test exception?
103
}
104         LOG.debug( "Leaving testConstructor2" );
105     }
106     
107     
108     public void testConstructor3()
109     {
110         LOG.debug( "Entering testConstructor3" );
111         try
112         {
113             new MyInterfaceTestCase( "test", Runnable JavaDoc.class, null );
114             fail("Did not throw IllegalArgumentException.");
115         }
116         catch (IllegalArgumentException JavaDoc e)
117         {
118             // test exception?
119
}
120         LOG.debug( "Leaving testConstructor3" );
121     }
122     
123     
124     public void testConstructor4()
125     {
126         LOG.debug( "Entering testConstructor4" );
127         ImplFactory f = new ImplFactory() {
128             public Object JavaDoc createImplObject() {
129                 return new Runnable JavaDoc() { public void run() {} };
130             }
131         };
132         try
133         {
134             new MyInterfaceTestCase( "test", null, f );
135             fail("Did not throw IllegalArgumentException.");
136         }
137         catch (IllegalArgumentException JavaDoc e)
138         {
139             // test exception?
140
}
141         LOG.debug( "Leaving testConstructor4" );
142     }
143     
144     
145     public void testCreate1()
146     {
147         LOG.debug( "Entering testCreate1" );
148         ImplFactory f = new ImplFactory() {
149             public Object JavaDoc createImplObject() {
150                 return "a string";
151             }
152         };
153         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
154             Runnable JavaDoc.class, f );
155         try
156         {
157             itc.createImplObject();
158             fail("Did not fail.");
159         }
160         catch (AssertionFailedError e)
161         {
162             // test error?
163
}
164         LOG.debug( "Leaving testCreate1" );
165     }
166     
167     
168     public void testCreate2()
169     {
170         LOG.debug( "Entering testCreate2" );
171         ImplFactory f = new ImplFactory() {
172             public Object JavaDoc createImplObject() {
173                 return null;
174             }
175         };
176         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
177             Runnable JavaDoc.class, f );
178         try
179         {
180             itc.createImplObject();
181             fail("Did not fail.");
182         }
183         catch (AssertionFailedError e)
184         {
185             // test error?
186
}
187         LOG.debug( "Leaving testCreate2" );
188     }
189     
190     
191     public void testCreate3()
192     {
193         LOG.debug( "Entering testCreate3" );
194         ImplFactory f = new ImplFactory() {
195             public Object JavaDoc createImplObject() {
196                 throw new IllegalArgumentException JavaDoc("IAE");
197             }
198         };
199         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
200             Runnable JavaDoc.class, f );
201         try
202         {
203             itc.createImplObject();
204             fail("Did not throw exception.");
205         }
206         catch (AssertionFailedError e)
207         {
208             // test error
209
assertTrue(
210                 "Does not contain the correct exception text.",
211                 e.getMessage().indexOf( " threw exception "+
212                     "java.lang.IllegalArgumentException: IAE during "+
213                     "creation:" ) > 0 );
214                 
215         }
216     }
217     
218     
219     final String JavaDoc teststring = "a string";
220     public void testCreate4()
221     {
222         LOG.debug( "Entering testCreate4" );
223         ImplFactory f = new ImplFactory() {
224             public Object JavaDoc createImplObject() {
225                 return teststring;
226             }
227         };
228         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
229             String JavaDoc.class, f );
230         Object JavaDoc o = itc.createImplObject();
231         assertSame(
232             "Did not return the exact object we thought we returned.",
233             teststring,
234             o );
235     }
236     
237     
238     final static String JavaDoc factoryname = "TestFactoryName";
239     public void testToString1()
240     {
241         LOG.debug( "Entering testToString1" );
242         ImplFactory f = new ImplFactory() {
243             public Object JavaDoc createImplObject() {
244                 return "";
245             }
246             public String JavaDoc toString() {
247                 return factoryname;
248             }
249         };
250         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
251             String JavaDoc.class, f );
252         itc.setUseClassInName( false );
253         assertEquals(
254             "Did not return the correct text.",
255             "test["+factoryname+"]("+itc.getClass().getName()+")",
256             itc.toString() );
257     }
258     
259     
260     public void testGetName1()
261     {
262         LOG.debug( "Entering testGetName1" );
263         ImplFactory f = new ImplFactory() {
264             public Object JavaDoc createImplObject() {
265                 return "";
266             }
267             public String JavaDoc toString() {
268                 return factoryname;
269             }
270         };
271         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
272             String JavaDoc.class, f );
273         itc.setUseClassInName( false );
274         assertEquals(
275             "Did not return the correct text.",
276             "test["+factoryname+"]",
277             itc.getName() );
278     }
279     
280     
281     public void testGetName2()
282     {
283         LOG.debug( "Entering testGetName1" );
284         ImplFactory f = new ImplFactory() {
285             public Object JavaDoc createImplObject() {
286                 return "";
287             }
288             public String JavaDoc toString() {
289                 return factoryname;
290             }
291         };
292         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
293             String JavaDoc.class, f );
294         itc.setUseClassInName( true );
295         assertEquals(
296             "Did not return the correct text.",
297             "InterfaceTestCaseUTest$MyInterfaceTestCase.test["+factoryname+"]",
298             itc.getName() );
299     }
300     
301     
302     public void testName1()
303     {
304         LOG.debug( "Entering testName1" );
305         ImplFactory f = new ImplFactory() {
306             public Object JavaDoc createImplObject() {
307                 return "";
308             }
309             public String JavaDoc toString() {
310                 return factoryname;
311             }
312         };
313         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
314             String JavaDoc.class, f );
315         itc.setUseClassInName( false );
316         assertEquals(
317             "Did not return the correct text.",
318             itc.getName(),
319             itc.name() );
320     }
321     
322     //---------
323

324     private static class MyCxFactory implements ICxFactory
325     {
326         public int createCount = 0;
327         public int tearDownCount = 0;
328         
329         public Object JavaDoc createImplObject()
330         {
331             ++this.createCount;
332             return new Integer JavaDoc( this.createCount );
333         }
334         
335         public void tearDown( Object JavaDoc o )
336         {
337             int i = ((Integer JavaDoc)o).intValue();
338             assertEquals(
339                 "Did not tear down in the right order.",
340                 createCount - tearDownCount,
341                 i );
342             ++tearDownCount;
343         }
344     }
345     
346     public void testTearDown1() throws Exception JavaDoc
347     {
348         LOG.debug( "Entering testTearDown1()" );
349         MyCxFactory f = new MyCxFactory();
350         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
351             Integer JavaDoc.class, f );
352         int instantCount = 100;
353         for (int i = 0; i < instantCount; ++i)
354         {
355             itc.createImplObject();
356         }
357         itc.tearDown();
358         assertEquals(
359             "Did not tear down all expected instantiated objects.",
360             instantCount,
361             f.tearDownCount );
362     }
363     
364     //---------
365

366     private static class MyCxFactory2 implements ICxFactory
367     {
368         public Object JavaDoc createImplObject()
369         {
370             return new Integer JavaDoc( 0 );
371         }
372         
373         public void tearDown( Object JavaDoc o ) throws Exception JavaDoc
374         {
375             throw new IllegalStateException JavaDoc();
376         }
377     }
378     
379     public void testTearDown2() throws Exception JavaDoc
380     {
381         LOG.debug( "Entering testTearDown2()" );
382         MyCxFactory2 f = new MyCxFactory2();
383         InterfaceTestCase itc = new MyInterfaceTestCase( "test",
384             Integer JavaDoc.class, f );
385         int instantCount = 100;
386         for (int i = 0; i < instantCount; ++i)
387         {
388             itc.createImplObject();
389         }
390         try
391         {
392             itc.tearDown();
393             fail( "tearDown did not propigate any exceptions to the top." );
394         }
395         catch (AssertionFailedError ex)
396         {
397             String JavaDoc s = ex.toString();
398             int count = -1;
399             int pos = 0;
400             while (pos >= 0)
401             {
402                 ++count;
403                 pos = s.indexOf( IllegalStateException JavaDoc.class.getName(),
404                     pos+1 );
405             }
406             assertEquals(
407                 "Did not catch or report all exceptions.",
408                 instantCount,
409                 count );
410         }
411     }
412     
413     
414     
415     //-------------------------------------------------------------------------
416
// Standard JUnit declarations
417

418     
419     public static Test suite()
420     {
421         TestSuite suite = new TestSuite( THIS_CLASS );
422         
423         return suite;
424     }
425     
426     public static void main( String JavaDoc[] args )
427     {
428         String JavaDoc[] name = { THIS_CLASS.getName() };
429         
430         // junit.textui.TestRunner.main( name );
431
// junit.swingui.TestRunner.main( name );
432

433         junit.textui.TestRunner.main( name );
434     }
435     
436     
437     /**
438      *
439      * @exception Exception thrown under any exceptional condition.
440      */

441     protected void setUp() throws Exception JavaDoc
442     {
443         super.setUp();
444         
445         // set ourself up
446
}
447     
448     
449     /**
450      *
451      * @exception Exception thrown under any exceptional condition.
452      */

453     protected void tearDown() throws Exception JavaDoc
454     {
455         // tear ourself down
456

457         
458         super.tearDown();
459     }
460 }
461
462
Popular Tags