1
2
3
4 package junit.framework;
5
6 import java.util.Enumeration;
7 import java.util.Vector;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class TestSuite implements Test {
31
32 private Vector fTests = new Vector(10);
33
34 private String fName;
35
36
37
38
39 public TestSuite() {
40 }
41
42
43
44
45
46
47 public TestSuite(Class theClass, String name) {
48 this(theClass);
49 setName(name);
50 }
51
52
53
54
55
56 public TestSuite(final Class theClass) {
57 if (TestCase.class.isAssignableFrom(theClass)) {
58 fName = theClass.getName();
59
60 TestCase test;
61 try {
62 test = (TestCase) theClass.newInstance();
63 TestMethod[] methods = test.getTestMethods();
64
65 if (methods != null) {
66 for (int i = 0; i < methods.length; i++) {
67 addTestMethod(methods[i], theClass);
68 }
69 }
70 if (fTests.size() == 0) {
71 addTest(warning("No tests found in " + theClass.getName()));
72 }
73 } catch (InstantiationException e) {
74
75 e.printStackTrace();
76 } catch (IllegalAccessException e) {
77
78 e.printStackTrace();
79 }
80 }
81 }
82
83
84
85
86 public TestSuite(String name) {
87 setName(name);
88 }
89
90
91
92
93 public void addTest(Test test) {
94 fTests.addElement(test);
95 }
96
97
98
99
100 public void addTestSuite(Class testClass) {
101 addTest(new TestSuite(testClass));
102 }
103
104 private void addTestMethod(TestMethod m, Class theClass) {
105 addTest(createTest(theClass, m));
106 }
107
108
109
110
111
112 static public Test createTest(Class theClass, TestMethod testMethod) {
113 Test test = null;
114 try {
115 test = (Test) theClass.newInstance();
116 if (test instanceof TestCase) {
117 ((TestCase) test).setTestMethod(testMethod);
118 }
119 } catch (InstantiationException e) {
120 return (warning("Cannot instantiate test case: " + testMethod + " (" + exceptionToString(e) + ")"));
121 } catch (IllegalAccessException e) {
122 return (warning("Cannot access test case: " + testMethod + " (" + exceptionToString(e) + ")"));
123 }
124 return (Test) test;
125 }
126
127
128
129
130 private static String exceptionToString(Throwable t) {
131
132
133
134
135 return t.getMessage();
136
137 }
138
139
140
141
142 public int countTestCases() {
143 int count = 0;
144 for (Enumeration e = tests(); e.hasMoreElements();) {
145 Test test = (Test) e.nextElement();
146 count = count + test.countTestCases();
147 }
148 return count;
149 }
150
151
152
153
154 public void run(TestResult result) {
155 for (Enumeration e = tests(); e.hasMoreElements();) {
156 if (result.shouldStop())
157 break;
158 Test test = (Test) e.nextElement();
159 runTest(test, result);
160 }
161 }
162
163 public void runTest(Test test, TestResult result) {
164 test.run(result);
165 }
166
167
168
169
170 public Test testAt(int index) {
171 return (Test) fTests.elementAt(index);
172 }
173
174
175
176
177 public int testCount() {
178 return fTests.size();
179 }
180
181
182
183
184 public Enumeration tests() {
185 return fTests.elements();
186 }
187
188
189
190 public String toString() {
191 if (getName() != null)
192 return getName();
193 return super.toString();
194 }
195
196
197
198
199
200
201
202 public void setName(String name) {
203 fName = name;
204 }
205
206
207
208
209 public String getName() {
210 return fName;
211 }
212
213
214
215
216 private static Test warning(final String message) {
217 return new TestCase(new TestMethod("warning") {
218 public void run(TestCase testCase) throws Throwable {
219 }
220 }) {
221 protected void runTest() {
222 fail(message);
223 }
224
225 public TestMethod[] getTestMethods() {
226 return null;
227 }
228 };
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243 }