KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > AbstractTestSuite


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.cactus.ServletTestCase;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestResult;
36
37 /**
38  * Test Suite that wraps all the tests of the suite in Cactus Test Case
39  * objects so that pure JUnit tests can be run on the server side.
40  *
41  * @version $Id: AbstractTestSuite.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
42  * @since 1.5
43  */

44 public abstract class AbstractTestSuite implements Test
45 {
46     /**
47      * Lists of tests to execute (Test objects)
48      */

49     private Vector JavaDoc tests = new Vector JavaDoc(10);
50
51     /**
52      * Name of the current test suite
53      */

54     private String JavaDoc name;
55     
56     /**
57      * @see junit.framework.TestSuite#TestSuite()
58      */

59     public AbstractTestSuite()
60     {
61     }
62
63     /**
64      * @see junit.framework.TestSuite#TestSuite(Class)
65      */

66     public AbstractTestSuite(final Class JavaDoc theClass)
67     {
68         setName(theClass.getName());
69         Constructor JavaDoc constructor;
70         try
71         {
72             // Avoid generating multiple error messages
73
constructor = getTestConstructor(theClass);
74         }
75         catch (NoSuchMethodException JavaDoc e)
76         {
77             addTest(warning("Class " + theClass.getName()
78                 + " has no public constructor TestCase(String name)"));
79             return;
80         }
81
82         if (!Modifier.isPublic(theClass.getModifiers()))
83         {
84             addTest(warning("Class " + theClass.getName() + " is not public"));
85             return;
86         }
87
88         Class JavaDoc superClass = theClass;
89         Vector JavaDoc names = new Vector JavaDoc();
90         while (Test.class.isAssignableFrom(superClass))
91         {
92             Method JavaDoc[] methods = superClass.getDeclaredMethods();
93             for (int i = 0; i < methods.length; i++)
94             {
95                 addTestMethod(methods[i], names, constructor);
96             }
97             superClass = superClass.getSuperclass();
98         }
99         if (this.tests.size() == 0)
100         {
101             addTest(warning("No tests found in " + theClass.getName()));
102         }
103     }
104
105     /**
106      * @see junit.framework.TestSuite#TestSuite(String)
107      */

108     public AbstractTestSuite(String JavaDoc theName)
109     {
110         setName(theName);
111     }
112
113     /**
114      * @see junit.framework.TestSuite#addTest(Test)
115      */

116     protected void addTest(Test theTest)
117     {
118         this.tests.addElement(theTest);
119     }
120
121     /**
122      * @see junit.framework.TestSuite#addTestSuite(Class)
123      */

124     protected void addTestSuite(Class JavaDoc theTestClass)
125     {
126         addTest(createTestSuite(theTestClass));
127     }
128
129     /**
130      * @see junit.framework.TestSuite#addTestMethod(Method, Vector, Constructor)
131      */

132     private void addTestMethod(Method JavaDoc theMethod, Vector JavaDoc theNames,
133         Constructor JavaDoc theConstructor)
134     {
135         String JavaDoc name = theMethod.getName();
136         if (theNames.contains(name))
137         {
138             return;
139         }
140         if (isPublicTestMethod(theMethod))
141         {
142             theNames.addElement(name);
143
144             try
145             {
146                 // Note: We wrap the Test in a Cactus Test Case
147
Object JavaDoc constructorInstance;
148                 if (theConstructor.getParameterTypes().length == 0)
149                 {
150                     constructorInstance = theConstructor.newInstance(
151                         new Object JavaDoc[0]);
152                     if (constructorInstance instanceof TestCase)
153                     {
154                         ((TestCase) constructorInstance).setName(name);
155                     }
156                 }
157                 else
158                 {
159                     constructorInstance = theConstructor.newInstance(
160                         new Object JavaDoc[] {name});
161                 }
162                 addTest(new ServletTestCase(name, (Test) constructorInstance));
163             }
164             catch (InstantiationException JavaDoc e)
165             {
166                 addTest(warning("Cannot instantiate test case: " + name
167                     + " (" + exceptionToString(e) + ")"));
168             }
169             catch (InvocationTargetException JavaDoc e)
170             {
171                 addTest(warning("Exception in constructor: " + name + " ("
172                     + exceptionToString(e.getTargetException()) + ")"));
173             }
174             catch (IllegalAccessException JavaDoc e)
175             {
176                 addTest(warning("Cannot access test case: " + name + " ("
177                     + exceptionToString(e) + ")"));
178             }
179         }
180         else
181         {
182             // Almost a test method
183
if (isTestMethod(theMethod))
184             {
185                 addTest(warning("Test method isn't public: "
186                     + theMethod.getName()));
187             }
188         }
189     }
190
191     /**
192      * @see junit.framework.TestSuite#exceptionToString(Throwable)
193      */

194     private String JavaDoc exceptionToString(Throwable JavaDoc theThrowable)
195     {
196         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
197         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(stringWriter);
198         theThrowable.printStackTrace(writer);
199         return stringWriter.toString();
200     }
201
202     /**
203      * @see junit.framework.TestSuite#countTestCases()
204      */

205     public int countTestCases()
206     {
207         int count = 0;
208         for (Enumeration JavaDoc e = tests(); e.hasMoreElements();)
209         {
210             Test test = (Test) e.nextElement();
211             count = count + test.countTestCases();
212         }
213         return count;
214     }
215
216     /**
217      * @see junit.framework.TestSuite#isPublicTestMethod(Method)
218      */

219     private boolean isPublicTestMethod(Method JavaDoc theMethod)
220     {
221         return isTestMethod(theMethod)
222             && Modifier.isPublic(theMethod.getModifiers());
223     }
224
225     /**
226      * @see junit.framework.TestSuite#isTestMethod(Method)
227      */

228     private boolean isTestMethod(Method JavaDoc theMethod)
229     {
230         String JavaDoc name = theMethod.getName();
231         Class JavaDoc[] parameters = theMethod.getParameterTypes();
232         Class JavaDoc returnType = theMethod.getReturnType();
233         return parameters.length == 0
234             && name.startsWith("test")
235             && returnType.equals(Void.TYPE);
236     }
237
238     /**
239      * @see junit.framework.TestSuite#run(TestResult)
240      */

241     public void run(TestResult theResult)
242     {
243         for (Enumeration JavaDoc e = tests(); e.hasMoreElements();)
244         {
245             if (theResult.shouldStop())
246             {
247                 break;
248             }
249             Test test = (Test) e.nextElement();
250             runTest(test, theResult);
251         }
252     }
253     
254     /**
255      * @see junit.framework.TestSuite#runTest(Test, TestResult)
256      */

257     protected void runTest(Test theTest, TestResult theResult)
258     {
259         theTest.run(theResult);
260     }
261     
262     /**
263      * @see junit.framework.TestSuite#testAt(int)
264      */

265     protected Test testAt(int theIndex)
266     {
267         return (Test) this.tests.elementAt(theIndex);
268     }
269
270     /**
271      * Gets a constructor which takes a single String as
272      * its argument or a no arg constructor.
273      *
274      * @param theClass the class for which to find the constructor
275      * @return the valid constructor found
276      * @exception NoSuchMethodException if no valid constructor is
277      * found
278      */

279     protected static Constructor JavaDoc getTestConstructor(Class JavaDoc theClass)
280         throws NoSuchMethodException JavaDoc
281     {
282         Constructor JavaDoc result;
283         try
284         {
285             result = theClass.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
286         }
287         catch (NoSuchMethodException JavaDoc e)
288         {
289             result = theClass.getConstructor(new Class JavaDoc[0]);
290         }
291         return result;
292     }
293
294     /**
295      * @see junit.framework.TestSuite#testCount()
296      */

297     protected int testCount()
298     {
299         return this.tests.size();
300     }
301
302     /**
303      * @see junit.framework.TestSuite#tests()
304      */

305     protected Enumeration JavaDoc tests()
306     {
307         return this.tests.elements();
308     }
309
310     /**
311      * @see junit.framework.TestSuite#toString()
312      */

313     public String JavaDoc toString()
314     {
315         if (getName() != null)
316         {
317             return getName();
318         }
319         return super.toString();
320     }
321
322     /**
323      * @see junit.framework.TestSuite#setName(String)
324      */

325     protected void setName(String JavaDoc theName)
326     {
327         this.name = theName;
328     }
329
330     /**
331      * @see junit.framework.TestSuite#getName()
332      */

333     protected String JavaDoc getName()
334     {
335         return this.name;
336     }
337
338     /**
339      * @see junit.framework.TestSuite#warning(String)
340      */

341     private static Test warning(final String JavaDoc theMessage)
342     {
343         return new TestCase("warning")
344         {
345             protected void runTest()
346             {
347                 fail(theMessage);
348             }
349         };
350     }
351
352     /**
353      * @param theTestClass the test class containing the tests to be included
354      * in the Cactus Test Suite
355      * @return a Cactus Test Suite (ex: ServletTestSuite) initialized with a
356      * test class
357      */

358     protected abstract Test createTestSuite(Class JavaDoc theTestClass);
359
360     /**
361      * @param theName the name of the Cactus Test Case
362      * @param theTest the wrapped test
363      * @return a Cactus Test Case object initialized with the give name and
364      * wrapped test
365      */

366     protected abstract Test createCactusTestCase(String JavaDoc theName, Test theTest);
367 }
368
Popular Tags