MockControl.java revision 47d431f63a66505a645f282416659a9758a91f1c
1/*
2 * Copyright 2001-2009 OFFIS, Tammo Freese
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock;
17
18import static org.easymock.EasyMock.*;
19
20import java.io.Serializable;
21
22import org.easymock.internal.*;
23
24/**
25 * A <code>MockControl</code> object controls the behavior of its associated
26 * mock object. For more information, see the EasyMock documentation.
27 *
28 * @param <T> type of the mock controlled
29 *
30 * @deprecated Since EasyMock 2.0, static methods on <code>EasyMock</code> are
31 * used to create and control mock objects.
32 */
33@Deprecated
34public class MockControl<T> implements Serializable {
35
36    private static final long serialVersionUID = 8741244302173698092L;
37
38    private final T mock;
39
40    private final MocksControl ctrl;
41
42    protected MockControl(MocksControl ctrl, Class<T> toMock) {
43        this.ctrl = ctrl;
44        this.mock = ctrl.createMock(toMock);
45    }
46
47    /**
48     * Creates a mock control object for the specified interface. The
49     * <code>MockControl</code> and its associated mock object will not check
50     * the order of expected method calls. An unexpected method call on the mock
51     * object will lead to an <code>AssertionError</code>.
52     *
53     * @param <T> type of the mock controlled
54     * @param toMock
55     *            the class of the interface to mock.
56     * @return the mock control.
57     */
58    public static <T> MockControl<T> createControl(Class<T> toMock) {
59        return new MockControl<T>((MocksControl) EasyMock.createControl(),
60                toMock);
61    }
62
63    /**
64     * Creates a mock control object for the specified interface. The
65     * <code>MockControl</code> and its associated mock object will check the
66     * order of expected method calls. An unexpected method call on the mock
67     * object will lead to an <code>AssertionError</code>.
68     *
69     * @param <T> type of the mock controlled
70     * @param toMock
71     *            the class of the interface to mock.
72     * @return the mock control.
73     */
74    public static <T> MockControl<T> createStrictControl(Class<T> toMock) {
75        return new MockControl<T>(
76                (MocksControl) EasyMock.createStrictControl(), toMock);
77    }
78
79    /**
80     * Creates a mock control object for the specified interface. The
81     * <code>MockControl</code> and its associated mock object will not check
82     * the order of expected method calls. An unexpected method call on the mock
83     * object will return an empty value (0, null, false).
84     *
85     * @param <T> type of the mock controlled
86     * @param toMock
87     *            the class of the interface to mock.
88     * @return the mock control.
89     */
90    public static <T> MockControl<T> createNiceControl(Class<T> toMock) {
91        return new MockControl<T>((MocksControl) EasyMock.createNiceControl(),
92                toMock);
93    }
94
95    /**
96     * Returns the mock object.
97     *
98     * @return the mock object of this control
99     */
100    public T getMock() {
101        return mock;
102    }
103
104    /**
105     * Resets the mock control and the mock object to the state directly after
106     * creation.
107     */
108    public final void reset() {
109        ctrl.reset();
110    }
111
112    /**
113     * Switches the mock object from record state to replay state. For more
114     * information, see the EasyMock documentation.
115     *
116     * @throws IllegalStateException
117     *             if the mock object already is in replay state.
118     */
119    public void replay() {
120        ctrl.replay();
121    }
122
123    /**
124     * Verifies that all expectations have been met. For more information, see
125     * the EasyMock documentation.
126     *
127     * @throws IllegalStateException
128     *             if the mock object is in record state.
129     * @throws AssertionError
130     *             if any expectation has not been met.
131     */
132    public void verify() {
133        ctrl.verify();
134    }
135
136    /**
137     * Records that the mock object will expect the last method call once, and
138     * will react by returning silently.
139     *
140     * @exception IllegalStateException
141     *                if the mock object is in replay state, if no method was
142     *                called on the mock object before, or if the last method
143     *                called on the mock was no void method.
144     */
145    public void setVoidCallable() {
146        expectLastCall(
147                "method call on the mock needed before setting void callable")
148                .once();
149    }
150
151    /**
152     * Records that the mock object will expect the last method call once, and
153     * will react by throwing the provided Throwable.
154     *
155     * @param throwable
156     *            the Throwable to throw.
157     * @exception IllegalStateException
158     *                if the mock object is in replay state or if no method was
159     *                called on the mock object before.
160     * @exception IllegalArgumentException
161     *                if the last method called on the mock cannot throw the
162     *                provided Throwable.
163     * @exception NullPointerException
164     *                if throwable is null.
165     */
166    public void setThrowable(Throwable throwable) {
167        expectLastCall(
168                "method call on the mock needed before setting Throwable")
169                .andThrow(throwable).once();
170    }
171
172    /**
173     * Records that the mock object will expect the last method call once, and
174     * will react by returning the provided return value.
175     *
176     * @param value
177     *            the return value.
178     * @throws IllegalStateException
179     *             if the mock object is in replay state, if no method was
180     *             called on the mock object before. or if the last method
181     *             called on the mock does not return <code>boolean</code>.
182     */
183    public void setReturnValue(Object value) {
184        expectLastCall(
185                "method call on the mock needed before setting return value")
186                .andReturn(value).once();
187    }
188
189    /**
190     * Records that the mock object will expect the last method call a fixed
191     * number of times, and will react by returning silently.
192     *
193     * @param times
194     *            the number of times that the call is expected.
195     * @exception IllegalStateException
196     *                if the mock object is in replay state, if no method was
197     *                called on the mock object before, or if the last method
198     *                called on the mock was no void method.
199     */
200    public void setVoidCallable(int times) {
201        expectLastCall(
202                "method call on the mock needed before setting void callable")
203                .times(times);
204    }
205
206    /**
207     * Records that the mock object will expect the last method call a fixed
208     * number of times, and will react by throwing the provided Throwable.
209     *
210     * @param throwable
211     *            the Throwable to throw.
212     * @param times
213     *            the number of times that the call is expected.
214     * @exception IllegalStateException
215     *                if the mock object is in replay state or if no method was
216     *                called on the mock object before.
217     * @exception IllegalArgumentException
218     *                if the last method called on the mock cannot throw the
219     *                provided Throwable.
220     * @exception NullPointerException
221     *                if throwable is null.
222     */
223    public void setThrowable(Throwable throwable, int times) {
224        expectLastCall(
225                "method call on the mock needed before setting Throwable")
226                .andThrow(throwable).times(times);
227    }
228
229    /**
230     * Records that the mock object will expect the last method call a fixed
231     * number of times, and will react by returning the provided return value.
232     *
233     * @param value
234     *            the return value.
235     * @param times
236     *            the number of times that the call is expected.
237     * @throws IllegalStateException
238     *             if the mock object is in replay state, if no method was
239     *             called on the mock object before. or if the last method
240     *             called on the mock does not return <code>boolean</code>.
241     */
242    public void setReturnValue(Object value, int times) {
243        expectLastCall(
244                "method call on the mock needed before setting return value")
245                .andReturn(value).times(times);
246    }
247
248    /**
249     * Records that the mock object will expect the last method call a fixed
250     * number of times, and will react by returning the provided return value.
251     *
252     * @param value
253     *            the return value.
254     * @param range
255     *            the number of times that the call is expected.
256     * @throws IllegalStateException
257     *             if the mock object is in replay state, if no method was
258     *             called on the mock object before. or if the last method
259     *             called on the mock does not return <code>boolean</code>.
260     */
261    public void setReturnValue(Object value, Range range) {
262        IExpectationSetters<Object> setter = expectLastCall(
263                "method call on the mock needed before setting return value")
264                .andReturn(value);
265        callWithConvertedRange(setter, range);
266    }
267
268    /**
269     * Records that the mock object will by default allow the last method
270     * specified by a method call.
271     *
272     * @exception IllegalStateException
273     *                if the mock object is in replay state, if no method was
274     *                called on the mock object before, or if the last method
275     *                called on the mock was no void method.
276     */
277    public void setDefaultVoidCallable() {
278        ((MocksControl) expectLastCall("method call on the mock needed before setting default void callable"))
279                .setLegacyDefaultVoidCallable();
280    }
281
282    /**
283     * Records that the mock object will by default allow the last method
284     * specified by a method call, and will react by throwing the provided
285     * Throwable.
286     *
287     * @param throwable
288     *            throwable the throwable to be thrown
289     * @exception IllegalArgumentException
290     *                if the last method called on the mock cannot throw the
291     *                provided Throwable.
292     * @exception NullPointerException
293     *                if throwable is null.
294     * @exception IllegalStateException
295     *                if the mock object is in replay state, or if no method was
296     *                called on the mock object before.
297     */
298    public void setDefaultThrowable(Throwable throwable) {
299        ctrl.setLegacyDefaultThrowable(throwable);
300    }
301
302    /**
303     * Records that the mock object will by default allow the last method
304     * specified by a method call, and will react by returning the provided
305     * return value.
306     *
307     * @param value
308     *            the return value.
309     * @throws IllegalStateException
310     *             if the mock object is in replay state, if no method was
311     *             called on the mock object before. or if the last method
312     *             called on the mock does not return <code>boolean</code>.
313     */
314    public void setDefaultReturnValue(Object value) {
315        ctrl.setLegacyDefaultReturnValue(value);
316    }
317
318    /**
319     * Sets the ArgumentsMatcher for the last method called on the mock object.
320     * The matcher must be set before any behavior for the method is defined.
321     *
322     * @param matcher the matcher for the last method called
323     * @throws IllegalStateException
324     *             if called in replay state, or if no method was called on the
325     *             mock object before.
326     */
327    public void setMatcher(ArgumentsMatcher matcher) {
328        ctrl.setLegacyMatcher(matcher);
329    }
330
331    /**
332     * Records that the mock object will expect the last method call between
333     * <code>minCount</code> and <code>maxCount</code> times, and will react
334     * by returning silently.
335     *
336     * @param minCount
337     *            the minimum number of times that the call is expected.
338     * @param maxCount
339     *            the maximum number of times that the call is expected.
340     * @exception IllegalStateException
341     *                if the mock object is in replay state, if no method was
342     *                called on the mock object before, or if the last method
343     *                called on the mock was no void method.
344     */
345    public void setVoidCallable(int minCount, int maxCount) {
346        expectLastCall(
347                "method call on the mock needed before setting void callable")
348                .times(minCount, maxCount);
349    }
350
351    public void setVoidCallable(Range range) {
352        IExpectationSetters<Object> setter = expectLastCall("method call on the mock needed before setting void callable");
353        callWithConvertedRange(setter, range);
354    }
355
356    /**
357     * Records that the mock object will expect the last method call between
358     * <code>minCount</code> and <code>maxCount</code> times, and will react
359     * by throwing the provided Throwable.
360     *
361     * @param throwable
362     *            the Throwable to throw.
363     * @param minCount
364     *            the minimum number of times that the call is expected.
365     * @param maxCount
366     *            the maximum number of times that the call is expected.
367     * @exception IllegalStateException
368     *                if the mock object is in replay state or if no method was
369     *                called on the mock object before.
370     * @exception IllegalArgumentException
371     *                if the last method called on the mock cannot throw the
372     *                provided Throwable.
373     * @exception NullPointerException
374     *                if throwable is null.
375     */
376    public void setThrowable(Throwable throwable, int minCount, int maxCount) {
377        expectLastCall(
378                "method call on the mock needed before setting Throwable")
379                .andThrow(throwable).times(minCount, maxCount);
380    }
381
382    public void setThrowable(Throwable throwable, Range range) {
383        IExpectationSetters<Object> setter = expectLastCall(
384                "method call on the mock needed before setting Throwable")
385                .andThrow(throwable);
386        callWithConvertedRange(setter, range);
387    }
388
389    /**
390     * Records that the mock object will expect the last method call between
391     * <code>minCount</code> and <code>maxCount</code> times, and will react
392     * by returning the provided return value.
393     *
394     * @param value
395     *            the return value.
396     * @param minCount
397     *            the minimum number of times that the call is expected.
398     * @param maxCount
399     *            the maximum number of times that the call is expected.
400     * @throws IllegalStateException
401     *             if the mock object is in replay state, if no method was
402     *             called on the mock object before. or if the last method
403     *             called on the mock does not return <code>boolean</code>.
404     */
405    public void setReturnValue(Object value, int minCount, int maxCount) {
406        expectLastCall(
407                "method call on the mock needed before setting return value")
408                .andReturn(value).times(minCount, maxCount);
409    }
410
411    /**
412     * Exactly one call.
413     */
414    public static final Range ONE = MocksControl.ONCE;
415
416    /**
417     * One or more calls.
418     */
419    public static final Range ONE_OR_MORE = MocksControl.AT_LEAST_ONCE;
420
421    /**
422     * Zero or more calls.
423     */
424    public static final Range ZERO_OR_MORE = MocksControl.ZERO_OR_MORE;
425
426    /**
427     * Matches if each expected argument is equal to the corresponding actual
428     * argument.
429     */
430    public static final ArgumentsMatcher EQUALS_MATCHER = new EqualsMatcher();
431
432    /**
433     * Matches always.
434     */
435    public static final ArgumentsMatcher ALWAYS_MATCHER = new AlwaysMatcher();
436
437    /**
438     * Matches if each expected argument is equal to the corresponding actual
439     * argument for non-array arguments; array arguments are compared with the
440     * appropriate <code>java.util.Arrays.equals()</code> -method.
441     */
442    public static final ArgumentsMatcher ARRAY_MATCHER = new ArrayMatcher();
443
444    /**
445     * Sets the default ArgumentsMatcher for all methods of the mock object. The
446     * matcher must be set before any behavior is defined on the mock object.
447     *
448     * @param matcher the default matcher for this control
449     * @throws IllegalStateException
450     *             if called in replay state, or if any behavior is already
451     *             defined on the mock object.
452     */
453    public void setDefaultMatcher(ArgumentsMatcher matcher) {
454        ctrl.setLegacyDefaultMatcher(matcher);
455    }
456
457    /**
458     * Same as {@link MockControl#setReturnValue(Object)}. For explanation, see
459     * "Convenience Methods for Return Values" in the EasyMock documentation.
460     *
461     * @param  mocked method return type
462     * @param  returned value type
463     * @param ignored
464     *            an ignored value.
465     * @param value value returned by the mock
466     */
467    public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value) {
468        EasyMock.expectLastCall().andReturn(value).once();
469    }
470
471    public void expectAndReturn(int ignored, int value) {
472        this.expectAndReturn((Object) ignored, (Object) value);
473    }
474
475    /**
476     * Same as {@link MockControl#setReturnValue(Object, Range)}. For
477     * explanation, see "Convenience Methods for Return Values" in the EasyMock
478     * documentation.
479     *
480     * @param  mocked method return type
481     * @param  returned value type
482     * @param ignored
483     *            an ignored value.
484     * @param value value returned by the mock
485     * @param range range of number of calls
486     */
487    public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
488            Range range) {
489        IExpectationSetters<Object> expectAndReturn = EasyMock.expectLastCall()
490                .andReturn(value);
491        callWithConvertedRange(expectAndReturn, range);
492    }
493
494    public void expectAndReturn(int ignored, int value, Range range) {
495        this.expectAndReturn((Object) ignored, (Object) value, range);
496    }
497
498    /**
499     * Same as {@link MockControl#setReturnValue(Object, int)}. For
500     * explanation, see "Convenience Methods for Return Values" in the EasyMock
501     * documentation.
502     *
503     * @param  mocked method return type
504     * @param  returned value type
505     * @param ignored
506     *            an ignored value.
507     * @param value value returned by the mock
508     * @param count number of times the call is expected
509     */
510    public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
511            int count) {
512        EasyMock.expectLastCall().andReturn(value).times(count);
513    }
514
515    public void expectAndReturn(int ignored, int value, int count) {
516        this.expectAndReturn((Object) ignored, (Object) value, count);
517    }
518
519    /**
520     * Same as {@link MockControl#setReturnValue(Object, int, int)}. For
521     * explanation, see "Convenience Methods for Return Values" in the EasyMock
522     * documentation.
523     *
524     * @param  mocked method return type
525     * @param  returned value type
526     * @param ignored
527     *            an ignored value.
528     * @param value value returned by the mock
529     * @param min minimum number of times the call is expected
530     * @param max maximum number of times the call is expected
531     */
532    public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
533            int min, int max) {
534        EasyMock.expectLastCall().andReturn(value).times(min, max);
535    }
536
537    public void expectAndReturn(int ignored, int value, int min, int max) {
538        this.expectAndReturn((Object) ignored, (Object) value, min, max);
539    }
540
541    /**
542     * Same as {@link MockControl#setThrowable(Throwable)}. For explanation,
543     * see "Convenience Methods for Throwables" in the EasyMock documentation.
544     *
545     * @param ignored
546     *            an ignored value.
547     * @param throwable to be thrown on the call
548     */
549    public void expectAndThrow(Object ignored, Throwable throwable) {
550        EasyMock.expect(ignored).andThrow(throwable).once();
551    }
552
553    /**
554     * Same as {@link MockControl#setThrowable(Throwable, Range)}. For
555     * explanation, see "Convenience Methods for Throwables" in the EasyMock
556     * documentation.
557     *
558     * @param ignored
559     *            an ignored value.
560     * @param throwable to be thrown on the call
561     * @param range range of number of calls
562     */
563    public void expectAndThrow(Object ignored, Throwable throwable, Range range) {
564        IExpectationSetters<Object> setter = EasyMock.expect(ignored).andThrow(
565                throwable);
566        callWithConvertedRange(setter, range);
567    }
568
569    /**
570     * Same as {@link MockControl#setThrowable(Throwable, int)}. For
571     * explanation, see "Convenience Methods for Throwables" in the EasyMock
572     * documentation.
573     *
574     * @param ignored
575     *            an ignored value.
576     * @param throwable to be thrown on the call
577     * @param count number of times the call is expected
578     */
579    public void expectAndThrow(Object ignored, Throwable throwable, int count) {
580        expect(ignored).andThrow(throwable).times(count);
581    }
582
583    /**
584     * Same as {@link MockControl#setThrowable(Throwable, int, int)}. For
585     * explanation, see "Convenience Methods for Throwables" in the EasyMock
586     * documentation.
587     *
588     * @param ignored
589     *            an ignored value.
590     * @param throwable to be thrown on the call
591     * @param min minimum number of times the call is expected
592     * @param max maximum number of times the call is expected
593     */
594    public void expectAndThrow(Object ignored, Throwable throwable, int min,
595            int max) {
596        expect(ignored).andThrow(throwable).times(min, max);
597    }
598
599    /**
600     * Same as {@link MockControl#setDefaultReturnValue(Object)}. For
601     * explanation, see "Convenience Methods for Return Values" in the EasyMock
602     * documentation.
603     *
604     * @param  mocked method return type
605     * @param  returned value type
606     * @param ignored
607     *            an ignored value.
608     * @param value value returned by the mock
609     */
610    public <V1, V2 extends V1> void expectAndDefaultReturn(V1 ignored, V2 value) {
611        EasyMock.expectLastCall().andStubReturn(value);
612    }
613
614    /**
615     * Same as {@link MockControl#setDefaultThrowable(Throwable)}. For
616     * explanation, see "Convenience Methods for Throwables" in the EasyMock
617     * documentation.
618     *
619     * @param ignored
620     *            an ignored value.
621     * @param throwable to be thrown on the call
622     */
623    public void expectAndDefaultThrow(Object ignored, Throwable throwable) {
624        expectLastCall(
625                "method call on the mock needed before setting default Throwable")
626                .andStubThrow(throwable);
627    }
628
629    private IExpectationSetters<Object> expectLastCall(String failureMessage) {
630        try {
631            return EasyMock.expectLastCall();
632        } catch (IllegalStateException e) {
633            throw new IllegalStateException(failureMessage);
634        }
635    }
636
637    private void callWithConvertedRange(IExpectationSetters<Object> setter, Range range) {
638        if (range == ONE) {
639            setter.once();
640        } else if (range == ONE_OR_MORE) {
641            setter.atLeastOnce();
642        } else if (range == ZERO_OR_MORE) {
643            setter.anyTimes();
644        } else {
645            throw new IllegalArgumentException("Unexpected Range");
646        }
647    }
648
649}