Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
152   343   70   4,47
70   274   0,46   34
34     2,06  
1    
 
  RecordState       Line # 30 152 0% 70 0 100% 1.0
 
  (797)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.internal;
18   
19    import java.io.Serializable;
20    import java.util.HashMap;
21    import java.util.List;
22    import java.util.Map;
23   
24    import org.easymock.IAnswer;
25    import org.easymock.IArgumentMatcher;
26   
27    /**
28    * @author OFFIS, Tammo Freese
29    */
 
30    public class RecordState implements IMocksControlState, Serializable {
31   
32    private static final long serialVersionUID = -5418279681566430252L;
33   
34    private ExpectedInvocation lastInvocation = null;
35   
36    private boolean lastInvocationUsed = true;
37   
38    private Result lastResult;
39   
40    private final IMocksBehavior behavior;
41   
42    private static Map<Class<?>, Object> emptyReturnValues = new HashMap<Class<?>, Object>();
43   
 
44  6 toggle static {
45  6 emptyReturnValues.put(Void.TYPE, null);
46  6 emptyReturnValues.put(Boolean.TYPE, Boolean.FALSE);
47  6 emptyReturnValues.put(Byte.TYPE, Byte.valueOf((byte) 0));
48  6 emptyReturnValues.put(Short.TYPE, Short.valueOf((short) 0));
49  6 emptyReturnValues.put(Character.TYPE, Character.valueOf((char) 0));
50  6 emptyReturnValues.put(Integer.TYPE, Integer.valueOf(0));
51  6 emptyReturnValues.put(Long.TYPE, Long.valueOf(0));
52  6 emptyReturnValues.put(Float.TYPE, Float.valueOf(0));
53  6 emptyReturnValues.put(Double.TYPE, Double.valueOf(0));
54    }
55   
56    private static Map<Class<?>, Class<?>> primitiveToWrapperType = new HashMap<Class<?>, Class<?>>();
57   
 
58  6 toggle static {
59  6 primitiveToWrapperType.put(Boolean.TYPE, Boolean.class);
60  6 primitiveToWrapperType.put(Byte.TYPE, Byte.class);
61  6 primitiveToWrapperType.put(Short.TYPE, Short.class);
62  6 primitiveToWrapperType.put(Character.TYPE, Character.class);
63  6 primitiveToWrapperType.put(Integer.TYPE, Integer.class);
64  6 primitiveToWrapperType.put(Long.TYPE, Long.class);
65  6 primitiveToWrapperType.put(Float.TYPE, Float.class);
66  6 primitiveToWrapperType.put(Double.TYPE, Double.class);
67    }
68   
 
69  1122 toggle public RecordState(final IMocksBehavior behavior) {
70  1122 this.behavior = behavior;
71    }
72   
 
73  1000 toggle public void assertRecordState() {
74    }
75   
 
76  1464 toggle public java.lang.Object invoke(final Invocation invocation) {
77  1464 closeMethod();
78  1458 final List<IArgumentMatcher> lastMatchers = LastControl.pullMatchers();
79  1458 lastInvocation = new ExpectedInvocation(invocation, lastMatchers);
80  1456 lastInvocationUsed = false;
81  1456 return emptyReturnValueFor(invocation.getMethod().getReturnType());
82    }
83   
 
84  798 toggle public void replay() {
85  798 closeMethod();
86  794 if (LastControl.pullMatchers() != null) {
87  2 throw new IllegalStateException("matcher calls were used outside expectations");
88    }
89    }
90   
 
91  2 toggle public void verify() {
92  2 throw new RuntimeExceptionWrapper(new IllegalStateException(
93    "calling verify is not allowed in record state"));
94    }
95   
 
96  816 toggle public void andReturn(Object value) {
97  816 requireMethodCall("return value");
98  806 value = convertNumberClassIfNeccessary(value);
99  806 requireAssignable(value);
100  798 if (lastResult != null) {
101  32 times(MocksControl.ONCE);
102    }
103  798 lastResult = Result.createReturnResult(value);
104    }
105   
 
106  86 toggle public void andThrow(final Throwable throwable) {
107  86 requireMethodCall("Throwable");
108  84 requireValidThrowable(throwable);
109  78 if (lastResult != null) {
110  12 times(MocksControl.ONCE);
111    }
112  78 lastResult = Result.createThrowResult(throwable);
113    }
114   
 
115  28 toggle public void andAnswer(final IAnswer<?> answer) {
116  28 requireMethodCall("answer");
117  26 requireValidAnswer(answer);
118  24 if (lastResult != null) {
119  2 times(MocksControl.ONCE);
120    }
121  24 lastResult = Result.createAnswerResult(answer);
122    }
123   
 
124  18 toggle public void andDelegateTo(final Object delegateTo) {
125  18 requireMethodCall("delegate");
126  16 requireValidDelegation(delegateTo);
127  14 if (lastResult != null) {
128  2 times(MocksControl.ONCE);
129    }
130  14 lastResult = Result.createDelegatingResult(delegateTo);
131    }
132   
 
133  104 toggle public void andStubReturn(Object value) {
134  104 requireMethodCall("stub return value");
135  94 value = convertNumberClassIfNeccessary(value);
136  94 requireAssignable(value);
137  92 if (lastResult != null) {
138  2 times(MocksControl.ONCE);
139    }
140  92 behavior.addStub(lastInvocation, Result.createReturnResult(value));
141  92 lastInvocationUsed = true;
142    }
143   
 
144  8 toggle public void asStub() {
145  8 requireMethodCall("stub behavior");
146  6 requireVoidMethod();
147  4 behavior.addStub(lastInvocation, Result.createReturnResult(null));
148  4 lastInvocationUsed = true;
149    }
150   
 
151  34 toggle public void andStubThrow(final Throwable throwable) {
152  34 requireMethodCall("stub Throwable");
153  32 requireValidThrowable(throwable);
154  26 if (lastResult != null) {
155  2 times(MocksControl.ONCE);
156    }
157  26 behavior.addStub(lastInvocation, Result.createThrowResult(throwable));
158  26 lastInvocationUsed = true;
159    }
160   
 
161  10 toggle public void andStubAnswer(final IAnswer<?> answer) {
162  10 requireMethodCall("stub answer");
163  8 requireValidAnswer(answer);
164  6 if (lastResult != null) {
165  2 times(MocksControl.ONCE);
166    }
167  6 behavior.addStub(lastInvocation, Result.createAnswerResult(answer));
168  6 lastInvocationUsed = true;
169    }
170   
 
171  8 toggle public void andStubDelegateTo(final Object delegateTo) {
172  8 requireMethodCall("stub delegate");
173  6 requireValidDelegation(delegateTo);
174  4 if (lastResult != null) {
175  2 times(MocksControl.ONCE);
176    }
177  4 behavior.addStub(lastInvocation, Result.createDelegatingResult(delegateTo));
178  4 lastInvocationUsed = true;
179    }
180   
 
181  1278 toggle public void times(final Range range) {
182  1278 requireMethodCall("times");
183  1268 requireLastResultOrVoidMethod();
184   
185  1266 behavior.addExpected(lastInvocation, lastResult != null ? lastResult : Result
186    .createReturnResult(null), range);
187  1262 lastInvocationUsed = true;
188  1262 lastResult = null;
189    }
190   
 
191  900 toggle private Object createNumberObject(final Object value, final Class<?> returnType) {
192  900 if (!(value instanceof Number)) {
193  760 return value;
194    }
195  140 final Number number = (Number) value;
196  140 if (returnType.equals(Byte.TYPE)) {
197  16 return number.byteValue();
198  124 } else if (returnType.equals(Short.TYPE)) {
199  4 return number.shortValue();
200  120 } else if (returnType.equals(Integer.TYPE)) {
201  72 return number.intValue();
202  48 } else if (returnType.equals(Long.TYPE)) {
203  14 return number.longValue();
204  34 } else if (returnType.equals(Float.TYPE)) {
205  14 return number.floatValue();
206  20 } else if (returnType.equals(Double.TYPE)) {
207  14 return number.doubleValue();
208    } else {
209  6 return number;
210    }
211    }
212   
 
213  900 toggle private Object convertNumberClassIfNeccessary(final Object o) {
214  900 final Class<?> returnType = lastInvocation.getMethod().getReturnType();
215  900 return createNumberObject(o, returnType);
216    }
217   
 
218  2270 toggle private void closeMethod() {
219  2270 if (lastInvocationUsed && lastResult == null) {
220  1252 return;
221    }
222  1018 if (!isLastResultOrVoidMethod()) {
223  10 throw new RuntimeExceptionWrapper(new IllegalStateException(
224    "missing behavior definition for the preceding method call " + lastInvocation.toString()));
225    }
226  1008 times(MocksControl.ONCE);
227    }
228   
 
229  1539 toggle public static Object emptyReturnValueFor(final Class<?> type) {
230  1540 return type.isPrimitive() ? emptyReturnValues.get(type) : null;
231    }
232   
 
233  2390 toggle private void requireMethodCall(final String failMessage) {
234  2390 if (lastInvocation == null) {
235  44 throw new RuntimeExceptionWrapper(new IllegalStateException(
236    "method call on the mock needed before setting " + failMessage));
237    }
238    }
239   
 
240  900 toggle private void requireAssignable(final Object returnValue) {
241  900 if (lastMethodIsVoidMethod()) {
242  2 throw new RuntimeExceptionWrapper(new IllegalStateException("void method cannot return a value"));
243    }
244  898 if (returnValue == null) {
245  6 final Class<?> returnedType = lastInvocation.getMethod().getReturnType();
246  6 if (returnedType.isPrimitive()) {
247  2 throw new RuntimeExceptionWrapper(new IllegalStateException(
248    "can't return null for a method returning a primitive type"));
249    }
250  4 return;
251    }
252  892 Class<?> returnedType = lastInvocation.getMethod().getReturnType();
253  892 if (returnedType.isPrimitive()) {
254  270 returnedType = primitiveToWrapperType.get(returnedType);
255   
256    }
257  892 if (!returnedType.isAssignableFrom(returnValue.getClass())) {
258  6 throw new RuntimeExceptionWrapper(new IllegalStateException("incompatible return value type"));
259    }
260    }
261   
 
262  116 toggle private void requireValidThrowable(final Throwable throwable) {
263  116 if (throwable == null) {
264  4 throw new RuntimeExceptionWrapper(new NullPointerException("null cannot be thrown"));
265    }
266  112 if (isValidThrowable(throwable)) {
267  104 return;
268    }
269   
270  8 throw new RuntimeExceptionWrapper(new IllegalArgumentException(
271    "last method called on mock cannot throw " + throwable.getClass().getName()));
272    }
273   
 
274  34 toggle private void requireValidAnswer(final IAnswer<?> answer) {
275  34 if (answer == null) {
276  4 throw new RuntimeExceptionWrapper(new NullPointerException("answer object must not be null"));
277    }
278    }
279   
 
280  22 toggle private void requireValidDelegation(final Object delegateTo) {
281  22 if (delegateTo == null) {
282  4 throw new RuntimeExceptionWrapper(
283    new NullPointerException("delegated to object must not be null"));
284    }
285    // Would be nice to validate delegateTo is implementing the mock
286    // interface (not possible right now)
287    }
288   
 
289  1268 toggle private void requireLastResultOrVoidMethod() {
290  1268 if (isLastResultOrVoidMethod()) {
291  1266 return;
292    }
293  2 throw new RuntimeExceptionWrapper(new IllegalStateException(
294    "last method called on mock is not a void method"));
295    }
296   
 
297  6 toggle private void requireVoidMethod() {
298  6 if (lastMethodIsVoidMethod()) {
299  4 return;
300    }
301  2 throw new RuntimeExceptionWrapper(new IllegalStateException(
302    "last method called on mock is not a void method"));
303    }
304   
 
305  2286 toggle private boolean isLastResultOrVoidMethod() {
306  2286 return lastResult != null || lastMethodIsVoidMethod();
307    }
308   
 
309  1604 toggle private boolean lastMethodIsVoidMethod() {
310  1604 final Class<?> returnType = lastInvocation.getMethod().getReturnType();
311  1604 return returnType.equals(Void.TYPE);
312    }
313   
 
314  112 toggle private boolean isValidThrowable(final Throwable throwable) {
315  112 if (throwable instanceof RuntimeException) {
316  90 return true;
317    }
318  22 if (throwable instanceof Error) {
319  4 return true;
320    }
321  18 final Class<?>[] exceptions = lastInvocation.getMethod().getExceptionTypes();
322  18 final Class<?> throwableClass = throwable.getClass();
323  18 for (final Class<?> exception : exceptions) {
324  14 if (exception.isAssignableFrom(throwableClass)) {
325  10 return true;
326    }
327    }
328  8 return false;
329    }
330   
 
331  8 toggle public void checkOrder(final boolean value) {
332  8 closeMethod();
333  8 behavior.checkOrder(value);
334    }
335   
 
336  4 toggle public void makeThreadSafe(final boolean threadSafe) {
337  4 behavior.makeThreadSafe(threadSafe);
338    }
339   
 
340  4 toggle public void checkIsUsedInOneThread(final boolean shouldBeUsedInOneThread) {
341  4 behavior.shouldBeUsedInOneThread(shouldBeUsedInOneThread);
342    }
343    }