KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package test.mockobjects.dynamic;
5
6 import junit.framework.TestCase;
7
8 import com.mockobjects.dynamic.*;
9
10
11 public class CallMatchTest extends TestCase {
12     private static final Object JavaDoc[] IGNORED_ARGS = new Object JavaDoc[0];
13     final String JavaDoc METHOD_NAME = "methodName";
14     Mock mock = new Mock(DummyInterface.class, "mock");
15     MockCallable mockCallable = new MockCallable();
16     MockConstraintMatcher mockConstraintMatcher = new MockConstraintMatcher();
17     CallSignature callSignature = new CallSignature(METHOD_NAME, mockConstraintMatcher, mockCallable);
18
19     public CallMatchTest(String JavaDoc name) {
20         super(name);
21     }
22
23     public void testCallArgumentsArePropagatedToDecorated()
24         throws Throwable JavaDoc {
25         final Object JavaDoc[] arguments = IGNORED_ARGS;
26         final String JavaDoc result = "result";
27
28         mockCallable.setExpectedCall(mock, METHOD_NAME, arguments);
29         mockCallable.setupCallReturn(result);
30
31         callSignature.call(mock, METHOD_NAME, arguments);
32
33         mockCallable.verifyExpectations();
34     }
35
36     public void testUncalledCallVerifies() {
37         mockCallable.setExpectedVerifyCalls(1);
38
39         callSignature.verify();
40
41         mockCallable.verifyExpectations();
42     }
43
44     public void testCallSuccessVerifies() throws Throwable JavaDoc {
45         mockCallable.setExpectedVerifyCalls(1);
46         mockCallable.setupCallReturn("result");
47
48         callSignature.call(mock, "methodName", IGNORED_ARGS);
49         callSignature.verify();
50
51         mockCallable.verifyExpectations();
52     }
53
54     public void testMultipleCallsSucceed() throws Throwable JavaDoc {
55         mockCallable.setupCallReturn("result");
56
57         callSignature.call(mock, METHOD_NAME, IGNORED_ARGS);
58         callSignature.call(mock, METHOD_NAME, IGNORED_ARGS);
59
60         mockCallable.verifyExpectations();
61     }
62
63     public void testCallFailure() throws Throwable JavaDoc {
64         // na
65
}
66
67     public void testCallDoesNotMatchWhenWrongName() throws Throwable JavaDoc {
68         assertFalse("call does not match", callSignature.matches("anotherName", IGNORED_ARGS));
69         mockCallable.verifyExpectations();
70     }
71
72     public void testMatchesAfterCalls() throws Throwable JavaDoc {
73         mockCallable.setupCallReturn("result");
74         mockCallable.setupMatchesReturn(true);
75
76         mockConstraintMatcher.setupMatches(true);
77
78         callSignature.call(mock, METHOD_NAME, IGNORED_ARGS);
79         assertTrue("matches after first call", callSignature.matches(METHOD_NAME, IGNORED_ARGS));
80         callSignature.call(mock, METHOD_NAME, IGNORED_ARGS);
81         assertTrue("matches after further calls", callSignature.matches(METHOD_NAME, IGNORED_ARGS));
82
83         mockCallable.verifyExpectations();
84     }
85
86     public void testMatchesDelegatesToContraintMatcher()
87         throws Throwable JavaDoc {
88         final String JavaDoc[] args = new String JavaDoc[] { "a1", "a2" };
89
90         mockConstraintMatcher.addExpectedMatches(args);
91         mockConstraintMatcher.setupMatches(true);
92
93         assertTrue("matches delegated to constraint matcher", callSignature.matches(METHOD_NAME, args));
94         mockConstraintMatcher.verify();
95     }
96
97     public void testMatchesFailure() throws Throwable JavaDoc {
98         mockConstraintMatcher.setupMatches(false);
99
100         assertFalse("Should not match if constraint matcher doesn't", callSignature.matches(METHOD_NAME, IGNORED_ARGS));
101     }
102 }
103
Popular Tags