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
40   548   28   1,43
0   128   0,7   28
28     1  
1    
 
  EasyMockSupport       Line # 49 40 0% 28 0 100% 1.0
 
  (80)
 
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;
18   
19    import java.lang.reflect.Method;
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    import org.easymock.internal.MockBuilder;
24   
25    /**
26    * Helper class to be used to keep tracks of mocks easily. See EasyMock
27    * documentation and SupportTest sample.
28    * <p>
29    * Example of usage:
30    *
31    * <pre>
32    * public class SupportTest extends EasyMockSupport {
33    * &#064;Test
34    * public void test() {
35    * firstMock = createMock(A.class);
36    * secondMock = createMock(B.class);
37    *
38    * replayAll(); // put both mocks in replay mode
39    *
40    * // ... use mocks ..
41    *
42    * verifyAll(); // verify both mocks
43    * }
44    * }
45    * </pre>
46    *
47    * @author Henri Tremblay
48    */
 
49    public class EasyMockSupport {
50   
51    /** List of all controls created */
52    protected final List<IMocksControl> controls = new ArrayList<IMocksControl>(5);
53   
54    /**
55    * Creates a mock object that extends the given class, order checking is
56    * enabled by default.
57    *
58    * @param <T>
59    * the class that the mock object should extend.
60    * @param toMock
61    * the class that the mock object should extend.
62    * @param mockedMethods
63    * methods that will be mocked, other methods will behave
64    * normally
65    * @return the mock object.
66    *
67    * @deprecated Use {@link #createMockBuilder(Class)} instead
68    */
 
69  2 toggle @Deprecated
70    public <T> T createStrictMock(final Class<T> toMock, final Method... mockedMethods) {
71  2 return createStrictControl().createMock(toMock, mockedMethods);
72    }
73   
74    /**
75    * Creates a mock object that extends the given class, order checking is
76    * enabled by default.
77    *
78    * @param <T>
79    * the class that the mock object should extend.
80    * @param name
81    * the name of the mock object.
82    * @param toMock
83    * the class that the mock object should extend.
84    * @param mockedMethods
85    * methods that will be mocked, other methods will behave
86    * normally
87    * @return the mock object.
88    *
89    * @deprecated Use {@link #createMockBuilder(Class)} instead
90    */
 
91  2 toggle @Deprecated
92    public <T> T createStrictMock(final String name, final Class<T> toMock, final Method... mockedMethods) {
93  2 return createStrictControl().createMock(name, toMock, mockedMethods);
94    }
95   
96    /**
97    * Creates a mock object that extends the given class, order checking is
98    * enabled by default.
99    *
100    * @param <T>
101    * the class that the mock object should extend.
102    * @param toMock
103    * the class that the mock object should extend.
104    * @param constructorArgs
105    * constructor and parameters used to instantiate the mock.
106    * @param mockedMethods
107    * methods that will be mocked, other methods will behave
108    * normally
109    * @return the mock object.
110    *
111    * @deprecated Use {@link #createMockBuilder(Class)} instead
112    */
 
113  2 toggle @Deprecated
114    public <T> T createStrictMock(final Class<T> toMock, final ConstructorArgs constructorArgs,
115    final Method... mockedMethods) {
116  2 return createStrictControl().createMock(toMock, constructorArgs, mockedMethods);
117    }
118   
119    /**
120    * Creates a mock object that extends the given class, order checking is
121    * enabled by default.
122    *
123    * @param <T>
124    * the class that the mock object should extend.
125    * @param name
126    * the name of the mock object.
127    * @param toMock
128    * the class that the mock object should extend.
129    * @param constructorArgs
130    * constructor and parameters used to instantiate the mock.
131    * @param mockedMethods
132    * methods that will be mocked, other methods will behave
133    * normally
134    * @return the mock object.
135    *
136    * @deprecated Use {@link #createMockBuilder(Class)} instead
137    */
 
138  2 toggle @Deprecated
139    public <T> T createStrictMock(final String name, final Class<T> toMock,
140    final ConstructorArgs constructorArgs, final Method... mockedMethods) {
141  2 return createStrictControl().createMock(name, toMock, constructorArgs, mockedMethods);
142    }
143   
144    /**
145    * Creates a mock object that extends the given class, order checking is
146    * disabled by default.
147    *
148    * @param <T>
149    * the class that the mock object should extend.
150    * @param toMock
151    * the class that the mock object should extend.
152    * @param mockedMethods
153    * methods that will be mocked, other methods will behave
154    * normally
155    * @return the mock object.
156    *
157    * @deprecated Use {@link #createMockBuilder(Class)} instead
158    */
 
159  2 toggle @Deprecated
160    public <T> T createMock(final Class<T> toMock, final Method... mockedMethods) {
161  2 return createControl().createMock(toMock, mockedMethods);
162    }
163   
164    /**
165    * Creates a mock object that extends the given class, order checking is
166    * disabled by default.
167    *
168    * @param <T>
169    * the class that the mock object should extend.
170    * @param name
171    * the name of the mock object.
172    * @param toMock
173    * the class that the mock object should extend.
174    * @param mockedMethods
175    * methods that will be mocked, other methods will behave
176    * normally
177    * @return the mock object.
178    *
179    * @deprecated Use {@link #createMockBuilder(Class)} instead
180    */
 
181  2 toggle @Deprecated
182    public <T> T createMock(final String name, final Class<T> toMock, final Method... mockedMethods) {
183  2 return createControl().createMock(name, toMock, mockedMethods);
184    }
185   
186    /**
187    * Creates a mock object that extends the given class, order checking is
188    * disabled by default.
189    *
190    * @param <T>
191    * the class that the mock object should extend.
192    * @param toMock
193    * the class that the mock object should extend.
194    * @param constructorArgs
195    * constructor and parameters used to instantiate the mock.
196    * @param mockedMethods
197    * methods that will be mocked, other methods will behave
198    * normally
199    * @return the mock object.
200    *
201    * @deprecated Use {@link #createMockBuilder(Class)} instead
202    */
 
203  2 toggle @Deprecated
204    public <T> T createMock(final Class<T> toMock, final ConstructorArgs constructorArgs,
205    final Method... mockedMethods) {
206  2 return createControl().createMock(toMock, constructorArgs, mockedMethods);
207    }
208   
209    /**
210    * Creates a mock object that extends the given class, order checking is
211    * disabled by default.
212    *
213    * @param <T>
214    * the class that the mock object should extend.
215    * @param name
216    * the name of the mock object.
217    * @param toMock
218    * the class that the mock object should extend.
219    * @param constructorArgs
220    * constructor and parameters used to instantiate the mock.
221    * @param mockedMethods
222    * methods that will be mocked, other methods will behave
223    * normally
224    * @return the mock object.
225    *
226    * @deprecated Use {@link #createMockBuilder(Class)} instead
227    */
 
228  2 toggle @Deprecated
229    public <T> T createMock(final String name, final Class<T> toMock, final ConstructorArgs constructorArgs,
230    final Method... mockedMethods) {
231  2 return createControl().createMock(name, toMock, constructorArgs, mockedMethods);
232    }
233   
234    /**
235    * Creates a mock object that extends the given class, order checking is
236    * disabled by default, and the mock object will return <code>0</code>,
237    * <code>null</code> or <code>false</code> for unexpected invocations.
238    *
239    * @param <T>
240    * the class that the mock object should extend.
241    * @param toMock
242    * the class that the mock object should extend.
243    * @param mockedMethods
244    * methods that will be mocked, other methods will behave
245    * normally
246    * @return the mock object.
247    *
248    * @deprecated Use {@link #createMockBuilder(Class)} instead
249    */
 
250  2 toggle @Deprecated
251    public <T> T createNiceMock(final Class<T> toMock, final Method... mockedMethods) {
252  2 return createNiceControl().createMock(toMock, mockedMethods);
253    }
254   
255    /**
256    * Creates a mock object that extends the given class, order checking is
257    * disabled by default, and the mock object will return <code>0</code>,
258    * <code>null</code> or <code>false</code> for unexpected invocations.
259    *
260    * @param <T>
261    * the class that the mock object should extend.
262    * @param name
263    * the name of the mock object.
264    * @param toMock
265    * the class that the mock object should extend.
266    * @param mockedMethods
267    * methods that will be mocked, other methods will behave
268    * normally
269    * @return the mock object.
270    *
271    * @deprecated Use {@link #createMockBuilder(Class)} instead
272    */
 
273  2 toggle @Deprecated
274    public <T> T createNiceMock(final String name, final Class<T> toMock, final Method... mockedMethods) {
275  2 return createNiceControl().createMock(name, toMock, mockedMethods);
276    }
277   
278    /**
279    * Creates a mock object that extends the given class, order checking is
280    * disabled by default, and the mock object will return <code>0</code>,
281    * <code>null</code> or <code>false</code> for unexpected invocations.
282    *
283    * @param <T>
284    * the class that the mock object should extend.
285    * @param toMock
286    * the class that the mock object should extend.
287    * @param constructorArgs
288    * constructor and parameters used to instantiate the mock.
289    * @param mockedMethods
290    * methods that will be mocked, other methods will behave
291    * normally
292    * @return the mock object.
293    *
294    * @deprecated Use {@link #createMockBuilder(Class)} instead
295    */
 
296  2 toggle @Deprecated
297    public <T> T createNiceMock(final Class<T> toMock, final ConstructorArgs constructorArgs,
298    final Method... mockedMethods) {
299  2 return createNiceControl().createMock(toMock, constructorArgs, mockedMethods);
300    }
301   
302    /**
303    * Creates a mock object that extends the given class, order checking is
304    * disabled by default, and the mock object will return <code>0</code>,
305    * <code>null</code> or <code>false</code> for unexpected invocations.
306    *
307    * @param <T>
308    * the class that the mock object should extend.
309    * @param name
310    * the name of the mock object.
311    * @param toMock
312    * the class that the mock object should extend.
313    * @param constructorArgs
314    * constructor and parameters used to instantiate the mock.
315    * @param mockedMethods
316    * methods that will be mocked, other methods will behave
317    * normally
318    * @return the mock object.
319    *
320    * @deprecated Use {@link #createMockBuilder(Class)} instead
321    */
 
322  2 toggle @Deprecated
323    public <T> T createNiceMock(final String name, final Class<T> toMock,
324    final ConstructorArgs constructorArgs, final Method... mockedMethods) {
325  2 return createNiceControl().createMock(name, toMock, constructorArgs, mockedMethods);
326    }
327   
328    /**
329    * Creates a mock object that implements the given interface, order checking
330    * is enabled by default.
331    *
332    * @param <T>
333    * the interface that the mock object should implement.
334    * @param toMock
335    * the class of the interface that the mock object should
336    * implement.
337    * @return the mock object.
338    */
 
339  10 toggle public <T> T createStrictMock(final Class<T> toMock) {
340  10 return createStrictControl().createMock(toMock);
341    }
342   
343    /**
344    * Creates a mock object that implements the given interface, order checking
345    * is enabled by default.
346    *
347    * @param name
348    * the name of the mock object.
349    * @param toMock
350    * the class of the interface that the mock object should
351    * implement.
352    * @param <T>
353    * the interface that the mock object should implement.
354    * @return the mock object.
355    * @throws IllegalArgumentException
356    * if the name is not a valid Java identifier.
357    */
 
358  6 toggle public <T> T createStrictMock(final String name, final Class<T> toMock) {
359  6 return createStrictControl().createMock(name, toMock);
360    }
361   
362    /**
363    * Creates a mock object that implements the given interface, order checking
364    * is disabled by default.
365    *
366    * @param <T>
367    * the interface that the mock object should implement.
368    * @param toMock
369    * the class of the interface that the mock object should
370    * implement.
371    * @return the mock object.
372    */
 
373  28 toggle public <T> T createMock(final Class<T> toMock) {
374  28 return createControl().createMock(toMock);
375    }
376   
377    /**
378    * Creates a mock object that implements the given interface, order checking
379    * is disabled by default.
380    *
381    * @param name
382    * the name of the mock object.
383    * @param toMock
384    * the class of the interface that the mock object should
385    * implement.
386    *
387    * @param <T>
388    * the interface that the mock object should implement.
389    * @return the mock object.
390    * @throws IllegalArgumentException
391    * if the name is not a valid Java identifier.
392    */
 
393  6 toggle public <T> T createMock(final String name, final Class<T> toMock) {
394  6 return createControl().createMock(name, toMock);
395    }
396   
397    /**
398    * Creates a mock object that implements the given interface, order checking
399    * is disabled by default, and the mock object will return <code>0</code>,
400    * <code>null</code> or <code>false</code> for unexpected invocations.
401    *
402    * @param <T>
403    * the interface that the mock object should implement.
404    * @param toMock
405    * the class of the interface that the mock object should
406    * implement.
407    * @return the mock object.
408    */
 
409  10 toggle public <T> T createNiceMock(final Class<T> toMock) {
410  10 return createNiceControl().createMock(toMock);
411    }
412   
413    /**
414    * Creates a mock object that implements the given interface, order checking
415    * is disabled by default, and the mock object will return <code>0</code>,
416    * <code>null</code> or <code>false</code> for unexpected invocations.
417    *
418    * @param name
419    * the name of the mock object.
420    * @param toMock
421    * the class of the interface that the mock object should
422    * implement.
423    *
424    * @param <T>
425    * the interface that the mock object should implement.
426    * @return the mock object.
427    * @throws IllegalArgumentException
428    * if the name is not a valid Java identifier.
429    */
 
430  6 toggle public <T> T createNiceMock(final String name, final Class<T> toMock) {
431  6 return createNiceControl().createMock(name, toMock);
432    }
433   
434    /**
435    * Creates a control, order checking is enabled by default.
436    *
437    * @return the control.
438    */
 
439  30 toggle public IMocksControl createStrictControl() {
440  30 final IMocksControl ctrl = EasyMock.createStrictControl();
441  30 controls.add(ctrl);
442  30 return ctrl;
443    }
444   
445    /**
446    * Create a mock builder allowing to create a partial mock for the given
447    * class or interface.
448    *
449    * @param <T>
450    * the interface that the mock object should implement.
451    * @param toMock
452    * the class of the interface that the mock object should
453    * implement.
454    * @return a mock builder to create a partial mock
455    */
 
456  16 toggle public <T> IMockBuilder<T> createMockBuilder(final Class<T> toMock) {
457  16 return new MockBuilder<T>(toMock, this);
458    }
459   
460    /**
461    * Creates a control, order checking is disabled by default.
462    *
463    * @return the control.
464    */
 
465  60 toggle public IMocksControl createControl() {
466  60 final IMocksControl ctrl = EasyMock.createControl();
467  60 controls.add(ctrl);
468  60 return ctrl;
469    }
470   
471    /**
472    * Creates a control, order checking is disabled by default, and the mock
473    * objects created by this control will return <code>0</code>,
474    * <code>null</code> or <code>false</code> for unexpected invocations.
475    *
476    * @return the control.
477    */
 
478  30 toggle public IMocksControl createNiceControl() {
479  30 final IMocksControl ctrl = EasyMock.createNiceControl();
480  30 controls.add(ctrl);
481  30 return ctrl;
482    }
483   
484    /**
485    * Switches all registered mock objects (more exactly: the controls of the
486    * mock objects) to replay mode. For details, see the EasyMock
487    * documentation.
488    */
 
489  60 toggle public void replayAll() {
490  60 for (final IMocksControl c : controls) {
491  94 c.replay();
492    }
493    }
494   
495    /**
496    * Resets all registered mock objects (more exactly: the controls of the
497    * mock objects). For details, see the EasyMock documentation.
498    */
 
499  8 toggle public void resetAll() {
500  8 for (final IMocksControl c : controls) {
501  12 c.reset();
502    }
503    }
504   
505    /**
506    * Verifies all registered mock objects (more exactly: the controls of the
507    * mock objects).
508    */
 
509  54 toggle public void verifyAll() {
510  54 for (final IMocksControl c : controls) {
511  82 c.verify();
512    }
513    }
514   
515    /**
516    * Resets all registered mock objects (more exactly: the controls of the
517    * mock objects) and turn them to a mock with nice behavior. For details,
518    * see the EasyMock documentation.
519    */
 
520  4 toggle public void resetAllToNice() {
521  4 for (final IMocksControl c : controls) {
522  6 c.resetToNice();
523    }
524    }
525   
526    /**
527    * Resets all registered mock objects (more exactly: the controls of the
528    * mock objects) and turn them to a mock with default behavior. For details,
529    * see the EasyMock documentation.
530    */
 
531  4 toggle public void resetAllToDefault() {
532  4 for (final IMocksControl c : controls) {
533  6 c.resetToDefault();
534    }
535    }
536   
537    /**
538    * Resets all registered mock objects (more exactly: the controls of the
539    * mock objects) and turn them to a mock with strict behavior. For details,
540    * see the EasyMock documentation.
541    */
 
542  4 toggle public void resetAllToStrict() {
543  4 for (final IMocksControl c : controls) {
544  6 c.resetToStrict();
545    }
546    }
547   
548    }