KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > dynamic > Mock


1 package com.mockobjects.dynamic;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Proxy JavaDoc;
6
7 import junit.framework.AssertionFailedError;
8
9 import com.mockobjects.Verifiable;
10 import com.mockobjects.constraint.Constraint;
11
12 public class Mock implements InvocationHandler JavaDoc, Verifiable {
13     private String JavaDoc name;
14     private Object JavaDoc proxy;
15     private CallFactory callFactory;
16     private CallableAddable callSequence;
17
18     public Mock(CallFactory callFactory, CallableAddable callableAddable, Class JavaDoc mockedClass, String JavaDoc name) {
19         this.name = name;
20         this.callFactory = callFactory;
21         this.callSequence = callableAddable;
22         this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class JavaDoc[] { mockedClass }, this);
23     }
24
25     public Mock(Class JavaDoc mockedClass, String JavaDoc nonDefaultName) {
26         this(new DefaultCallFactory(), new CallBag(), mockedClass, nonDefaultName);
27     }
28
29     public Mock(Class JavaDoc mockedClass) {
30         this(mockedClass, mockNameFromClass(mockedClass));
31     }
32
33     public void reset() {
34         this.callSequence.reset();
35     }
36     
37     public static String JavaDoc mockNameFromClass(Class JavaDoc c) {
38         return "mock" + className(c);
39     }
40     
41     public static String JavaDoc className(Class JavaDoc c) {
42         String JavaDoc name = c.getName();
43         int dotIndex = name.lastIndexOf('.');
44
45         if (dotIndex >= 0) {
46             return name.substring(dotIndex + 1);
47         } else {
48             return name;
49         }
50     }
51
52     private ConstraintMatcher createConstraintMatcher(Object JavaDoc constraintArg) {
53         // Can't overload this method as callee had an Object parameter, and java
54
// doesn't do a secondary dispatch on the true underlying type
55

56         if (constraintArg instanceof Constraint[]) {
57             // to support possible legacy usage of new Contraint[] {...}
58
return new FullConstraintMatcher((Constraint[])constraintArg);
59         } else if (constraintArg instanceof Constraint) {
60             // to support usage of C.lt(5) type constraints
61
return C.args((Constraint)constraintArg);
62         } else {
63             // normal usage of the overloaded expect/match object parameter
64
return C.args(C.eq(constraintArg));
65         }
66     }
67     
68     public String JavaDoc getMockName() {
69         return this.name;
70     }
71     
72     public String JavaDoc toString() {
73         return this.name;
74     }
75
76     public Object JavaDoc proxy() {
77         return this.proxy;
78     }
79
80     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
81         throws Throwable JavaDoc {
82         try {
83             if (isCheckingEqualityOnProxy(method, args)) {
84                 return new Boolean JavaDoc(args[0] == this.proxy);
85             } else if (isMockNameGetter(method, args)) {
86                 return this.getMockName();
87             } else {
88                 return callSequence.call(this, method.getName(), (args == null ? new Object JavaDoc[0] : args));
89             }
90         } catch (AssertionFailedError ex) {
91             throw new AssertionFailedError(name + ": " + ex.getMessage());
92         }
93     }
94
95     private boolean isCheckingEqualityOnProxy(Method JavaDoc method, Object JavaDoc[] args) {
96         return (method.getName().equals("equals")) && (args.length == 1) && (Proxy.isProxyClass(args[0].getClass()));
97     }
98     
99     private boolean isMockNameGetter(Method JavaDoc method, Object JavaDoc[] args) {
100         return (method.getName().equals("getMockName")) && (args.length == 0);
101     }
102
103     public void verify() {
104         try {
105             callSequence.verify();
106         } catch (AssertionFailedError ex) {
107             throw new AssertionFailedError(name + ": " + ex.getMessage());
108         }
109     }
110
111     public void expect(String JavaDoc methodName) {
112         expect(methodName, C.NO_ARGS);
113     }
114     
115     public void expect(String JavaDoc methodName, Object JavaDoc singleEqualArg) {
116         expect(methodName, createConstraintMatcher(singleEqualArg));
117     }
118         
119     public void expect(String JavaDoc methodName, ConstraintMatcher args) {
120         callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createVoidStub())));
121     }
122     
123     public void expectAndReturn(String JavaDoc methodName, Object JavaDoc result) {
124         this.expectAndReturn(methodName, C.NO_ARGS, result);
125     }
126     
127     public void expectAndReturn(String JavaDoc methodName, boolean result) {
128         this.expectAndReturn(methodName, new Boolean JavaDoc(result));
129     }
130     
131     public void expectAndReturn(String JavaDoc methodName, int result) {
132         this.expectAndReturn(methodName, new Integer JavaDoc(result));
133     }
134
135     public void expectAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, Object JavaDoc result) {
136         this.expectAndReturn(methodName, createConstraintMatcher(singleEqualArg), result);
137     }
138     
139     public void expectAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, boolean result) {
140         this.expectAndReturn(methodName, singleEqualArg, new Boolean JavaDoc(result));
141     }
142     
143     public void expectAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, int result) {
144         this.expectAndReturn(methodName, singleEqualArg, new Integer JavaDoc(result));
145     }
146     
147     public void expectAndReturn(String JavaDoc methodName, ConstraintMatcher args, Object JavaDoc result) {
148         callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result))));
149     }
150     
151     public void expectAndReturn(String JavaDoc methodName, ConstraintMatcher args, boolean result) {
152         this.expectAndReturn(methodName, args, new Boolean JavaDoc(result));
153     }
154     
155     public void expectAndReturn(String JavaDoc methodName, ConstraintMatcher args, int result) {
156         this.expectAndReturn(methodName, args, new Integer JavaDoc(result));
157     }
158
159     public void expectAndThrow(String JavaDoc methodName, Throwable JavaDoc exception) {
160         this.expectAndThrow(methodName, C.NO_ARGS, exception);
161     }
162     
163     public void expectAndThrow(String JavaDoc methodName, Object JavaDoc singleEqualArg, Throwable JavaDoc exception) {
164         this.expectAndThrow(methodName, createConstraintMatcher(singleEqualArg), exception);
165     }
166     
167     public void expectAndThrow(String JavaDoc methodName, ConstraintMatcher args, Throwable JavaDoc exception) {
168         callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(exception))));
169     }
170     
171     public void matchAndReturn(String JavaDoc methodName, Object JavaDoc result) {
172         this.matchAndReturn(methodName, C.NO_ARGS, result);
173     }
174     
175     public void matchAndReturn(String JavaDoc methodName, boolean result) {
176         this.matchAndReturn(methodName, new Boolean JavaDoc(result));
177     }
178     
179     public void matchAndReturn(String JavaDoc methodName, int result) {
180         this.matchAndReturn(methodName, new Integer JavaDoc(result));
181     }
182     
183     public void matchAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, Object JavaDoc result) {
184         this.matchAndReturn(methodName, createConstraintMatcher(singleEqualArg), result);
185     }
186     
187     public void matchAndReturn(String JavaDoc methodName, boolean singleEqualArg, Object JavaDoc result) {
188         this.matchAndReturn(methodName, new Boolean JavaDoc(singleEqualArg), result);
189     }
190     
191     public void matchAndReturn(String JavaDoc methodName, int singleEqualArg, Object JavaDoc result) {
192         this.matchAndReturn(methodName, new Integer JavaDoc(singleEqualArg), result);
193     }
194     
195     public void matchAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, boolean result) {
196         this.matchAndReturn(methodName, singleEqualArg, new Boolean JavaDoc(result));
197     }
198     
199     public void matchAndReturn(String JavaDoc methodName, Object JavaDoc singleEqualArg, int result) {
200         this.matchAndReturn(methodName, singleEqualArg, new Integer JavaDoc(result));
201     }
202     
203     public void matchAndReturn(String JavaDoc methodName, ConstraintMatcher args, Object JavaDoc result) {
204         callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result)));
205     }
206     
207     public void matchAndReturn(String JavaDoc methodName, ConstraintMatcher args, boolean result) {
208         this.matchAndReturn(methodName, args, new Boolean JavaDoc(result));
209     }
210
211     public void matchAndReturn(String JavaDoc methodName, ConstraintMatcher args, int result) {
212         this.matchAndReturn(methodName, args, new Integer JavaDoc(result));
213     }
214     
215     public void matchAndThrow(String JavaDoc methodName, Throwable JavaDoc throwable) {
216         this.matchAndThrow(methodName, C.NO_ARGS, throwable);
217     }
218     
219     public void matchAndThrow(String JavaDoc methodName, Object JavaDoc singleEqualArg, Throwable JavaDoc throwable) {
220         this.matchAndThrow(methodName, createConstraintMatcher(singleEqualArg), throwable);
221     }
222     
223     public void matchAndThrow(String JavaDoc methodName, boolean singleEqualArg, Throwable JavaDoc throwable) {
224         this.matchAndThrow(methodName,new Boolean JavaDoc(singleEqualArg), throwable);
225     }
226     
227     public void matchAndThrow(String JavaDoc methodName, int singleEqualArg, Throwable JavaDoc throwable) {
228         this.matchAndThrow(methodName,new Integer JavaDoc(singleEqualArg), throwable);
229     }
230     
231     public void matchAndThrow(String JavaDoc methodName, ConstraintMatcher args, Throwable JavaDoc throwable) {
232         callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(throwable)));
233     }
234         
235     /** @deprecated @see OrderedMock
236     */

237     public void expect(String JavaDoc methodName, CallSequence deprecated) {
238         throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead...");
239     }
240     
241     /** @deprecated @see OrderedMock
242     */

243     public void expectAndReturn(String JavaDoc methodName, CallSequence deprecated, Object JavaDoc result) {
244         throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead...");
245     }
246     
247     /** @deprecated @see OrderedMock
248     */

249     public void expectAndThrow(String JavaDoc methodName, CallSequence deprecated, Throwable JavaDoc throwable) {
250         throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead...");
251     }
252     
253     /** @deprecated @see expect
254      */

255     public void expectVoid(String JavaDoc methodName, ConstraintMatcher args) {
256         this.expect(methodName, args);
257     }
258     
259     /** @deprecated @see expect
260      */

261     public void expectVoid(String JavaDoc methodName, Object JavaDoc equalArg) {
262         this.expect(methodName,equalArg);
263     }
264     
265     /** @deprecated @see expect
266     */

267     public void expectVoid(String JavaDoc methodName) {
268         this.expect(methodName);
269     }
270     
271     /** @deprecated Not required, as if methodName is called, you will get a an exception
272     */

273     public void expectNotCalled(String JavaDoc methodName) {
274     }
275 }
276
Popular Tags