KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 14-Apr-2003
3  */

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