1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.lang.reflect.Method; |
8 |
| import java.util.HashMap; |
9 |
| import java.util.List; |
10 |
| import java.util.Map; |
11 |
| |
12 |
| import org.easymock.ArgumentsMatcher; |
13 |
| import org.easymock.IAnswer; |
14 |
| import org.easymock.IArgumentMatcher; |
15 |
| import org.easymock.MockControl; |
16 |
| |
17 |
| public class RecordState implements IMocksControlState { |
18 |
| |
19 |
| private ExpectedInvocation lastInvocation; |
20 |
| |
21 |
| private boolean lastInvocationUsed = true; |
22 |
| |
23 |
| private Result lastResult; |
24 |
| |
25 |
| private IMocksBehavior behavior; |
26 |
| |
27 |
| private static Map<Class, Object> emptyReturnValues = new HashMap<Class, Object>(); |
28 |
| |
29 |
| static { |
30 |
1
| emptyReturnValues.put(Void.TYPE, null);
|
31 |
1
| emptyReturnValues.put(Boolean.TYPE, Boolean.FALSE);
|
32 |
1
| emptyReturnValues.put(Byte.TYPE, new Byte((byte) 0));
|
33 |
1
| emptyReturnValues.put(Short.TYPE, new Short((short) 0));
|
34 |
1
| emptyReturnValues.put(Character.TYPE, new Character((char) 0));
|
35 |
1
| emptyReturnValues.put(Integer.TYPE, new Integer(0));
|
36 |
1
| emptyReturnValues.put(Long.TYPE, new Long(0));
|
37 |
1
| emptyReturnValues.put(Float.TYPE, new Float(0));
|
38 |
1
| emptyReturnValues.put(Double.TYPE, new Double(0));
|
39 |
| } |
40 |
| |
41 |
| private static Map<Class, Class> primitiveToWrapperType = new HashMap<Class, Class>(); |
42 |
| |
43 |
| static { |
44 |
1
| primitiveToWrapperType.put(Boolean.TYPE, Boolean.class);
|
45 |
1
| primitiveToWrapperType.put(Byte.TYPE, Byte.class);
|
46 |
1
| primitiveToWrapperType.put(Short.TYPE, Short.class);
|
47 |
1
| primitiveToWrapperType.put(Character.TYPE, Character.class);
|
48 |
1
| primitiveToWrapperType.put(Integer.TYPE, Integer.class);
|
49 |
1
| primitiveToWrapperType.put(Long.TYPE, Long.class);
|
50 |
1
| primitiveToWrapperType.put(Float.TYPE, Float.class);
|
51 |
1
| primitiveToWrapperType.put(Double.TYPE, Double.class);
|
52 |
| } |
53 |
| |
54 |
460
| public RecordState(IMocksBehavior behavior) {
|
55 |
460
| this.behavior = behavior;
|
56 |
| } |
57 |
| |
58 |
366
| public void assertRecordState() {
|
59 |
| } |
60 |
| |
61 |
436
| public java.lang.Object invoke(Invocation invocation) {
|
62 |
436
| closeMethod();
|
63 |
434
| List<IArgumentMatcher> lastMatchers = LastControl.pullMatchers();
|
64 |
434
| lastInvocation = new ExpectedInvocation(invocation, lastMatchers);
|
65 |
433
| lastInvocationUsed = false;
|
66 |
433
| return emptyReturnValueFor(invocation.getMethod().getReturnType());
|
67 |
| } |
68 |
| |
69 |
375
| public void replay() {
|
70 |
375
| closeMethod();
|
71 |
373
| if (LastControl.pullMatchers() != null) {
|
72 |
1
| throw new IllegalStateException("matcher calls were not used outside expectations");
|
73 |
| } |
74 |
| } |
75 |
| |
76 |
1
| public void verify() {
|
77 |
1
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
78 |
| "calling verify is not allowed in record state")); |
79 |
| } |
80 |
| |
81 |
235
| public void andReturn(Object value) {
|
82 |
235
| requireMethodCall("return value");
|
83 |
235
| value = convertNumberClassIfNeccessary(value);
|
84 |
235
| requireAssignable(value);
|
85 |
216
| if (lastResult != null) {
|
86 |
8
| times(MocksControl.ONCE);
|
87 |
| } |
88 |
216
| lastResult = Result.createReturnResult(value);
|
89 |
| } |
90 |
| |
91 |
45
| public void andThrow(Throwable throwable) {
|
92 |
45
| requireMethodCall("Throwable");
|
93 |
45
| requireValidThrowable(throwable);
|
94 |
42
| if (lastResult != null) {
|
95 |
2
| times(MocksControl.ONCE);
|
96 |
| } |
97 |
42
| lastResult = Result.createThrowResult(throwable);
|
98 |
| } |
99 |
| |
100 |
9
| public void andAnswer(IAnswer answer) {
|
101 |
9
| requireMethodCall("answer");
|
102 |
9
| requireValidAnswer(answer);
|
103 |
8
| if (lastResult != null) {
|
104 |
1
| times(MocksControl.ONCE);
|
105 |
| } |
106 |
8
| lastResult = Result.createAnswerResult(answer);
|
107 |
| } |
108 |
| |
109 |
23
| public void andStubReturn(Object value) {
|
110 |
23
| requireMethodCall("stub return value");
|
111 |
23
| value = convertNumberClassIfNeccessary(value);
|
112 |
23
| requireAssignable(value);
|
113 |
23
| if (lastResult != null) {
|
114 |
1
| times(MocksControl.ONCE);
|
115 |
| } |
116 |
23
| behavior.addStub(lastInvocation, Result.createReturnResult(value));
|
117 |
23
| lastInvocationUsed = true;
|
118 |
| } |
119 |
| |
120 |
31
| public void setDefaultReturnValue(Object value) {
|
121 |
31
| requireMethodCall("default return value");
|
122 |
25
| value = convertNumberClassIfNeccessary(value);
|
123 |
25
| requireAssignable(value);
|
124 |
20
| if (lastResult != null) {
|
125 |
1
| times(MocksControl.ONCE);
|
126 |
| } |
127 |
20
| behavior.addStub(
|
128 |
| lastInvocation.withMatcher(MockControl.ALWAYS_MATCHER), Result |
129 |
| .createReturnResult(value)); |
130 |
20
| lastInvocationUsed = true;
|
131 |
| } |
132 |
| |
133 |
2
| public void asStub() {
|
134 |
2
| requireMethodCall("stub behavior");
|
135 |
2
| requireVoidMethod();
|
136 |
1
| behavior.addStub(lastInvocation, Result.createReturnResult(null));
|
137 |
1
| lastInvocationUsed = true;
|
138 |
| } |
139 |
| |
140 |
1
| public void setDefaultVoidCallable() {
|
141 |
1
| requireMethodCall("default void callable");
|
142 |
1
| requireVoidMethod();
|
143 |
1
| behavior.addStub(
|
144 |
| lastInvocation.withMatcher(MockControl.ALWAYS_MATCHER), Result |
145 |
| .createReturnResult(null)); |
146 |
1
| lastInvocationUsed = true;
|
147 |
| } |
148 |
| |
149 |
6
| public void andStubThrow(Throwable throwable) {
|
150 |
6
| requireMethodCall("stub Throwable");
|
151 |
6
| requireValidThrowable(throwable);
|
152 |
6
| if (lastResult != null) {
|
153 |
1
| times(MocksControl.ONCE);
|
154 |
| } |
155 |
6
| behavior.addStub(lastInvocation, Result.createThrowResult(throwable));
|
156 |
6
| lastInvocationUsed = true;
|
157 |
| } |
158 |
| |
159 |
8
| public void setDefaultThrowable(Throwable throwable) {
|
160 |
8
| requireMethodCall("default Throwable");
|
161 |
6
| requireValidThrowable(throwable);
|
162 |
3
| if (lastResult != null) {
|
163 |
1
| times(MocksControl.ONCE);
|
164 |
| } |
165 |
3
| behavior.addStub(
|
166 |
| lastInvocation.withMatcher(MockControl.ALWAYS_MATCHER), Result |
167 |
| .createThrowResult(throwable)); |
168 |
3
| lastInvocationUsed = true;
|
169 |
| } |
170 |
| |
171 |
3
| public void andStubAnswer(IAnswer answer) {
|
172 |
3
| requireMethodCall("stub answer");
|
173 |
3
| requireValidAnswer(answer);
|
174 |
2
| if (lastResult != null) {
|
175 |
1
| times(MocksControl.ONCE);
|
176 |
| } |
177 |
2
| behavior.addStub(lastInvocation, Result.createAnswerResult(answer));
|
178 |
2
| lastInvocationUsed = true;
|
179 |
| } |
180 |
| |
181 |
386
| public void times(Range range) {
|
182 |
386
| requireMethodCall("times");
|
183 |
386
| requireLastResultOrVoidMethod();
|
184 |
| |
185 |
385
| behavior.addExpected(lastInvocation, lastResult != null ? lastResult
|
186 |
| : Result.createReturnResult(null), range); |
187 |
383
| lastInvocationUsed = true;
|
188 |
383
| lastResult = null;
|
189 |
| } |
190 |
| |
191 |
283
| private Object createNumberObject(Object value, Class returnType) {
|
192 |
283
| if (!(value instanceof Number)) {
|
193 |
239
| return value;
|
194 |
| } |
195 |
44
| Number number = (Number) value;
|
196 |
44
| if (returnType.equals(Byte.TYPE)) {
|
197 |
2
| return number.byteValue();
|
198 |
42
| } else if (returnType.equals(Short.TYPE)) {
|
199 |
2
| return number.shortValue();
|
200 |
40
| } else if (returnType.equals(Character.TYPE)) {
|
201 |
2
| return (char) number.intValue();
|
202 |
38
| } else if (returnType.equals(Integer.TYPE)) {
|
203 |
2
| return number.intValue();
|
204 |
36
| } else if (returnType.equals(Long.TYPE)) {
|
205 |
7
| return number.longValue();
|
206 |
29
| } else if (returnType.equals(Float.TYPE)) {
|
207 |
7
| return number.floatValue();
|
208 |
22
| } else if (returnType.equals(Double.TYPE)) {
|
209 |
7
| return number.doubleValue();
|
210 |
| } else { |
211 |
15
| return number;
|
212 |
| } |
213 |
| } |
214 |
| |
215 |
283
| private Object convertNumberClassIfNeccessary(Object o) {
|
216 |
283
| Class returnType = lastInvocation.getMethod().getReturnType();
|
217 |
283
| return createNumberObject(o, returnType);
|
218 |
| } |
219 |
| |
220 |
813
| private void closeMethod() {
|
221 |
813
| if (lastInvocationUsed && lastResult == null) {
|
222 |
623
| return;
|
223 |
| } |
224 |
190
| if (!isLastResultOrVoidMethod()) {
|
225 |
4
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
226 |
| "missing behavior definition for the preceeding method call " |
227 |
| + lastInvocation.toString())); |
228 |
| } |
229 |
186
| this.times(MockControl.ONE);
|
230 |
| } |
231 |
| |
232 |
446
| public static Object emptyReturnValueFor(Class type) {
|
233 |
446
| return type.isPrimitive() ? emptyReturnValues.get(type) : null;
|
234 |
| } |
235 |
| |
236 |
763
| private void requireMethodCall(String failMessage) {
|
237 |
763
| if (lastInvocation == null) {
|
238 |
9
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
239 |
| "method call on the mock needed before setting " |
240 |
| + failMessage)); |
241 |
| } |
242 |
| } |
243 |
| |
244 |
283
| private void requireAssignable(Object returnValue) {
|
245 |
283
| if (lastMethodIsVoidMethod()) {
|
246 |
1
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
247 |
| "void method cannot return a value")); |
248 |
| } |
249 |
282
| if (returnValue == null) {
|
250 |
1
| return;
|
251 |
| } |
252 |
281
| Class<?> returnedType = lastInvocation.getMethod().getReturnType();
|
253 |
281
| if (returnedType.isPrimitive()) {
|
254 |
61
| returnedType = primitiveToWrapperType.get(returnedType);
|
255 |
| |
256 |
| } |
257 |
281
| if (!returnedType.isAssignableFrom(returnValue.getClass())) {
|
258 |
23
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
259 |
| "incompatible return value type")); |
260 |
| } |
261 |
| } |
262 |
| |
263 |
57
| private void requireValidThrowable(Throwable throwable) {
|
264 |
57
| if (throwable == null)
|
265 |
2
| throw new RuntimeExceptionWrapper(new NullPointerException(
|
266 |
| "null cannot be thrown")); |
267 |
55
| if (isValidThrowable(throwable))
|
268 |
51
| return;
|
269 |
| |
270 |
4
| throw new RuntimeExceptionWrapper(new IllegalArgumentException(
|
271 |
| "last method called on mock cannot throw " |
272 |
| + throwable.getClass().getName())); |
273 |
| } |
274 |
| |
275 |
12
| private void requireValidAnswer(IAnswer answer) {
|
276 |
12
| if (answer == null)
|
277 |
2
| throw new RuntimeExceptionWrapper(new NullPointerException(
|
278 |
| "answer object must not be null")); |
279 |
| } |
280 |
| |
281 |
386
| private void requireLastResultOrVoidMethod() {
|
282 |
386
| if (isLastResultOrVoidMethod()) {
|
283 |
385
| return;
|
284 |
| } |
285 |
1
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
286 |
| "last method called on mock is not a void method")); |
287 |
| } |
288 |
| |
289 |
3
| private void requireVoidMethod() {
|
290 |
3
| if (lastMethodIsVoidMethod()) {
|
291 |
2
| return;
|
292 |
| } |
293 |
1
| throw new RuntimeExceptionWrapper(new IllegalStateException(
|
294 |
| "last method called on mock is not a void method")); |
295 |
| } |
296 |
| |
297 |
576
| private boolean isLastResultOrVoidMethod() {
|
298 |
576
| return lastResult != null || lastMethodIsVoidMethod();
|
299 |
| } |
300 |
| |
301 |
515
| private boolean lastMethodIsVoidMethod() {
|
302 |
515
| Class returnType = lastInvocation.getMethod().getReturnType();
|
303 |
515
| return returnType.equals(Void.TYPE);
|
304 |
| } |
305 |
| |
306 |
55
| private boolean isValidThrowable(Throwable throwable) {
|
307 |
55
| if (throwable instanceof RuntimeException) {
|
308 |
40
| return true;
|
309 |
| } |
310 |
15
| if (throwable instanceof Error) {
|
311 |
2
| return true;
|
312 |
| } |
313 |
13
| Class<?>[] exceptions = lastInvocation.getMethod().getExceptionTypes();
|
314 |
13
| Class<?> throwableClass = throwable.getClass();
|
315 |
13
| for (Class<?> exception : exceptions) {
|
316 |
11
| if (exception.isAssignableFrom(throwableClass))
|
317 |
9
| return true;
|
318 |
| } |
319 |
4
| return false;
|
320 |
| } |
321 |
| |
322 |
2
| public void checkOrder(boolean value) {
|
323 |
2
| closeMethod();
|
324 |
2
| behavior.checkOrder(value);
|
325 |
| } |
326 |
| |
327 |
6
| public void setDefaultMatcher(ArgumentsMatcher matcher) {
|
328 |
6
| behavior.setDefaultMatcher(matcher);
|
329 |
| } |
330 |
| |
331 |
14
| public void setMatcher(Method method, ArgumentsMatcher matcher) {
|
332 |
14
| requireMethodCall("matcher");
|
333 |
13
| behavior.setMatcher(lastInvocation.getMethod(), matcher);
|
334 |
| } |
335 |
| } |