KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TestClassParserUTest.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 junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import java.io.IOException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.Vector JavaDoc;
36
37
38 /**
39  * Tests the TestClassParser class.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @since March 1, 2002
43  * @version $Date: 2003/05/24 17:15:47 $
44  */

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

50     private static final Class JavaDoc THIS_CLASS = TestClassParserUTest.class;
51 // private static final IJUnitDocumentor LOG = (new JUnitLog(THIS_CLASS)).getDocumentor();
52

53     public TestClassParserUTest( String JavaDoc name )
54     {
55         super( name );
56     }
57
58     
59
60
61     //-------------------------------------------------------------------------
62
// Tests
63

64     
65     public void testConstructor1()
66     {
67         try
68         {
69             new TestClassParser( null );
70         }
71         catch (IllegalArgumentException JavaDoc e)
72         {
73             // test exception?
74
}
75     }
76     
77     
78     public void testConstructor2()
79     {
80         new TestClassParser( getClass() );
81     }
82     
83     
84     public static class TesterNoTestMethods implements Test
85     {
86         public int countTestCases() { return 0; }
87         public void run( junit.framework.TestResult tr ) {}
88     }
89     
90     
91     public static class TesterOneTestMethod implements Test
92     {
93         public int countTestCases() { return 0; }
94         public void run( junit.framework.TestResult tr ) {}
95         
96         public void testMyTest() {}
97     }
98
99
100     
101     
102     public static class TesterBadMethod implements Test
103     {
104         public int countTestCases() { return 0; }
105         public void run( junit.framework.TestResult tr ) {}
106         
107         public void usedForTesting()
108         {
109         }
110         
111         protected void testUsedForTesting()
112         {}
113     }
114
115     
116     
117     private class StaticClass {}
118     public class InnerClass {}
119     
120     public void testGetTestMethods1()
121     {
122         TestClassParser tcp = new TestClassParser( String JavaDoc.class );
123         Method JavaDoc m[] = tcp.getTestMethods();
124         assertNotNull(
125             "Must never return null.",
126             m );
127         assertEquals(
128             "String should have no test methods.",
129             0,
130             m.length );
131         assertTrue(
132             "Must never return the same array, but rather a copy.",
133             m != tcp.getTestMethods() );
134     }
135     
136     public void testGetTestMethods2()
137     {
138         TestClassParser tcp = new TestClassParser( Runnable JavaDoc.class );
139         Method JavaDoc m[] = tcp.getTestMethods();
140         assertNotNull(
141             "Must never return null.",
142             m );
143         assertEquals(
144             "Runnable should have no test methods.",
145             0,
146             m.length );
147         assertTrue(
148             "Must never return the same array, but rather a copy.",
149             m != tcp.getTestMethods() );
150     }
151     
152     public void testGetTestMethods3()
153     {
154         TestClassParser tcp = new TestClassParser( StaticClass.class );
155         Method JavaDoc m[] = tcp.getTestMethods();
156         assertNotNull(
157             "Must never return null.",
158             m );
159         assertEquals(
160             "Runnable should have no test methods.",
161             0,
162             m.length );
163         assertTrue(
164             "Must never return the same array, but rather a copy.",
165             m != tcp.getTestMethods() );
166     }
167     
168     public void testGetTestMethods4()
169     {
170         TestClassParser tcp = new TestClassParser( InnerClass.class );
171         Method JavaDoc m[] = tcp.getTestMethods();
172         assertNotNull(
173             "Must never return null.",
174             m );
175         assertEquals(
176             "Runnable should have no test methods.",
177             0,
178             m.length );
179         assertTrue(
180             "Must never return the same array, but rather a copy.",
181             m != tcp.getTestMethods() );
182     }
183     
184     public void testGetTestMethods5()
185     {
186         TestClassParser tcp = new TestClassParser( TesterNoTestMethods.class );
187         Method JavaDoc m[] = tcp.getTestMethods();
188         assertNotNull(
189             "Must never return null.",
190             m );
191         assertEquals(
192             "Runnable should have no test methods.",
193             0,
194             m.length );
195         assertTrue(
196             "Must never return the same array, but rather a copy.",
197             m != tcp.getTestMethods() );
198     }
199     
200     public void testGetTestMethods6()
201     {
202         TestClassParser tcp = new TestClassParser( TesterOneTestMethod.class );
203         Method JavaDoc m[] = tcp.getTestMethods();
204         assertNotNull(
205             "Must never return null.",
206             m );
207         assertEquals(
208             "Runnable should have one test method.",
209             1,
210             m.length );
211         assertTrue(
212             "Must never return the same array, but rather a copy.",
213             m != tcp.getTestMethods() );
214     }
215     
216     public void testClearWarnings1()
217     {
218         TestClassParser tcp = new TestClassParser( TesterOneTestMethod.class );
219         tcp.clearWarnings();
220     }
221     
222     public void testClearWarnings2()
223     {
224         TestClassParser tcp = new TestClassParser( Object JavaDoc.class );
225         tcp.clearWarnings();
226     }
227     
228     public void testGetName1()
229     {
230         TestClassParser tcp = new TestClassParser( TesterOneTestMethod.class );
231         String JavaDoc name = tcp.getName();
232         assertEquals(
233             "Returned invalid test name.",
234             TesterOneTestMethod.class.getName(),
235             name );
236     }
237     
238     public void testGetName2()
239     {
240         TestClassParser tcp = new TestClassParser( Object JavaDoc.class );
241         String JavaDoc name = tcp.getName();
242         assertEquals(
243             "Returned invalid test name.",
244             Object JavaDoc.class.getName(),
245             name );
246     }
247     
248     public void testAddTestMethod1() throws Exception JavaDoc
249     {
250         TestClassParser tcp = new TestClassParser( TesterOneTestMethod.class );
251         int warningCount = tcp.getWarnings().length;
252         Vector JavaDoc v = new Vector JavaDoc();
253         Method JavaDoc m = THIS_CLASS.getMethod( "testAddTestMethod1", new Class JavaDoc[0] );
254         v.addElement( "testAddTestMethod1" );
255         tcp.addTestMethod( m, v );
256         
257         // ensure that there are more warnings
258
assertEquals(
259             "Incorrectly changed the warnings.",
260             warningCount,
261             tcp.getWarnings().length );
262     }
263     
264     public void testAddTestMethod2() throws Exception JavaDoc
265     {
266         TestClassParser tcp = new TestClassParser( TesterBadMethod.class );
267         int warningCount = tcp.getWarnings().length;
268         Vector JavaDoc v = new Vector JavaDoc();
269         Method JavaDoc m = TesterBadMethod.class.getMethod( "usedForTesting", new Class JavaDoc[0] );
270         tcp.addTestMethod( m, v );
271         
272         // ensure that there are more warnings
273
assertEquals(
274             "Incorrectly changed the warnings.",
275             warningCount,
276             tcp.getWarnings().length );
277     }
278     
279     /* this is tested elsewhere.
280     public void testAddTestMethod3() throws Exception
281     {
282         TestClassParser tcp = new TestClassParser( TesterBadMethod.class );
283         int warningCount = tcp.getWarnings().length;
284         Vector v = new Vector();
285         Method m = TesterBadMethod.class.getMethod( "testUsedForTesting", new Class[0] );
286         tcp.addTestMethod( m, v );
287         
288         // ensure that there are more warnings
289         assertEquals(
290             "Did not add an additional warning.",
291             warningCount + 1,
292             tcp.getWarnings().length );
293     }
294     
295     
296     
297     //-------------------------------------------------------------------------
298     // Standard JUnit declarations
299     
300     
301     public static Test suite()
302     {
303         TestSuite suite = new TestSuite( THIS_CLASS );
304         
305         return suite;
306     }
307     
308     public static void main( String[] args )
309     {
310         String[] name = { THIS_CLASS.getName() };
311         
312         // junit.textui.TestRunner.main( name );
313         // junit.swingui.TestRunner.main( name );
314         
315         junit.textui.TestRunner.main( name );
316     }
317     
318     
319     /**
320      *
321      * @exception Exception thrown under any exceptional condition.
322      */

323     protected void setUp() throws Exception JavaDoc
324     {
325         super.setUp();
326         
327         // set ourself up
328
}
329     
330     
331     /**
332      *
333      * @exception Exception thrown under any exceptional condition.
334      */

335     protected void tearDown() throws Exception JavaDoc
336     {
337         // tear ourself down
338

339         
340         super.tearDown();
341     }
342 }
343
344
Popular Tags