1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic.support;
3
4 import junit.framework.AssertionFailedError;
5 import org.jmock.dynamic.Invocation;
6 import org.jmock.dynamic.Invokable;
7 import org.jmock.expectation.*;
8
9 public class MockCallable implements Invokable {
10
11 final public String name;
12
13 public ExpectationValue callInvocation = new ExpectationValue("call");
14
15 private ReturnValue callResult = new ReturnValue("call.return");
16 private Throwable callThrow = null;
17
18 private ExpectationValue matchesMethodName = new org.jmock.expectation.ExpectationValue("matches.methodName");
19 private ExpectationList matchesArgs = new ExpectationList("matches.args");
20 public boolean matches = false;
21 private ExpectationCounter matchesCount = new ExpectationCounter("matches.count");
22
23 private ExpectationCounter verifyCount = new ExpectationCounter("verify.count");
24 private AssertionFailedError verifyError = null;
25
26 public MockCallable(String name) {
27 this.name = name;
28 }
29
30 public void setupCallReturn(Object result) {
31 callResult.setValue(result);
32 }
33
34 public void setupCallThrow(Throwable thrown) {
35 callThrow = thrown;
36 }
37
38 public Object invoke(Invocation anInvocation) throws Throwable {
39 callInvocation.setActual(anInvocation);
40
41 if (callThrow != null) {
42 throw callThrow;
43 } else {
44 return callResult.getValue();
45 }
46 }
47
48 public void setExpectedMatches(String methodName, Object[] args) {
49 matchesMethodName.setExpected(methodName);
50 matchesArgs.addExpectedMany(args);
51 }
52
53 public void setExpectedMatchesCount(int count) {
54 matchesCount.setExpected(count);
55 }
56
57 public boolean matches(Invocation invocation) {
58 matchesMethodName.setActual(invocation.getMethodName());
59 matchesArgs.addActualMany(invocation.getParameterValues().toArray());
60 matchesCount.inc();
61 return matches;
62 }
63
64 public void setExpectedVerifyCalls(int count) {
65 verifyCount.setExpected(count);
66 }
67
68 public void setupVerifyThrow(AssertionFailedError err) {
69 verifyError = err;
70 }
71
72 /***
73 * @deprecated to avoid calling verify instead of verifyExpectations
74 */
75 public void verify() {
76 verifyCount.inc();
77 if (verifyError != null) throw verifyError;
78 }
79
80 /***
81 * We have to rename 'verify' because we want to mock the behaviour of the
82 * verify method itself.
83 */
84 public void verifyExpectations() {
85 Verifier.verifyObject(this);
86 }
87
88 public String getDescription() {
89 return name;
90 }
91
92 public String toString() {
93 return "MockCallable " + name;
94 }
95 }
This page was automatically generated by Maven