-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
InvocationsFinder.java
186 lines (166 loc) · 5.83 KB
/
InvocationsFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.mockito.internal.verification.api.InOrderContext;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.Location;
import org.mockito.invocation.MatchableInvocation;
public class InvocationsFinder {
private InvocationsFinder() {}
public static List<Invocation> findInvocations(
List<Invocation> invocations, MatchableInvocation wanted) {
return invocations.stream().filter(wanted::matches).collect(Collectors.toList());
}
public static List<Invocation> findAllMatchingUnverifiedChunks(
List<Invocation> invocations,
MatchableInvocation wanted,
InOrderContext orderingContext) {
List<Invocation> unverified = removeVerifiedInOrder(invocations, orderingContext);
return unverified.stream().filter(wanted::matches).collect(Collectors.toList());
}
/**
* some examples how it works:
*
* Given invocations sequence:
* 1,1,2,1
*
* if wanted is 1 and mode is times(2) then returns
* 1,1
*
* if wanted is 1 and mode is atLeast() then returns
* 1,1,1
*
* if wanted is 1 and mode is times(x), where x != 2 then returns
* 1,1,1
*/
public static List<Invocation> findMatchingChunk(
List<Invocation> invocations,
MatchableInvocation wanted,
int wantedCount,
InOrderContext context) {
List<Invocation> unverified = removeVerifiedInOrder(invocations, context);
List<Invocation> firstChunk = getFirstMatchingChunk(wanted, unverified);
if (wantedCount != firstChunk.size()) {
return findAllMatchingUnverifiedChunks(invocations, wanted, context);
} else {
return firstChunk;
}
}
private static List<Invocation> getFirstMatchingChunk(
MatchableInvocation wanted, List<Invocation> unverified) {
List<Invocation> firstChunk = new LinkedList<>();
for (Invocation invocation : unverified) {
if (wanted.matches(invocation)) {
firstChunk.add(invocation);
} else if (!firstChunk.isEmpty()) {
break;
}
}
return firstChunk;
}
public static Invocation findFirstMatchingUnverifiedInvocation(
List<Invocation> invocations, MatchableInvocation wanted, InOrderContext context) {
for (Invocation invocation : removeVerifiedInOrder(invocations, context)) {
if (wanted.matches(invocation)) {
return invocation;
}
}
return null;
}
public static Invocation findSimilarInvocation(
List<Invocation> invocations, MatchableInvocation wanted) {
Invocation firstSimilar = null;
for (Invocation invocation : invocations) {
if (!wanted.hasSimilarMethod(invocation)) {
continue;
}
if (firstSimilar == null) {
firstSimilar = invocation;
}
if (wanted.hasSameMethod(invocation)) {
return invocation;
}
}
return firstSimilar;
}
public static Invocation findFirstUnverified(List<Invocation> invocations) {
return findFirstUnverified(invocations, null);
}
static Invocation findFirstUnverified(List<Invocation> invocations, Object mock) {
for (Invocation i : invocations) {
boolean mockIsValid = mock == null || mock == i.getMock();
if (!i.isVerified() && mockIsValid) {
return i;
}
}
return null;
}
public static Location getLastLocation(List<Invocation> invocations) {
if (invocations.isEmpty()) {
return null;
} else {
Invocation last = invocations.get(invocations.size() - 1);
return last.getLocation();
}
}
public static Invocation findPreviousVerifiedInOrder(
List<Invocation> invocations, InOrderContext context) {
List<Invocation> verifiedOnly =
invocations.stream().filter(context::isVerified).collect(Collectors.toList());
if (verifiedOnly.isEmpty()) {
return null;
} else {
return verifiedOnly.get(verifiedOnly.size() - 1);
}
}
private static List<Invocation> removeVerifiedInOrder(
List<Invocation> invocations, InOrderContext orderingContext) {
List<Invocation> unverified = new LinkedList<>();
for (Invocation i : invocations) {
if (orderingContext.isVerified(i)) {
unverified.clear();
} else {
unverified.add(i);
}
}
return unverified;
}
public static List<Location> getAllLocations(List<Invocation> invocations) {
List<Location> locations = new LinkedList<>();
for (Invocation invocation : invocations) {
locations.add(invocation.getLocation());
}
return locations;
}
/**
* i3 is unverified here:
*
* i1, i2, i3
* v
*
* all good here:
*
* i1, i2, i3
* v v
*
* @param context
* @param orderedInvocations
*/
public static Invocation findFirstUnverifiedInOrder(
InOrderContext context, List<Invocation> orderedInvocations) {
Invocation candidate = null;
for (Invocation i : orderedInvocations) {
if (!context.isVerified(i)) {
candidate = candidate != null ? candidate : i;
} else {
candidate = null;
}
}
return candidate;
}
}