forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JUnit4TestRunner.java
265 lines (239 loc) · 8.92 KB
/
JUnit4TestRunner.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package org.testng.junit;
import java.util.*;
import java.util.regex.Pattern;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.testng.*;
import org.testng.collections.Lists;
import org.testng.internal.ITestResultNotifier;
import org.testng.internal.TestResult;
import org.testng.internal.invokers.IInvocationStatus;
import org.testng.internal.invokers.InvokedMethod;
/**
* A JUnit TestRunner that records/triggers all information/events necessary to TestNG.
*
* @deprecated - Support for running JUnit tests stands deprecated as of TestNG <code>7.6.2</code>
*/
@Deprecated
public class JUnit4TestRunner implements IJUnitTestRunner {
private final ITestObjectFactory objectFactory;
private final ITestResultNotifier m_parentRunner;
private final List<ITestListener> m_listeners;
private final List<ITestNGMethod> m_methods = Lists.newArrayList();
private Collection<IInvokedMethodListener> m_invokeListeners = Lists.newArrayList();
private final Map<Description, ITestResult> m_foundMethods = new WeakHashMap<>();
public JUnit4TestRunner(ITestObjectFactory objectFactory, ITestResultNotifier tr) {
this.objectFactory = objectFactory;
m_parentRunner = tr;
m_listeners = m_parentRunner.getTestListeners();
}
/**
* Needed from TestRunner in order to figure out what JUnit test methods were run.
*
* @return the list of all JUnit test methods run
*/
@Override
public List<ITestNGMethod> getTestMethods() {
return m_methods;
}
public void setInvokedMethodListeners(Collection<IInvokedMethodListener> listeners) {
m_invokeListeners = listeners;
}
/**
* A <code>start</code> implementation that ignores the <code>TestResult</code>
*
* @param testClass the JUnit test class
*/
@Override
public void run(Class testClass, String... methods) {
start(testClass, methods);
}
/**
* Starts a test run. Analyzes the command line arguments and runs the given test suite.
*
* @param testCase The test class
* @param methods The test methods
* @return The result
*/
public Result start(final Class testCase, final String... methods) {
try {
JUnitCore core = new JUnitCore();
core.addListener(new RL());
Request r = Request.aClass(testCase);
return core.run(
r.filterWith(
new Filter() {
@Override
public boolean shouldRun(Description description) {
if (description == null) {
return false;
}
if (methods.length == 0) {
if (description.getTestClass() != null) {
ITestResult tr = createTestResult(objectFactory, description);
m_foundMethods.put(description, tr);
}
// run everything
return true;
}
for (String m : methods) {
Pattern p = Pattern.compile(m);
if (p.matcher(description.getMethodName()).matches()) {
ITestResult tr = createTestResult(objectFactory, description);
m_foundMethods.put(description, tr);
return true;
}
}
return false;
}
@Override
public String describe() {
return "TestNG method filter";
}
}));
} catch (Throwable t) {
throw new TestNGException("Failure in JUnit mode for class " + testCase.getName(), t);
}
}
private class RL extends RunListener {
private List<Description> notified = new LinkedList<>();
@Override
public void testAssumptionFailure(Failure failure) {
notified.add(failure.getDescription());
ITestResult tr = m_foundMethods.get(failure.getDescription());
validate(tr, failure.getDescription());
runAfterInvocationListeners(tr);
tr.setStatus(TestResult.SKIP);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
tr.setThrowable(failure.getException());
m_parentRunner.addSkippedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestSkipped(tr);
}
}
@Override
public void testFailure(Failure failure) throws Exception {
if (failure == null) {
return;
}
if (isAssumptionFailed(failure)) {
this.testAssumptionFailure(failure);
return;
}
notified.add(failure.getDescription());
ITestResult tr = m_foundMethods.get(failure.getDescription());
if (tr == null) {
// Not a test method, should be a config
tr = createTestResult(objectFactory, failure.getDescription());
runAfterInvocationListeners(tr);
tr.setStatus(TestResult.FAILURE);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
tr.setThrowable(failure.getException());
for (IConfigurationListener l : m_parentRunner.getConfigurationListeners()) {
l.onConfigurationFailure(tr);
}
for (Description childDesc : failure.getDescription().getChildren()) {
testIgnored(childDesc);
}
} else {
runAfterInvocationListeners(tr);
tr.setStatus(TestResult.FAILURE);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
tr.setThrowable(failure.getException());
m_parentRunner.addFailedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestFailure(tr);
}
}
}
@Override
public void testFinished(Description description) throws Exception {
ITestResult tr = m_foundMethods.get(description);
validate(tr, description);
runAfterInvocationListeners(tr);
if (!notified.contains(description)) {
tr.setStatus(TestResult.SUCCESS);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
m_parentRunner.addPassedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestSuccess(tr);
}
}
m_methods.add(tr.getMethod());
}
@Override
public void testIgnored(Description description) throws Exception {
if (!notified.contains(description)) {
notified.add(description);
ITestResult tr = m_foundMethods.get(description);
validate(tr, description);
runAfterInvocationListeners(tr);
tr.setStatus(TestResult.SKIP);
tr.setEndMillis(tr.getStartMillis());
m_parentRunner.addSkippedTest(tr.getMethod(), tr);
m_methods.add(tr.getMethod());
for (ITestListener l : m_listeners) {
l.onTestSkipped(tr);
}
}
}
@Override
public void testRunFinished(Result result) throws Exception {}
@Override
public void testRunStarted(Description description) throws Exception {}
@Override
public void testStarted(Description description) throws Exception {
ITestResult tr = m_foundMethods.get(description);
validate(tr, description);
for (ITestListener l : m_listeners) {
l.onTestStart(tr);
}
}
private void runAfterInvocationListeners(ITestResult tr) {
InvokedMethod im = new InvokedMethod(tr.getEndMillis(), tr);
for (IInvokedMethodListener l : m_invokeListeners) {
l.afterInvocation(im, tr);
}
}
private void validate(ITestResult tr, Description description) {
if (tr == null) {
throw new TestNGException(stringify(description));
}
}
private String stringify(Description description) {
return description.getClassName() + "." + description.getMethodName() + "()";
}
}
private ITestResult createTestResult(ITestObjectFactory objectFactory, Description test) {
JUnit4TestClass tc = new JUnit4TestClass(test);
JUnitTestMethod tm = new JUnit4TestMethod(objectFactory, tc, test);
ITestContext ctx = null;
if (m_parentRunner instanceof ITestContext) {
ctx = (ITestContext) m_parentRunner;
}
TestResult tr = TestResult.newContextAwareTestResult(tm, ctx);
InvokedMethod im = new InvokedMethod(tr.getStartMillis(), tr);
if (tr.getMethod() instanceof IInvocationStatus) {
((IInvocationStatus) tr.getMethod()).setInvokedAt(im.getDate());
}
for (IInvokedMethodListener l : m_invokeListeners) {
l.beforeInvocation(im, tr);
}
return tr;
}
private static boolean isAssumptionFailed(Failure failure) {
//noinspection ThrowableResultOfMethodCallIgnored
final Throwable exception = failure.getException();
//noinspection SimplifiableIfStatement
if (exception == null) {
return false;
}
return "org.junit.internal.AssumptionViolatedException"
.equals(exception.getClass().getCanonicalName());
}
}