13
13
import static org .junit .platform .commons .support .AnnotationSupport .findAnnotation ;
14
14
import static org .junit .platform .commons .support .AnnotationSupport .findRepeatableAnnotations ;
15
15
import static org .junit .platform .commons .support .AnnotationSupport .isAnnotated ;
16
- import static org .junit .platform .commons .util .CollectionUtils .getFirstElement ;
17
16
18
- import java .lang .reflect .Constructor ;
19
17
import java .lang .reflect .Method ;
20
- import java .util .Optional ;
21
18
import java .util .concurrent .atomic .AtomicLong ;
22
19
import java .util .stream .Stream ;
23
20
29
26
import org .junit .jupiter .params .provider .ArgumentsProvider ;
30
27
import org .junit .jupiter .params .provider .ArgumentsSource ;
31
28
import org .junit .jupiter .params .support .AnnotationConsumerInitializer ;
32
- import org .junit .platform .commons .JUnitException ;
33
- import org .junit .platform .commons .PreconditionViolationException ;
34
- import org .junit .platform .commons .support .ModifierSupport ;
35
29
import org .junit .platform .commons .util .ExceptionUtils ;
36
30
import org .junit .platform .commons .util .Preconditions ;
37
- import org .junit .platform .commons .util .ReflectionUtils ;
38
31
39
32
/**
40
33
* @since 5.0
@@ -57,7 +50,7 @@ public boolean supportsTestTemplate(ExtensionContext context) {
57
50
return false ;
58
51
}
59
52
60
- ParameterizedTestMethodContext methodContext = new ParameterizedTestMethodContext (testMethod );
53
+ ParameterizedTestMethodContext methodContext = new ParameterizedTestMethodContext (testMethod , context );
61
54
62
55
Preconditions .condition (methodContext .hasPotentiallyValidSignature (),
63
56
() -> String .format (
@@ -89,7 +82,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
89
82
return findRepeatableAnnotations (templateMethod , ArgumentsSource .class )
90
83
.stream ()
91
84
.map (ArgumentsSource ::value )
92
- .map (clazz -> instantiateArgumentsProvider ( clazz , extensionContext ))
85
+ .map (clazz -> ParameterizedTestSpiInstantiator . instantiate ( ArgumentsProvider . class , clazz , extensionContext ))
93
86
.map (provider -> AnnotationConsumerInitializer .initialize (templateMethod , provider ))
94
87
.flatMap (provider -> arguments (provider , extensionContext ))
95
88
.map (arguments -> {
@@ -102,35 +95,6 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
102
95
// @formatter:on
103
96
}
104
97
105
- private ArgumentsProvider instantiateArgumentsProvider (Class <? extends ArgumentsProvider > clazz ,
106
- ExtensionContext extensionContext ) {
107
- return extensionContext .getExecutableInvoker ().invoke (findConstructor (ArgumentsProvider .class , clazz ));
108
- }
109
-
110
- @ SuppressWarnings ("unchecked" )
111
- private static <T > Constructor <? extends T > findConstructor (Class <T > spiClass , Class <? extends T > clazz ) {
112
- Optional <Constructor <?>> defaultConstructor = getFirstElement (
113
- ReflectionUtils .findConstructors (clazz , it -> it .getParameterCount () == 0 ));
114
- if (defaultConstructor .isPresent ()) {
115
- return (Constructor <? extends T >) defaultConstructor .get ();
116
- }
117
- if (ModifierSupport .isNotStatic (clazz )) {
118
- String message = String .format ("The %s [%s] must be either a top-level class or a static nested class" ,
119
- spiClass .getSimpleName (), clazz .getName ());
120
- throw new JUnitException (message );
121
- }
122
- try {
123
- return ReflectionUtils .getDeclaredConstructor (clazz );
124
- }
125
- catch (PreconditionViolationException ex ) {
126
- String message = String .format (
127
- "Failed to find constructor for %s [%s]. "
128
- + "Please ensure that a no-argument or a single constructor exists." ,
129
- spiClass .getSimpleName (), clazz .getName ());
130
- throw new JUnitException (message );
131
- }
132
- }
133
-
134
98
private ExtensionContext .Store getStore (ExtensionContext context ) {
135
99
return context .getStore (Namespace .create (ParameterizedTestExtension .class , context .getRequiredTestMethod ()));
136
100
}
0 commit comments