KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > dynamic > CallBagTest


1 package test.mockobjects.dynamic;
2
3 import com.mockobjects.dynamic.*;
4 import com.mockobjects.util.*;
5
6 import junit.framework.*;
7
8 public class CallBagTest extends TestCase {
9     final String JavaDoc METHOD_A_NAME = "methodA";
10     final String JavaDoc METHOD_A_RESULT = "resultA";
11     final String JavaDoc METHOD_B_NAME = "methodB";
12     final String JavaDoc METHOD_B_RESULT = "resultB";
13     
14     final Throwable JavaDoc METHOD_A_EXCEPTION = new DummyThrowable("Configured test throwable");
15     final String JavaDoc[] METHOD_A_ARGS = new String JavaDoc[] { "a1", "a2" };
16     final ConstraintMatcher METHOD_A_CONSTRAINTS = C.args(C.eq("a1"), C.eq("a2"));
17     final String JavaDoc[] METHOD_B_ARGS = new String JavaDoc[] { "b1", "b2" };
18     private CallBag callSet = new CallBag();
19     private Mock unusedMock = null;
20     
21     private MockCallable methodA = new MockCallable();
22     private MockCallable methodB = new MockCallable();
23     private MockCallable mockCallable = new MockCallable();
24     
25
26     public CallBagTest(String JavaDoc name) {
27         super(name);
28     }
29
30     public void testCallFailsOnEmptySet() throws Throwable JavaDoc {
31         try {
32             callSet.call(unusedMock, "missingMethod", new Object JavaDoc[0]);
33         } catch (AssertionFailedError ex) {
34             AssertMo.assertIncludes("reports empty set in error message", "no methods", ex.getMessage());
35
36             return;
37         }
38
39         fail("Should fail for a missing item");
40     }
41
42     public void testCallPassedToContainedElements() throws Throwable JavaDoc {
43         methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS);
44         methodA.setupMatchesReturn(true);
45         methodA.setExpectedCall(unusedMock, METHOD_A_NAME, METHOD_A_ARGS);
46         methodA.setupCallReturn(METHOD_A_RESULT);
47
48         methodB.setExpectedCallCount(0);
49
50         callSet.addExpect(methodA);
51         callSet.addExpect(methodB);
52
53         assertSame("expected result from method A", METHOD_A_RESULT,
54             callSet.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS));
55
56         methodA.verifyExpectations();
57         methodB.verifyExpectations();
58     }
59     
60     public void testExpectOverridesMatch() throws Throwable JavaDoc {
61         
62         Callable methodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result1"));
63         Callable anotherMethodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2"));
64     
65         callSet.addMatch(methodASignature);
66         callSet.addExpect(new CallOnceExpectation(anotherMethodASignature));
67
68         assertSame("expected result from method B, as expect has precendence over match", "result2",
69             callSet.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS));
70     }
71
72     public void testCallPassedToContainedElementsOtherOrder()
73         throws Throwable JavaDoc {
74         methodA.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS);
75         methodA.setupMatchesReturn(false);
76         methodA.setExpectedCallCount(0);
77         methodB.setExpectedCall(unusedMock, METHOD_B_NAME, METHOD_B_ARGS);
78
79         methodB.setupCallReturn(METHOD_B_RESULT);
80         methodB.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS);
81         methodB.setupMatchesReturn(true);
82
83         callSet.addExpect(methodA);
84         callSet.addExpect(methodB);
85
86         assertSame("expected result from method B", METHOD_B_RESULT,
87             callSet.call(unusedMock, METHOD_B_NAME, METHOD_B_ARGS));
88
89         methodA.verifyExpectations();
90         methodB.verifyExpectations();
91     }
92
93     public void testConfiguredResultReturned() throws Throwable JavaDoc {
94         final String JavaDoc result = "result";
95
96         mockCallable.setupCallReturn(result);
97         mockCallable.setupMatchesReturn(true);
98         
99         callSet.addExpect(mockCallable);
100
101         assertSame("result is returned by mock", result, callSet.call(unusedMock, "method", new Object JavaDoc[0]));
102     }
103
104     public void testCallableThrowableThrown()
105         throws Throwable JavaDoc {
106         final Throwable JavaDoc throwable = new DummyThrowable();
107
108         mockCallable.setupMatchesReturn(true);
109         mockCallable.setupCallThrow(throwable);
110         
111         callSet.addExpect(mockCallable);
112
113         try {
114             callSet.call(unusedMock, "hello", new String JavaDoc[0]);
115         } catch (Throwable JavaDoc ex) {
116             assertSame("exception is caught by mock", throwable, ex);
117         }
118     }
119
120     public void testEmptySetVerifies() throws Exception JavaDoc {
121         callSet.verify();
122     }
123
124     public void testFailureIfNoElementMatches() throws Throwable JavaDoc {
125         final String JavaDoc methodCName = "methodC";
126         final String JavaDoc[] methodCArgs = { "c1", "c2" };
127
128         methodA.setExpectedMatches(methodCName, methodCArgs);
129         methodA.setupMatchesReturn(false);
130         methodA.setExpectedCallCount(0);
131         methodA.setupGetDescription("***methodA-description****");
132         methodB.setExpectedCall(unusedMock, methodCName, methodCArgs);
133         methodB.setupMatchesReturn(false);
134         methodB.setExpectedCallCount(0);
135         methodB.setupGetDescription("***methodB-description****");
136
137         callSet.addExpect(methodA);
138         callSet.addExpect(methodB);
139
140         try {
141             callSet.call(unusedMock, methodCName, methodCArgs);
142         } catch (AssertionFailedError ex) {
143             AssertMo.assertIncludes("method name is in error message", "methodC", ex.getMessage());
144             AssertMo.assertIncludes("argument is in error message (1)", methodCArgs[0], ex.getMessage());
145             AssertMo.assertIncludes("argument is in error message (2)", methodCArgs[1], ex.getMessage());
146
147             AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), ex.getMessage());
148             AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), ex.getMessage());
149
150             return;
151         }
152
153         fail("Should fail for a missing item");
154     }
155
156     public void testVerifiesIfAllContainedElementsVerify()
157         throws Throwable JavaDoc {
158         methodA.setExpectedVerifyCalls(1);
159         methodB.setExpectedVerifyCalls(1);
160
161         callSet.addExpect(methodA);
162         callSet.addExpect(methodB);
163         callSet.verify();
164
165         methodA.verifyExpectations();
166         methodB.verifyExpectations();
167     }
168
169     public void testVerifyFailsIfContainedElementDoesNotVerify()
170         throws Exception JavaDoc {
171         methodA.setExpectedVerifyCalls(1);
172         methodA.setupVerifyThrow(new AssertionFailedError("verify failed"));
173         callSet.addExpect(methodA);
174
175         try {
176             callSet.verify();
177         } catch (AssertionFailedError ex) {
178             methodA.verifyExpectations();
179
180             return;
181         }
182
183         fail("Should have got a failure for contained element failing");
184     }
185 }
186
Popular Tags