KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)IftcOrigCreator.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.TestSuite;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.lang.reflect.Constructor JavaDoc;
35
36 import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory;
37
38
39 /**
40  * Creates Interface test cases based on the original JUnit construction
41  * mechanism.
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:21 $
45  * @since November 4, 2002
46  */

47 public class IftcOrigCreator implements ITestCreator
48 {
49     private ImplFactory factories[];
50     
51     
52     /**
53      * Default constructor.
54      *
55      * @param f factory list used in generating the tests. If this is
56      * <tt>null</tt>, or if any entries are <tt>null</tt>, then an
57      * IllegalArgumentException will be thrown.
58      */

59     public IftcOrigCreator( ImplFactory[] f )
60     {
61         if (f == null)
62         {
63             throw new IllegalArgumentException JavaDoc("no null args");
64         }
65         
66         // allow for empty factory list
67
int len = f.length;
68         this.factories = new ImplFactory[ len ];
69         for (int i = len; --i >= 0;)
70         {
71             if (f[i] == null)
72             {
73                 throw new IllegalArgumentException JavaDoc("no null args");
74             }
75             this.factories[i] = f[i];
76         }
77     }
78     
79     
80     /**
81      * Creates a new test, based on the given class and method of the class.
82      * This calls <tt>createTest( Class, Object[] )</tt> to create the new
83      * class, which itself calls <tt>getConstructorArgTypes( Class )</tt> to
84      * determine which constructor to get. Also,
85      * <tt>createTestArguments( Class, Method )</tt> is called to generate the
86      * constructor's arguments.
87      *
88      * @param theClass the class to parse for testing.
89      * @param m the method that will be tested with the new class instance.
90      * @exception InstantiationException if there was a problem creating the
91      * class.
92      * @exception NoSuchMethodException if the method does not exist in the
93      * class.
94      * @see #createTest( Class, Object[] )
95      */

96     public Test createTest( Class JavaDoc theClass, Method JavaDoc method )
97             throws InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
98             InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
99             ClassCastException JavaDoc
100     {
101         TestSuite suite = new TestSuite();
102         
103         int goodTestCount = 0;
104         for (int i = 0; i < this.factories.length; ++i)
105         {
106             Test t = createTest( theClass, createTestArguments( theClass,
107                 method, this.factories[i] ) );
108             if (t != null)
109             {
110                 ++goodTestCount;
111                 suite.addTest( t );
112             }
113         }
114         
115         if (goodTestCount <= 0)
116         {
117             suite.addTest( TestClassCreator.createWarningTest(
118                 "No factories or valid instances for test class "+
119                 theClass.getName()+", method "+method.getName()+"." ) );
120         }
121         
122         return suite;
123     }
124     
125     
126     /**
127      * Checks if the creator can be used on the given class.
128      *
129      * @param theClass the class to check if parsing is acceptable.
130      */

131     public boolean canCreate( Class JavaDoc theClass )
132     {
133         try
134         {
135             Constructor JavaDoc c = getConstructor( theClass );
136             return (c != null);
137         }
138         catch (Exception JavaDoc ex)
139         {
140             return false;
141         }
142     }
143     
144     
145     /**
146      * Discovers the constructor for the test class which will be used in
147      * the instantiation of a new instance of the class. This constructor
148      * will be discovered through a call to
149      * <tt>getConstructorArgTypes</tt>. The returned constructor must be
150      * callable through <tt>createTestArguments</tt>.
151      *
152      * @param theClass the class to parse for testing.
153      * @return the constructor to create a new test instance with.
154      * @exception NoSuchMethodException if the class does not have a
155      * constructor with the arguments returned by
156      * <tt>getConstructorArgTypes</tt>.
157      * @see #getConstructorArgTypes( Class )
158      * @see #createTest( Class, Method )
159      * @see #createTestArguments( Class, Method, ImplFactory )
160      */

161     protected Constructor JavaDoc getConstructor( final Class JavaDoc theClass )
162             throws NoSuchMethodException JavaDoc
163     {
164         return theClass.getConstructor(
165             getConstructorArgTypes( theClass ) );
166     }
167     
168     
169     /**
170      * Allows for pluggable constructor types.
171      *
172      * @param theClass the class to parse for testing.
173      * @return the set of classes which define the constructor to extract.
174      */

175     protected Class JavaDoc[] getConstructorArgTypes( Class JavaDoc theClass )
176     {
177         /*
178         return new Class[] {
179             findClass( "java.lang.String" ),
180             findClass( "net.sourceforge.groboutils.junit.v1.iftc.ImplFactory" )
181         };
182         */

183         
184         return new Class JavaDoc[] {
185             String JavaDoc.class,
186             ImplFactory.class
187         };
188     }
189     
190     
191     /**
192      *
193      *
194      * @param theClass the class to parse for testing.
195      * @param m the method that will be tested with the new class instance.
196      */

197     protected Object JavaDoc[] createTestArguments( Class JavaDoc theClass, Method JavaDoc method,
198             ImplFactory implf )
199     {
200         return new Object JavaDoc[] { method.getName(), implf };
201     }
202     
203     
204     /**
205      * Creates a new test class instance.
206      *
207      * @param theClass the class to parse for testing.
208      * @param constructorArgs arguments for the constructor retrieved through
209      * <tt>getConstructor()</tt>.
210      * @return the new Test.
211      * @exception InstantiationException if a new instance could not be made
212      * of the test class.
213      * @exception NoSuchMethodException if the constructor could not be found.
214      * @see #getConstructor( Class )
215      */

216     protected Test createTest( Class JavaDoc theClass, Object JavaDoc[] constructorArgs )
217             throws InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
218             InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
219             ClassCastException JavaDoc
220     {
221         Constructor JavaDoc c = getConstructor( theClass );
222         Test t;
223         try
224         {
225             t = (Test)c.newInstance( constructorArgs );
226         }
227         catch (IllegalArgumentException JavaDoc iae)
228         {
229             StringBuffer JavaDoc args = new StringBuffer JavaDoc(
230                 "Arguments didn't match for constructor " );
231             args.append( c ).append( " in class " ).append(
232                 theClass.getName() ).append( ". Arguments = [" );
233             for (int i = 0; i < constructorArgs.length; ++i)
234             {
235                 if (i > 0)
236                 {
237                     args.append( ", " );
238                 }
239                 args.append( constructorArgs[i].getClass().getName() ).
240                     append( " = '" ).
241                     append( constructorArgs[i] ).
242                     append( "'" );
243             }
244             args.append("]: ").append( iae );
245             throw new InstantiationException JavaDoc( args.toString() );
246         }
247         return t;
248     }
249
250
251
252     
253     
254     /*
255      * JDK 1.1 needs its own implementation of this, to avoid some hairy
256      * situations.
257     private static final Class findClass( String name )
258     {
259         try
260         {
261             return Class.forName( name );
262         }
263         catch (ClassNotFoundException cnfe)
264         {
265             throw new IllegalStateException( cnfe.getMessage() );
266         }
267     }
268      */

269
270 }
271
272
Popular Tags