1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import org.jmock.*;
5 import org.jmock.expectation.ExpectationCounter;
6 import org.jmock.expectation.ExpectationValue;
7 import org.jmock.expectation.Verifier;
8 import org.jmock.matcher.ArgumentsMatcher;
9 import org.jmock.matcher.MethodNameMatcher;
10 import org.jmock.matcher.StatelessInvocationMatcher;
11 import org.jmock.stub.VoidStub;
12
13 public class InvocationMockerTest extends AbstractTestCase {
14 private InvocationMatcher matchAll = new StatelessInvocationMatcher() {
15 public boolean matches(Invocation invocation) {
16 return true;
17 }
18 };
19 private InvocationMatcher matchNone = new StatelessInvocationMatcher() {
20 public boolean matches(Invocation invocation) {
21 return false;
22 }
23 };
24
25 public class MockInvocationMatcher implements InvocationMatcher {
26 public ExpectationValue invocation = new ExpectationValue("MockInvocationMatcher.invoked");
27 public ExpectationValue match = new ExpectationValue("MockInvocationMatcher.matches");
28 public ExpectationCounter verifyCalls = new ExpectationCounter("Verify calls");
29
30 public boolean matches(Invocation invocation) {
31 match.setActual(invocation);
32 return true;
33 }
34
35 public void invoked(Invocation invocation) {
36 this.invocation.setActual(invocation);
37 }
38
39 public void verify() {
40 verifyCalls.inc();
41 }
42 }
43
44 public class MockStub implements Stub {
45 public ExpectationValue stubInvocation = new ExpectationValue("Stub invocation");
46
47 public Object invoke(Invocation invocation) throws Throwable {
48 stubInvocation.setActual(invocation);
49 return "stub result";
50 }
51
52 public String getDescription() {
53 return "MockStub";
54 }
55 };
56
57 private Invocation exampleInvocation =
58 new Invocation("example", new Class[]{String.class, String.class}, Void.class,
59 new Object[]{"arg1", "arg2"});
60
61
62 public void testMatchesIfEverythingMatches() {
63 InvocationMocker invocationMocker =
64 new InvocationMocker(new InvocationMatcher[]{matchAll, matchAll}, null);
65
66 assertTrue("Should have matched", invocationMocker.matches(exampleInvocation));
67 }
68
69 public void testDoesNotMatchIfEverythingMatches() {
70 InvocationMocker invocationMocker =
71 new InvocationMocker(new InvocationMatcher[]{matchAll, matchNone}, null);
72
73 assertFalse("Should not have matched", invocationMocker.matches(exampleInvocation));
74 }
75
76 public void testMatchesInvocationWithParameters() {
77 InvocationMocker invocationMocker = new InvocationMocker(
78 new InvocationMatcher[]{
79 new MethodNameMatcher("example"),
80 new ArgumentsMatcher(new Constraint[]{C.eq("arg1"), C.eq("arg2")})}, null);
81
82 assertTrue("Should have matched", invocationMocker.matches(exampleInvocation));
83 }
84
85 public void testDoesNotMatchWithDifferentParameter() {
86 InvocationMocker invocationMocker = new InvocationMocker(
87 new InvocationMatcher[]{
88 new MethodNameMatcher("example"),
89 new ArgumentsMatcher(new Constraint[]{C.eq("arg1"), C.eq("not arg2")})}, null);
90
91 assertFalse("Should not have matched", invocationMocker.matches(exampleInvocation));
92 }
93
94 public void testMatchesInvocationBeforeCallingStub() throws Throwable {
95 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();
96
97 InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[]{mockInvocationMatcher}, new VoidStub());
98 mockInvocationMatcher.invocation.setExpected(exampleInvocation);
99
100 mocker.invoke(exampleInvocation);
101
102 Verifier.verifyObject(mockInvocationMatcher);
103 }
104
105 public void testDelegatesVerifyToInvocationMatchers() throws Throwable {
106 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();
107
108 InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[]{mockInvocationMatcher}, new VoidStub());
109 mockInvocationMatcher.verifyCalls.setExpected(1);
110
111 mocker.verify();
112
113 Verifier.verifyObject(mockInvocationMatcher);
114 }
115
116
117 public void testDelegatesInvocationToStubObject() throws Throwable {
118 MockStub mockStub = new MockStub();
119
120 InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[0], mockStub);
121
122 mockStub.stubInvocation.setExpected(exampleInvocation);
123
124 assertEquals("Should be invoke result", "stub result", mocker.invoke(exampleInvocation));
125
126 Verifier.verifyObject(mockStub);
127 }
128
129 public void testCanAddExtraMatchers() throws Throwable {
130 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();
131
132 InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[0], new VoidStub());
133 mockInvocationMatcher.match.setExpected(exampleInvocation);
134 mockInvocationMatcher.invocation.setExpected(exampleInvocation);
135
136 mocker.addMatcher(mockInvocationMatcher);
137 mocker.matches(exampleInvocation);
138 mocker.invoke(exampleInvocation);
139
140 Verifier.verifyObject(mockInvocationMatcher);
141 }
142 }
This page was automatically generated by Maven