-
Notifications
You must be signed in to change notification settings - Fork 3
/
SmartDirtiesPostDiscoveryFilter.java
110 lines (94 loc) · 4.86 KB
/
SmartDirtiesPostDiscoveryFilter.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
package com.github.seregamorph.testsmartcontext;
import static java.util.Collections.singletonList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.platform.engine.FilterResult;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.launcher.PostDiscoveryFilter;
import org.junit.vintage.engine.descriptor.RunnerTestDescriptor;
import org.junit.vintage.engine.descriptor.VintageTestDescriptor;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
/**
* Auto-discovered JUnit platform {@link PostDiscoveryFilter} which reorders and groups integration test classes
* according to their configuration. Note: this class sorts only JUnit 4 tests executed via
* <a href="https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-running">vintage-engine</a>.
* <p>
* For TestNG test classes - see {@link com.github.seregamorph.testsmartcontext.testng.SmartDirtiesSuiteListener}, for
* Jupiter test classes - see {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer}.
*
* @deprecated support of JUnit 4 will be removed in 1.0 release
*/
@Deprecated
public class SmartDirtiesPostDiscoveryFilter implements PostDiscoveryFilter {
@Override
public FilterResult apply(TestDescriptor testDescriptor) {
List<TestDescriptor> childrenToReorder = testDescriptor.getChildren().stream()
.filter(childTestDescriptor -> {
// If it is a testng-engine running TestNG test, rely on SmartDirtiesSuiteListener, because
// TestNG will alphabetically reorder it first anyway.
// Jupiter engine has its own sorting via SmartDirtiesClassOrderer, so skip them as well.
// Reorder only JUnit4 here:
return getTestClassOrNull(childTestDescriptor) != null;
})
.collect(Collectors.toList());
if (childrenToReorder.isEmpty()) {
return FilterResult.included("Empty list");
}
Set<Class<?>> uniqueClasses = childrenToReorder.stream()
.map(this::getTestClass)
.collect(Collectors.toSet());
if (uniqueClasses.size() == 1) {
// This filter is executed several times during discover and execute phases and
// it's not possible to distinguish them here. Sometimes per single test is sent as argument,
// sometimes - the whole suite. If it's a suite more than 1, we can save it and never update.
// If it's 1 - we should also distinguish single test execution.
if (SmartDirtiesTestsHolder.classOrderStateMapSize() <= 1) {
Class<?> testClass = getTestClass(childrenToReorder.get(0));
SmartDirtiesTestsHolder.setTestClassesLists(singletonList(singletonList(testClass)));
}
// the logic here may differ for JUnit 4 via Maven vs IntelliJ:
// Maven calls this filter several times (first per each test, then with all tests)
return FilterResult.included("Skipping single element");
}
childrenToReorder.forEach(testDescriptor::removeChild);
SmartDirtiesTestsSorter sorter = SmartDirtiesTestsSorter.getInstance();
List<List<Class<?>>> testClassesLists = sorter.sort(childrenToReorder, this::getTestClass);
childrenToReorder.forEach(testDescriptor::addChild);
SmartDirtiesTestsHolder.setTestClassesLists(testClassesLists);
return FilterResult.included("sorted");
}
@NonNull
private Class<?> getTestClass(TestDescriptor testDescriptor) {
Class<?> testClass = getTestClassOrNull(testDescriptor);
if (testClass == null) {
throw new UnsupportedOperationException("Unsupported TestDescriptor type " + testDescriptor.getClass()
+ ", failed to obtain test class");
}
return testClass;
}
@SuppressWarnings("RedundantIfStatement")
@Nullable
private Class<?> getTestClassOrNull(TestDescriptor testDescriptor) {
if (JUnitPlatformSupport.isJUnitVintageEnginePresent()) {
Class<?> testClass = getTestClassJUnitVintageEngine(testDescriptor);
if (testClass != null) {
return testClass;
}
}
return null;
}
@Nullable
private Class<?> getTestClassJUnitVintageEngine(TestDescriptor testDescriptor) {
if (testDescriptor instanceof RunnerTestDescriptor) {
RunnerTestDescriptor runnerTestDescriptor = (RunnerTestDescriptor) testDescriptor;
return runnerTestDescriptor.getDescription().getTestClass();
}
if (testDescriptor instanceof VintageTestDescriptor) {
VintageTestDescriptor vintageTestDescriptor = (VintageTestDescriptor) testDescriptor;
return vintageTestDescriptor.getDescription().getTestClass();
}
return null;
}
}