/
RunListenerAdapter.java
251 lines (213 loc) · 9.55 KB
/
RunListenerAdapter.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
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.vintage.engine.execution;
import static org.junit.vintage.engine.descriptor.VintageTestDescriptor.SEGMENT_TYPE_DYNAMIC;
import java.util.Optional;
import java.util.function.Function;
import org.junit.Ignore;
import org.junit.platform.engine.EngineExecutionListener;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.UniqueId;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.vintage.engine.descriptor.RunnerTestDescriptor;
import org.junit.vintage.engine.descriptor.TestSourceProvider;
import org.junit.vintage.engine.descriptor.VintageTestDescriptor;
import org.junit.vintage.engine.support.UniqueIdReader;
import org.junit.vintage.engine.support.UniqueIdStringifier;
/**
* @since 4.12
*/
class RunListenerAdapter extends RunListener {
private final TestRun testRun;
private final EngineExecutionListener listener;
private final TestSourceProvider testSourceProvider;
private final Function<Description, String> uniqueIdExtractor;
RunListenerAdapter(TestRun testRun, EngineExecutionListener listener, TestSourceProvider testSourceProvider) {
this.testRun = testRun;
this.listener = listener;
this.testSourceProvider = testSourceProvider;
this.uniqueIdExtractor = new UniqueIdReader().andThen(new UniqueIdStringifier());
}
@Override
public void testRunStarted(Description description) {
if (description.isSuite() && description.getAnnotation(Ignore.class) == null) {
fireExecutionStarted(testRun.getRunnerTestDescriptor(), EventType.REPORTED);
}
}
@Override
public void testSuiteStarted(Description description) {
RunnerTestDescriptor runnerTestDescriptor = testRun.getRunnerTestDescriptor();
// runnerTestDescriptor is reported in testRunStarted
if (!runnerTestDescriptor.getDescription().equals(description)) {
testStarted(lookupOrRegisterNextTestDescriptor(description), EventType.REPORTED);
}
}
@Override
public void testIgnored(Description description) {
testIgnored(lookupOrRegisterNextTestDescriptor(description), determineReasonForIgnoredTest(description));
}
@Override
public void testStarted(Description description) {
testStarted(lookupOrRegisterNextTestDescriptor(description), EventType.REPORTED);
}
@Override
public void testAssumptionFailure(Failure failure) {
handleFailure(failure, TestExecutionResult::aborted);
}
@Override
public void testFailure(Failure failure) {
handleFailure(failure, TestExecutionResult::failed);
}
@Override
public void testFinished(Description description) {
testFinished(lookupOrRegisterCurrentTestDescriptor(description));
}
@Override
public void testSuiteFinished(Description description) {
RunnerTestDescriptor runnerTestDescriptor = testRun.getRunnerTestDescriptor();
// runnerTestDescriptor is reported in testRunFinished
if (!runnerTestDescriptor.getDescription().equals(description)) {
reportContainerFinished(lookupOrRegisterNextTestDescriptor(description));
}
}
@Override
public void testRunFinished(Result result) {
reportContainerFinished(testRun.getRunnerTestDescriptor());
}
private void reportContainerFinished(TestDescriptor containerTestDescriptor) {
if (testRun.isNotSkipped(containerTestDescriptor)) {
if (testRun.isNotStarted(containerTestDescriptor)) {
fireExecutionStarted(containerTestDescriptor, EventType.SYNTHETIC);
}
testRun.getInProgressTestDescriptorsWithSyntheticStartEvents().stream() //
.filter(this::canFinish) //
.forEach(this::fireExecutionFinished);
if (testRun.isNotFinished(containerTestDescriptor)) {
fireExecutionFinished(containerTestDescriptor);
}
}
}
private TestDescriptor lookupOrRegisterNextTestDescriptor(Description description) {
return lookupOrRegisterTestDescriptor(description, testRun::lookupNextTestDescriptor);
}
private TestDescriptor lookupOrRegisterCurrentTestDescriptor(Description description) {
return lookupOrRegisterTestDescriptor(description, testRun::lookupCurrentTestDescriptor);
}
private TestDescriptor lookupOrRegisterTestDescriptor(Description description,
Function<Description, Optional<VintageTestDescriptor>> lookup) {
return lookup.apply(description).orElseGet(() -> registerDynamicTestDescriptor(description, lookup));
}
private VintageTestDescriptor registerDynamicTestDescriptor(Description description,
Function<Description, Optional<VintageTestDescriptor>> lookup) {
// workaround for dynamic children as used by Spock's Runner
TestDescriptor parent = findParent(description, lookup);
UniqueId uniqueId = parent.getUniqueId().append(SEGMENT_TYPE_DYNAMIC, uniqueIdExtractor.apply(description));
VintageTestDescriptor dynamicDescriptor = new VintageTestDescriptor(uniqueId, description,
testSourceProvider.findTestSource(description));
parent.addChild(dynamicDescriptor);
testRun.registerDynamicTest(dynamicDescriptor);
dynamicTestRegistered(dynamicDescriptor);
return dynamicDescriptor;
}
private TestDescriptor findParent(Description description,
Function<Description, Optional<VintageTestDescriptor>> lookup) {
// @formatter:off
return Optional.ofNullable(description.getTestClass())
.map(Description::createSuiteDescription)
.flatMap(lookup)
.orElseGet(testRun::getRunnerTestDescriptor);
// @formatter:on
}
private void handleFailure(Failure failure, Function<Throwable, TestExecutionResult> resultCreator) {
handleFailure(failure, resultCreator, lookupOrRegisterCurrentTestDescriptor(failure.getDescription()));
}
private void handleFailure(Failure failure, Function<Throwable, TestExecutionResult> resultCreator,
TestDescriptor testDescriptor) {
TestExecutionResult result = resultCreator.apply(failure.getException());
testRun.storeResult(testDescriptor, result);
if (testRun.isNotStarted(testDescriptor)) {
testStarted(testDescriptor, EventType.SYNTHETIC);
}
if (testRun.isNotFinished(testDescriptor) && testDescriptor.isContainer()
&& testRun.hasSyntheticStartEvent(testDescriptor)
&& testRun.isDescendantOfRunnerTestDescriptor(testDescriptor)) {
testFinished(testDescriptor);
}
}
private void testIgnored(TestDescriptor testDescriptor, String reason) {
fireExecutionFinishedForInProgressNonAncestorTestDescriptorsWithSyntheticStartEvents(testDescriptor);
fireExecutionStartedIncludingUnstartedAncestors(testDescriptor.getParent());
fireExecutionSkipped(testDescriptor, reason);
}
private String determineReasonForIgnoredTest(Description description) {
Ignore ignoreAnnotation = description.getAnnotation(Ignore.class);
return Optional.ofNullable(ignoreAnnotation).map(Ignore::value).orElse("<unknown>");
}
private void dynamicTestRegistered(TestDescriptor testDescriptor) {
fireExecutionStartedIncludingUnstartedAncestors(testDescriptor.getParent());
listener.dynamicTestRegistered(testDescriptor);
}
private void testStarted(TestDescriptor testDescriptor, EventType eventType) {
fireExecutionFinishedForInProgressNonAncestorTestDescriptorsWithSyntheticStartEvents(testDescriptor);
fireExecutionStartedIncludingUnstartedAncestors(testDescriptor.getParent());
fireExecutionStarted(testDescriptor, eventType);
}
private void fireExecutionFinishedForInProgressNonAncestorTestDescriptorsWithSyntheticStartEvents(
TestDescriptor testDescriptor) {
testRun.getInProgressTestDescriptorsWithSyntheticStartEvents().stream() //
.filter(it -> !isAncestor(it, testDescriptor) && canFinish(it)) //
.forEach(this::fireExecutionFinished);
}
private boolean isAncestor(TestDescriptor candidate, TestDescriptor testDescriptor) {
Optional<TestDescriptor> parent = testDescriptor.getParent();
if (!parent.isPresent()) {
return false;
}
if (parent.get().equals(candidate)) {
return true;
}
return isAncestor(candidate, parent.get());
}
private void testFinished(TestDescriptor descriptor) {
fireExecutionFinished(descriptor);
}
private void fireExecutionStartedIncludingUnstartedAncestors(Optional<TestDescriptor> parent) {
if (parent.isPresent() && canStart(parent.get())) {
fireExecutionStartedIncludingUnstartedAncestors(parent.get().getParent());
fireExecutionStarted(parent.get(), EventType.SYNTHETIC);
}
}
private boolean canStart(TestDescriptor testDescriptor) {
return testRun.isNotStarted(testDescriptor) //
&& (testDescriptor.equals(testRun.getRunnerTestDescriptor())
|| testRun.isDescendantOfRunnerTestDescriptor(testDescriptor));
}
private boolean canFinish(TestDescriptor testDescriptor) {
return testRun.isNotFinished(testDescriptor) //
&& testRun.isDescendantOfRunnerTestDescriptor(testDescriptor)
&& testRun.areAllFinishedOrSkipped(testDescriptor.getChildren());
}
private void fireExecutionSkipped(TestDescriptor testDescriptor, String reason) {
testRun.markSkipped(testDescriptor);
listener.executionSkipped(testDescriptor, reason);
}
private void fireExecutionStarted(TestDescriptor testDescriptor, EventType eventType) {
testRun.markStarted(testDescriptor, eventType);
listener.executionStarted(testDescriptor);
}
private void fireExecutionFinished(TestDescriptor testDescriptor) {
testRun.markFinished(testDescriptor);
listener.executionFinished(testDescriptor, testRun.getStoredResultOrSuccessful(testDescriptor));
}
}