36
36
import org .junit .jupiter .engine .descriptor .TestMethodTestDescriptor ;
37
37
import org .junit .jupiter .engine .descriptor .TestTemplateInvocationTestDescriptor ;
38
38
import org .junit .jupiter .engine .descriptor .TestTemplateTestDescriptor ;
39
- import org .junit .jupiter .engine .discovery .predicates .IsNestedTestClass ;
40
39
import org .junit .jupiter .engine .discovery .predicates .IsTestClassWithTests ;
41
40
import org .junit .jupiter .engine .discovery .predicates .IsTestFactoryMethod ;
42
41
import org .junit .jupiter .engine .discovery .predicates .IsTestMethod ;
62
61
class MethodSelectorResolver implements SelectorResolver {
63
62
64
63
private static final MethodFinder methodFinder = new MethodFinder ();
65
- private static final Predicate <Class <?>> testClassPredicate = new IsTestClassWithTests ().or (
66
- new IsNestedTestClass ());
64
+ private final Predicate <Class <?>> testClassPredicate ;
67
65
68
66
private final JupiterConfiguration configuration ;
69
67
private final DiscoveryIssueReporter issueReporter ;
68
+ private final List <MethodType > methodTypes ;
70
69
71
70
MethodSelectorResolver (JupiterConfiguration configuration , DiscoveryIssueReporter issueReporter ) {
72
71
this .configuration = configuration ;
73
72
this .issueReporter = issueReporter ;
73
+ this .methodTypes = MethodType .allPossibilities (issueReporter );
74
+ IsTestClassWithTests classPredicate = new IsTestClassWithTests (issueReporter );
75
+ this .testClassPredicate = classPredicate .or (classPredicate .isNestedTestClass );
74
76
}
75
77
76
78
@ Override
@@ -92,7 +94,7 @@ private Resolution resolve(Context context, List<Class<?>> enclosingClasses, Cla
92
94
}
93
95
Method method = methodSupplier .get ();
94
96
// @formatter:off
95
- Set <Match > matches = Arrays .stream (MethodType . values () )
97
+ Set <Match > matches = methodTypes .stream ()
96
98
.map (methodType -> methodType .resolve (enclosingClasses , testClass , method , context , configuration ))
97
99
.filter (Optional ::isPresent )
98
100
.map (Optional ::get )
@@ -116,7 +118,7 @@ private Resolution resolve(Context context, List<Class<?>> enclosingClasses, Cla
116
118
public Resolution resolve (UniqueIdSelector selector , Context context ) {
117
119
UniqueId uniqueId = selector .getUniqueId ();
118
120
// @formatter:off
119
- return Arrays .stream (MethodType . values () )
121
+ return methodTypes .stream ()
120
122
.map (methodType -> methodType .resolveUniqueIdIntoTestDescriptor (uniqueId , context , configuration ))
121
123
.filter (Optional ::isPresent )
122
124
.map (Optional ::get )
@@ -164,48 +166,34 @@ private Supplier<Set<? extends DiscoverySelector>> expansionCallback(TestDescrip
164
166
};
165
167
}
166
168
167
- private enum MethodType {
168
-
169
- TEST (new IsTestMethod (), TestMethodTestDescriptor .SEGMENT_TYPE ) {
170
- @ Override
171
- protected TestDescriptor createTestDescriptor (UniqueId uniqueId , Class <?> testClass , Method method ,
172
- Supplier <List <Class <?>>> enclosingInstanceTypes , JupiterConfiguration configuration ) {
173
- return new TestMethodTestDescriptor (uniqueId , testClass , method , enclosingInstanceTypes , configuration );
174
- }
175
- },
176
-
177
- TEST_FACTORY (new IsTestFactoryMethod (), TestFactoryTestDescriptor .SEGMENT_TYPE ,
178
- TestFactoryTestDescriptor .DYNAMIC_CONTAINER_SEGMENT_TYPE ,
179
- TestFactoryTestDescriptor .DYNAMIC_TEST_SEGMENT_TYPE ) {
180
- @ Override
181
- protected TestDescriptor createTestDescriptor (UniqueId uniqueId , Class <?> testClass , Method method ,
182
- Supplier <List <Class <?>>> enclosingInstanceTypes , JupiterConfiguration configuration ) {
183
- return new TestFactoryTestDescriptor (uniqueId , testClass , method , enclosingInstanceTypes ,
184
- configuration );
185
- }
186
- },
187
-
188
- TEST_TEMPLATE (new IsTestTemplateMethod (), TestTemplateTestDescriptor .SEGMENT_TYPE ,
189
- TestTemplateInvocationTestDescriptor .SEGMENT_TYPE ) {
190
- @ Override
191
- protected TestDescriptor createTestDescriptor (UniqueId uniqueId , Class <?> testClass , Method method ,
192
- Supplier <List <Class <?>>> enclosingInstanceTypes , JupiterConfiguration configuration ) {
193
- return new TestTemplateTestDescriptor (uniqueId , testClass , method , enclosingInstanceTypes ,
194
- configuration );
195
- }
196
- };
169
+ private static class MethodType {
170
+
171
+ static List <MethodType > allPossibilities (DiscoveryIssueReporter issueReporter ) {
172
+ return Arrays .asList ( //
173
+ new MethodType (new IsTestMethod (issueReporter ), TestMethodTestDescriptor ::new ,
174
+ TestMethodTestDescriptor .SEGMENT_TYPE ), //
175
+ new MethodType (new IsTestFactoryMethod (issueReporter ), TestFactoryTestDescriptor ::new ,
176
+ TestFactoryTestDescriptor .SEGMENT_TYPE , TestFactoryTestDescriptor .DYNAMIC_CONTAINER_SEGMENT_TYPE ,
177
+ TestFactoryTestDescriptor .DYNAMIC_TEST_SEGMENT_TYPE ), //
178
+ new MethodType (new IsTestTemplateMethod (issueReporter ), TestTemplateTestDescriptor ::new ,
179
+ TestTemplateTestDescriptor .SEGMENT_TYPE , TestTemplateInvocationTestDescriptor .SEGMENT_TYPE ) //
180
+ );
181
+ }
197
182
198
183
private final Predicate <Method > methodPredicate ;
184
+ private final TestDescriptorFactory testDescriptorFactory ;
199
185
private final String segmentType ;
200
186
private final Set <String > dynamicDescendantSegmentTypes ;
201
187
202
- MethodType (Predicate <Method > methodPredicate , String segmentType , String ... dynamicDescendantSegmentTypes ) {
188
+ private MethodType (Predicate <Method > methodPredicate , TestDescriptorFactory testDescriptorFactory ,
189
+ String segmentType , String ... dynamicDescendantSegmentTypes ) {
203
190
this .methodPredicate = methodPredicate ;
191
+ this .testDescriptorFactory = testDescriptorFactory ;
204
192
this .segmentType = segmentType ;
205
193
this .dynamicDescendantSegmentTypes = new LinkedHashSet <>(Arrays .asList (dynamicDescendantSegmentTypes ));
206
194
}
207
195
208
- private Optional <TestDescriptor > resolve (List <Class <?>> enclosingClasses , Class <?> testClass , Method method ,
196
+ Optional <TestDescriptor > resolve (List <Class <?>> enclosingClasses , Class <?> testClass , Method method ,
209
197
Context context , JupiterConfiguration configuration ) {
210
198
if (!methodPredicate .test (method )) {
211
199
return Optional .empty ();
@@ -221,7 +209,7 @@ private DiscoverySelector selectClass(List<Class<?>> enclosingClasses, Class<?>
221
209
return DiscoverySelectors .selectNestedClass (enclosingClasses , testClass );
222
210
}
223
211
224
- private Optional <TestDescriptor > resolveUniqueIdIntoTestDescriptor (UniqueId uniqueId , Context context ,
212
+ Optional <TestDescriptor > resolveUniqueIdIntoTestDescriptor (UniqueId uniqueId , Context context ,
225
213
JupiterConfiguration configuration ) {
226
214
UniqueId .Segment lastSegment = uniqueId .getLastSegment ();
227
215
if (segmentType .equals (lastSegment .getType ())) {
@@ -244,8 +232,8 @@ private Optional<TestDescriptor> resolveUniqueIdIntoTestDescriptor(UniqueId uniq
244
232
private TestDescriptor createTestDescriptor (TestDescriptor parent , Class <?> testClass , Method method ,
245
233
JupiterConfiguration configuration ) {
246
234
UniqueId uniqueId = createUniqueId (method , parent );
247
- return createTestDescriptor (uniqueId , testClass , method , (( TestClassAware ) parent ):: getEnclosingTestClasses ,
248
- configuration );
235
+ return testDescriptorFactory . create (uniqueId , testClass , method ,
236
+ (( TestClassAware ) parent ):: getEnclosingTestClasses , configuration );
249
237
}
250
238
251
239
private UniqueId createUniqueId (Method method , TestDescriptor parent ) {
@@ -254,8 +242,10 @@ private UniqueId createUniqueId(Method method, TestDescriptor parent) {
254
242
return parent .getUniqueId ().append (segmentType , methodId );
255
243
}
256
244
257
- protected abstract TestDescriptor createTestDescriptor (UniqueId uniqueId , Class <?> testClass , Method method ,
258
- Supplier <List <Class <?>>> enclosingInstanceTypes , JupiterConfiguration configuration );
245
+ interface TestDescriptorFactory {
246
+ TestDescriptor create (UniqueId uniqueId , Class <?> testClass , Method method ,
247
+ Supplier <List <Class <?>>> enclosingInstanceTypes , JupiterConfiguration configuration );
248
+ }
259
249
260
250
}
261
251
0 commit comments