KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TestClassCreator.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 java.util.Vector JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36
37 import junit.framework.TestSuite;
38 import junit.framework.TestCase;
39 import junit.framework.Test;
40
41 import org.apache.log4j.Logger;
42
43
44 /**
45  * Creates Test instances based on a <tt>TestClassParser</tt>.
46  * <P>
47  * Ripped the test method discovery code out of junit.framework.TestSuite to
48  * allow it to have usable logic.
49  * <P>
50  * This is not covered under the GroboUtils license, but rather under the
51  * JUnit license (IBM Public License). This heading may not be totally
52  * in line with the license, so I'll change it when I find out what needs to
53  * be changed.
54  *
55  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
56  * @version $Date: 2003/02/10 22:52:21 $
57  * @since November 4, 2002
58  */

59 public class TestClassCreator
60 {
61     private static final Logger LOG = Logger.getLogger(
62         TestClassCreator.class );
63     
64     private ITestCreator creator;
65     private Vector JavaDoc warnings = new Vector JavaDoc();
66     
67     
68     /**
69      * Creates an instance that creates test instances based on the given
70      * creator.
71      *
72      * @exception IllegalArgumentException if <tt>theClass</tt> is
73      * <tt>null</tt>.
74      */

75     public TestClassCreator( final ITestCreator tc )
76     {
77         if (tc == null)
78         {
79             throw new IllegalArgumentException JavaDoc("no null arguments");
80         }
81         this.creator = tc;
82     }
83     
84     
85     //-------------------------------------------------------------------------
86
// Public methods
87

88     
89     /**
90      * Retrieve all warnings generated during the introspection of the class,
91      * or test creation. If a <tt>clearWarnings()</tt> call was ever made, then
92      * only those warnings that were encountered after the call will be
93      * returned.
94      *
95      * @return an array of all warnings generated while creating the test
96      * array.
97      */

98     public String JavaDoc[] getWarnings()
99     {
100         String JavaDoc w[] = new String JavaDoc[ this.warnings.size() ];
101         this.warnings.copyInto( w );
102         return w;
103     }
104     
105     
106     /**
107      * Remove all current warnings.
108      */

109     public void clearWarnings()
110     {
111         this.warnings.removeAllElements();
112     }
113     
114     
115     /**
116      * This will create all test objects for the test registered with the
117      * parser. Any errors reported during generation will be added to the
118      * warnings list.
119      *
120      * @return all valid tests created through inspection.
121      */

122     public Test[] createTests( TestClassParser tcp )
123     {
124         Vector JavaDoc t = new Vector JavaDoc();
125         if (this.creator.canCreate( tcp.getTestClass() ))
126         {
127             Method JavaDoc m[] = tcp.getTestMethods();
128             for (int i = 0; i < m.length; ++i)
129             {
130                 try
131                 {
132                     Test tt = this.creator.createTest(
133                         tcp.getTestClass(), m[i] );
134                     if (tt != null)
135                     {
136                         t.addElement( tt );
137                     }
138                     else
139                     {
140                         warning( "Could not create test for class " +
141                             tcp.getTestClass().getName() + " and method " +
142                             m[i].getName() + "." );
143                     }
144                 }
145                 catch (InstantiationException JavaDoc ie)
146                 {
147                     warning( "Method " + m[i].getName() +
148                         " could not be added as a test: " +
149                         ie );
150                 }
151                 catch (NoSuchMethodException JavaDoc nsme)
152                 {
153                     warning( "No valid constructor for " +
154                         tcp.getTestClass().getName() +
155                         ": " + nsme );
156                 }
157                 catch (InvocationTargetException JavaDoc ite)
158                 {
159                     warning( "Construction of class " +
160                         tcp.getTestClass().getName() +
161                         " caused an exception: "+ ite.getTargetException() );
162                 }
163                 catch (IllegalAccessException JavaDoc iae)
164                 {
165                     warning( "Protection on constructor for class "+
166                         tcp.getTestClass().getName() +
167                         " was invalid: " + iae );
168                 }
169                 catch (ClassCastException JavaDoc cce)
170                 {
171                     warning( "Class " + tcp.getTestClass().getName() +
172                         " is not of Test type." );
173                 }
174             }
175         }
176         else
177         {
178             warning( "TestCreator does not know how to handle class " +
179                 tcp.getTestClass().getName() + "." );
180         }
181         
182         Test tt[] = new Test[ t.size() ];
183         t.copyInto( tt );
184         return tt;
185     }
186     
187     
188     /**
189      * For every warning currently known in this creator and the parser,
190      * create a Test that fails with the warning's message. Note that after
191      * creating a test with the warnings, this instance will still know about
192      * the warnings.
193      *
194      * @return an array of tests that fail with a particular warning.
195      */

196     public Test[] createWarningTests( TestClassParser tcp )
197     {
198         String JavaDoc s1[] = getWarnings();
199         String JavaDoc s2[] = tcp.getWarnings();
200         Test t[] = new Test[ s1.length + s2.length ];
201         for (int i = 0; i < s1.length; ++i)
202         {
203             t[i] = createWarningTest( s1[i] );
204         }
205         for (int i = 0; i < s2.length; ++i)
206         {
207             t[i+s1.length] = createWarningTest( s2[i] );
208         }
209         return t;
210     }
211     
212     
213     /**
214      * Create a new TestSuite, containing the tests returned by the call to
215      * <tt>createTests()</tt>. No warning tests will be added.
216      *
217      * @return a new TestSuite with all the valid, discovered tests.
218      */

219     public TestSuite createTestSuite( TestClassParser tcp )
220     {
221         TestSuite ts = new TestSuite( tcp.getName() );
222         Test t[] = createTests( tcp );
223         for (int i = 0; i < t.length; ++i)
224         {
225             if (t[i] != null)
226             {
227                 ts.addTest( t[i] );
228             }
229         }
230         return ts;
231     }
232     
233     
234     /**
235      * Create a new TestSuite, containing the tests returned by the call to
236      * <tt>createTests()</tt> and <tt>createWarningTests</tt>.
237      *
238      * @return a new TestSuite with all the valid, discovered tests, and the
239      * warning tests.
240      */

241     public TestSuite createAllTestSuite( TestClassParser tcp )
242     {
243         TestSuite ts = createTestSuite( tcp );
244         Test t[] = createWarningTests( tcp );
245         for (int i = 0; i < t.length; ++i)
246         {
247             if (t[i] != null)
248             {
249                 ts.addTest( t[i] );
250             }
251         }
252         return ts;
253     }
254     
255     
256     /**
257      * Create a new Test that will fail with the given error message.
258      *
259      * @param message the text to report in the failure of the created
260      * Test.
261      * @return a test that will fail with the given message.
262      */

263     public static Test createWarningTest( final String JavaDoc message )
264     {
265         return new TestCase( "warning" ) {
266                 protected void runTest() {
267                     fail( message );
268                 }
269             };
270     }
271     
272     
273     
274     //-------------------------------------------------------------------------
275
// Protected methods
276

277      
278     /**
279      * Adds a warning message to the inner list of warnings.
280      *
281      * @param message the message describing the warning.
282      */

283     protected void warning( final String JavaDoc message )
284     {
285         LOG.info( "WARNING: "+message );
286         this.warnings.addElement( message );
287     }
288 }
289
Popular Tags