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