KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > parser > DelegateTestCreatorUTest


1 /*
2  * @(#)DelegateTestCreatorUTest.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.parser;
28
29 import net.sourceforge.groboutils.junit.v1.iftc.*;
30 import java.lang.reflect.*;
31
32 import org.easymock.EasyMock;
33 import org.easymock.MockControl;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38
39 /**
40  * Tests the DelegateTestCreator class.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @since November 4, 2002
44  * @version $Date: 2003/02/10 22:52:24 $
45  */

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

51     private static final Class JavaDoc THIS_CLASS = DelegateTestCreatorUTest.class;
52     private static final org.apache.log4j.Logger LOG =
53         org.apache.log4j.Logger.getLogger( THIS_CLASS );
54     
55     public DelegateTestCreatorUTest( String JavaDoc name )
56     {
57         super( name );
58     }
59     
60     
61     // mock object!
62
private static class MyTestCreator implements ITestCreator
63     {
64         int canCreateCount = 0;
65         int createTestCount = 0;
66         boolean canCreate = false;
67         Test test = null;
68         
69         public Test createTest( Class JavaDoc theClass, Method method )
70         {
71             ++this.createTestCount;
72             return this.test;
73         }
74     
75         public boolean canCreate( Class JavaDoc theClass )
76         {
77             ++this.canCreateCount;
78             return this.canCreate;
79         }
80     }
81     
82     
83     //-------------------------------------------------------------------------
84
// Tests
85

86     
87     public void testConstructor1()
88     {
89         try
90         {
91             new DelegateTestCreator( null );
92             fail( "Did not throw IllegalArgumentException." );
93         }
94         catch (IllegalArgumentException JavaDoc e)
95         {
96             // test exception?
97
}
98     }
99     
100     
101     public void testConstructor2()
102     {
103         try
104         {
105             new DelegateTestCreator( new ITestCreator[0] );
106             fail( "Did not throw IllegalArgumentException." );
107         }
108         catch (IllegalArgumentException JavaDoc e)
109         {
110             // test exception?
111
}
112     }
113     
114     
115     public void testCanCreate1()
116     {
117         MyTestCreator tc1 = new MyTestCreator();
118         MyTestCreator tc2 = new MyTestCreator();
119         DelegateTestCreator dtc = new DelegateTestCreator(
120             new ITestCreator[] { tc1, tc2 } );
121         
122         boolean res = dtc.canCreate( null );
123         
124         assertTrue(
125             "Did not return correct result.",
126             !res );
127         assertEquals(
128             "Did not call canCreate correct number of times for first instance.",
129             1,
130             tc1.canCreateCount );
131         assertEquals(
132             "Did not call canCreate correct number of times for second instance.",
133             1,
134             tc2.canCreateCount );
135     }
136     
137     
138     public void testCanCreate2()
139     {
140         MyTestCreator tc1 = new MyTestCreator();
141         MyTestCreator tc2 = new MyTestCreator();
142         tc1.canCreate = true;
143         DelegateTestCreator dtc = new DelegateTestCreator(
144             new ITestCreator[] { tc1, tc2 } );
145         
146         boolean res = dtc.canCreate( null );
147         
148         assertTrue(
149             "Did not return correct result.",
150             res );
151         
152         // order of checks shouldn't be dictated here.
153
assertEquals(
154             "Did not call canCreate correct number of times for first instance.",
155             1,
156             tc1.canCreateCount );
157         assertEquals(
158             "Did not call canCreate correct number of times for second instance.",
159             1,
160             tc2.canCreateCount );
161     }
162     
163     
164     public void testCanCreate3()
165     {
166         MyTestCreator tc1 = new MyTestCreator();
167         MyTestCreator tc2 = new MyTestCreator();
168         tc2.canCreate = true;
169         DelegateTestCreator dtc = new DelegateTestCreator(
170             new ITestCreator[] { tc1, tc2 } );
171         
172         boolean res = dtc.canCreate( null );
173         
174         assertTrue(
175             "Did not return correct result.",
176             res );
177         
178         // order of checks shouldn't be dictated here.
179
assertEquals(
180             "Did not call canCreate correct number of times for first instance.",
181             0,
182             tc1.canCreateCount );
183         assertEquals(
184             "Did not call canCreate correct number of times for second instance.",
185             1,
186             tc2.canCreateCount );
187     }
188     
189     
190     public void testCanCreate4()
191     {
192         MyTestCreator tc1 = new MyTestCreator();
193         MyTestCreator tc2 = new MyTestCreator();
194         tc1.canCreate = true;
195         tc2.canCreate = true;
196         DelegateTestCreator dtc = new DelegateTestCreator(
197             new ITestCreator[] { tc1, tc2 } );
198         
199         boolean res = dtc.canCreate( null );
200         
201         assertTrue(
202             "Did not return correct result.",
203             res );
204         
205         // order of checks shouldn't be dictated here.
206
assertEquals(
207             "Did not call canCreate correct number of times for first instance.",
208             0,
209             tc1.canCreateCount );
210         assertEquals(
211             "Did not call canCreate correct number of times for second instance.",
212             1,
213             tc2.canCreateCount );
214     }
215     
216     
217     public void testCanCreate5()
218     {
219         MyTestCreator tc1 = new MyTestCreator();
220         DelegateTestCreator dtc = new DelegateTestCreator(
221             new ITestCreator[] { tc1 } );
222         
223         boolean res = dtc.canCreate( null );
224         
225         assertTrue(
226             "Did not return correct result.",
227             !res );
228         assertEquals(
229             "Did not call canCreate correct number of times for first instance.",
230             1,
231             tc1.canCreateCount );
232     }
233     
234     
235     public void testCanCreate6()
236     {
237         MyTestCreator tc1 = new MyTestCreator();
238         tc1.canCreate = true;
239         DelegateTestCreator dtc = new DelegateTestCreator(
240             new ITestCreator[] { tc1 } );
241         
242         boolean res = dtc.canCreate( null );
243         
244         assertTrue(
245             "Did not return correct result.",
246             res );
247         assertEquals(
248             "Did not call canCreate correct number of times for first instance.",
249             1,
250             tc1.canCreateCount );
251     }
252     
253     
254     
255     //-------------------------------------------------------------------------
256
// Standard JUnit declarations
257

258     
259     public static Test suite()
260     {
261         InterfaceTestSuite suite = ITestCreatorUTestI.suite();
262         
263         // yes, this is an inner class inside an inner class!
264
// shudder - luckily, this is only for testing.
265
suite.addFactory( new CxFactory( "A" ) {
266             public Object JavaDoc createImplObject() {
267                 return new DelegateTestCreator( new ITestCreator[] {
268                         new MyTestCreator(), new MyTestCreator()
269                     } );
270             }
271         } );
272         suite.addTestSuite( THIS_CLASS );
273         
274         return suite;
275     }
276     
277     public static void main( String JavaDoc[] args )
278     {
279         String JavaDoc[] name = { THIS_CLASS.getName() };
280         
281         // junit.textui.TestRunner.main( name );
282
// junit.swingui.TestRunner.main( name );
283

284         junit.textui.TestRunner.main( name );
285     }
286     
287     
288     /**
289      *
290      * @exception Exception thrown under any exceptional condition.
291      */

292     protected void setUp() throws Exception JavaDoc
293     {
294         super.setUp();
295         
296         // set ourself up
297
}
298     
299     
300     /**
301      *
302      * @exception Exception thrown under any exceptional condition.
303      */

304     protected void tearDown() throws Exception JavaDoc
305     {
306         // tear ourself down
307

308         
309         super.tearDown();
310     }
311 }
312
313
Popular Tags