11
11
package org .junit .jupiter .params .provider ;
12
12
13
13
import static java .lang .String .format ;
14
+ import static java .util .Arrays .stream ;
15
+ import static java .util .stream .Collectors .joining ;
14
16
import static java .util .stream .Collectors .toList ;
15
17
import static org .junit .jupiter .params .provider .Arguments .arguments ;
16
18
import static org .junit .platform .commons .util .AnnotationUtils .isAnnotated ;
17
19
import static org .junit .platform .commons .util .CollectionUtils .isConvertibleToStream ;
18
20
19
21
import java .lang .reflect .Method ;
20
- import java .util .Arrays ;
21
22
import java .util .List ;
22
23
import java .util .function .Predicate ;
23
24
import java .util .stream .Stream ;
28
29
import org .junit .jupiter .api .extension .ExtensionContext ;
29
30
import org .junit .jupiter .params .support .AnnotationConsumer ;
30
31
import org .junit .platform .commons .JUnitException ;
31
- import org .junit .platform .commons .util .ClassUtils ;
32
32
import org .junit .platform .commons .util .CollectionUtils ;
33
33
import org .junit .platform .commons .util .Preconditions ;
34
34
import org .junit .platform .commons .util .ReflectionUtils ;
@@ -50,7 +50,7 @@ public void accept(MethodSource annotation) {
50
50
public Stream <Arguments > provideArguments (ExtensionContext context ) {
51
51
Object testInstance = context .getTestInstance ().orElse (null );
52
52
// @formatter:off
53
- return Arrays . stream (this .methodNames )
53
+ return stream (this .methodNames )
54
54
.map (factoryMethodName -> getFactoryMethod (context , factoryMethodName ))
55
55
.map (factoryMethod -> context .getExecutableInvoker ().invoke (factoryMethod , testInstance ))
56
56
.flatMap (CollectionUtils ::toStream )
@@ -73,11 +73,20 @@ private static boolean looksLikeAFullyQualifiedMethodName(String factoryMethodNa
73
73
if (factoryMethodName .contains ("#" )) {
74
74
return true ;
75
75
}
76
- if ( factoryMethodName . contains ( "." ) && factoryMethodName .contains ( "(" )) {
77
- // Excluding cases of simple method names with parameters
78
- return factoryMethodName . indexOf ( "." ) < factoryMethodName . indexOf ( "(" ) ;
76
+ int indexOfDot = factoryMethodName .indexOf ( "." );
77
+ if ( indexOfDot == - 1 ) {
78
+ return false ;
79
79
}
80
- return factoryMethodName .contains ("." );
80
+ int indexOfOpeningParenthesis = factoryMethodName .indexOf ("(" );
81
+ if (indexOfOpeningParenthesis > 0 ) {
82
+ // Exclude simple method names with parameters
83
+ return indexOfDot < indexOfOpeningParenthesis ;
84
+ }
85
+ // If we get this far, we conclude the supplied factory method name "looks"
86
+ // like it was intended to be a fully qualified method name, even if the
87
+ // syntax is invalid. We do this in order to provide better diagnostics for
88
+ // the user when a fully qualified method name is in fact invalid.
89
+ return true ;
81
90
}
82
91
83
92
private Method getFactoryMethodByFullyQualifiedName (String fullyQualifiedMethodName ) {
@@ -143,12 +152,21 @@ private List<Method> findFactoryMethodsBySimpleName(Class<?> testClass, Method t
143
152
144
153
private static List <Method > filterFactoryMethodsWithMatchingParameters (List <Method > factoryMethods ,
145
154
String factoryMethodName , String factoryMethodParameters ) {
155
+
146
156
if (!factoryMethodName .endsWith (")" )) {
147
- // If parameters are not specified, no choice is made
157
+ // If parameters are not specified, nothing is filtered.
148
158
return factoryMethods ;
149
159
}
150
- Predicate <Method > hasRequiredParameters = method -> factoryMethodParameters .equals (
151
- ClassUtils .nullSafeToString (method .getParameterTypes ()));
160
+
161
+ // Compare against canonical parameter list, ignoring whitespace.
162
+ String parameterList = factoryMethodParameters .replaceAll ("\\ s+" , "" );
163
+ Predicate <Method > hasRequiredParameters = method -> {
164
+ if (parameterList .isEmpty ()) {
165
+ return method .getParameterCount () == 0 ;
166
+ }
167
+ return parameterList .equals (stream (method .getParameterTypes ()).map (Class ::getName ).collect (joining ("," )));
168
+ };
169
+
152
170
return factoryMethods .stream ().filter (hasRequiredParameters ).collect (toList ());
153
171
}
154
172
0 commit comments