EasyMock 2.3

Clover coverage report - EasyMock 2.3
Coverage timestamp: mar. juil. 10 2007 02:46:04 CEST
FRAMES NO FRAMES

file stats: | LOC: | 335 |  | Methods: | 32
| NCLOC: | 284 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

RecordState.java

100%

100%

100%

100%

1 |  | /*
2 |  | * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
3 |  | * This program is made available under the terms of the MIT License.
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 | 470 | public RecordState(IMocksBehavior behavior) {
55 | 470 | this.behavior = behavior;
56 |  | }
57 |  |
58 | 375 | public void assertRecordState() {
59 |  | }
60 |  |
61 | 452 | public java.lang.Object invoke(Invocation invocation) {
62 | 452 | closeMethod();
63 | 450 | List<IArgumentMatcher> lastMatchers = LastControl.pullMatchers();
64 | 450 | lastInvocation = new ExpectedInvocation(invocation, lastMatchers);
65 | 449 | lastInvocationUsed = false;
66 | 449 | return emptyReturnValueFor(invocation.getMethod().getReturnType());
67 |  | }
68 |  |
69 | 383 | public void replay() {
70 | 383 | closeMethod();
71 | 381 | if (LastControl.pullMatchers() != null) {
72 | 1 | throw new IllegalStateException("matcher calls were 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 | 247 | public void andReturn(Object value) {
82 | 247 | requireMethodCall("return value");
83 | 247 | value = convertNumberClassIfNeccessary(value);
84 | 247 | requireAssignable(value);
85 | 228 | if (lastResult != null) {
86 | 8 | times(MocksControl.ONCE);
87 |  | }
88 | 228 | 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 | 402 | public void times(Range range) {
182 | 402 | requireMethodCall("times");
183 | 402 | requireLastResultOrVoidMethod();
184 |  |
185 | 401 | behavior.addExpected(lastInvocation, lastResult != null ? lastResult
186 |  | : Result.createReturnResult(null), range);
187 | 399 | lastInvocationUsed = true;
188 | 399 | lastResult = null;
189 |  | }
190 |  |
191 | 295 | private Object createNumberObject(Object value, Class returnType) {
192 | 295 | if (!(value instanceof Number)) {
193 | 251 | 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 | 295 | private Object convertNumberClassIfNeccessary(Object o) {
216 | 295 | Class returnType = lastInvocation.getMethod().getReturnType();
217 | 295 | return createNumberObject(o, returnType);
218 |  | }
219 |  |
220 | 838 | private void closeMethod() {
221 | 838 | if (lastInvocationUsed && lastResult == null) {
222 | 632 | return;
223 |  | }
224 | 206 | if (!isLastResultOrVoidMethod()) {
225 | 4 | throw new RuntimeExceptionWrapper(new IllegalStateException(
226 |  | "missing behavior definition for the preceeding method call "
227 |  | + lastInvocation.toString()));
228 |  | }
229 | 202 | this.times(MockControl.ONE);
230 |  | }
231 |  |
232 | 462 | public static Object emptyReturnValueFor(Class type) {
233 | 462 | return type.isPrimitive() ? emptyReturnValues.get(type) : null;
234 |  | }
235 |  |
236 | 791 | private void requireMethodCall(String failMessage) {
237 | 791 | 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 | 295 | private void requireAssignable(Object returnValue) {
245 | 295 | if (lastMethodIsVoidMethod()) {
246 | 1 | throw new RuntimeExceptionWrapper(new IllegalStateException(
247 |  | "void method cannot return a value"));
248 |  | }
249 | 294 | if (returnValue == null) {
250 | 1 | return;
251 |  | }
252 | 293 | Class<?> returnedType = lastInvocation.getMethod().getReturnType();
253 | 293 | if (returnedType.isPrimitive()) {
254 | 61 | returnedType = primitiveToWrapperType.get(returnedType);
255 |  |
256 |  | }
257 | 293 | 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 | 402 | private void requireLastResultOrVoidMethod() {
282 | 402 | if (isLastResultOrVoidMethod()) {
283 | 401 | 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 | 608 | private boolean isLastResultOrVoidMethod() {
298 | 608 | return lastResult != null || lastMethodIsVoidMethod();
299 |  | }
300 |  |
301 | 535 | private boolean lastMethodIsVoidMethod() {
302 | 535 | Class returnType = lastInvocation.getMethod().getReturnType();
303 | 535 | 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 | 3 | public void checkOrder(boolean value) {
323 | 3 | closeMethod();
324 | 3 | 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 |  | }