/
TestRun.java
276 lines (235 loc) · 9.8 KB
/
TestRun.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
266
267
268
269
270
271
272
273
274
275
276
/*
* 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 java.util.Collections.emptyList;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Stream.concat;
import static org.junit.platform.engine.TestExecutionResult.failed;
import static org.junit.platform.engine.TestExecutionResult.successful;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.runner.Description;
import org.junit.vintage.engine.descriptor.RunnerTestDescriptor;
import org.junit.vintage.engine.descriptor.VintageTestDescriptor;
import org.opentest4j.MultipleFailuresError;
/**
* @since 4.12
*/
class TestRun {
private final RunnerTestDescriptor runnerTestDescriptor;
private final Set<TestDescriptor> runnerDescendants;
private final Map<Description, VintageDescriptors> descriptionToDescriptors;
private final Map<TestDescriptor, List<TestExecutionResult>> executionResults = new LinkedHashMap<>();
private final Set<TestDescriptor> skippedDescriptors = new LinkedHashSet<>();
private final Set<TestDescriptor> startedDescriptors = new HashSet<>();
private final Map<TestDescriptor, EventType> inProgressDescriptors = new LinkedHashMap<>();
private final Set<TestDescriptor> finishedDescriptors = new LinkedHashSet<>();
private final ThreadLocal<Deque<VintageTestDescriptor>> inProgressDescriptorsByStartingThread = ThreadLocal.withInitial(
ArrayDeque::new);
TestRun(RunnerTestDescriptor runnerTestDescriptor) {
this.runnerTestDescriptor = runnerTestDescriptor;
runnerDescendants = new LinkedHashSet<>(runnerTestDescriptor.getDescendants());
// @formatter:off
descriptionToDescriptors = concat(Stream.of(runnerTestDescriptor), runnerDescendants.stream())
.map(VintageTestDescriptor.class::cast)
.collect(toMap(VintageTestDescriptor::getDescription, VintageDescriptors::new, VintageDescriptors::merge, HashMap::new));
// @formatter:on
}
void registerDynamicTest(VintageTestDescriptor testDescriptor) {
descriptionToDescriptors.computeIfAbsent(testDescriptor.getDescription(), __ -> new VintageDescriptors()).add(
testDescriptor);
runnerDescendants.add(testDescriptor);
}
RunnerTestDescriptor getRunnerTestDescriptor() {
return runnerTestDescriptor;
}
Collection<TestDescriptor> getInProgressTestDescriptorsWithSyntheticStartEvents() {
List<TestDescriptor> result = inProgressDescriptors.entrySet().stream() //
.filter(entry -> entry.getValue().equals(EventType.SYNTHETIC)) //
.map(Entry::getKey) //
.collect(toCollection(ArrayList::new));
Collections.reverse(result);
return result;
}
boolean isDescendantOfRunnerTestDescriptor(TestDescriptor testDescriptor) {
return runnerDescendants.contains(testDescriptor);
}
boolean hasSyntheticStartEvent(TestDescriptor testDescriptor) {
return inProgressDescriptors.get(testDescriptor) == EventType.SYNTHETIC;
}
Optional<VintageTestDescriptor> lookupNextTestDescriptor(Description description) {
return lookupUnambiguouslyOrApplyFallback(description, VintageDescriptors::getNextUnstarted);
}
Optional<VintageTestDescriptor> lookupCurrentTestDescriptor(Description description) {
return lookupUnambiguouslyOrApplyFallback(description, __ -> {
VintageTestDescriptor lastStarted = inProgressDescriptorsByStartingThread.get().peekLast();
if (lastStarted != null && description.equals(lastStarted.getDescription())) {
return Optional.of(lastStarted);
}
return Optional.empty();
});
}
private Optional<VintageTestDescriptor> lookupUnambiguouslyOrApplyFallback(Description description,
Function<VintageDescriptors, Optional<VintageTestDescriptor>> fallback) {
VintageDescriptors vintageDescriptors = descriptionToDescriptors.getOrDefault(description,
VintageDescriptors.NONE);
Optional<VintageTestDescriptor> result = vintageDescriptors.getUnambiguously(description);
if (!result.isPresent()) {
result = fallback.apply(vintageDescriptors);
}
return result;
}
void markSkipped(TestDescriptor testDescriptor) {
skippedDescriptors.add(testDescriptor);
if (testDescriptor instanceof VintageTestDescriptor) {
VintageTestDescriptor vintageDescriptor = (VintageTestDescriptor) testDescriptor;
descriptionToDescriptors.get(vintageDescriptor.getDescription()).incrementSkippedOrStarted();
}
}
boolean isNotSkipped(TestDescriptor testDescriptor) {
return !isSkipped(testDescriptor);
}
boolean isSkipped(TestDescriptor testDescriptor) {
return skippedDescriptors.contains(testDescriptor);
}
void markStarted(TestDescriptor testDescriptor, EventType eventType) {
inProgressDescriptors.put(testDescriptor, eventType);
startedDescriptors.add(testDescriptor);
if (testDescriptor instanceof VintageTestDescriptor) {
VintageTestDescriptor vintageDescriptor = (VintageTestDescriptor) testDescriptor;
inProgressDescriptorsByStartingThread.get().addLast(vintageDescriptor);
descriptionToDescriptors.get(vintageDescriptor.getDescription()).incrementSkippedOrStarted();
}
}
boolean isNotStarted(TestDescriptor testDescriptor) {
return !startedDescriptors.contains(testDescriptor);
}
void markFinished(TestDescriptor testDescriptor) {
inProgressDescriptors.remove(testDescriptor);
finishedDescriptors.add(testDescriptor);
if (testDescriptor instanceof VintageTestDescriptor) {
VintageTestDescriptor descriptor = (VintageTestDescriptor) testDescriptor;
inProgressDescriptorsByStartingThread.get().removeLastOccurrence(descriptor);
}
}
boolean isNotFinished(TestDescriptor testDescriptor) {
return !isFinished(testDescriptor);
}
boolean isFinished(TestDescriptor testDescriptor) {
return finishedDescriptors.contains(testDescriptor);
}
boolean areAllFinishedOrSkipped(Set<? extends TestDescriptor> testDescriptors) {
return testDescriptors.stream().allMatch(this::isFinishedOrSkipped);
}
boolean isFinishedOrSkipped(TestDescriptor testDescriptor) {
return isFinished(testDescriptor) || isSkipped(testDescriptor);
}
void storeResult(TestDescriptor testDescriptor, TestExecutionResult result) {
List<TestExecutionResult> testExecutionResults = executionResults.computeIfAbsent(testDescriptor,
key -> new ArrayList<>());
testExecutionResults.add(result);
}
TestExecutionResult getStoredResultOrSuccessful(TestDescriptor testDescriptor) {
List<TestExecutionResult> testExecutionResults = executionResults.get(testDescriptor);
if (testExecutionResults == null) {
return successful();
}
if (testExecutionResults.size() == 1) {
return testExecutionResults.get(0);
}
// @formatter:off
List<Throwable> failures = testExecutionResults
.stream()
.map(TestExecutionResult::getThrowable)
.map(Optional::get)
.collect(toList());
// @formatter:on
MultipleFailuresError multipleFailuresError = new MultipleFailuresError("", failures);
failures.forEach(multipleFailuresError::addSuppressed);
return failed(multipleFailuresError);
}
private static class VintageDescriptors {
private static final VintageDescriptors NONE = new VintageDescriptors(emptyList());
private final List<VintageTestDescriptor> descriptors;
private int skippedOrStartedCount;
static VintageDescriptors merge(VintageDescriptors a, VintageDescriptors b) {
List<VintageTestDescriptor> mergedDescriptors = new ArrayList<>(
a.descriptors.size() + b.descriptors.size());
mergedDescriptors.addAll(a.descriptors);
mergedDescriptors.addAll(b.descriptors);
return new VintageDescriptors(mergedDescriptors);
}
VintageDescriptors(VintageTestDescriptor vintageTestDescriptor) {
this();
add(vintageTestDescriptor);
}
VintageDescriptors() {
this(new ArrayList<>(1));
}
VintageDescriptors(List<VintageTestDescriptor> descriptors) {
this.descriptors = descriptors;
}
void add(VintageTestDescriptor descriptor) {
descriptors.add(descriptor);
}
/**
* Returns the {@link TestDescriptor} that represents the specified
* {@link Description}.
*
* <p>There are edge cases where multiple {@link Description Descriptions}
* with the same {@code uniqueId} exist, e.g. when using overloaded methods
* to define {@linkplain org.junit.experimental.theories.Theory theories}.
* In this case, we try to find the correct {@link TestDescriptor} by
* checking for object identity on the {@link Description} it represents.
*
* @param description the {@code Description} to look up
*/
Optional<VintageTestDescriptor> getUnambiguously(Description description) {
if (descriptors.isEmpty()) {
return Optional.empty();
}
if (descriptors.size() == 1) {
return Optional.of(descriptors.get(0));
}
// @formatter:off
return descriptors.stream()
.filter(testDescriptor -> description == testDescriptor.getDescription())
.findFirst();
// @formatter:on
}
public void incrementSkippedOrStarted() {
skippedOrStartedCount++;
}
public Optional<VintageTestDescriptor> getNextUnstarted() {
if (skippedOrStartedCount < descriptors.size()) {
return Optional.of(descriptors.get(skippedOrStartedCount));
}
return Optional.empty();
}
}
}