|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model.junit; |
|
38 |
| |
|
39 |
| import junit.framework.*; |
|
40 |
| |
|
41 |
| import java.io.File; |
|
42 |
| import java.util.Enumeration; |
|
43 |
| import java.util.List; |
|
44 |
| import java.util.ArrayList; |
|
45 |
| import java.util.Arrays; |
|
46 |
| |
|
47 |
| import edu.rice.cs.util.Log; |
|
48 |
| import edu.rice.cs.util.StringOps; |
|
49 |
| import edu.rice.cs.util.UnexpectedException; |
|
50 |
| import edu.rice.cs.util.swing.Utilities; |
|
51 |
| import edu.rice.cs.util.classloader.ClassFileError; |
|
52 |
| import edu.rice.cs.plt.io.IOUtil; |
|
53 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
54 |
| import edu.rice.cs.plt.tuple.Pair; |
|
55 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
56 |
| import edu.rice.cs.plt.reflect.ShadowingClassLoader; |
|
57 |
| |
|
58 |
| import java.lang.reflect.Modifier; |
|
59 |
| |
|
60 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
61 |
| import static edu.rice.cs.plt.debug.DebugUtil.error; |
|
62 |
| |
|
63 |
| import edu.rice.cs.drjava.model.compiler.LanguageLevelStackTraceMapper; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| public class JUnitTestManager { |
|
70 |
| |
|
71 |
| protected static final Log _log = new Log("JUnitTestManager.txt", false); |
|
72 |
| |
|
73 |
| |
|
74 |
| private final JUnitModelCallback _jmc; |
|
75 |
| |
|
76 |
| |
|
77 |
| private final Lambda<ClassLoader, ClassLoader> _loaderFactory; |
|
78 |
| |
|
79 |
| |
|
80 |
| private JUnitTestRunner _testRunner; |
|
81 |
| |
|
82 |
| |
|
83 |
| private TestSuite _suite = null; |
|
84 |
| |
|
85 |
| |
|
86 |
| private List<String> _testClassNames = null; |
|
87 |
| |
|
88 |
| |
|
89 |
| private List<File> _testFiles = null; |
|
90 |
| |
|
91 |
| |
|
92 |
180
| public JUnitTestManager(JUnitModelCallback jmc, Lambda<ClassLoader, ClassLoader> loaderFactory) {
|
|
93 |
180
| _jmc = jmc;
|
|
94 |
180
| _loaderFactory = loaderFactory;
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
18
| public List<String> findTestClasses(final List<String> classNames, final List<File> files) {
|
|
103 |
| |
|
104 |
18
| _log.log("findTestClasses(" + classNames + ", " + files + ")");
|
|
105 |
| |
|
106 |
18
| if (_testClassNames != null && ! _testClassNames.isEmpty())
|
|
107 |
0
| throw new IllegalStateException("Test suite is still pending!");
|
|
108 |
| |
|
109 |
18
| _testRunner = makeRunner();
|
|
110 |
| |
|
111 |
18
| _testClassNames = new ArrayList<String>();
|
|
112 |
18
| _testFiles = new ArrayList<File>();
|
|
113 |
18
| _suite = new TestSuite();
|
|
114 |
| |
|
115 |
18
| for (Pair<String, File> pair : IterUtil.zip(classNames, files)) {
|
|
116 |
20
| String cName = pair.first();
|
|
117 |
20
| try {
|
|
118 |
20
| if (_isJUnitTest(_testRunner.loadPossibleTest(cName))) {
|
|
119 |
16
| _testClassNames.add(cName);
|
|
120 |
16
| _testFiles.add(pair.second());
|
|
121 |
16
| _suite.addTest(new JUnit4TestAdapter(_testRunner.loadPossibleTest(cName)));
|
|
122 |
| } |
|
123 |
| } |
|
124 |
0
| catch (ClassNotFoundException e) { error.log(e); }
|
|
125 |
| catch(LinkageError e) { |
|
126 |
| |
|
127 |
0
| String path = IOUtil.attemptAbsoluteFile(pair.second()).getPath();
|
|
128 |
0
| _jmc.classFileError(new ClassFileError(cName, path, e));
|
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
18
| _log.log("returning: " + _testClassNames);
|
|
134 |
18
| return _testClassNames;
|
|
135 |
| } |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
16
| @SuppressWarnings("unchecked")
|
|
142 |
| public boolean runTestSuite() { |
|
143 |
| |
|
144 |
16
| _log.log("runTestSuite() called");
|
|
145 |
| |
|
146 |
0
| if (_testClassNames == null || _testClassNames.isEmpty()) return false;
|
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
16
| try {
|
|
151 |
| |
|
152 |
16
| TestResult result = _testRunner.runSuite(_suite);
|
|
153 |
| |
|
154 |
15
| JUnitError[] errors = new JUnitError[result.errorCount() + result.failureCount()];
|
|
155 |
| |
|
156 |
15
| Enumeration<TestFailure> failures = result.failures();
|
|
157 |
15
| Enumeration<TestFailure> errEnum = result.errors();
|
|
158 |
| |
|
159 |
15
| int i = 0;
|
|
160 |
| |
|
161 |
15
| while (errEnum.hasMoreElements()) {
|
|
162 |
13
| TestFailure tErr = errEnum.nextElement();
|
|
163 |
| |
|
164 |
13
| errors[i] = _makeJUnitError(tErr, _testClassNames, true, _testFiles);
|
|
165 |
13
| i++;
|
|
166 |
| } |
|
167 |
| |
|
168 |
15
| while (failures.hasMoreElements()) {
|
|
169 |
0
| TestFailure tFail = failures.nextElement();
|
|
170 |
| |
|
171 |
0
| errors[i] = _makeJUnitError(tFail, _testClassNames, false, _testFiles);
|
|
172 |
0
| i++;
|
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
15
| _reset();
|
|
179 |
15
| _jmc.testSuiteEnded(errors);
|
|
180 |
| } |
|
181 |
| catch(Exception e) { |
|
182 |
0
| JUnitError[] errors = new JUnitError[1];
|
|
183 |
0
| errors[0] = new JUnitError(null, -1, -1, e.getMessage(), false, "", "", e.toString(), e.getStackTrace());
|
|
184 |
0
| _reset();
|
|
185 |
0
| _jmc.testSuiteEnded(errors);
|
|
186 |
| |
|
187 |
| } |
|
188 |
15
| _log.log("Exiting runTestSuite()");
|
|
189 |
15
| return true;
|
|
190 |
| } |
|
191 |
| |
|
192 |
15
| private void _reset() {
|
|
193 |
15
| _suite = null;
|
|
194 |
15
| _testClassNames = null;
|
|
195 |
15
| _testFiles = null;
|
|
196 |
15
| _log.log("test manager state reset");
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
20
| private boolean _isJUnitTest(Class<?> c) {
|
|
206 |
| |
|
207 |
20
| boolean result = (Test.class.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers()) && !Modifier.isInterface(c.getModifiers()) ||
|
|
208 |
| (new JUnit4TestAdapter(c).getTests().size()>0)) && !new JUnit4TestAdapter(c).getTests().get(0).toString().contains("initializationError") |
|
209 |
| ; |
|
210 |
20
| debug.logValues(new String[]{"c", "isJUnitTest(c)"}, c, result);
|
|
211 |
20
| return result;
|
|
212 |
| } |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
13
| private JUnitError _makeJUnitError(TestFailure failure, List<String> classNames, boolean isError, List<File> files) {
|
|
222 |
| |
|
223 |
| |
|
224 |
13
| Test failedTest = failure.failedTest();
|
|
225 |
13
| String testName;
|
|
226 |
| |
|
227 |
13
| if(failedTest instanceof JUnit4TestCaseFacade)
|
|
228 |
| { |
|
229 |
13
| testName = ((JUnit4TestCaseFacade) failedTest).toString();
|
|
230 |
13
| testName = testName.substring(0,testName.indexOf('('));
|
|
231 |
| } |
|
232 |
0
| else testName = failedTest.getClass().getName();
|
|
233 |
| |
|
234 |
13
| String testString = failure.toString();
|
|
235 |
13
| int firstIndex = testString.indexOf('(') + 1;
|
|
236 |
13
| int secondIndex = testString.indexOf(')');
|
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
13
| String className;
|
|
241 |
13
| if (firstIndex != secondIndex)
|
|
242 |
13
| className = testString.substring(firstIndex, secondIndex);
|
|
243 |
| else |
|
244 |
0
| className = testString.substring(0, firstIndex-1);
|
|
245 |
| |
|
246 |
| |
|
247 |
13
| String classNameAndTest = className + "." + testName;
|
|
248 |
| |
|
249 |
13
| String exception = failure.thrownException().toString();
|
|
250 |
13
| StackTraceElement[] stackTrace = failure.thrownException().getStackTrace();
|
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
13
| StringBuilder sb = new StringBuilder();
|
|
258 |
13
| sb.append(exception);
|
|
259 |
13
| sb.append('\n');
|
|
260 |
13
| for(StackTraceElement s: stackTrace) {
|
|
261 |
453
| sb.append("\tat ");
|
|
262 |
453
| sb.append(s);
|
|
263 |
| } |
|
264 |
13
| String combined = sb.toString();
|
|
265 |
13
| int lineNum = -1;
|
|
266 |
| |
|
267 |
13
| if (combined.indexOf(classNameAndTest) == -1) {
|
|
268 |
| |
|
269 |
4
| String trace = failure.trace();
|
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
4
| trace = trace.substring(trace.indexOf('\n')+1);
|
|
277 |
4
| if (trace.trim().length()>0) {
|
|
278 |
4
| while (trace.indexOf("junit.framework.Assert") != -1 &&
|
|
279 |
| trace.indexOf("junit.framework.Assert") < trace.indexOf("(")) { |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
5
| trace = trace.substring(trace.indexOf('\n') + 1);
|
|
287 |
| } |
|
288 |
4
| trace = trace.substring(trace.indexOf('(')+1);
|
|
289 |
4
| trace = trace.substring(0, trace.indexOf(')'));
|
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
4
| if (combined.indexOf(className) == -1) {
|
|
294 |
3
| int dotPos = trace.lastIndexOf('.');
|
|
295 |
3
| if (dotPos!=-1) {
|
|
296 |
3
| className = trace.substring(0,dotPos);
|
|
297 |
3
| classNameAndTest = className + "." + testName;
|
|
298 |
| } |
|
299 |
| } |
|
300 |
| |
|
301 |
4
| try {
|
|
302 |
4
| lineNum = Integer.parseInt(trace.substring(trace.indexOf(':') + 1)) - 1;
|
|
303 |
| } |
|
304 |
0
| catch (NumberFormatException e) { lineNum = 0; }
|
|
305 |
| } |
|
306 |
| } |
|
307 |
| |
|
308 |
13
| if (lineNum < 0) {
|
|
309 |
9
| lineNum = _lineNumber(combined, classNameAndTest);
|
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
13
| String message = (isError) ? failure.thrownException().toString():
|
|
315 |
| failure.thrownException().getMessage(); |
|
316 |
| |
|
317 |
13
| boolean isFailure = (failure.thrownException() instanceof AssertionError || failure.thrownException() instanceof AssertionFailedError) &&
|
|
318 |
| !classNameAndTest.equals("junit.framework.TestSuite$1.warning"); |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
13
| int indexOfClass = classNames.indexOf(className);
|
|
340 |
13
| File file;
|
|
341 |
9
| if (indexOfClass != -1) file = files.get(indexOfClass);
|
|
342 |
4
| else file = _jmc.getFileForClassName(className);
|
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
13
| if (file == null) {
|
|
348 |
0
| return new JUnitError(new File("nofile"), 0, 0, message, !isFailure, testName, className, exception, stackTrace);
|
|
349 |
| } |
|
350 |
| |
|
351 |
13
| return new JUnitError(file, lineNum, 0, message, !isFailure, testName, className, exception, stackTrace);
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
9
| private int _lineNumber(String sw, String classname) {
|
|
356 |
| |
|
357 |
9
| int lineNum;
|
|
358 |
9
| int idxClassname = sw.indexOf(classname);
|
|
359 |
0
| if (idxClassname == -1) return -1;
|
|
360 |
| |
|
361 |
9
| String theLine = sw.substring(idxClassname, sw.length());
|
|
362 |
| |
|
363 |
9
| theLine = theLine.substring(theLine.indexOf(classname), theLine.length());
|
|
364 |
9
| theLine = theLine.substring(theLine.indexOf("(") + 1, theLine.length());
|
|
365 |
9
| theLine = theLine.substring(0, theLine.indexOf(")"));
|
|
366 |
| |
|
367 |
9
| try {
|
|
368 |
9
| int i = theLine.indexOf(":") + 1;
|
|
369 |
9
| lineNum = Integer.parseInt(theLine.substring(i, theLine.length())) - 1;
|
|
370 |
| } |
|
371 |
0
| catch (NumberFormatException e) { lineNum = 0; }
|
|
372 |
| |
|
373 |
9
| return lineNum;
|
|
374 |
| } |
|
375 |
| |
|
376 |
| |
|
377 |
18
| private JUnitTestRunner makeRunner() {
|
|
378 |
18
| ClassLoader current = JUnitTestManager.class.getClassLoader();
|
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
18
| ClassLoader parent = ShadowingClassLoader.whiteList(current, "junit", "org.junit");
|
|
383 |
18
| return new JUnitTestRunner(_jmc, _loaderFactory.value(parent));
|
|
384 |
| } |
|
385 |
| } |