KickJava   Java API By Example, From Geeks To Geeks.

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


1 package test.mockobjects.dynamic;
2
3 import junit.framework.*;
4
5 import com.mockobjects.*;
6 import com.mockobjects.dynamic.Callable;
7 import com.mockobjects.dynamic.Mock;
8 import com.mockobjects.util.*;
9
10 public class MockCallable implements Callable {
11     
12     private ExpectationCounter callCount = new ExpectationCounter("call.count");
13     private ExpectationValue callMock = new ExpectationValue("call.mock");
14     private ExpectationValue callMethodName = new ExpectationValue("call.methodName");
15     private ExpectationList callArgs = new ExpectationList("call.args");
16     private ReturnValue callResult = new ReturnValue("call.return");
17     private Throwable JavaDoc callThrow = null;
18     
19     private ExpectationValue matchesMethodName = new ExpectationValue("matches.methodName");
20     private ExpectationList matchesArgs = new ExpectationList("matches.args");
21     private ReturnValue matchesResult = new ReturnValue("matches.return");
22     private ExpectationCounter matchesCount = new ExpectationCounter("matches.count");
23     
24     private ExpectationCounter verifyCount = new ExpectationCounter("verify.count");
25     private AssertionFailedError verifyError = null;
26     
27     private ReturnValue toStringResult = new ReturnValue("toString.return");
28     
29     
30     public void setExpectedCallCount( int count ) {
31         callCount.setExpected(count);
32     }
33     
34     public void setExpectedCall( Mock mock, String JavaDoc methodName, Object JavaDoc[] args ) {
35         callMock.setExpected(mock);
36         callMethodName.setExpected(methodName);
37         callArgs.addExpectedMany(args);
38     }
39     
40     public void setupCallReturn( Object JavaDoc result ) {
41         callResult.setValue(result);
42     }
43     
44     public void setupCallThrow( Throwable JavaDoc thrown ) {
45         callThrow = thrown;
46     }
47     
48     public Object JavaDoc call(Mock mock, String JavaDoc methodName, Object JavaDoc[] args) throws Throwable JavaDoc {
49         callMock.setActual(mock);
50         callMethodName.setActual(methodName);
51         callArgs.addActualMany(args);
52         callCount.inc();
53         
54         if( callThrow != null ) {
55             throw callThrow;
56         } else {
57             return callResult.getValue();
58         }
59     }
60     
61     public void setExpectedMatches( String JavaDoc methodName, Object JavaDoc[] args ) {
62         matchesMethodName.setExpected(methodName);
63         matchesArgs.addExpectedMany(args);
64     }
65     
66     public void setExpectedMatchesCount(int count) {
67         matchesCount.setExpected(count);
68     }
69     
70     public void setupMatchesReturn( boolean result ) {
71         matchesResult.setValue(result);
72     }
73     
74     public boolean matches(String JavaDoc methodName, Object JavaDoc[] args) {
75         matchesMethodName.setActual(methodName);
76         matchesArgs.addActualMany(args);
77         matchesCount.inc();
78         return matchesResult.getBooleanValue();
79     }
80
81     public void setExpectedVerifyCalls( int count ) {
82         verifyCount.setExpected(count);
83     }
84
85     public void setupVerifyThrow( AssertionFailedError err ) {
86         verifyError = err;
87     }
88     
89     /** @deprecated to avoid calling verify instead of verifyExpectations
90      */

91     public void verify() {
92         verifyCount.inc();
93         if( verifyError != null ) throw verifyError;
94     }
95     
96     /** We have to rename 'verify' because we want to mock the behaviour of the
97      * verify method itself.
98      */

99     public void verifyExpectations() {
100         Verifier.verifyObject(this);
101     }
102     
103     public void setupGetDescription( String JavaDoc result ) {
104         toStringResult.setValue(result);
105     }
106     
107     public String JavaDoc getDescription() {
108         return (String JavaDoc)toStringResult.getValue();
109     }
110 }
111
Popular Tags