4
4
*/
5
5
package org .mockito .internal .invocation ;
6
6
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
-
11
7
import java .util .ArrayList ;
12
8
import java .util .List ;
13
9
20
16
public class MatcherApplicationStrategy {
21
17
22
18
private final Invocation invocation ;
23
- private final List <ArgumentMatcher <?>> matchers ;
24
- private final MatcherApplicationType matchingType ;
19
+ private final List <? extends ArgumentMatcher <?>> matchers ;
25
20
26
21
private MatcherApplicationStrategy (
27
- Invocation invocation ,
28
- List <ArgumentMatcher <?>> matchers ,
29
- MatcherApplicationType matchingType ) {
22
+ Invocation invocation , List <? extends ArgumentMatcher <?>> matchers ) {
30
23
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 ;
39
25
}
40
26
41
27
/**
@@ -51,10 +37,8 @@ private MatcherApplicationStrategy(
51
37
* @return never <code>null</code>
52
38
*/
53
39
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 );
58
42
}
59
43
60
44
/**
@@ -74,11 +58,28 @@ public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(
74
58
* </ul>
75
59
*/
76
60
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 ) ;
79
63
}
80
64
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 ) {
82
83
for (int i = 0 ; i < arguments .length ; i ++) {
83
84
ArgumentMatcher <?> matcher = matchers .get (i );
84
85
Object argument = arguments [i ];
@@ -90,33 +91,16 @@ public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) {
90
91
return true ;
91
92
}
92
93
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 ) {
111
95
ArgumentMatcher <?> argumentMatcher = lastMatcher (matchers );
112
96
if (argumentMatcher instanceof HamcrestArgumentMatcher <?>) {
113
97
return ((HamcrestArgumentMatcher <?>) argumentMatcher ).isVarargMatcher ();
114
98
}
115
99
return argumentMatcher instanceof VarargMatcher ;
116
100
}
117
101
118
- private static List <ArgumentMatcher <?>> appendLastMatcherNTimes (
119
- List < ArgumentMatcher <?>> matchers , int timesToAppendLastMatcher ) {
102
+ private List <? extends ArgumentMatcher <?>> appendLastMatcherNTimes (
103
+ int timesToAppendLastMatcher ) {
120
104
ArgumentMatcher <?> lastMatcher = lastMatcher (matchers );
121
105
122
106
List <ArgumentMatcher <?>> expandedMatchers = new ArrayList <ArgumentMatcher <?>>(matchers );
@@ -132,13 +116,7 @@ private static int varargLength(Invocation invocation) {
132
116
return expandedArgumentCount - rawArgumentCount ;
133
117
}
134
118
135
- private static ArgumentMatcher <?> lastMatcher (List <ArgumentMatcher <?>> matchers ) {
119
+ private static ArgumentMatcher <?> lastMatcher (List <? extends ArgumentMatcher <?>> matchers ) {
136
120
return matchers .get (matchers .size () - 1 );
137
121
}
138
-
139
- enum MatcherApplicationType {
140
- ONE_MATCHER_PER_ARGUMENT ,
141
- MATCH_EACH_VARARGS_WITH_LAST_MATCHER ,
142
- ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS ;
143
- }
144
122
}
0 commit comments