Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
31   102   10   5,17
0   67   0,32   3
6     1,67  
2    
 
  CallbackTest       Line # 30 27 0% 6 0 100% 1.0
  CallbackTest.Callback       Line # 34 4 0% 4 1 87,5% 0.875
 
  (2)
 
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 org.easymock.IAnswer;
23    import org.easymock.tests.IMethods;
24    import org.junit.Before;
25    import org.junit.Test;
26   
27    /**
28    * @author OFFIS, Tammo Freese
29    */
 
30    public class CallbackTest {
31   
32    private IMethods mock;
33   
 
34    private static class Callback<T> implements IAnswer<T> {
35    private int callCount;
36   
37    private final T result;
38   
 
39  6 toggle public Callback(final T result) {
40  6 this.result = result;
41    }
42   
 
43  0 toggle public void run() {
44    }
45   
 
46  6 toggle public int getCallCount() {
47  6 return callCount;
48    }
49   
 
50  10 toggle public T answer() throws Throwable {
51  10 callCount++;
52  10 return result;
53    }
54    }
55   
 
56  2 toggle @Before
57    public void setUp() {
58  2 mock = createStrictMock(IMethods.class);
59    }
60   
 
61  2 toggle @Test
62    public void callback() {
63  2 final Callback<String> c1 = new Callback<String>("1");
64  2 final Callback<Object> c2 = new Callback<Object>(null);
65  2 final Callback<Object> c3 = new Callback<Object>(null);
66   
67  2 expect(mock.oneArg("2")).andAnswer(c1).times(2);
68  2 mock.simpleMethodWithArgument("One");
69  2 expectLastCall().andAnswer(c2);
70  2 mock.simpleMethodWithArgument("Two");
71  2 expectLastCall().andAnswer(c3).times(2);
72   
73  2 replay(mock);
74   
75  2 mock.oneArg("2");
76  2 mock.oneArg("2");
77  2 try {
78  2 mock.oneArg("2");
79    } catch (final AssertionError ignored) {
80    }
81  2 try {
82  2 mock.simpleMethodWithArgument("Two");
83    } catch (final AssertionError ignored) {
84    }
85  2 mock.simpleMethodWithArgument("One");
86  2 try {
87  2 mock.simpleMethodWithArgument("One");
88    } catch (final AssertionError ignored) {
89    }
90  2 mock.simpleMethodWithArgument("Two");
91  2 mock.simpleMethodWithArgument("Two");
92  2 try {
93  2 mock.simpleMethodWithArgument("Two");
94    } catch (final AssertionError ignored) {
95    }
96  2 verify(mock);
97   
98  2 assertEquals(2, c1.getCallCount());
99  2 assertEquals(1, c2.getCallCount());
100  2 assertEquals(2, c3.getCallCount());
101    }
102    }