KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > dtf > MultipleTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.dtf;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestResult;
12 import org.jboss.logging.Logger;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.lang.reflect.Modifier JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 /**
21  * Extends the regular JUnit TestCase class to allow multiple test methods
22  * to be run under a single test case class.
23  *
24  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
25  */

26 public class MultipleTestCase extends TestCase
27 {
28    private List JavaDoc testMethods = new ArrayList JavaDoc();
29
30    private static final Logger log = Logger.getLogger(MultipleTestCase.class);
31
32    public MultipleTestCase(String JavaDoc name)
33    {
34       super(name);
35       findTestMethods();
36    }
37
38    protected TestResult createResult()
39    {
40       return new MultipleTestResult();
41    }
42
43    public List JavaDoc getTestMethods()
44    {
45       return testMethods;
46    }
47
48    /**
49     * Counts the number of test cases executed by run(TestResult result).
50     */

51    public int countTestCases()
52    {
53       return testMethods.size();
54    }
55
56    private void findTestMethods()
57    {
58       Class JavaDoc superClass = this.getClass();
59       while(Test.class.isAssignableFrom(superClass))
60       {
61          Method JavaDoc[] methods = superClass.getDeclaredMethods();
62          for(int i = 0; i < methods.length; i++)
63          {
64             addTestMethod(methods[i], testMethods);
65          }
66          superClass = superClass.getSuperclass();
67       }
68       if(testMethods.size() == 0)
69       {
70          log.error(this + " does not contain any test methods.");
71          //TODO: Other than log, how should this be communicated? -TME
72
}
73
74    }
75
76    private void addTestMethod(Method JavaDoc m, List JavaDoc names)
77    {
78       String JavaDoc name = m.getName();
79       if(names.contains(name))
80       {
81          return;
82       }
83       if(!isPublicTestMethod(m))
84       {
85          if(isTestMethod(m))
86          {
87             log.warn("Method " + name + " is a test method, but must be made public.");
88          }
89          return;
90       }
91       names.add(name);
92    }
93
94    private boolean isPublicTestMethod(Method JavaDoc m)
95    {
96       return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
97    }
98
99    private boolean isTestMethod(Method JavaDoc m)
100    {
101       String JavaDoc name = m.getName();
102       Class JavaDoc[] parameters = m.getParameterTypes();
103       Class JavaDoc returnType = m.getReturnType();
104       return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
105    }
106
107    public void runBare(String JavaDoc testMethod) throws Throwable JavaDoc
108    {
109       setUp();
110       try
111       {
112          runTest(testMethod);
113       }
114       finally
115       {
116          tearDown();
117       }
118    }
119
120    protected void runTest(String JavaDoc testMethod) throws Throwable JavaDoc
121    {
122       assertNotNull(testMethod);
123       Method JavaDoc runMethod = null;
124       try
125       {
126          // use getMethod to get all public inherited
127
// methods. getDeclaredMethods returns all
128
// methods of this class but excludes the
129
// inherited ones.
130
runMethod = getClass().getMethod(testMethod, null);
131       }
132       catch(NoSuchMethodException JavaDoc e)
133       {
134          fail("Method \"" + testMethod + "\" not found");
135       }
136       if(!Modifier.isPublic(runMethod.getModifiers()))
137       {
138          fail("Method \"" + testMethod + "\" should be public");
139       }
140
141       try
142       {
143          runMethod.invoke(this, new Class JavaDoc[0]);
144       }
145       catch(InvocationTargetException JavaDoc e)
146       {
147          e.fillInStackTrace();
148          throw e.getTargetException();
149       }
150       catch(IllegalAccessException JavaDoc e)
151       {
152          e.fillInStackTrace();
153          throw e;
154       }
155    }
156
157 }
Popular Tags