1111package org .junit .jupiter .params .provider ;
1212
1313import static java .lang .String .format ;
14+ import static java .util .Arrays .stream ;
15+ import static java .util .stream .Collectors .joining ;
1416import static java .util .stream .Collectors .toList ;
1517import static org .junit .jupiter .params .provider .Arguments .arguments ;
1618import static org .junit .platform .commons .util .AnnotationUtils .isAnnotated ;
1719import static org .junit .platform .commons .util .CollectionUtils .isConvertibleToStream ;
1820
1921import java .lang .reflect .Method ;
20- import java .util .Arrays ;
2122import java .util .List ;
2223import java .util .function .Predicate ;
2324import java .util .stream .Stream ;
2829import org .junit .jupiter .api .extension .ExtensionContext ;
2930import org .junit .jupiter .params .support .AnnotationConsumer ;
3031import org .junit .platform .commons .JUnitException ;
31- import org .junit .platform .commons .util .ClassUtils ;
3232import org .junit .platform .commons .util .CollectionUtils ;
3333import org .junit .platform .commons .util .Preconditions ;
3434import org .junit .platform .commons .util .ReflectionUtils ;
@@ -50,7 +50,7 @@ public void accept(MethodSource annotation) {
5050 public Stream <Arguments > provideArguments (ExtensionContext context ) {
5151 Object testInstance = context .getTestInstance ().orElse (null );
5252 // @formatter:off
53- return Arrays . stream (this .methodNames )
53+ return stream (this .methodNames )
5454 .map (factoryMethodName -> getFactoryMethod (context , factoryMethodName ))
5555 .map (factoryMethod -> context .getExecutableInvoker ().invoke (factoryMethod , testInstance ))
5656 .flatMap (CollectionUtils ::toStream )
@@ -73,11 +73,20 @@ private static boolean looksLikeAFullyQualifiedMethodName(String factoryMethodNa
7373 if (factoryMethodName .contains ("#" )) {
7474 return true ;
7575 }
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 ;
7979 }
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 ;
8190 }
8291
8392 private Method getFactoryMethodByFullyQualifiedName (String fullyQualifiedMethodName ) {
@@ -143,12 +152,21 @@ private List<Method> findFactoryMethodsBySimpleName(Class<?> testClass, Method t
143152
144153 private static List <Method > filterFactoryMethodsWithMatchingParameters (List <Method > factoryMethods ,
145154 String factoryMethodName , String factoryMethodParameters ) {
155+
146156 if (!factoryMethodName .endsWith (")" )) {
147- // If parameters are not specified, no choice is made
157+ // If parameters are not specified, nothing is filtered.
148158 return factoryMethods ;
149159 }
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+
152170 return factoryMethods .stream ().filter (hasRequiredParameters ).collect (toList ());
153171 }
154172
0 commit comments