44 */
55package org .mockito .internal .invocation ;
66
7- import static org .mockito .internal .invocation .MatcherApplicationStrategy .MatcherApplicationType .ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS ;
8- import static org .mockito .internal .invocation .MatcherApplicationStrategy .MatcherApplicationType .MATCH_EACH_VARARGS_WITH_LAST_MATCHER ;
9- import static org .mockito .internal .invocation .MatcherApplicationStrategy .MatcherApplicationType .ONE_MATCHER_PER_ARGUMENT ;
10-
117import java .util .ArrayList ;
128import java .util .List ;
139
2016public class MatcherApplicationStrategy {
2117
2218 private final Invocation invocation ;
23- private final List <ArgumentMatcher <?>> matchers ;
24- private final MatcherApplicationType matchingType ;
19+ private final List <? extends ArgumentMatcher <?>> matchers ;
2520
2621 private MatcherApplicationStrategy (
27- Invocation invocation ,
28- List <ArgumentMatcher <?>> matchers ,
29- MatcherApplicationType matchingType ) {
22+ Invocation invocation , List <? extends ArgumentMatcher <?>> matchers ) {
3023 this .invocation = invocation ;
31- if (matchingType == MATCH_EACH_VARARGS_WITH_LAST_MATCHER ) {
32- int times = varargLength (invocation );
33- this .matchers = appendLastMatcherNTimes (matchers , times );
34- } else {
35- this .matchers = matchers ;
36- }
37-
38- this .matchingType = matchingType ;
24+ this .matchers = matchers ;
3925 }
4026
4127 /**
@@ -51,10 +37,8 @@ private MatcherApplicationStrategy(
5137 * @return never <code>null</code>
5238 */
5339 public static MatcherApplicationStrategy getMatcherApplicationStrategyFor (
54- Invocation invocation , List <ArgumentMatcher <?>> matchers ) {
55-
56- MatcherApplicationType type = getMatcherApplicationType (invocation , matchers );
57- return new MatcherApplicationStrategy (invocation , matchers , type );
40+ Invocation invocation , List <? extends ArgumentMatcher <?>> matchers ) {
41+ return new MatcherApplicationStrategy (invocation , matchers );
5842 }
5943
6044 /**
@@ -74,11 +58,28 @@ public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(
7458 * </ul>
7559 */
7660 public boolean forEachMatcherAndArgument (ArgumentMatcherAction action ) {
77- if (matchingType == ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS ) {
78- return false ;
61+ if (invocation . getArguments (). length == matchers . size () ) {
62+ return argsMatch ( invocation . getArguments (), matchers , action ) ;
7963 }
8064
81- Object [] arguments = invocation .getArguments ();
65+ final boolean isVararg =
66+ invocation .getMethod ().isVarArgs ()
67+ && invocation .getRawArguments ().length == matchers .size ()
68+ && isLastMatcherVarargMatcher (matchers );
69+
70+ if (isVararg ) {
71+ int times = varargLength (invocation );
72+ final List <? extends ArgumentMatcher <?>> matchers = appendLastMatcherNTimes (times );
73+ return argsMatch (invocation .getArguments (), matchers , action );
74+ }
75+
76+ return false ;
77+ }
78+
79+ private boolean argsMatch (
80+ Object [] arguments ,
81+ List <? extends ArgumentMatcher <?>> matchers ,
82+ ArgumentMatcherAction action ) {
8283 for (int i = 0 ; i < arguments .length ; i ++) {
8384 ArgumentMatcher <?> matcher = matchers .get (i );
8485 Object argument = arguments [i ];
@@ -90,33 +91,16 @@ public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) {
9091 return true ;
9192 }
9293
93- private static MatcherApplicationType getMatcherApplicationType (
94- Invocation invocation , List <ArgumentMatcher <?>> matchers ) {
95- final int rawArguments = invocation .getRawArguments ().length ;
96- final int expandedArguments = invocation .getArguments ().length ;
97- final int matcherCount = matchers .size ();
98-
99- if (expandedArguments == matcherCount ) {
100- return ONE_MATCHER_PER_ARGUMENT ;
101- }
102-
103- if (rawArguments == matcherCount && isLastMatcherVarargMatcher (matchers )) {
104- return MATCH_EACH_VARARGS_WITH_LAST_MATCHER ;
105- }
106-
107- return ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS ;
108- }
109-
110- private static boolean isLastMatcherVarargMatcher (final List <ArgumentMatcher <?>> matchers ) {
94+ private static boolean isLastMatcherVarargMatcher (List <? extends ArgumentMatcher <?>> matchers ) {
11195 ArgumentMatcher <?> argumentMatcher = lastMatcher (matchers );
11296 if (argumentMatcher instanceof HamcrestArgumentMatcher <?>) {
11397 return ((HamcrestArgumentMatcher <?>) argumentMatcher ).isVarargMatcher ();
11498 }
11599 return argumentMatcher instanceof VarargMatcher ;
116100 }
117101
118- private static List <ArgumentMatcher <?>> appendLastMatcherNTimes (
119- List < ArgumentMatcher <?>> matchers , int timesToAppendLastMatcher ) {
102+ private List <? extends ArgumentMatcher <?>> appendLastMatcherNTimes (
103+ int timesToAppendLastMatcher ) {
120104 ArgumentMatcher <?> lastMatcher = lastMatcher (matchers );
121105
122106 List <ArgumentMatcher <?>> expandedMatchers = new ArrayList <ArgumentMatcher <?>>(matchers );
@@ -132,13 +116,7 @@ private static int varargLength(Invocation invocation) {
132116 return expandedArgumentCount - rawArgumentCount ;
133117 }
134118
135- private static ArgumentMatcher <?> lastMatcher (List <ArgumentMatcher <?>> matchers ) {
119+ private static ArgumentMatcher <?> lastMatcher (List <? extends ArgumentMatcher <?>> matchers ) {
136120 return matchers .get (matchers .size () - 1 );
137121 }
138-
139- enum MatcherApplicationType {
140- ONE_MATCHER_PER_ARGUMENT ,
141- MATCH_EACH_VARARGS_WITH_LAST_MATCHER ,
142- ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS ;
143- }
144122}
0 commit comments