1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock;
3
4 import org.jmock.dynamic.*;
5 import org.jmock.expectation.Verifiable;
6 import org.jmock.matcher.ArgumentsMatcher;
7
8 public class Mock implements Verifiable {
9 private InvokableFactory invokableFactory;
10 private DynamicMock coreMock;
11
12 public Mock(InvokableFactory invokableFactory, InvocationDispatcher invocationDispatcher, Class mockedClass, String name) {
13 coreMock = new CoreMock(mockedClass, name, invocationDispatcher);
14
15 this.invokableFactory = invokableFactory;
16 }
17
18 public Mock(Class mockedClass, String nonDefaultName) {
19 this(new InvokableFactory(), new LIFOInvocationDispatcher(), mockedClass, nonDefaultName);
20 }
21
22 public Mock(Class mockedClass) {
23 this(mockedClass, CoreMock.mockNameFromClass(mockedClass));
24 }
25
26 public String toString() {
27 return coreMock.toString();
28 }
29
30 private InvocationMatcher createConstraintMatcher(Object constraintArg) {
31 // Can't overload this method as callee had an Object parameter, and java
32 // doesn't do a secondary dispatch on the true underlying type
33
34 if (constraintArg instanceof Constraint[]) {
35 // to support possible legacy usage of new Contraint[] {...}
36 return new ArgumentsMatcher((Constraint[]) constraintArg);
37 } else if (constraintArg instanceof Constraint) {
38 // to support usage of C.lt(5) type constraints
39 return C.args((Constraint) constraintArg);
40 } else {
41 // normal usage of the overloaded expect/match object parameter
42 return C.args(C.eq(constraintArg));
43 }
44 }
45
46 public void expect(String methodName, InvocationMatcher args) {
47 coreMock.add(invokableFactory.createVoidExpectation(methodName, args));
48 }
49
50 public void expectAndReturn(String methodName, InvocationMatcher args, Object result) {
51 coreMock.add(invokableFactory.createReturnExpectation(methodName, args, result));
52 }
53
54 public void expectAndThrow(String methodName, InvocationMatcher args, Throwable throwable) {
55 coreMock.add(invokableFactory.createThrowableExpectation(methodName, args, throwable));
56 }
57
58 public void match(String methodName, InvocationMatcher args) {
59 coreMock.add(invokableFactory.createVoidStub(methodName, args));
60 }
61
62 public void matchAndReturn(String methodName, InvocationMatcher args, Object result) {
63 coreMock.add(invokableFactory.createReturnStub(methodName, args, result));
64 }
65
66 public void matchAndThrow(String methodName, InvocationMatcher args, Throwable throwable) {
67 coreMock.add(invokableFactory.createThrowableStub(methodName, args, throwable));
68 }
69
70 /*
71 * --- Sugar methods ----
72 */
73
74 public void expect(String methodName) {
75 expect(methodName, C.NO_ARGS);
76 }
77
78 public void expect(String methodName, Object singleEqualArg) {
79 expect(methodName, createConstraintMatcher(singleEqualArg));
80 }
81
82 public void expectAndReturn(String methodName, Object result) {
83 expectAndReturn(methodName, C.NO_ARGS, result);
84 }
85
86 public void expectAndReturn(String methodName, boolean result) {
87 expectAndReturn(methodName, new Boolean(result));
88 }
89
90 public void expectAndReturn(String methodName, int result) {
91 expectAndReturn(methodName, new Integer(result));
92 }
93
94 public void expectAndReturn(String methodName, Object singleEqualArg, Object result) {
95 expectAndReturn(methodName, createConstraintMatcher(singleEqualArg), result);
96 }
97
98 public void expectAndReturn(String methodName, Object singleEqualArg, boolean result) {
99 expectAndReturn(methodName, singleEqualArg, new Boolean(result));
100 }
101
102 public void expectAndReturn(String methodName, Object singleEqualArg, int result) {
103 expectAndReturn(methodName, singleEqualArg, new Integer(result));
104 }
105
106 public void expectAndReturn(String methodName, InvocationMatcher args, boolean result) {
107 expectAndReturn(methodName, args, new Boolean(result));
108 }
109
110 public void expectAndReturn(String methodName, InvocationMatcher args, int result) {
111 expectAndReturn(methodName, args, new Integer(result));
112 }
113
114 public void expectAndThrow(String methodName, Throwable exception) {
115 expectAndThrow(methodName, C.NO_ARGS, exception);
116 }
117
118 public void expectAndThrow(String methodName, Object singleEqualArg, Throwable exception) {
119 expectAndThrow(methodName, createConstraintMatcher(singleEqualArg), exception);
120 }
121
122 public void match(String methodName) {
123 match(methodName, C.NO_ARGS);
124 }
125
126 public void match(String methodName, Object singleEqualArg) {
127 match(methodName, createConstraintMatcher(singleEqualArg));
128 }
129
130 public void match(String methodName, int singleEqualArg) {
131 match(methodName, new Integer(singleEqualArg));
132 }
133
134 public void match(String methodName, boolean singleEqualArg) {
135 match(methodName, new Boolean(singleEqualArg));
136 }
137
138 public void matchAndReturn(String methodName, Object result) {
139 matchAndReturn(methodName, C.NO_ARGS, result);
140 }
141
142 public void matchAndReturn(String methodName, boolean result) {
143 matchAndReturn(methodName, new Boolean(result));
144 }
145
146 public void matchAndReturn(String methodName, int result) {
147 matchAndReturn(methodName, new Integer(result));
148 }
149
150 public void matchAndReturn(String methodName, Object singleEqualArg, Object result) {
151 matchAndReturn(methodName, createConstraintMatcher(singleEqualArg), result);
152 }
153
154 public void matchAndReturn(String methodName, boolean singleEqualArg, Object result) {
155 matchAndReturn(methodName, new Boolean(singleEqualArg), result);
156 }
157
158 public void matchAndReturn(String methodName, int singleEqualArg, Object result) {
159 matchAndReturn(methodName, new Integer(singleEqualArg), result);
160 }
161
162 public void matchAndReturn(String methodName, Object singleEqualArg, boolean result) {
163 matchAndReturn(methodName, singleEqualArg, new Boolean(result));
164 }
165
166 public void matchAndReturn(String methodName, Object singleEqualArg, int result) {
167 matchAndReturn(methodName, singleEqualArg, new Integer(result));
168 }
169
170 public void matchAndReturn(String methodName, InvocationMatcher args, boolean result) {
171 matchAndReturn(methodName, args, new Boolean(result));
172 }
173
174 public void matchAndReturn(String methodName, InvocationMatcher args, int result) {
175 matchAndReturn(methodName, args, new Integer(result));
176 }
177
178 public void matchAndThrow(String methodName, Throwable throwable) {
179 matchAndThrow(methodName, C.NO_ARGS, throwable);
180 }
181
182 public void matchAndThrow(String methodName, Object singleEqualArg, Throwable throwable) {
183 matchAndThrow(methodName, createConstraintMatcher(singleEqualArg), throwable);
184 }
185
186 public void matchAndThrow(String methodName, boolean singleEqualArg, Throwable throwable) {
187 matchAndThrow(methodName, new Boolean(singleEqualArg), throwable);
188 }
189
190 public void matchAndThrow(String methodName, int singleEqualArg, Throwable throwable) {
191 matchAndThrow(methodName, new Integer(singleEqualArg), throwable);
192 }
193
194 /***
195 * @deprecated @see expect
196 */
197 public void expectVoid(String methodName, InvocationMatcher args) {
198 this.expect(methodName, args);
199 }
200
201 /***
202 * @deprecated @see expect
203 */
204 public void expectVoid(String methodName, Object equalArg) {
205 this.expect(methodName, equalArg);
206 }
207
208 /***
209 * @deprecated @see expect
210 */
211 public void expectVoid(String methodName) {
212 this.expect(methodName);
213 }
214
215 /***
216 * @deprecated Not required, as if methodName is called, you will get an exception
217 */
218 public void expectNotCalled(String methodName) {
219 }
220
221 public Object proxy() {
222 return coreMock.proxy();
223 }
224
225 public void reset() {
226 coreMock.reset();
227 }
228
229 public void verify() {
230 coreMock.verify();
231 }
232
233 }
This page was automatically generated by Maven