Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
173   349   26   8,24
4   256   0,15   10,5
21     1,24  
2    
 
  CaptureTest       Line # 34 172 0% 25 3 98,5% 0.9846939
  CaptureTest.A       Line # 36 1 0% 1 2 0% 0.0
 
  (32)
 
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.util.Arrays;
23   
24    import org.easymock.Capture;
25    import org.easymock.CaptureType;
26    import org.easymock.tests.IMethods;
27    import org.junit.After;
28    import org.junit.Before;
29    import org.junit.Test;
30   
31    /**
32    * @author Henri Tremblay
33    */
 
34    public class CaptureTest {
35   
 
36    public static class A {
 
37  0 toggle public String foo(final IMethods methods) {
38  0 return methods.oneArg(2);
39    }
40    }
41   
 
42  32 toggle @Before
43    public void setUp() throws Exception {
44    }
45   
 
46  32 toggle @After
47    public void tearDown() throws Exception {
48    }
49   
 
50  8 toggle private Capture<Integer> testCaptureType(final CaptureType type) {
51  8 final IMethods mock = createMock(IMethods.class);
52  8 final Capture<Integer> captured = new Capture<Integer>(type);
53   
54  8 expect(mock.oneArg(capture(captured))).andReturn("1");
55  8 expect(mock.oneArg(anyInt())).andReturn("1");
56  8 expect(mock.oneArg(capture(captured))).andReturn("2").times(2);
57  8 mock.twoArgumentMethod(capture(captured), eq(5));
58  8 mock.twoArgumentMethod(capture(captured), capture(captured));
59   
60  8 replay(mock);
61   
62  8 mock.oneArg(0);
63  8 mock.oneArg(1);
64  8 mock.oneArg(2);
65  8 mock.oneArg(3);
66  8 mock.twoArgumentMethod(4, 5);
67  8 mock.twoArgumentMethod(6, 7);
68   
69  8 verify(mock);
70   
71  8 return captured;
72    }
73   
 
74  2 toggle @Test
75    public void testCaptureFirst() {
76  2 final Capture<Integer> captured = testCaptureType(CaptureType.FIRST);
77  2 assertEquals(0, (int) captured.getValue());
78    }
79   
 
80  2 toggle @Test
81    public void testCaptureLast() {
82  2 final Capture<Integer> captured = testCaptureType(CaptureType.LAST);
83  2 assertEquals(7, (int) captured.getValue());
84    }
85   
 
86  2 toggle @Test
87    public void testCaptureAll() {
88  2 final Capture<Integer> captured = testCaptureType(CaptureType.ALL);
89  2 assertEquals(Arrays.asList(0, 2, 3, 4, 6, 7), captured.getValues());
90    }
91   
 
92  2 toggle @Test
93    public void testCaptureNone() {
94  2 final Capture<Integer> captured = testCaptureType(CaptureType.NONE);
95  2 assertFalse(captured.hasCaptured());
96    }
97   
98    // capture in thread
99    // after replay issue?
100   
 
101  2 toggle @Test
102    public void testCaptureRightOne() {
103  2 final Capture<String> captured = new Capture<String>();
104  2 final IMethods mock = createMock(IMethods.class);
105   
106  2 expect(mock.oneArg(and(eq("test"), capture(captured)))).andReturn("answer1");
107  2 expect(mock.oneArg("a")).andReturn("answer2");
108   
109  2 replay(mock);
110   
111  2 assertEquals("answer2", mock.oneArg("a"));
112  2 assertFalse(captured.hasCaptured());
113   
114  2 assertEquals("answer1", mock.oneArg("test"));
115  2 assertEquals("test", captured.getValue());
116   
117  2 verify(mock);
118    }
119   
 
120  2 toggle @Test
121    public void testPrimitiveVsObject() {
122  2 final Capture<Integer> capture = new Capture<Integer>();
123  2 final IMethods mock = createMock(IMethods.class);
124   
125  2 expect(mock.oneArg(capture(capture))).andReturn("answer");
126  2 expect(mock.oneArg((Integer) capture(capture))).andReturn("answer");
127   
128  2 replay(mock);
129   
130  2 assertEquals("answer", mock.oneArg(2));
131  2 assertEquals(2, capture.getValue().intValue());
132   
133  2 assertEquals("answer", mock.oneArg(Integer.valueOf(3)));
134  2 assertEquals(3, capture.getValue().intValue());
135   
136  2 verify(mock);
137    }
138   
 
139  2 toggle @Test
140    public void testAnd() {
141  2 final Capture<String> captured = new Capture<String>();
142  2 final IMethods mock = createMock(IMethods.class);
143   
144  2 expect(mock.oneArg(and(capture(captured), eq("test")))).andReturn("answer");
145   
146  2 replay(mock);
147   
148  2 assertEquals("answer", mock.oneArg("test"));
149  2 assertEquals("test", captured.getValue());
150   
151  2 verify(mock);
152    }
153   
 
154  2 toggle @Test
155    public void testPrimitive() {
156  2 final Capture<Integer> captureI = new Capture<Integer>();
157  2 final Capture<Long> captureL = new Capture<Long>();
158  2 final Capture<Float> captureF = new Capture<Float>();
159  2 final Capture<Double> captureD = new Capture<Double>();
160  2 final Capture<Byte> captureB = new Capture<Byte>();
161  2 final Capture<Character> captureC = new Capture<Character>();
162  2 final Capture<Boolean> captureBool = new Capture<Boolean>();
163   
164  2 final IMethods mock = createMock(IMethods.class);
165   
166  2 expect(mock.oneArg(capture(captureI))).andReturn("answerI");
167  2 expect(mock.oneArg(capture(captureL))).andReturn("answerL");
168  2 expect(mock.oneArg(capture(captureF))).andReturn("answerF");
169  2 expect(mock.oneArg(capture(captureD))).andReturn("answerD");
170  2 expect(mock.oneArg(capture(captureB))).andReturn("answerB");
171  2 expect(mock.oneArg(capture(captureC))).andReturn("answerC");
172  2 expect(mock.oneArg(capture(captureBool))).andReturn("answerZ");
173   
174  2 replay(mock);
175   
176  2 assertEquals("answerI", mock.oneArg(1));
177  2 assertEquals("answerL", mock.oneArg(2l));
178  2 assertEquals("answerF", mock.oneArg(3.0f));
179  2 assertEquals("answerD", mock.oneArg(4.0));
180  2 assertEquals("answerB", mock.oneArg((byte) 5));
181  2 assertEquals("answerC", mock.oneArg((char) 6));
182  2 assertEquals("answerZ", mock.oneArg(true));
183   
184  2 assertEquals(1, captureI.getValue().intValue());
185  2 assertEquals(2l, captureL.getValue().longValue());
186  2 assertEquals(3.0f, captureF.getValue().floatValue(), 0.0);
187  2 assertEquals(4.0, captureD.getValue().doubleValue(), 0.0);
188  2 assertEquals((byte) 5, captureB.getValue().byteValue());
189  2 assertEquals((char) 6, captureC.getValue().charValue());
190  2 assertEquals(true, captureBool.getValue().booleanValue());
191   
192  2 verify(mock);
193    }
194   
 
195  2 toggle @Test
196    public void testCapture() {
197  2 final Capture<String> capture = new Capture<String>();
198  2 assertFalse(capture.hasCaptured());
199  2 try {
200  2 capture.getValue();
201  0 fail("Should not be allowed");
202    } catch (final AssertionError e) {
203  2 assertEquals("Nothing captured yet", e.getMessage());
204    }
205  2 assertEquals("Nothing captured yet", capture.toString());
206  2 capture.setValue("s");
207  2 assertTrue(capture.hasCaptured());
208  2 assertEquals("s", capture.getValue());
209  2 assertEquals("s", capture.toString());
210  2 capture.reset();
211  2 assertFalse(capture.hasCaptured());
212  2 try {
213  2 capture.getValue();
214  0 fail();
215    } catch (final AssertionError e) {
216  2 assertEquals("Nothing captured yet", e.getMessage());
217    }
218   
219  2 capture.setValue(null);
220  2 assertTrue(capture.hasCaptured());
221  2 assertNull(capture.getValue());
222  2 assertEquals("null", capture.toString());
223    }
224   
 
225  2 toggle @Test
226    public void testCaptureMultiple() {
227  2 final Capture<String> capture = new Capture<String>(CaptureType.ALL);
228  2 capture.setValue("a");
229  2 capture.setValue("b");
230  2 try {
231  2 capture.getValue();
232  0 fail();
233    } catch (final AssertionError e) {
234  2 assertEquals("More than one value captured: " + capture.getValues(), e.getMessage());
235    }
236  2 assertEquals(Arrays.asList("a", "b"), capture.getValues());
237    }
238   
 
239  2 toggle @Test
240    public void testCapture_2617107() {
241   
242  2 final IMethods mock = createMock(IMethods.class);
243   
244  2 final Capture<String> cap1 = new Capture<String>();
245  2 final Capture<String> cap2 = new Capture<String>();
246  2 final Capture<String> cap3 = new Capture<String>();
247  2 final Capture<String> cap4 = new Capture<String>();
248   
249  2 mock.simpleMethodWithArgument(and(isA(String.class), capture(cap1)));
250  2 mock.simpleMethodWithArgument(and(isA(String.class), capture(cap2)));
251  2 mock.simpleMethodWithArgument(and(isA(String.class), capture(cap3)));
252  2 mock.simpleMethodWithArgument(and(isA(String.class), capture(cap4)));
253   
254  2 replay(mock);
255   
256  2 final String[] s = { "one", "two", "three", "four" };
257   
258  2 for (final String element : s) {
259  8 mock.simpleMethodWithArgument(element);
260    }
261   
262  2 assertEquals("one", cap1.getValue());
263  2 assertEquals("two", cap2.getValue());
264  2 assertEquals("three", cap3.getValue());
265  2 assertEquals("four", cap4.getValue());
266   
267  2 verify(mock);
268    }
269   
 
270  2 toggle @Test
271    public void testCaptureNonStrictControl_2133741() {
272  2 testCaptureHelper(createMock(IMethods.class));
273    }
274   
 
275  2 toggle @Test
276    public void testCaptureStrictControl_2133741() {
277  2 testCaptureHelper(createStrictMock(IMethods.class));
278    }
279   
 
280  4 toggle protected void testCaptureHelper(final IMethods mock) {
281  4 final Capture<String> capture1 = new Capture<String>();
282  4 final Capture<String> capture2 = new Capture<String>();
283   
284  4 mock.simpleMethodWithArgument(capture(capture1));
285  4 mock.simpleMethodWithArgument(capture(capture2));
286   
287  4 replay(mock);
288  4 mock.simpleMethodWithArgument("a");
289  4 mock.simpleMethodWithArgument("b");
290  4 verify(mock);
291   
292  4 assertTrue(capture1.hasCaptured());
293  4 assertTrue(capture2.hasCaptured());
294  4 assertFalse(capture1.getValue() == capture2.getValue());
295    }
296   
 
297  2 toggle @Test
298    public void testCapture1_2446744() {
299  2 final Capture<String> capture1 = new Capture<String>();
300  2 final Capture<String> capture2 = new Capture<String>();
301  2 final Capture<String> capture3 = new Capture<String>();
302  2 final IMethods mock = createMock(IMethods.class);
303  2 expect(mock.oneArg(capture(capture1))).andReturn("1").once();
304  2 expect(mock.oneArg(capture(capture2))).andReturn("2").once();
305  2 expect(mock.oneArg(capture(capture3))).andReturn("3").once();
306   
307  2 replay(mock);
308   
309  8 for (int i = 0; i < 3; i++) {
310  6 final String string = "Run" + (i + 1);
311  6 mock.oneArg(string);
312    }
313   
314  2 assertEquals("Run3", capture3.getValue());
315  2 assertEquals("Run2", capture2.getValue());
316  2 assertEquals("Run1", capture1.getValue());
317    }
318   
 
319  2 toggle @Test
320    public void testCapture2_2446744() {
321  2 final Capture<String> capture = new Capture<String>(CaptureType.ALL);
322  2 final IMethods mock = createMock(IMethods.class);
323  2 expect(mock.oneArg(capture(capture))).andReturn("1").once();
324  2 expect(mock.oneArg(capture(capture))).andReturn("2").once();
325  2 expect(mock.oneArg(capture(capture))).andReturn("3").once();
326   
327  2 replay(mock);
328   
329  8 for (int i = 0; i < 3; i++) {
330  6 final String string = "Run" + (i + 1);
331  6 mock.oneArg(string);
332    }
333   
334  2 assertEquals(Arrays.asList("Run1", "Run2", "Run3"), capture.getValues());
335    }
336   
 
337  2 toggle @Test
338    public void testCaptureFromStub() {
339  2 final Capture<String> capture = new Capture<String>(CaptureType.ALL);
340  2 final IMethods mock = createMock(IMethods.class);
341  2 expect(mock.oneArg(capture(capture))).andStubReturn("1");
342   
343  2 replay(mock);
344   
345  2 mock.oneArg("test");
346   
347  2 assertEquals("test", capture.getValue());
348    }
349    }