/
InvocationContainerImpl.java
182 lines (154 loc) · 6.28 KB
/
InvocationContainerImpl.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing;
import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.mockito.internal.invocation.StubInfoImpl;
import org.mockito.internal.verification.DefaultRegisteredInvocations;
import org.mockito.internal.verification.RegisteredInvocations;
import org.mockito.internal.verification.SingleRegisteredInvocation;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.InvocationContainer;
import org.mockito.invocation.MatchableInvocation;
import org.mockito.mock.MockCreationSettings;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubbing;
import org.mockito.stubbing.ValidableAnswer;
@SuppressWarnings("unchecked")
public class InvocationContainerImpl implements InvocationContainer, Serializable {
private static final long serialVersionUID = -5334301962749537177L;
private final LinkedList<StubbedInvocationMatcher> stubbed = new LinkedList<>();
private final DoAnswerStyleStubbing doAnswerStyleStubbing;
private final RegisteredInvocations registeredInvocations;
private final Strictness mockStrictness;
private MatchableInvocation invocationForStubbing;
public InvocationContainerImpl(MockCreationSettings mockSettings) {
this.registeredInvocations = createRegisteredInvocations(mockSettings);
this.mockStrictness = mockSettings.isLenient() ? Strictness.LENIENT : null;
this.doAnswerStyleStubbing = new DoAnswerStyleStubbing();
}
public void setInvocationForPotentialStubbing(MatchableInvocation invocation) {
registeredInvocations.add(invocation.getInvocation());
this.invocationForStubbing = invocation;
}
public void resetInvocationForPotentialStubbing(MatchableInvocation invocationMatcher) {
this.invocationForStubbing = invocationMatcher;
}
public void addAnswer(Answer answer, Strictness stubbingStrictness) {
registeredInvocations.removeLast();
addAnswer(answer, false, stubbingStrictness);
}
/** Adds new stubbed answer and returns the invocation matcher the answer was added to. */
public StubbedInvocationMatcher addAnswer(
Answer answer, boolean isConsecutive, Strictness stubbingStrictness) {
Invocation invocation = invocationForStubbing.getInvocation();
mockingProgress().stubbingCompleted();
if (answer instanceof ValidableAnswer) {
((ValidableAnswer) answer).validateFor(invocation);
}
synchronized (stubbed) {
if (isConsecutive) {
stubbed.getFirst().addAnswer(answer);
} else {
Strictness effectiveStrictness =
stubbingStrictness != null ? stubbingStrictness : this.mockStrictness;
stubbed.addFirst(
new StubbedInvocationMatcher(
answer, invocationForStubbing, effectiveStrictness));
}
return stubbed.getFirst();
}
}
public void addConsecutiveAnswer(Answer answer) {
addAnswer(answer, true, null);
}
Object answerTo(Invocation invocation) throws Throwable {
return findAnswerFor(invocation).answer(invocation);
}
public StubbedInvocationMatcher findAnswerFor(Invocation invocation) {
synchronized (stubbed) {
for (StubbedInvocationMatcher s : stubbed) {
if (s.matches(invocation)) {
s.markStubUsed(invocation);
// TODO we should mark stubbed at the point of stubbing, not at the point where
// the stub is being used
invocation.markStubbed(new StubInfoImpl(s));
return s;
}
}
}
return null;
}
/**
* Sets the answers declared with 'doAnswer' style.
*/
public void setAnswersForStubbing(List<Answer<?>> answers, Strictness strictness) {
doAnswerStyleStubbing.setAnswers(answers, strictness);
}
public boolean hasAnswersForStubbing() {
return !doAnswerStyleStubbing.isSet();
}
public boolean hasInvocationForPotentialStubbing() {
return !registeredInvocations.isEmpty();
}
public void setMethodForStubbing(MatchableInvocation invocation) {
invocationForStubbing = invocation;
assert hasAnswersForStubbing();
for (int i = 0; i < doAnswerStyleStubbing.getAnswers().size(); i++) {
addAnswer(
doAnswerStyleStubbing.getAnswers().get(i),
i != 0,
doAnswerStyleStubbing.getStubbingStrictness());
}
doAnswerStyleStubbing.clear();
}
@Override
public String toString() {
return "invocationForStubbing: " + invocationForStubbing;
}
public List<Invocation> getInvocations() {
return registeredInvocations.getAll();
}
public void clearInvocations() {
registeredInvocations.clear();
}
/**
* Stubbings in descending order, most recent first
*/
public List<Stubbing> getStubbingsDescending() {
return (List) stubbed;
}
/**
* Stubbings in ascending order, most recent last
*/
public Collection<Stubbing> getStubbingsAscending() {
List<Stubbing> result = new LinkedList<>(stubbed);
Collections.reverse(result);
return result;
}
public Object invokedMock() {
return invocationForStubbing.getInvocation().getMock();
}
private RegisteredInvocations createRegisteredInvocations(MockCreationSettings mockSettings) {
return mockSettings.isStubOnly()
? new SingleRegisteredInvocation()
: new DefaultRegisteredInvocations();
}
public Answer findStubbedAnswer() {
synchronized (stubbed) {
for (StubbedInvocationMatcher s : stubbed) {
if (invocationForStubbing.matches(s.getInvocation())) {
return s;
}
}
}
return null;
}
}