KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > TestSuite


1 package junitx.framework;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestResult;
6
7 import java.io.PrintWriter JavaDoc;
8 import java.io.StringWriter JavaDoc;
9 import java.lang.reflect.Constructor JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.Modifier JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 /**
18  * This class is copy-pasted from <tt>junit.framework.TestSuite</tt>. This was
19  * only done to have access to the <tt>fTests</tt> field in order to sort it.
20  */

21 class TestSuite implements Test {
22
23     protected List JavaDoc fTests = new Vector JavaDoc(10);
24     private String JavaDoc fName;
25
26     /**
27      * Constructs an empty TestSuite.
28      */

29     public TestSuite() {
30     }
31
32     /**
33      * Constructs a TestSuite from the given class. Adds all the methods
34      * starting with "test" as test cases to the suite.
35      * Parts of this method was written at 2337 meters in the Hüffihütte,
36      * Kanton Uri
37      */

38     public TestSuite(final Class JavaDoc theClass) {
39         fName = theClass.getName();
40         Constructor JavaDoc constructor = null;
41         try {
42             constructor = getConstructor(theClass);
43         } catch (NoSuchMethodException JavaDoc e) {
44             addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name)"));
45             return;
46         }
47
48         if (!Modifier.isPublic(theClass.getModifiers())) {
49             addTest(warning("Class " + theClass.getName() + " is not public"));
50             return;
51         }
52
53         Class JavaDoc superClass = theClass;
54         Vector JavaDoc names = new Vector JavaDoc();
55         while (Test.class.isAssignableFrom(superClass)) {
56             Method JavaDoc[] methods = superClass.getDeclaredMethods();
57             for (int i = 0; i < methods.length; i++) {
58                 addTestMethod(methods[i], names, constructor);
59             }
60             superClass = superClass.getSuperclass();
61         }
62         if (fTests.size() == 0) {
63             addTest(warning("No tests found in " + theClass.getName()));
64         }
65     }
66
67     /**
68      * Constructs an empty TestSuite.
69      */

70     public TestSuite(String JavaDoc name) {
71         fName = name;
72     }
73
74     /**
75      * Adds a test to the suite.
76      */

77     public void addTest(Test test) {
78         fTests.add(test);
79     }
80
81     /**
82      * Adds the tests from the given class to the suite
83      */

84     public void addTestSuite(Class JavaDoc testClass) {
85         addTest(new TestSuite(testClass));
86     }
87
88     private void addTestMethod(Method JavaDoc m, List JavaDoc names, Constructor JavaDoc constructor) {
89         String JavaDoc name = m.getName();
90         if (names.contains(name)) {
91             return;
92         }
93
94         if (isPublicTestMethod(m)) {
95             names.add(name);
96
97             Object JavaDoc[] args = new Object JavaDoc[]{name};
98             try {
99                 addTest((Test) constructor.newInstance(args));
100             } catch (InstantiationException JavaDoc e) {
101                 addTest(warning("Cannot instantiate test case: " + name + " (" + exceptionToString(e) + ")"));
102             } catch (InvocationTargetException JavaDoc e) {
103                 addTest(warning("Exception in constructor: " + name + " (" + exceptionToString(e.getTargetException()) + ")"));
104             } catch (IllegalAccessException JavaDoc e) {
105                 addTest(warning("Cannot access test case: " + name + " (" + exceptionToString(e) + ")"));
106             }
107
108         } else { // almost a test method
109
if (isTestMethod(m)) {
110                 addTest(warning("Test method isn't public: " + m.getName()));
111             }
112         }
113     }
114
115     /**
116      * Converts the stack trace into a string
117      */

118     private String JavaDoc exceptionToString(Throwable JavaDoc t) {
119         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
120         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(stringWriter);
121         t.printStackTrace(writer);
122         return stringWriter.toString();
123
124     }
125
126     /**
127      * Counts the number of test cases that will be run by this test.
128      */

129     public int countTestCases() {
130         int count = 0;
131         for (Enumeration JavaDoc e = tests(); e.hasMoreElements();) {
132             Test test = (Test) e.nextElement();
133             count = count + test.countTestCases();
134         }
135         return count;
136     }
137
138     /**
139      * Gets a constructor which takes a single String as
140      * its argument.
141      */

142     private Constructor JavaDoc getConstructor(Class JavaDoc theClass) throws NoSuchMethodException JavaDoc {
143         Class JavaDoc[] args = {String JavaDoc.class};
144         return theClass.getConstructor(args);
145     }
146
147     /**
148      */

149     private boolean isPublicTestMethod(Method JavaDoc m) {
150         return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
151     }
152
153     /**
154      */

155     private boolean isTestMethod(Method JavaDoc m) {
156         String JavaDoc name = m.getName();
157         Class JavaDoc[] parameters = m.getParameterTypes();
158         Class JavaDoc returnType = m.getReturnType();
159         return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
160     }
161
162     /**
163      * Runs the tests and collects their result in a TestResult.
164      */

165     public void run(TestResult result) {
166         for (Enumeration JavaDoc e = tests(); e.hasMoreElements();) {
167             if (result.shouldStop()) {
168                 break;
169             }
170             Test test = (Test) e.nextElement();
171             runTest(test, result);
172         }
173     }
174
175     public void runTest(Test test, TestResult result) {
176         test.run(result);
177     }
178
179     /**
180      * Returns the test at the given index
181      */

182     public Test testAt(int index) {
183         return (Test) fTests.get(index);
184     }
185
186     /**
187      * Returns the number of tests in this suite
188      */

189     public int testCount() {
190         return fTests.size();
191     }
192
193     /**
194      * Returns the tests as an enumeration
195      */

196     public Enumeration JavaDoc tests() {
197         // todo fix this
198
return new Vector JavaDoc(fTests).elements();
199     }
200
201     /**
202      */

203     public String JavaDoc toString() {
204         if (getName() != null) {
205             return getName();
206         }
207         return super.toString();
208     }
209
210     /**
211      * Sets the name of the suite.
212      * @param name The name to set
213      */

214     public void setName(String JavaDoc name) {
215         fName = name;
216     }
217
218     /**
219      * Returns the name of the suite. Not all
220      * test suites have a name and this method
221      * can return null.
222      */

223     public String JavaDoc getName() {
224         return fName;
225     }
226
227     /**
228      * Returns a test which will fail and log a warning message.
229      */

230     private Test warning(final String JavaDoc message) {
231         return new TestCase("warning") {
232             protected void runTest() {
233                 fail(message);
234             }
235         };
236     }
237
238 }
239
240
Popular Tags