KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)JUnitOrigCreator.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
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33 import java.lang.reflect.Constructor JavaDoc;
34
35
36 /**
37  * Emulates the construction mechanism for all JUnit versions.
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @version $Date: 2003/02/10 22:52:21 $
41  * @since November 3, 2002
42  */

43 public class JUnitOrigCreator implements ITestCreator
44 {
45     /**
46      * Creates a new test, based on the given class and method of the class.
47      * This calls <tt>createTest( Class, Object[] )</tt> to create the new
48      * class, which itself calls <tt>getConstructorArgTypes( Class )</tt> to
49      * determine which constructor to get. Also,
50      * <tt>createTestArguments( Class, Method )</tt> is called to generate the
51      * constructor's arguments.
52      *
53      * @param theClass the class to parse for testing.
54      * @param m the method that will be tested with the new class instance.
55      * @exception InstantiationException if there was a problem creating the
56      * class.
57      * @exception NoSuchMethodException if the method does not exist in the
58      * class.
59      * @see #createTest( Class, Object[] )
60      */

61     public Test createTest( Class JavaDoc theClass, Method JavaDoc method )
62             throws InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
63             InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
64             ClassCastException JavaDoc
65     {
66         return createTest( theClass,
67             createTestArguments( theClass, method ) );
68     }
69     
70     
71     /**
72      * Checks if the creator can be used on the given class.
73      *
74      * @param theClass the class to check if parsing is acceptable.
75      */

76     public boolean canCreate( Class JavaDoc theClass )
77     {
78         try
79         {
80             Constructor JavaDoc c = getConstructor( theClass );
81             return (c != null);
82         }
83         catch (Exception JavaDoc ex)
84         {
85             return false;
86         }
87     }
88     
89     
90     /**
91      * Discovers the constructor for the test class which will be used in
92      * the instantiation of a new instance of the class. This constructor
93      * will be discovered through a call to
94      * <tt>getConstructorArgTypes</tt>. The returned constructor must be
95      * callable through <tt>createTestArguments</tt>.
96      *
97      * @param theClass the class to parse for testing.
98      * @return the constructor to create a new test instance with.
99      * @exception NoSuchMethodException if the class does not have a
100      * constructor with the arguments returned by
101      * <tt>getConstructorArgTypes</tt>.
102      * @see #getConstructorArgTypes( Class )
103      * @see #createTest( Class, Method )
104      * @see #createTestArguments( Class, Method )
105      */

106     protected Constructor JavaDoc getConstructor( final Class JavaDoc theClass )
107             throws NoSuchMethodException JavaDoc
108     {
109         return theClass.getConstructor(
110             getConstructorArgTypes( theClass ) );
111     }
112     
113     
114     /**
115      * Allows for pluggable constructor types. The default action is to
116      * return <tt>java.lang.String</tt>.
117      *
118      * @param theClass the class to parse for testing.
119      * @return the set of classes which define the constructor to extract.
120      */

121     protected Class JavaDoc[] getConstructorArgTypes( Class JavaDoc theClass )
122     {
123         return new Class JavaDoc[] { String JavaDoc.class };
124     }
125     
126     
127     /**
128      *
129      *
130      * @param theClass the class to parse for testing.
131      * @param m the method that will be tested with the new class instance.
132      */

133     protected Object JavaDoc[] createTestArguments( Class JavaDoc theClass, Method JavaDoc method )
134     {
135         return new Object JavaDoc[] { method.getName() };
136     }
137     
138     
139     /**
140      * Creates a new test class instance.
141      *
142      * @param theClass the class to parse for testing.
143      * @param constructorArgs arguments for the constructor retrieved through
144      * <tt>getConstructor()</tt>.
145      * @return the new Test.
146      * @exception InstantiationException if a new instance could not be made
147      * of the test class.
148      * @exception NoSuchMethodException if the constructor could not be found.
149      * @see #getConstructor( Class )
150      */

151     protected Test createTest( Class JavaDoc theClass, Object JavaDoc[] constructorArgs )
152             throws InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
153             InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
154             ClassCastException JavaDoc
155     {
156         Constructor JavaDoc c = getConstructor( theClass );
157         Test t;
158         try
159         {
160             t = (Test)c.newInstance( constructorArgs );
161         }
162         catch (IllegalArgumentException JavaDoc iae)
163         {
164             StringBuffer JavaDoc args = new StringBuffer JavaDoc(
165                 "Arguments didn't match for constructor " );
166             args.append( c ).append( " in class " ).append(
167                 theClass.getName() ).append( ". Arguments = [" );
168             for (int i = 0; i < constructorArgs.length; ++i)
169             {
170                 if (i > 0)
171                 {
172                     args.append( ", " );
173                 }
174                 args.append( constructorArgs[i].getClass().getName() ).
175                     append( " = '" ).
176                     append( constructorArgs[i] ).
177                     append( "'" );
178             }
179             args.append("]: ").append( iae );
180             throw new InstantiationException JavaDoc( args.toString() );
181         }
182         return t;
183     }
184 }
185
186
Popular Tags