/
InterceptedInvocation.java
169 lines (137 loc) · 4.48 KB
/
InterceptedInvocation.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
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.invocation;
import org.mockito.internal.invocation.mockref.MockReference;
import org.mockito.internal.exceptions.VerificationAwareInvocation;
import org.mockito.internal.reporting.PrintSettings;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.Location;
import org.mockito.invocation.StubInfo;
import java.lang.reflect.Method;
import java.util.Arrays;
import static org.mockito.internal.exceptions.Reporter.cannotCallAbstractRealMethod;
public class InterceptedInvocation implements Invocation, VerificationAwareInvocation {
private static final long serialVersionUID = 475027563923510472L;
private final MockReference<Object> mockRef;
private final MockitoMethod mockitoMethod;
private final Object[] arguments, rawArguments;
private final RealMethod realMethod;
private final int sequenceNumber;
private final Location location;
private boolean verified;
private boolean isIgnoredForVerification;
private StubInfo stubInfo;
public InterceptedInvocation(MockReference<Object> mockRef,
MockitoMethod mockitoMethod,
Object[] arguments,
RealMethod realMethod,
Location location,
int sequenceNumber) {
this.mockRef = mockRef;
this.mockitoMethod = mockitoMethod;
this.arguments = ArgumentsProcessor.expandArgs(mockitoMethod, arguments);
this.rawArguments = arguments;
this.realMethod = realMethod;
this.location = location;
this.sequenceNumber = sequenceNumber;
}
@Override
public boolean isVerified() {
return verified || isIgnoredForVerification;
}
@Override
public int getSequenceNumber() {
return sequenceNumber;
}
@Override
public Location getLocation() {
return location;
}
@Override
public Object[] getRawArguments() {
return rawArguments;
}
@Override
public Class<?> getRawReturnType() {
return mockitoMethod.getReturnType();
}
@Override
public void markVerified() {
verified = true;
}
@Override
public StubInfo stubInfo() {
return stubInfo;
}
@Override
public void markStubbed(StubInfo stubInfo) {
this.stubInfo = stubInfo;
}
@Override
public boolean isIgnoredForVerification() {
return isIgnoredForVerification;
}
@Override
public void ignoreForVerification() {
isIgnoredForVerification = true;
}
@Override
public Object getMock() {
return mockRef.get();
}
@Override
public Method getMethod() {
return mockitoMethod.getJavaMethod();
}
@Override
public Object[] getArguments() {
return arguments;
}
@Override
@SuppressWarnings("unchecked")
public <T> T getArgument(int index) {
return (T) arguments[index];
}
@Override
public <T> T getArgument(int index, Class<T> clazz) {
return clazz.cast(arguments[index]);
}
@Override
public Object callRealMethod() throws Throwable {
if (!realMethod.isInvokable()) {
throw cannotCallAbstractRealMethod();
}
return realMethod.invoke();
}
@Override
public int hashCode() {
//TODO SF we need to provide hash code implementation so that there are no unexpected, slight perf issues
return 1;
}
@Override
public boolean equals(Object o) {
if (o == null || !o.getClass().equals(this.getClass())) {
return false;
}
InterceptedInvocation other = (InterceptedInvocation) o;
return this.mockRef.get().equals(other.mockRef.get())
&& this.mockitoMethod.equals(other.mockitoMethod)
&& this.equalArguments(other.arguments);
}
private boolean equalArguments(Object[] arguments) {
return Arrays.equals(arguments, this.arguments);
}
public String toString() {
return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this);
}
public final static RealMethod NO_OP = new RealMethod() {
public boolean isInvokable() {
return false;
}
public Object invoke() throws Throwable {
return null;
}
};
}