Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
147   385   40   4,9
6   288   0,27   15
30     1,33  
2    
 
  EasyMockClassExtensionTest       Line # 38 136 0% 33 7 95,7% 0.9570552
  EasyMockClassExtensionTest.ParamEntry       Line # 40 11 0% 7 0 100% 1.0
 
  (34)
 
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.tests2;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import java.lang.reflect.Method;
23    import java.math.BigDecimal;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.List;
27   
28    import org.easymock.ConstructorArgs;
29    import org.easymock.EasyMock;
30    import org.easymock.IMockBuilder;
31    import org.easymock.internal.EasyMockProperties;
32    import org.easymock.tests2.MocksControlTest.A;
33    import org.junit.Test;
34   
35    /**
36    * @author Henri Tremblay
37    */
 
38    public class EasyMockClassExtensionTest {
39   
 
40    private static class ParamEntry {
41    Class<?>[] types;
42   
43    Object[] values;
44   
 
45  12 toggle ParamEntry(final Class<?>[] types, final Object[] values) {
46  12 this.types = types;
47  12 this.values = values;
48    }
49   
 
50  36 toggle boolean isNamed() {
51  36 return types[0] == String.class;
52    }
53   
 
54  36 toggle boolean isConstructorCalled() {
55  36 return Arrays.asList(types).contains(ConstructorArgs.class);
56    }
57   
 
58  36 toggle A getMock(final String methodName) throws Exception {
59  36 final Method m = EasyMock.class.getMethod(methodName, types);
60  36 return (A) m.invoke(null, values);
61    }
62   
 
63  36 toggle public void test(final A mock) {
64  36 if (isNamed()) {
65  18 testNamed(mock);
66    }
67  36 if (isConstructorCalled()) {
68  12 testPartial_ConstructorCalled(mock);
69    } else {
70  24 testPartial_NoConstructorCalled(mock);
71    }
72    }
73    }
74   
75    /** Types of all method flavors */
76    /** Types of all method flavors */
77    private static final Class<?>[][] PARAMETER_TYPES = new Class<?>[][] { new Class[] { Class.class }, //
78    new Class[] { String.class, Class.class }, //
79    new Class[] { Class.class, Method[].class }, //
80    new Class[] { String.class, Class.class, Method[].class }, //
81    new Class[] { Class.class, ConstructorArgs.class, Method[].class }, //
82    new Class[] { String.class, Class.class, ConstructorArgs.class, Method[].class } //
83    };
84   
85    /** Values to pass to each method call */
86    private static final Object[][] PARAMETER_VALUES;
87   
88    /** All 6 flavors of method calls */
89    private static final ParamEntry[] PARAMETERS = new ParamEntry[PARAMETER_TYPES.length];
90   
 
91  2 toggle static {
92   
93  2 Method[] methods;
94  2 try {
95  2 methods = new Method[] { A.class.getMethod("add", Integer.TYPE), A.class.getMethod("toString") };
96    } catch (final NoSuchMethodException e) {
97  0 throw new RuntimeException(e);
98    }
99  2 ConstructorArgs args;
100  2 try {
101  2 args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 3);
102    } catch (final SecurityException e) {
103  0 throw new RuntimeException(e);
104    } catch (final NoSuchMethodException e) {
105  0 throw new RuntimeException(e);
106    }
107   
108  2 PARAMETER_VALUES = new Object[][] { new Object[] { A.class }, //
109    new Object[] { "myMock", A.class }, //
110    new Object[] { A.class, methods }, //
111    new Object[] { "myMock", A.class, methods }, //
112    new Object[] { A.class, args, methods }, //
113    new Object[] { "myMock", A.class, args, methods } //
114    };
115   
116  14 for (int i = 0; i < PARAMETERS.length; i++) {
117  12 PARAMETERS[i] = new ParamEntry(PARAMETER_TYPES[i], PARAMETER_VALUES[i]);
118    }
119    }
120   
 
121  2 toggle @Test
122    public void testDisablingClassMocking() {
123  2 EasyMockProperties.getInstance().setProperty(DISABLE_CLASS_MOCKING, Boolean.TRUE.toString());
124  2 try {
125  2 final ArrayList<?> list = createMock(ArrayList.class);
126  0 fail("Class mocking should be disabled");
127    } catch (final IllegalArgumentException e) {
128  2 assertEquals("Class mocking is currently disabled. Change " + EasyMock.DISABLE_CLASS_MOCKING
129    + " to true do modify this behavior", e.getMessage());
130    } finally {
131  2 EasyMockProperties.getInstance().setProperty(DISABLE_CLASS_MOCKING, null);
132    }
133    }
134   
 
135  2 toggle @Test
136    public void testClassMocking() {
137  2 final ArrayList<?> list = createMock(ArrayList.class);
138  2 testList(list);
139    }
140   
 
141  2 toggle @Test
142    public void testInterfaceMocking() {
143  2 final List<?> list = createMock(List.class);
144  2 testList(list);
145    }
146   
 
147  4 toggle private void testList(final List<?> list) {
148  4 expect(list.size()).andReturn(3);
149  4 replay(list);
150  4 assertEquals(3, list.size());
151  4 verify(list);
152    }
153   
 
154  2 toggle @Test
155    public void testResetReplay() {
156  2 final ArrayList<?> list = createStrictMock(ArrayList.class);
157  2 expect(list.size()).andReturn(3);
158  2 reset(list);
159  2 expect(list.size()).andReturn(1);
160  2 replay(list);
161  2 assertEquals(1, list.size());
162  2 verify(list);
163    }
164   
 
165  2 toggle @Test
166    public void testResetTo() {
167  2 final ArrayList<?> list = createMock(ArrayList.class);
168    // Just to make sure the all can be called on a mock
169  2 resetToNice(list);
170  2 resetToStrict(list);
171  2 resetToDefault(list);
172    }
173   
 
174  2 toggle @Test
175    public void testMakeThreadSafe() {
176  2 final ArrayList<?> list = createMock(ArrayList.class);
177    // Just to make sure the all can be called on a mock
178  2 makeThreadSafe(list, true);
179    }
180   
 
181  2 toggle @Test
182    public void testVarargs() {
183  2 final ArrayList<?> list2 = createStrictMock(ArrayList.class);
184  2 final ArrayList<?> list1 = createStrictMock(ArrayList.class);
185   
186  2 expect(list1.size()).andReturn(1);
187  2 expect(list2.size()).andReturn(2);
188  2 reset(list1, list2);
189   
190  2 expect(list1.size()).andReturn(3);
191  2 expect(list2.size()).andReturn(4);
192  2 replay(list1, list2);
193   
194  2 assertEquals(3, list1.size());
195  2 assertEquals(4, list2.size());
196   
197  2 verify(list1, list2);
198    }
199   
 
200  2 toggle @SuppressWarnings("unchecked")
201    @Test
202    public void testCheckOrder() {
203  2 final ArrayList<Integer> list = createStrictMock(ArrayList.class);
204  2 checkOrder(list, false);
205  2 expect(list.add(1)).andReturn(true);
206  2 expect(list.add(3)).andReturn(true);
207  2 replay(list);
208  2 list.add(3);
209  2 list.add(1);
210  2 verify(list);
211    }
212   
 
213  2 toggle @SuppressWarnings("unchecked")
214    @Test
215    public void testStrictMock_Partial() throws Exception {
216  2 final ArrayList<Integer> list = createMockBuilder(ArrayList.class).addMockedMethod("add",
217    Object.class).createStrictMock();
218   
219  2 expect(list.add(1)).andReturn(true);
220  2 expect(list.add(2)).andReturn(true);
221   
222  2 replay(list);
223   
224  2 assertTrue(list.isEmpty());
225   
226  2 try {
227  2 list.add(2);
228  0 fail();
229    } catch (final AssertionError e) {
230    }
231    }
232   
 
233  2 toggle @SuppressWarnings("unchecked")
234    @Test
235    public void testMock_Partial() throws Exception {
236  2 final ArrayList<Integer> list = createMockBuilder(ArrayList.class).addMockedMethod("add",
237    Object.class).createMock();
238   
239  2 expect(list.add(1)).andReturn(true);
240  2 expect(list.add(2)).andReturn(true);
241   
242  2 replay(list);
243   
244  2 assertTrue(list.isEmpty());
245   
246  2 list.add(2);
247  2 list.add(1);
248   
249  2 verify(list);
250    }
251   
 
252  2 toggle @Test
253    public void testNiceMock_Partial() throws Exception {
254  2 final ArrayList<?> list = createMockBuilder(ArrayList.class).addMockedMethod("get").createNiceMock();
255   
256  2 replay(list);
257   
258  2 assertNull(list.get(0));
259  2 assertTrue(list.isEmpty());
260    }
261   
 
262  2 toggle @SuppressWarnings("unchecked")
263    @Test
264    public void testCompare() {
265  2 final BigDecimal expected = new BigDecimal("15.6");
266  2 final BigDecimal actual = new BigDecimal("15.60");
267   
268  2 final ArrayList<BigDecimal> list = createMock(ArrayList.class);
269  2 expect(list.add(cmpEq(expected))).andReturn(true);
270   
271  2 replay(list);
272   
273  2 list.add(actual);
274   
275  2 verify(list);
276    }
277   
 
278  2 toggle @SuppressWarnings("unchecked")
279    @Test
280    public void testNamedMock() throws Exception {
281  2 ArrayList<BigDecimal> list = createMock("mockName", ArrayList.class);
282  2 assertEquals("mockName", list.toString());
283  2 list = createStrictMock("mockName", ArrayList.class);
284  2 assertEquals("mockName", list.toString());
285  2 list = createNiceMock("mockName", ArrayList.class);
286  2 assertEquals("mockName", list.toString());
287   
288    // Note that toString needs to be mocked if you want EasyMock default
289    // toString() behavior
290  2 final Method m = ArrayList.class.getMethod("toString", (Class<?>[]) null);
291   
292  2 list = createMockBuilder(ArrayList.class).addMockedMethod(m).createMock("mockName");
293  2 assertEquals("mockName", list.toString());
294  2 list = createMockBuilder(ArrayList.class).addMockedMethod(m).createStrictMock("mockName");
295  2 assertEquals("mockName", list.toString());
296  2 list = createMockBuilder(ArrayList.class).addMockedMethod(m).createNiceMock("mockName");
297  2 assertEquals("mockName", list.toString());
298    }
299   
 
300  2 toggle @Test
301    public void testStrictMock() throws Exception {
302  2 for (final ParamEntry p : PARAMETERS) {
303  12 final A mock = p.getMock("createStrictMock");
304  12 p.test(mock);
305  12 testStrict(mock);
306    }
307    }
308   
 
309  2 toggle @Test
310    public void testNormalMock() throws Exception {
311  2 for (final ParamEntry p : PARAMETERS) {
312  12 final A mock = p.getMock("createMock");
313  12 p.test(mock);
314  12 testNormal(mock);
315    }
316    }
317   
 
318  2 toggle @Test
319    public void testNiceMock() throws Exception {
320  2 for (final ParamEntry p : PARAMETERS) {
321  12 final A mock = p.getMock("createNiceMock");
322  12 p.test(mock);
323  12 testNice(mock);
324    }
325    }
326   
 
327  2 toggle @Test
328    public void testCreateMockBuilder() {
329  2 final IMockBuilder<A> builder = createMockBuilder(A.class);
330  2 final A a = builder.withConstructor(int.class).withArgs(2).createMock();
331  2 assertEquals(2, a.i);
332    }
333   
334    // 3 mock types
335   
 
336  12 toggle private static void testStrict(final A mock) {
337  12 reset(mock); // just in case we are not in a stable state
338  12 expect(mock.add(1)).andReturn(true);
339  12 expect(mock.add(2)).andReturn(true);
340  12 replay(mock);
341  12 try {
342  12 mock.add(2);
343  0 fail("Should be ordered");
344    } catch (final AssertionError e) {
345    }
346    }
347   
 
348  12 toggle private static void testNormal(final A mock) {
349  12 reset(mock); // just in case we are not in a stable state
350  12 expect(mock.add(1)).andReturn(true);
351  12 expect(mock.add(2)).andReturn(true);
352  12 replay(mock);
353    // unordered
354  12 mock.add(2);
355  12 mock.add(1);
356    // but not nice
357  12 try {
358  12 mock.add(3);
359  0 fail("Should be ordered");
360    } catch (final AssertionError e) {
361    }
362    }
363   
 
364  12 toggle private static void testNice(final A mock) {
365  12 reset(mock); // just in case we are not in a stable state
366  12 replay(mock);
367  12 assertFalse(mock.add(2));
368  12 verify(mock);
369    }
370   
371    // call flavors
372   
 
373  18 toggle private static void testNamed(final A mock) {
374  18 assertEquals("myMock", mock.toString());
375    }
376   
 
377  24 toggle private static void testPartial_NoConstructorCalled(final A mock) {
378    // not really nice since I'm looking at the inner implementation
379  24 assertEquals(0, mock.i);
380    }
381   
 
382  12 toggle private static void testPartial_ConstructorCalled(final A mock) {
383  12 assertEquals(3, mock.i);
384    }
385    }