1
2
3 package org.virtualmock;
4
5 import junit.framework.TestCase;
6 import org.virtualmock.call.CallManager;
7 import org.virtualmock.matcher.AlwaysMatcher;
8 import org.virtualmock.matcher.ArgMatcher;
9 import org.virtualmock.matcher.EqualsMatcher;
10 import org.virtualmock.verify.VerificationManager;
11
12
13 /***
14 * The main interface to the VirtualMock runtime system. The unit test class
15 * makes method calls to this class to perform VirtualMock tasks.
16 *
17 * @author Chad Woolley
18 * @version $Revision: 1.82 $
19 */
20 public class VM {
21 /*** Log4j logger */
22
23
24
25
26 /*** Predefined EqualsMatcher. */
27 public static final EqualsMatcher EQUALS_MATCHER = new EqualsMatcher();
28
29 /*** Predefined AlwaysMatcher. */
30 public static final AlwaysMatcher ALWAYS_MATCHER = new AlwaysMatcher();
31 private CallManager callManager = null;
32 private VerificationManager verificationManager = null;
33
34 /***
35 * Creates a new VM object.
36 *
37 * @param callManager the CallManager to be used by this VM runtime.
38 * @param verificationManager the VerificationManager to be used by this VM
39 * runtime.
40 */
41 public VM(CallManager callManager, VerificationManager verificationManager) {
42 this.callManager = callManager;
43 this.verificationManager = verificationManager;
44 }
45
46 /***
47 * Sets the TestCase class.
48 *
49 * @param case1 the TestCase
50 */
51 public void setTestCase(TestCase case1) {
52 callManager.setTestCase(case1);
53 }
54
55 /***
56 * Gets the TestCase class.
57 *
58 * @return the TestCase
59 */
60 public TestCase getTestCase() {
61 return callManager.getTestCase();
62 }
63
64 /***
65 * This tells VirtualMock that subsequent calls to this class will be
66 * intercepted by VirtualMock. This allows the "recording" of mock calls
67 * that will be expected to be called by the class under test. The
68 * recorded calls will later be matched against the actual calls that are
69 * made during the playback phase. It also allows other VirtualMock
70 * functions to be performed for the class.
71 *
72 * @param classToMock The class for which VirtualMock will intercept calls.
73 */
74 public void addMockClass(Class classToMock) {
75 callManager.addMockClass(classToMock.getName());
76 }
77
78 /***
79 * Sets VirtualMock into Playback Mode (defaulting to unordered playback
80 * type). This means that the first matching and uninvoked call found
81 * will be played back, regardless of the order in which it was recorded.
82 */
83 public void playbackCalls() {
84 callManager.setPhase(Constants.PLAYBACK_PHASE);
85 }
86
87 /***
88 * <p>
89 * Define an ArgMatcher for the last recorded call, which should only have
90 * a single argument.
91 * </p>
92 *
93 * <p>
94 * This is an overloaded convenience method for
95 * setArgMatcher(ArgMatcher[]), which can be used to avoid creating an
96 * array if you only want to record a matcher for one argument.
97 * </p>
98 *
99 * @param argMatcher The ArgMatcher that VirtualMock will use to verify
100 * this argument when the last recorded call is invoked.
101 */
102 public void recordArgMatcher(ArgMatcher argMatcher) {
103 ArgMatcher[] argMatchers = new ArgMatcher[1];
104 argMatchers[0] = argMatcher;
105 callManager.recordArgMatchers(argMatchers);
106 }
107
108 /***
109 * <p>
110 * Define an ArgMatcher for the last recorded call, which should only have
111 * a two arguments.
112 * </p>
113 *
114 * <p>
115 * This is an overloaded convenience method for
116 * setArgMatcher(ArgMatcher[]), which can be used to avoid creating an
117 * array if you only want to record a matcher for two arguments.
118 * </p>
119 *
120 * @param firstArgMatcher The ArgMatcher that VirtualMock will use to match
121 * the first argument when the last recorded call is invoked.
122 * @param secondArgMatcher The ArgMatcher that VirtualMock will use to
123 * match the second argument when the last recorded call is
124 * invoked.
125 */
126 public void recordArgMatchers(ArgMatcher firstArgMatcher,
127 ArgMatcher secondArgMatcher) {
128 ArgMatcher[] argMatchers = new ArgMatcher[2];
129 argMatchers[0] = firstArgMatcher;
130 argMatchers[1] = secondArgMatcher;
131 callManager.recordArgMatchers(argMatchers);
132 }
133
134 /***
135 * <p>
136 * Define an ArgMatcher for the last recorded call, which should only have
137 * a three arguments.
138 * </p>
139 *
140 * <p>
141 * This is an overloaded convenience method for
142 * setArgMatcher(ArgMatcher[]), which can be used to avoid creating an
143 * array if you only want to record a matcher for three arguments.
144 * </p>
145 *
146 * @param firstArgMatcher The ArgMatcher that VirtualMock will use to match
147 * the first argument when the last recorded call is invoked.
148 * @param secondArgMatcher The ArgMatcher that VirtualMock will use to
149 * match the second argument when the last recorded call is
150 * invoked.
151 * @param thirdArgMatcher The ArgMatcher that VirtualMock will use to match
152 * the third argument when the last recorded call is invoked.
153 */
154 public void recordArgMatchers(ArgMatcher firstArgMatcher,
155 ArgMatcher secondArgMatcher, ArgMatcher thirdArgMatcher) {
156 ArgMatcher[] argMatchers = new ArgMatcher[3];
157 argMatchers[0] = firstArgMatcher;
158 argMatchers[1] = secondArgMatcher;
159 argMatchers[2] = thirdArgMatcher;
160 callManager.recordArgMatchers(argMatchers);
161 }
162
163 /***
164 * <p>
165 * Define an ArgMatcher for the last recorded call, which should have the
166 * same number of arguments as there are elements in the array that is
167 * passed in.
168 * </p>
169 *
170 * @param argMatchers The array of ArgMatchers that VirtualMock will use to
171 * match the arguments when the last recorded call is invoked.
172 */
173 public void recordArgMatchers(ArgMatcher[] argMatchers) {
174 callManager.recordArgMatchers(argMatchers);
175 }
176
177 /***
178 * Specifies that a throwable will be thrown by the last recorded call.
179 *
180 * @param throwable the Throwable which the last recorded call will throw
181 * when it is invoked.
182 */
183 public void recordException(Throwable throwable) {
184 callManager.recordThrowableForLastCall(throwable);
185 }
186
187 /***
188 * Specifies the return value for the last recorded call.
189 *
190 * @param returnValue the value which the last recorded call will return
191 * when it is invoked.
192 */
193 public void recordReturnValue(Object returnValue) {
194 callManager.recordReturnValueForLastCall(returnValue);
195 }
196
197 /***
198 * Specifies the int return value for the last recorded call.
199 *
200 * @param intValue the int value which the last recorded call will return
201 * when it is invoked.
202 */
203 public void recordReturnValue(int intValue) {
204 callManager.recordReturnValueForLastCall(new Integer(intValue));
205 }
206
207 /***
208 * Specifies the float return value for the last recorded call.
209 *
210 * @param floatValue the float value which the last recorded call will
211 * return when it is invoked.
212 */
213 public void recordReturnValue(float floatValue) {
214 callManager.recordReturnValueForLastCall(new Float(floatValue));
215 }
216
217 /***
218 * Specifies the double return value for the last recorded call.
219 *
220 * @param doubleValue the double value which the last recorded call will
221 * return when it is invoked.
222 */
223 public void recordReturnValue(double doubleValue) {
224 callManager.recordReturnValueForLastCall(new Double(doubleValue));
225 }
226
227 /***
228 * Specifies the byte return value for the last recorded call.
229 *
230 * @param byteValue the byte value which the last recorded call will return
231 * when it is invoked.
232 */
233 public void recordReturnValue(byte byteValue) {
234 callManager.recordReturnValueForLastCall(new Byte(byteValue));
235 }
236
237 /***
238 * Specifies the long return value for the last recorded call.
239 *
240 * @param longValue the long value which the last recorded call will return
241 * when it is invoked.
242 */
243 public void recordReturnValue(long longValue) {
244 callManager.recordReturnValueForLastCall(new Long(longValue));
245 }
246
247 /***
248 * Specifies the short return value for the last recorded call.
249 *
250 * @param shortValue the short value which the last recorded call will
251 * return when it is invoked.
252 */
253 public void recordReturnValue(short shortValue) {
254 callManager.recordReturnValueForLastCall(new Short(shortValue));
255 }
256
257 /***
258 * Specifies the boolean return value for the last recorded call.
259 *
260 * @param booleanValue the boolean value which the last recorded call will
261 * return when it is invoked.
262 */
263 public void recordReturnValue(boolean booleanValue) {
264 callManager.recordReturnValueForLastCall(new Boolean(booleanValue));
265 }
266
267 /***
268 * Specifies the char return value for the last recorded call.
269 *
270 * @param charValue the char value which the last recorded call will return
271 * when it is invoked.
272 */
273 public void recordReturnValue(char charValue) {
274 callManager.recordReturnValueForLastCall(new Character(charValue));
275 }
276
277 /***
278 * Causes all verify-phase verifications to be performed.
279 *
280 * @todo The verify phase should be set on any type of verify call. Need
281 * to think of a reliable way to set it rather than in each call.
282 */
283 public void verify() {
284 callManager.setPhase(Constants.VERIFY_PHASE);
285 verificationManager.verify();
286 }
287 }