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
119   350   60   3,61
8   262   0,5   16,5
33     1,82  
2    
 
  MocksControl       Line # 29 119 0% 60 0 100% 1.0
  MocksControl.MockType       Line # 37 0 - 0 0 - -1.0
 
  (835)
 
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 static org.easymock.internal.ClassExtensionHelper.*;
20   
21    import java.io.Serializable;
22    import java.lang.reflect.Method;
23   
24    import org.easymock.*;
25   
26    /**
27    * @author OFFIS, Tammo Freese
28    */
 
29    public class MocksControl implements IMocksControl, IExpectationSetters<Object>, Serializable {
30   
31    private static final long serialVersionUID = 443604921336702014L;
32   
33    private IMocksControlState state;
34   
35    private IMocksBehavior behavior;
36   
 
37    public enum MockType {
38    NICE, DEFAULT, STRICT
39    }
40   
41    private MockType type;
42   
 
43  1020 toggle public MocksControl(final MockType type) {
44  1020 this.type = type;
45  1020 reset();
46    }
47   
 
48  18 toggle public MockType getType() {
49  18 return type;
50    }
51   
 
52  6472 toggle public IMocksControlState getState() {
53  6472 return state;
54    }
55   
 
56  864 toggle public <T> T createMock(final Class<T> toMock) {
57  864 try {
58  864 state.assertRecordState();
59  862 final IProxyFactory<T> proxyFactory = createProxyFactory(toMock);
60  858 return proxyFactory.createProxy(toMock, new ObjectMethodsFilter(toMock,
61    new MockInvocationHandler(this), null));
62    } catch (final RuntimeExceptionWrapper e) {
63  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
64    }
65    }
66   
 
67  140 toggle public <T> T createMock(final String name, final Class<T> toMock) {
68  140 try {
69  140 state.assertRecordState();
70  138 final IProxyFactory<T> proxyFactory = createProxyFactory(toMock);
71  138 return proxyFactory.createProxy(toMock, new ObjectMethodsFilter(toMock,
72    new MockInvocationHandler(this), name));
73    } catch (final RuntimeExceptionWrapper e) {
74  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
75    }
76    }
77   
 
78  100 toggle public <T> T createMock(final String name, final Class<T> toMock, final Method... mockedMethods) {
79   
80  100 if (toMock.isInterface()) {
81  4 throw new IllegalArgumentException("Partial mocking doesn't make sense for interface");
82    }
83   
84  96 final T mock = createMock(name, toMock);
85   
86    // Set the mocked methods on the interceptor
87  86 getInterceptor(mock).setMockedMethods(mockedMethods);
88   
89  86 return mock;
90    }
91   
 
92  38 toggle public <T> T createMock(final Class<T> toMock, final Method... mockedMethods) {
93   
94  38 if (toMock.isInterface()) {
95  4 throw new IllegalArgumentException("Partial mocking doesn't make sense for interface");
96    }
97   
98  34 final T mock = createMock(toMock);
99   
100    // Set the mocked methods on the interceptor
101  34 getInterceptor(mock).setMockedMethods(mockedMethods);
102   
103  34 return mock;
104    }
105   
 
106  16 toggle public <T> T createMock(final Class<T> toMock, final ConstructorArgs constructorArgs,
107    final Method... mockedMethods) {
108    // Trick to allow the ClassProxyFactory to access constructor args
109  16 setCurrentConstructorArgs(constructorArgs);
110  16 try {
111  16 return createMock(toMock, mockedMethods);
112    } finally {
113  16 setCurrentConstructorArgs(null);
114    }
115    }
116   
 
117  40 toggle public <T> T createMock(final String name, final Class<T> toMock, final ConstructorArgs constructorArgs,
118    final Method... mockedMethods) {
119    // Trick to allow the ClassProxyFactory to access constructor args
120  40 setCurrentConstructorArgs(constructorArgs);
121  40 try {
122  40 return createMock(name, toMock, mockedMethods);
123    } finally {
124  40 setCurrentConstructorArgs(null);
125    }
126    }
127   
 
128  1000 toggle protected <T> IProxyFactory<T> createProxyFactory(final Class<T> toMock) {
129  1000 if (toMock.isInterface()) {
130  732 return new JavaProxyFactory<T>();
131    }
132  268 final String classMockingDisabled = EasyMockProperties.getInstance().getProperty(
133    EasyMock.DISABLE_CLASS_MOCKING);
134  268 if (Boolean.valueOf(classMockingDisabled)) {
135  2 throw new IllegalArgumentException("Class mocking is currently disabled. Change "
136    + EasyMock.DISABLE_CLASS_MOCKING + " to true do modify this behavior");
137    }
138  266 try {
139  266 return new ClassProxyFactory<T>();
140    } catch (final NoClassDefFoundError e) {
141  2 throw new RuntimeException(
142    "Class mocking requires to have cglib and objenesis librairies in the classpath", e);
143    }
144    }
145   
 
146  1122 toggle public final void reset() {
147  1122 behavior = new MocksBehavior(type == MockType.NICE);
148  1122 behavior.checkOrder(type == MockType.STRICT);
149  1122 state = new RecordState(behavior);
150  1122 LastControl.reportLastControl(null);
151    }
152   
 
153  10 toggle public void resetToNice() {
154  10 type = MockType.NICE;
155  10 reset();
156    }
157   
 
158  10 toggle public void resetToDefault() {
159  10 type = MockType.DEFAULT;
160  10 reset();
161    }
162   
 
163  10 toggle public void resetToStrict() {
164  10 type = MockType.STRICT;
165  10 reset();
166    }
167   
 
168  800 toggle public void replay() {
169  800 try {
170  800 state.replay();
171  792 state = new ReplayState(behavior);
172  792 LastControl.reportLastControl(null);
173    } catch (final RuntimeExceptionWrapper e) {
174  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
175    }
176    }
177   
 
178  578 toggle public void verify() {
179  578 try {
180  578 state.verify();
181    } catch (final RuntimeExceptionWrapper e) {
182  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
183    } catch (final AssertionErrorWrapper e) {
184  38 throw (AssertionError) e.getAssertionError().fillInStackTrace();
185    }
186    }
187   
 
188  10 toggle public void checkOrder(final boolean value) {
189  10 try {
190  10 state.checkOrder(value);
191    } catch (final RuntimeExceptionWrapper e) {
192  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
193    }
194    }
195   
 
196  6 toggle public void makeThreadSafe(final boolean threadSafe) {
197  6 try {
198  6 state.makeThreadSafe(threadSafe);
199    } catch (final RuntimeExceptionWrapper e) {
200  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
201    }
202    }
203   
 
204  6 toggle public void checkIsUsedInOneThread(final boolean shouldBeUsedInOneThread) {
205  6 try {
206  6 state.checkIsUsedInOneThread(shouldBeUsedInOneThread);
207    } catch (final RuntimeExceptionWrapper e) {
208  2 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
209    }
210    }
211   
212    // methods from IBehaviorSetters
213   
 
214  816 toggle public IExpectationSetters<Object> andReturn(final Object value) {
215  816 try {
216  816 state.andReturn(value);
217  798 return this;
218    } catch (final RuntimeExceptionWrapper e) {
219  18 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
220    }
221    }
222   
 
223  86 toggle public IExpectationSetters<Object> andThrow(final Throwable throwable) {
224  86 try {
225  86 state.andThrow(throwable);
226  78 return this;
227    } catch (final RuntimeExceptionWrapper e) {
228  8 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
229    }
230    }
231   
 
232  28 toggle public IExpectationSetters<Object> andAnswer(final IAnswer<? extends Object> answer) {
233  28 try {
234  28 state.andAnswer(answer);
235  24 return this;
236    } catch (final RuntimeExceptionWrapper e) {
237  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
238    }
239    }
240   
 
241  18 toggle public IExpectationSetters<Object> andDelegateTo(final Object answer) {
242  18 try {
243  18 state.andDelegateTo(answer);
244  14 return this;
245    } catch (final RuntimeExceptionWrapper e) {
246  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
247    }
248    }
249   
 
250  106 toggle public void andStubReturn(final Object value) {
251  106 try {
252  106 state.andStubReturn(value);
253    } catch (final RuntimeExceptionWrapper e) {
254  14 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
255    }
256    }
257   
 
258  36 toggle public void andStubThrow(final Throwable throwable) {
259  36 try {
260  36 state.andStubThrow(throwable);
261    } catch (final RuntimeExceptionWrapper e) {
262  10 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
263    }
264    }
265   
 
266  10 toggle public void andStubAnswer(final IAnswer<? extends Object> answer) {
267  10 try {
268  10 state.andStubAnswer(answer);
269    } catch (final RuntimeExceptionWrapper e) {
270  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
271    }
272    }
273   
 
274  8 toggle public void andStubDelegateTo(final Object delegateTo) {
275  8 try {
276  8 state.andStubDelegateTo(delegateTo);
277    } catch (final RuntimeExceptionWrapper e) {
278  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
279    }
280    }
281   
 
282  10 toggle public void asStub() {
283  10 try {
284  10 state.asStub();
285    } catch (final RuntimeExceptionWrapper e) {
286  6 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
287    }
288    }
289   
 
290  74 toggle public IExpectationSetters<Object> times(final int times) {
291  74 try {
292  74 state.times(new Range(times));
293  68 return this;
294    } catch (final RuntimeExceptionWrapper e) {
295  6 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
296    }
297    }
298   
 
299  36 toggle public IExpectationSetters<Object> times(final int min, final int max) {
300  36 try {
301  36 state.times(new Range(min, max));
302  28 return this;
303    } catch (final RuntimeExceptionWrapper e) {
304  8 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
305    }
306    }
307   
 
308  40 toggle public IExpectationSetters<Object> once() {
309  40 try {
310  40 state.times(ONCE);
311  36 return this;
312    } catch (final RuntimeExceptionWrapper e) {
313  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
314    }
315    }
316   
 
317  58 toggle public IExpectationSetters<Object> atLeastOnce() {
318  58 try {
319  58 state.times(AT_LEAST_ONCE);
320  54 return this;
321    } catch (final RuntimeExceptionWrapper e) {
322  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
323    }
324    }
325   
 
326  16 toggle public IExpectationSetters<Object> anyTimes() {
327  16 try {
328  16 state.times(ZERO_OR_MORE);
329  12 return this;
330    } catch (final RuntimeExceptionWrapper e) {
331  4 throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
332    }
333    }
334   
335    /**
336    * Exactly one call.
337    */
338    public static final Range ONCE = new Range(1);
339   
340    /**
341    * One or more calls.
342    */
343    public static final Range AT_LEAST_ONCE = new Range(1, Integer.MAX_VALUE);
344   
345    /**
346    * Zero or more calls.
347    */
348    public static final Range ZERO_OR_MORE = new Range(0, Integer.MAX_VALUE);
349   
350    }