EasyMock.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 java.lang.reflect.Proxy;
19import java.util.Comparator;
20
21import org.easymock.internal.*;
22import org.easymock.internal.matchers.*;
23
24public class EasyMock {
25
26    /**
27     * Since EasyMock 2.4, by default, a mock wasn't allowed to be called in
28     * multiple threads unless it was made thread-safe (See
29     * {@link #makeThreadSafe(Object, boolean)} method). Since EasyMock 2.5,
30     * this isn't the default anymore. For backward compatibility, this property
31     * can bring EasyMock 2.4 behavior back.
32     */
33    public static final String ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT = "easymock.enableThreadSafetyCheckByDefault";
34
35    /**
36     * Since EasyMock 2.5, by default a mock is thread-safe. For backward
37     * compatibility, this property can change the default. A given mock still
38     * can be made thread-safe by calling
39     * {@link #makeThreadSafe(Object, boolean)}.
40     */
41    public static final String NOT_THREAD_SAFE_BY_DEFAULT = "easymock.notThreadSafeByDefault";
42
43    /**
44     * Creates a mock object that implements the given interface, order checking
45     * is enabled by default.
46     *
47     * @param <T>
48     *            the interface that the mock object should implement.
49     * @param toMock
50     *            the class of the interface that the mock object should
51     *            implement.
52     * @return the mock object.
53     */
54    public static <T> T createStrictMock(Class<T> toMock) {
55        return createStrictControl().createMock(toMock);
56    }
57
58    /**
59     * Creates a mock object that implements the given interface, order checking
60     * is enabled by default.
61     * @param name the name of the mock object.
62     * @param toMock
63     *            the class of the interface that the mock object should
64     *            implement.
65     * @param <T>
66     *            the interface that the mock object should implement.
67     * @return the mock object.
68     * @throws IllegalArgumentException if the name is not a valid Java identifier.
69     */
70    public static <T> T createStrictMock(String name, Class<T> toMock) {
71        return createStrictControl().createMock(name, toMock);
72    }
73
74    /**
75     * Creates a mock object that implements the given interface, order checking
76     * is disabled by default.
77     *
78     * @param <T>
79     *            the interface that the mock object should implement.
80     * @param toMock
81     *            the class of the interface that the mock object should
82     *            implement.
83     * @return the mock object.
84     */
85    public static <T> T createMock(Class<T> toMock) {
86        return createControl().createMock(toMock);
87    }
88
89    /**
90     * Creates a mock object that implements the given interface, order checking
91     * is disabled by default.
92     * @param name the name of the mock object.
93     * @param toMock
94     *            the class of the interface that the mock object should
95     *            implement.
96     *
97     * @param <T>
98     *            the interface that the mock object should implement.
99     * @return the mock object.
100     * @throws IllegalArgumentException if the name is not a valid Java identifier.
101     */
102    public static <T> T createMock(String name, Class<T> toMock) {
103        return createControl().createMock(name, toMock);
104    }
105
106    /**
107     * Creates a mock object that implements the given interface, order checking
108     * is disabled by default, and the mock object will return <code>0</code>,
109     * <code>null</code> or <code>false</code> for unexpected invocations.
110     *
111     * @param <T>
112     *            the interface that the mock object should implement.
113     * @param toMock
114     *            the class of the interface that the mock object should
115     *            implement.
116     * @return the mock object.
117     */
118    public static <T> T createNiceMock(Class<T> toMock) {
119        return createNiceControl().createMock(toMock);
120    }
121
122    /**
123     * Creates a mock object that implements the given interface, order checking
124     * is disabled by default, and the mock object will return <code>0</code>,
125     * <code>null</code> or <code>false</code> for unexpected invocations.
126     * @param name the name of the mock object.
127     * @param toMock
128     *            the class of the interface that the mock object should
129     *            implement.
130     *
131     * @param <T>
132     *            the interface that the mock object should implement.
133     * @return the mock object.
134     * @throws IllegalArgumentException if the name is not a valid Java identifier.
135     */
136    public static <T> T createNiceMock(String name, Class<T> toMock) {
137        return createNiceControl().createMock(name, toMock);
138    }
139
140    /**
141     * Creates a control, order checking is enabled by default.
142     *
143     * @return the control.
144     */
145    public static IMocksControl createStrictControl() {
146        return new MocksControl(MocksControl.MockType.STRICT);
147    }
148
149    /**
150     * Creates a control, order checking is disabled by default.
151     *
152     * @return the control.
153     */
154    public static IMocksControl createControl() {
155        return new MocksControl(MocksControl.MockType.DEFAULT);
156    }
157
158    /**
159     * Creates a control, order checking is disabled by default, and the mock
160     * objects created by this control will return <code>0</code>,
161     * <code>null</code> or <code>false</code> for unexpected invocations.
162     *
163     * @return the control.
164     */
165    public static IMocksControl createNiceControl() {
166        return new MocksControl(MocksControl.MockType.NICE);
167    }
168
169    /**
170     * Returns the expectation setter for the last expected invocation in the
171     * current thread.
172     *
173     * @param <T> type returned by the expected method
174     * @param value
175     *            the parameter is used to transport the type to the
176     *            ExpectationSetter. It allows writing the expected call as
177     *            argument, i.e.
178     *            <code>expect(mock.getName()).andReturn("John Doe")<code>.
179     *
180     * @return the expectation setter.
181     */
182    public static <T> IExpectationSetters<T> expect(T value) {
183        return EasyMock.getControlForLastCall();
184    }
185
186    /**
187     * Returns the expectation setter for the last expected invocation in the
188     * current thread. This method is used for expected invocations on void
189     * methods.
190     *
191     * @param <T> type returned by the expected method
192     * @return the expectation setter.
193     */
194    public static <T> IExpectationSetters<T> expectLastCall() {
195        return getControlForLastCall();
196    }
197
198    @SuppressWarnings("unchecked")
199    private static <T> IExpectationSetters<T> getControlForLastCall() {
200        MocksControl lastControl = LastControl.lastControl();
201        if (lastControl == null) {
202            throw new IllegalStateException("no last call on a mock available");
203        }
204        return (IExpectationSetters<T>) lastControl;
205    }
206
207    /**
208     * Expects any boolean argument. For details, see the EasyMock
209     * documentation.
210     *
211     * @return <code>false</code>.
212     */
213    public static boolean anyBoolean() {
214        reportMatcher(Any.ANY);
215        return false;
216    }
217
218    /**
219     * Expects any byte argument. For details, see the EasyMock documentation.
220     *
221     * @return <code>0</code>.
222     */
223    public static byte anyByte() {
224        reportMatcher(Any.ANY);
225        return 0;
226    }
227
228    /**
229     * Expects any char argument. For details, see the EasyMock documentation.
230     *
231     * @return <code>0</code>.
232     */
233    public static char anyChar() {
234        reportMatcher(Any.ANY);
235        return 0;
236    }
237
238    /**
239     * Expects any int argument. For details, see the EasyMock documentation.
240     *
241     * @return <code>0</code>.
242     */
243    public static int anyInt() {
244        reportMatcher(Any.ANY);
245        return 0;
246    }
247
248    /**
249     * Expects any long argument. For details, see the EasyMock documentation.
250     *
251     * @return <code>0</code>.
252     */
253    public static long anyLong() {
254        reportMatcher(Any.ANY);
255        return 0;
256    }
257
258    /**
259     * Expects any float argument. For details, see the EasyMock documentation.
260     *
261     * @return <code>0</code>.
262     */
263    public static float anyFloat() {
264        reportMatcher(Any.ANY);
265        return 0;
266    }
267
268    /**
269     * Expects any double argument. For details, see the EasyMock documentation.
270     *
271     * @return <code>0</code>.
272     */
273    public static double anyDouble() {
274        reportMatcher(Any.ANY);
275        return 0;
276    }
277
278    /**
279     * Expects any short argument. For details, see the EasyMock documentation.
280     *
281     * @return <code>0</code>.
282     */
283    public static short anyShort() {
284        reportMatcher(Any.ANY);
285        return 0;
286    }
287
288    /**
289     * Expects any Object argument. For details, see the EasyMock documentation.
290     *
291     * @param <T> type of the method argument to match
292     * @return <code>null</code>.
293     */
294    public static <T> T anyObject() {
295        reportMatcher(Any.ANY);
296        return null;
297    }
298
299    /**
300     * Expects a comparable argument greater than or equal the given value. For details, see
301     * the EasyMock documentation.
302     *
303     * @param <T> type of the method argument to match
304     * @param value
305     *            the given value.
306     * @return <code>null</code>.
307     */
308    public static <T extends Comparable<T>> T geq(Comparable<T> value) {
309        reportMatcher(new GreaterOrEqual<T>(value));
310        return null;
311    }
312
313    /**
314     * Expects a byte argument greater than or equal to the given value. For
315     * details, see the EasyMock documentation.
316     *
317     * @param value
318     *            the given value.
319     * @return <code>0</code>.
320     */
321    public static byte geq(byte value) {
322        reportMatcher(new GreaterOrEqual<Byte>(value));
323        return 0;
324    }
325
326    /**
327     * Expects a double argument greater than or equal to the given value. For
328     * details, see the EasyMock documentation.
329     *
330     * @param value
331     *            the given value.
332     * @return <code>0</code>.
333     */
334    public static double geq(double value) {
335        reportMatcher(new GreaterOrEqual<Double>(value));
336        return 0;
337    }
338
339    /**
340     * Expects a float argument greater than or equal to the given value. For
341     * details, see the EasyMock documentation.
342     *
343     * @param value
344     *            the given value.
345     * @return <code>0</code>.
346     */
347    public static float geq(float value) {
348        reportMatcher(new GreaterOrEqual<Float>(value));
349        return 0;
350    }
351
352    /**
353     * Expects an int argument greater than or equal to the given value. For
354     * details, see the EasyMock documentation.
355     *
356     * @param value
357     *            the given value.
358     * @return <code>0</code>.
359     */
360    public static int geq(int value) {
361        reportMatcher(new GreaterOrEqual<Integer>(value));
362        return 0;
363    }
364
365    /**
366     * Expects a long argument greater than or equal to the given value. For
367     * details, see the EasyMock documentation.
368     *
369     * @param value
370     *            the given value.
371     * @return <code>0</code>.
372     */
373    public static long geq(long value) {
374        reportMatcher(new GreaterOrEqual<Long>(value));
375        return 0;
376    }
377
378    /**
379     * Expects a short argument greater than or equal to the given value. For
380     * details, see the EasyMock documentation.
381     *
382     * @param value
383     *            the given value.
384     * @return <code>0</code>.
385     */
386    public static short geq(short value) {
387        reportMatcher(new GreaterOrEqual<Short>(value));
388        return 0;
389    }
390
391    /**
392     * Expects a comparable argument less than or equal the given value. For details, see
393     * the EasyMock documentation.
394     *
395     * @param <T> type of the method argument to match
396     * @param value
397     *            the given value.
398     * @return <code>null</code>.
399     */
400    public static <T extends Comparable<T>> T leq(Comparable<T> value) {
401        reportMatcher(new LessOrEqual<T>(value));
402        return null;
403    }
404
405    /**
406     * Expects a byte argument less than or equal to the given value. For
407     * details, see the EasyMock documentation.
408     *
409     * @param value
410     *            the given value.
411     * @return <code>0</code>.
412     */
413    public static byte leq(byte value) {
414        reportMatcher(new LessOrEqual<Byte>(value));
415        return 0;
416    }
417
418    /**
419     * Expects a double argument less than or equal to the given value. For
420     * details, see the EasyMock documentation.
421     *
422     * @param value
423     *            the given value.
424     * @return <code>0</code>.
425     */
426    public static double leq(double value) {
427        reportMatcher(new LessOrEqual<Double>(value));
428        return 0;
429    }
430
431    /**
432     * Expects a float argument less than or equal to the given value. For
433     * details, see the EasyMock documentation.
434     *
435     * @param value
436     *            the given value.
437     * @return <code>0</code>.
438     */
439    public static float leq(float value) {
440        reportMatcher(new LessOrEqual<Float>(value));
441        return 0;
442    }
443
444    /**
445     * Expects an int argument less than or equal to the given value. For
446     * details, see the EasyMock documentation.
447     *
448     * @param value
449     *            the given value.
450     * @return <code>0</code>.
451     */
452    public static int leq(int value) {
453        reportMatcher(new LessOrEqual<Integer>(value));
454        return 0;
455    }
456
457    /**
458     * Expects a long argument less than or equal to the given value. For
459     * details, see the EasyMock documentation.
460     *
461     * @param value
462     *            the given value.
463     * @return <code>0</code>.
464     */
465    public static long leq(long value) {
466        reportMatcher(new LessOrEqual<Long>(value));
467        return 0;
468    }
469
470    /**
471     * Expects a short argument less than or equal to the given value. For
472     * details, see the EasyMock documentation.
473     *
474     * @param value
475     *            the given value.
476     * @return <code>0</code>.
477     */
478    public static short leq(short value) {
479        reportMatcher(new LessOrEqual<Short>(value));
480        return 0;
481    }
482
483    /**
484     * Expects a comparable argument greater than the given value. For details, see
485     * the EasyMock documentation.
486     *
487     * @param <T> type of the method argument to match
488     * @param value
489     *            the given value.
490     * @return <code>null</code>.
491     */
492    public static <T extends Comparable<T>> T gt(Comparable<T> value) {
493        reportMatcher(new GreaterThan<T>(value));
494        return null;
495    }
496
497    /**
498     * Expects a byte argument greater than the given value. For details, see
499     * the EasyMock documentation.
500     *
501     * @param value
502     *            the given value.
503     * @return <code>0</code>.
504     */
505    public static byte gt(byte value) {
506        reportMatcher(new GreaterThan<Byte>(value));
507        return 0;
508    }
509
510    /**
511     * Expects a double argument greater than the given value. For details, see
512     * the EasyMock documentation.
513     *
514     * @param value
515     *            the given value.
516     * @return <code>0</code>.
517     */
518    public static double gt(double value) {
519        reportMatcher(new GreaterThan<Double>(value));
520        return 0;
521    }
522
523    /**
524     * Expects a float argument greater than the given value. For details, see
525     * the EasyMock documentation.
526     *
527     * @param value
528     *            the given value.
529     * @return <code>0</code>.
530     */
531    public static float gt(float value) {
532        reportMatcher(new GreaterThan<Float>(value));
533        return 0;
534    }
535
536    /**
537     * Expects an int argument greater than the given value. For details, see
538     * the EasyMock documentation.
539     *
540     * @param value
541     *            the given value.
542     * @return <code>0</code>.
543     */
544    public static int gt(int value) {
545        reportMatcher(new GreaterThan<Integer>(value));
546        return 0;
547    }
548
549    /**
550     * Expects a long argument greater than the given value. For details, see
551     * the EasyMock documentation.
552     *
553     * @param value
554     *            the given value.
555     * @return <code>0</code>.
556     */
557    public static long gt(long value) {
558        reportMatcher(new GreaterThan<Long>(value));
559        return 0;
560    }
561
562    /**
563     * Expects a short argument greater than the given value. For details, see
564     * the EasyMock documentation.
565     *
566     * @param value
567     *            the given value.
568     * @return <code>0</code>.
569     */
570    public static short gt(short value) {
571        reportMatcher(new GreaterThan<Short>(value));
572        return 0;
573    }
574
575    /**
576     * Expects a comparable argument less than the given value. For details, see
577     * the EasyMock documentation.
578     *
579     * @param <T> type of the method argument to match
580     * @param value
581     *            the given value.
582     * @return <code>null</code>.
583     */
584    public static <T extends Comparable<T>> T lt(Comparable<T> value) {
585        reportMatcher(new LessThan<T>(value));
586        return null;
587    }
588
589    /**
590     * Expects a byte argument less than the given value. For details, see the
591     * EasyMock documentation.
592     *
593     * @param value
594     *            the given value.
595     * @return <code>0</code>.
596     */
597    public static byte lt(byte value) {
598        reportMatcher(new LessThan<Byte>(value));
599        return 0;
600    }
601
602    /**
603     * Expects a double argument less than the given value. For details, see the
604     * EasyMock documentation.
605     *
606     * @param value
607     *            the given value.
608     * @return <code>0</code>.
609     */
610    public static double lt(double value) {
611        reportMatcher(new LessThan<Double>(value));
612        return 0;
613    }
614
615    /**
616     * Expects a float argument less than the given value. For details, see the
617     * EasyMock documentation.
618     *
619     * @param value
620     *            the given value.
621     * @return <code>0</code>.
622     */
623    public static float lt(float value) {
624        reportMatcher(new LessThan<Float>(value));
625        return 0;
626    }
627
628    /**
629     * Expects an int argument less than the given value. For details, see the
630     * EasyMock documentation.
631     *
632     * @param value
633     *            the given value.
634     * @return <code>0</code>.
635     */
636    public static int lt(int value) {
637        reportMatcher(new LessThan<Integer>(value));
638        return 0;
639    }
640
641    /**
642     * Expects a long argument less than the given value. For details, see the
643     * EasyMock documentation.
644     *
645     * @param value
646     *            the given value.
647     * @return <code>0</code>.
648     */
649    public static long lt(long value) {
650        reportMatcher(new LessThan<Long>(value));
651        return 0;
652    }
653
654    /**
655     * Expects a short argument less than the given value. For details, see the
656     * EasyMock documentation.
657     *
658     * @param value
659     *            the given value.
660     * @return <code>0</code>.
661     */
662    public static short lt(short value) {
663        reportMatcher(new LessThan<Short>(value));
664        return 0;
665    }
666
667    /**
668     * Expects an object implementing the given class. For details, see the
669     * EasyMock documentation.
670     *
671     * @param <T>
672     *            the accepted type.
673     * @param clazz
674     *            the class of the accepted type.
675     * @return <code>null</code>.
676     */
677    public static <T> T isA(Class<T> clazz) {
678        reportMatcher(new InstanceOf(clazz));
679        return null;
680    }
681
682    /**
683     * Expects a string that contains the given substring. For details, see the
684     * EasyMock documentation.
685     *
686     * @param substring
687     *            the substring.
688     * @return <code>null</code>.
689     */
690    public static String contains(String substring) {
691        reportMatcher(new Contains(substring));
692        return null;
693    }
694
695    /**
696     * Expects a boolean that matches both given expectations.
697     *
698     * @param first
699     *            placeholder for the first expectation.
700     * @param second
701     *            placeholder for the second expectation.
702     * @return <code>false</code>.
703     */
704    public static boolean and(boolean first, boolean second) {
705        LastControl.reportAnd(2);
706        return false;
707    }
708
709    /**
710     * Expects a byte that matches both given expectations.
711     *
712     * @param first
713     *            placeholder for the first expectation.
714     * @param second
715     *            placeholder for the second expectation.
716     * @return <code>0</code>.
717     */
718    public static byte and(byte first, byte second) {
719        LastControl.reportAnd(2);
720        return 0;
721    }
722
723    /**
724     * Expects a char that matches both given expectations.
725     *
726     * @param first
727     *            placeholder for the first expectation.
728     * @param second
729     *            placeholder for the second expectation.
730     * @return <code>0</code>.
731     */
732    public static char and(char first, char second) {
733        LastControl.reportAnd(2);
734        return 0;
735    }
736
737    /**
738     * Expects a double that matches both given expectations.
739     *
740     * @param first
741     *            placeholder for the first expectation.
742     * @param second
743     *            placeholder for the second expectation.
744     * @return <code>0</code>.
745     */
746    public static double and(double first, double second) {
747        LastControl.reportAnd(2);
748        return 0;
749    }
750
751    /**
752     * Expects a float that matches both given expectations.
753     *
754     * @param first
755     *            placeholder for the first expectation.
756     * @param second
757     *            placeholder for the second expectation.
758     * @return <code>0</code>.
759     */
760    public static float and(float first, float second) {
761        LastControl.reportAnd(2);
762        return 0;
763    }
764
765    /**
766     * Expects an int that matches both given expectations.
767     *
768     * @param first
769     *            placeholder for the first expectation.
770     * @param second
771     *            placeholder for the second expectation.
772     * @return <code>0</code>.
773     */
774    public static int and(int first, int second) {
775        LastControl.reportAnd(2);
776        return 0;
777    }
778
779    /**
780     * Expects a long that matches both given expectations.
781     *
782     * @param first
783     *            placeholder for the first expectation.
784     * @param second
785     *            placeholder for the second expectation.
786     * @return <code>0</code>.
787     */
788    public static long and(long first, long second) {
789        LastControl.reportAnd(2);
790        return 0;
791    }
792
793    /**
794     * Expects a short that matches both given expectations.
795     *
796     * @param first
797     *            placeholder for the first expectation.
798     * @param second
799     *            placeholder for the second expectation.
800     * @return <code>0</code>.
801     */
802    public static short and(short first, short second) {
803        LastControl.reportAnd(2);
804        return 0;
805    }
806
807    /**
808     * Expects an Object that matches both given expectations.
809     *
810     * @param <T>
811     *            the type of the object, it is passed through to prevent casts.
812     * @param first
813     *            placeholder for the first expectation.
814     * @param second
815     *            placeholder for the second expectation.
816     * @return <code>null</code>.
817     */
818    public static <T> T and(T first, T second) {
819        LastControl.reportAnd(2);
820        return null;
821    }
822
823    /**
824     * Expects a boolean that matches one of the given expectations.
825     *
826     * @param first
827     *            placeholder for the first expectation.
828     * @param second
829     *            placeholder for the second expectation.
830     * @return <code>false</code>.
831     */
832    public static boolean or(boolean first, boolean second) {
833        LastControl.reportOr(2);
834        return false;
835    }
836
837    /**
838     * Expects a byte that matches one of the given expectations.
839     *
840     * @param first
841     *            placeholder for the first expectation.
842     * @param second
843     *            placeholder for the second expectation.
844     * @return <code>0</code>.
845     */
846    public static byte or(byte first, byte second) {
847        LastControl.reportOr(2);
848        return 0;
849    }
850
851    /**
852     * Expects a char that matches one of the given expectations.
853     *
854     * @param first
855     *            placeholder for the first expectation.
856     * @param second
857     *            placeholder for the second expectation.
858     * @return <code>0</code>.
859     */
860    public static char or(char first, char second) {
861        LastControl.reportOr(2);
862        return 0;
863    }
864
865    /**
866     * Expects a double that matches one of the given expectations.
867     *
868     * @param first
869     *            placeholder for the first expectation.
870     * @param second
871     *            placeholder for the second expectation.
872     * @return <code>0</code>.
873     */
874    public static double or(double first, double second) {
875        LastControl.reportOr(2);
876        return 0;
877    }
878
879    /**
880     * Expects a float that matches one of the given expectations.
881     *
882     * @param first
883     *            placeholder for the first expectation.
884     * @param second
885     *            placeholder for the second expectation.
886     * @return <code>0</code>.
887     */
888    public static float or(float first, float second) {
889        LastControl.reportOr(2);
890        return 0;
891    }
892
893    /**
894     * Expects an int that matches one of the given expectations.
895     *
896     * @param first
897     *            placeholder for the first expectation.
898     * @param second
899     *            placeholder for the second expectation.
900     * @return <code>0</code>.
901     */
902    public static int or(int first, int second) {
903        LastControl.reportOr(2);
904        return first;
905    }
906
907    /**
908     * Expects a long that matches one of the given expectations.
909     *
910     * @param first
911     *            placeholder for the first expectation.
912     * @param second
913     *            placeholder for the second expectation.
914     * @return <code>0</code>.
915     */
916    public static long or(long first, long second) {
917        LastControl.reportOr(2);
918        return 0;
919    }
920
921    /**
922     * Expects a short that matches one of the given expectations.
923     *
924     * @param first
925     *            placeholder for the first expectation.
926     * @param second
927     *            placeholder for the second expectation.
928     * @return <code>0</code>.
929     */
930    public static short or(short first, short second) {
931        LastControl.reportOr(2);
932        return 0;
933    }
934
935    /**
936     * Expects an Object that matches one of the given expectations.
937     *
938     * @param <T>
939     *            the type of the object, it is passed through to prevent casts.
940     * @param first
941     *            placeholder for the first expectation.
942     * @param second
943     *            placeholder for the second expectation.
944     * @return <code>null</code>.
945     */
946    public static <T> T or(T first, T second) {
947        LastControl.reportOr(2);
948        return null;
949    }
950
951    /**
952     * Expects a boolean that does not match the given expectation.
953     *
954     * @param first
955     *            placeholder for the expectation.
956     * @return <code>false</code>.
957     */
958    public static boolean not(boolean first) {
959        LastControl.reportNot();
960        return false;
961    }
962
963    /**
964     * Expects a byte that does not match the given expectation.
965     *
966     * @param first
967     *            placeholder for the expectation.
968     * @return <code>0</code>.
969     */
970    public static byte not(byte first) {
971        LastControl.reportNot();
972        return 0;
973    }
974
975    /**
976     * Expects a char that does not match the given expectation.
977     *
978     * @param first
979     *            placeholder for the expectation.
980     * @return <code>0</code>.
981     */
982    public static char not(char first) {
983        LastControl.reportNot();
984        return 0;
985    }
986
987    /**
988     * Expects a double that does not match the given expectation.
989     *
990     * @param first
991     *            placeholder for the expectation.
992     * @return <code>0</code>.
993     */
994    public static double not(double first) {
995        LastControl.reportNot();
996        return 0;
997    }
998
999    /**
1000     * Expects a float that does not match the given expectation.
1001     *
1002     * @param first
1003     *            placeholder for the expectation.
1004     * @return <code>0</code>.
1005     */
1006    public static float not(float first) {
1007        LastControl.reportNot();
1008        return first;
1009    }
1010
1011    /**
1012     * Expects an int that does not match the given expectation.
1013     *
1014     * @param first
1015     *            placeholder for the expectation.
1016     * @return <code>0</code>.
1017     */
1018    public static int not(int first) {
1019        LastControl.reportNot();
1020        return 0;
1021    }
1022
1023    /**
1024     * Expects a long that does not match the given expectation.
1025     *
1026     * @param first
1027     *            placeholder for the expectation.
1028     * @return <code>0</code>.
1029     */
1030    public static long not(long first) {
1031        LastControl.reportNot();
1032        return 0;
1033    }
1034
1035    /**
1036     * Expects a short that does not match the given expectation.
1037     *
1038     * @param first
1039     *            placeholder for the expectation.
1040     * @return <code>0</code>.
1041     */
1042    public static short not(short first) {
1043        LastControl.reportNot();
1044        return 0;
1045    }
1046
1047    /**
1048     * Expects an Object that does not match the given expectation.
1049     *
1050     * @param <T>
1051     *            the type of the object, it is passed through to prevent casts.
1052     * @param first
1053     *            placeholder for the expectation.
1054     * @return <code>null</code>.
1055     */
1056    public static <T> T not(T first) {
1057        LastControl.reportNot();
1058        return null;
1059    }
1060
1061    /**
1062     * Expects a boolean that is equal to the given value.
1063     *
1064     * @param value
1065     *            the given value.
1066     * @return <code>0</code>.
1067     */
1068    public static boolean eq(boolean value) {
1069        reportMatcher(new Equals(value));
1070        return false;
1071    }
1072
1073    /**
1074     * Expects a byte that is equal to the given value.
1075     *
1076     * @param value
1077     *            the given value.
1078     * @return <code>0</code>.
1079     */
1080    public static byte eq(byte value) {
1081        reportMatcher(new Equals(value));
1082        return 0;
1083    }
1084
1085    /**
1086     * Expects a char that is equal to the given value.
1087     *
1088     * @param value
1089     *            the given value.
1090     * @return <code>0</code>.
1091     */
1092    public static char eq(char value) {
1093        reportMatcher(new Equals(value));
1094        return 0;
1095    }
1096
1097    /**
1098     * Expects a double that is equal to the given value.
1099     *
1100     * @param value
1101     *            the given value.
1102     * @return <code>0</code>.
1103     */
1104    public static double eq(double value) {
1105        reportMatcher(new Equals(value));
1106        return 0;
1107    }
1108
1109    /**
1110     * Expects a float that is equal to the given value.
1111     *
1112     * @param value
1113     *            the given value.
1114     * @return <code>0</code>.
1115     */
1116    public static float eq(float value) {
1117        reportMatcher(new Equals(value));
1118        return 0;
1119    }
1120
1121    /**
1122     * Expects an int that is equal to the given value.
1123     *
1124     * @param value
1125     *            the given value.
1126     * @return <code>0</code>.
1127     */
1128    public static int eq(int value) {
1129        reportMatcher(new Equals(value));
1130        return 0;
1131    }
1132
1133    /**
1134     * Expects a long that is equal to the given value.
1135     *
1136     * @param value
1137     *            the given value.
1138     * @return <code>0</code>.
1139     */
1140    public static long eq(long value) {
1141        reportMatcher(new Equals(value));
1142        return 0;
1143    }
1144
1145    /**
1146     * Expects a short that is equal to the given value.
1147     *
1148     * @param value
1149     *            the given value.
1150     * @return <code>0</code>.
1151     */
1152    public static short eq(short value) {
1153        reportMatcher(new Equals(value));
1154        return 0;
1155    }
1156
1157    /**
1158     * Expects an Object that is equal to the given value.
1159     *
1160     * @param <T> type of the method argument to match
1161     * @param value
1162     *            the given value.
1163     * @return <code>null</code>.
1164     */
1165    public static <T> T eq(T value) {
1166        reportMatcher(new Equals(value));
1167        return null;
1168    }
1169
1170    /**
1171     * Expects a boolean array that is equal to the given array, i.e. it has to
1172     * have the same length, and each element has to be equal.
1173     *
1174     * @param value
1175     *            the given arry.
1176     * @return <code>null</code>.
1177     */
1178    public static boolean[] aryEq(boolean[] value) {
1179        reportMatcher(new ArrayEquals(value));
1180        return null;
1181    }
1182
1183    /**
1184     * Expects a byte array that is equal to the given array, i.e. it has to
1185     * have the same length, and each element has to be equal.
1186     *
1187     * @param value
1188     *            the given arry.
1189     * @return <code>null</code>.
1190     */
1191    public static byte[] aryEq(byte[] value) {
1192        reportMatcher(new ArrayEquals(value));
1193        return null;
1194    }
1195
1196    /**
1197     * Expects a char array that is equal to the given array, i.e. it has to
1198     * have the same length, and each element has to be equal.
1199     *
1200     * @param value
1201     *            the given arry.
1202     * @return <code>null</code>.
1203     */
1204    public static char[] aryEq(char[] value) {
1205        reportMatcher(new ArrayEquals(value));
1206        return null;
1207    }
1208
1209    /**
1210     * Expects a double array that is equal to the given array, i.e. it has to
1211     * have the same length, and each element has to be equal.
1212     *
1213     * @param value
1214     *            the given arry.
1215     * @return <code>null</code>.
1216     */
1217    public static double[] aryEq(double[] value) {
1218        reportMatcher(new ArrayEquals(value));
1219        return null;
1220    }
1221
1222    /**
1223     * Expects a float array that is equal to the given array, i.e. it has to
1224     * have the same length, and each element has to be equal.
1225     *
1226     * @param value
1227     *            the given arry.
1228     * @return <code>null</code>.
1229     */
1230    public static float[] aryEq(float[] value) {
1231        reportMatcher(new ArrayEquals(value));
1232        return null;
1233    }
1234
1235    /**
1236     * Expects an int array that is equal to the given array, i.e. it has to
1237     * have the same length, and each element has to be equal.
1238     *
1239     * @param value
1240     *            the given arry.
1241     * @return <code>null</code>.
1242     */
1243    public static int[] aryEq(int[] value) {
1244        reportMatcher(new ArrayEquals(value));
1245        return null;
1246    }
1247
1248    /**
1249     * Expects a long array that is equal to the given array, i.e. it has to
1250     * have the same length, and each element has to be equal.
1251     *
1252     * @param value
1253     *            the given arry.
1254     * @return <code>null</code>.
1255     */
1256    public static long[] aryEq(long[] value) {
1257        reportMatcher(new ArrayEquals(value));
1258        return null;
1259    }
1260
1261    /**
1262     * Expects a short array that is equal to the given array, i.e. it has to
1263     * have the same length, and each element has to be equal.
1264     *
1265     * @param value
1266     *            the given arry.
1267     * @return <code>null</code>.
1268     */
1269    public static short[] aryEq(short[] value) {
1270        reportMatcher(new ArrayEquals(value));
1271        return null;
1272    }
1273
1274    /**
1275     * Expects an Object array that is equal to the given array, i.e. it has to
1276     * have the same type, length, and each element has to be equal.
1277     *
1278     * @param <T>
1279     *            the type of the array, it is passed through to prevent casts.
1280     * @param value
1281     *            the given arry.
1282     * @return <code>null</code>.
1283     */
1284    public static <T> T[] aryEq(T[] value) {
1285        reportMatcher(new ArrayEquals(value));
1286        return null;
1287    }
1288
1289    /**
1290     * Expects null.
1291     *
1292     * @param <T> type of the method argument to match
1293     * @return <code>null</code>.
1294     */
1295    public static <T> T isNull() {
1296        reportMatcher(Null.NULL);
1297        return null;
1298    }
1299
1300    /**
1301     * Expects not null.
1302     *
1303     * @param <T> type of the method argument to match
1304     * @return <code>null</code>.
1305     */
1306    public static <T> T notNull() {
1307        reportMatcher(NotNull.NOT_NULL);
1308        return null;
1309    }
1310
1311    /**
1312     * Expects a string that contains a substring that matches the given regular
1313     * expression. For details, see the EasyMock documentation.
1314     *
1315     * @param regex
1316     *            the regular expression.
1317     * @return <code>null</code>.
1318     */
1319    public static String find(String regex) {
1320        reportMatcher(new Find(regex));
1321        return null;
1322    }
1323
1324    /**
1325     * Expects a string that matches the given regular expression. For details,
1326     * see the EasyMock documentation.
1327     *
1328     * @param regex
1329     *            the regular expression.
1330     * @return <code>null</code>.
1331     */
1332    public static String matches(String regex) {
1333        reportMatcher(new Matches(regex));
1334        return null;
1335    }
1336
1337    /**
1338     * Expects a string that starts with the given prefix. For details, see the
1339     * EasyMock documentation.
1340     *
1341     * @param prefix
1342     *            the prefix.
1343     * @return <code>null</code>.
1344     */
1345    public static String startsWith(String prefix) {
1346        reportMatcher(new StartsWith(prefix));
1347        return null;
1348    }
1349
1350    /**
1351     * Expects a string that ends with the given suffix. For details, see the
1352     * EasyMock documentation.
1353     *
1354     * @param suffix
1355     *            the suffix.
1356     * @return <code>null</code>.
1357     */
1358    public static String endsWith(String suffix) {
1359        reportMatcher(new EndsWith(suffix));
1360        return null;
1361    }
1362
1363    /**
1364     * Expects a double that has an absolute difference to the given value that
1365     * is less than the given delta. For details, see the EasyMock
1366     * documentation.
1367     *
1368     * @param value
1369     *            the given value.
1370     * @param delta
1371     *            the given delta.
1372     * @return <code>0</code>.
1373     */
1374    public static double eq(double value, double delta) {
1375        reportMatcher(new EqualsWithDelta(value, delta));
1376        return 0;
1377    }
1378
1379    /**
1380     * Expects a float that has an absolute difference to the given value that
1381     * is less than the given delta. For details, see the EasyMock
1382     * documentation.
1383     *
1384     * @param value
1385     *            the given value.
1386     * @param delta
1387     *            the given delta.
1388     * @return <code>0</code>.
1389     */
1390    public static float eq(float value, float delta) {
1391        reportMatcher(new EqualsWithDelta(value, delta));
1392        return 0;
1393    }
1394
1395    /**
1396     * Expects an Object that is the same as the given value. For details, see
1397     * the EasyMock documentation.
1398     *
1399     * @param <T>
1400     *            the type of the object, it is passed through to prevent casts.
1401     * @param value
1402     *            the given value.
1403     * @return <code>null</code>.
1404     */
1405    public static <T> T same(T value) {
1406        reportMatcher(new Same(value));
1407        return null;
1408    }
1409
1410    /**
1411     * Expects a comparable argument equals to the given value according to their
1412     * compareTo method. For details, see the EasMock documentation.
1413     *
1414     * @param <T> type of the method argument to match
1415     * @param value
1416     *            the given value.
1417     * @return <code>null</code>.
1418     */
1419    public static <T extends Comparable<T>> T cmpEq(Comparable<T> value) {
1420        reportMatcher(new CompareEqual<T>(value));
1421        return null;
1422    }
1423
1424    /**
1425     * Expects an argument that will be compared using the provided comparator.
1426     * The following comparison will take place:
1427     * <p>
1428     * <code>comparator.compare(actual, expected) operator 0</code>
1429     * </p>
1430     * For details, see the EasyMock documentation.
1431     *
1432     * @param <T> type of the method argument to match
1433     * @param value the given value.
1434     * @param comparator Comparator used to compare the actual with expected value.
1435     * @param operator The comparison operator.
1436     * @return <code>null</code>
1437     */
1438    public static <T> T cmp(T value, Comparator<? super T> comparator, LogicalOperator operator) {
1439        reportMatcher(new Compare<T>(value, comparator, operator));
1440        return null;
1441    }
1442
1443
1444    /**
1445     * Expects a byte that is equal to the given value.
1446     *
1447     * @param value
1448     *            the given value.
1449     * @return <code>0</code>.
1450     */
1451
1452
1453    /**
1454     * Expect any object but captures it for later use.
1455     *
1456     * @param <T> Type of the captured object
1457     * @param captured Where the parameter is captured
1458     * @return <code>null</code>
1459     */
1460    public static <T> T capture(Capture<T> captured) {
1461        reportMatcher(new Captures<T>(captured));
1462        return null;
1463    }
1464
1465    /******************************************************
1466     * ANDROID CHANGE - comment out to fix compile error
1467     * name clash: capture(org.easymock.Capture<java.lang.Long>) and
1468     *  <T>capture(org.easymock.Capture<T>) have the same erasure
1469     */
1470    /**
1471     * Expect any int but captures it for later use.
1472     *
1473     * @param captured Where the parameter is captured
1474     * @return <code>0</code>
1475     */
1476//    public static int capture(Capture<Integer> captured) {
1477//        reportMatcher(new Captures<Integer>(captured));
1478//        return 0;
1479//    }
1480
1481    /**
1482     * Expect any long but captures it for later use.
1483     *
1484     * @param captured Where the parameter is captured
1485     * @return <code>0</code>
1486     */
1487//    public static long capture(Capture<Long> captured) {
1488//        reportMatcher(new Captures<Long>(captured));
1489//        return 0;
1490//    }
1491
1492    /**
1493     * Expect any float but captures it for later use.
1494     *
1495     * @param captured Where the parameter is captured
1496     * @return <code>0</code>
1497     */
1498//    public static float capture(Capture<Float> captured) {
1499//        reportMatcher(new Captures<Float>(captured));
1500//        return 0;
1501//    }
1502
1503    /**
1504     * Expect any double but captures it for later use.
1505     *
1506     * @param captured Where the parameter is captured
1507     * @return <code>0</code>
1508     */
1509//    public static double capture(Capture<Double> captured) {
1510//        reportMatcher(new Captures<Double>(captured));
1511//        return 0;
1512//    }
1513
1514    /**
1515     * Expect any byte but captures it for later use.
1516     *
1517     * @param captured Where the parameter is captured
1518     * @return <code>0</code>
1519     */
1520//    public static byte capture(Capture<Byte> captured) {
1521//        reportMatcher(new Captures<Byte>(captured));
1522//        return 0;
1523//    }
1524
1525    /**
1526     * Expect any char but captures it for later use.
1527     *
1528     * @param captured Where the parameter is captured
1529     * @return <code>0</code>
1530     */
1531//    public static char capture(Capture<Character> captured) {
1532//        reportMatcher(new Captures<Character>(captured));
1533//        return 0;
1534//    }
1535
1536    /**********  END ANDROID CHANGE **********/
1537
1538    /**
1539     * Switches the given mock objects (more exactly: the controls of the mock
1540     * objects) to replay mode. For details, see the EasyMock documentation.
1541     *
1542     * @param mocks
1543     *            the mock objects.
1544     */
1545    public static void replay(Object... mocks) {
1546        for (Object mock : mocks) {
1547            getControl(mock).replay();
1548        }
1549    }
1550
1551    /**
1552     * Resets the given mock objects (more exactly: the controls of the mock
1553     * objects). For details, see the EasyMock documentation.
1554     *
1555     * @param mocks
1556     *            the mock objects.
1557     */
1558    public static void reset(Object... mocks) {
1559        for (Object mock : mocks) {
1560            getControl(mock).reset();
1561        }
1562    }
1563
1564    /**
1565     * Resets the given mock objects (more exactly: the controls of the mock
1566     * objects) and turn them to a mock with nice behavior. For details, see
1567     * the EasyMock documentation.
1568     *
1569     * @param mocks
1570     *            the mock objects
1571     */
1572    public static void resetToNice(Object... mocks) {
1573        for (Object mock : mocks) {
1574            getControl(mock).resetToNice();
1575        }
1576    }
1577
1578    /**
1579     * Resets the given mock objects (more exactly: the controls of the mock
1580     * objects) and turn them to a mock with default behavior. For details, see
1581     * the EasyMock documentation.
1582     *
1583     * @param mocks
1584     *            the mock objects
1585     */
1586    public static void resetToDefault(Object... mocks) {
1587        for (Object mock : mocks) {
1588            getControl(mock).resetToDefault();
1589        }
1590    }
1591
1592    /**
1593     * Resets the given mock objects (more exactly: the controls of the mock
1594     * objects) and turn them to a mock with strict behavior. For details, see
1595     * the EasyMock documentation.
1596     *
1597     * @param mocks
1598     *            the mock objects
1599     */
1600    public static void resetToStrict(Object... mocks) {
1601        for (Object mock : mocks) {
1602            getControl(mock).resetToStrict();
1603        }
1604    }
1605
1606    /**
1607     * Verifies the given mock objects (more exactly: the controls of the mock
1608     * objects).
1609     *
1610     * @param mocks
1611     *            the mock objects.
1612     */
1613    public static void verify(Object... mocks) {
1614        for (Object mock : mocks) {
1615            getControl(mock).verify();
1616        }
1617    }
1618
1619    /**
1620     * Switches order checking of the given mock object (more exactly: the
1621     * control of the mock object) the on and off. For details, see the EasyMock
1622     * documentation.
1623     *
1624     * @param mock
1625     *            the mock object.
1626     * @param state
1627     *            <code>true</code> switches order checking on,
1628     *            <code>false</code> switches it off.
1629     */
1630    public static void checkOrder(Object mock, boolean state) {
1631        getControl(mock).checkOrder(state);
1632    }
1633
1634    /**
1635     * Reports an argument matcher. This method is needed to define own argument
1636     * matchers. For details, see the EasyMock documentation.
1637     *
1638     * @param matcher
1639     */
1640    public static void reportMatcher(IArgumentMatcher matcher) {
1641        LastControl.reportMatcher(matcher);
1642    }
1643
1644    private static MocksControl getControl(Object mock) {
1645        return ((ObjectMethodsFilter) Proxy
1646        .getInvocationHandler(mock)).getDelegate().getControl();
1647    }
1648
1649    /**
1650     * Returns the arguments of the current mock method call, if inside an
1651     * <code>IAnswer</code> callback - be careful here, reordering parameters of
1652     * method changes the semantics of your tests.
1653     *
1654     * @return the arguments of the current mock method call.
1655     * @throws IllegalStateException
1656     *             if called outside of <code>IAnswer</code> callbacks.
1657     */
1658    public static Object[] getCurrentArguments() {
1659        Invocation result = LastControl.getCurrentInvocation();
1660        if (result == null) {
1661            throw new IllegalStateException(
1662                    "current arguments are only available when executing callback methods");
1663        }
1664        return result.getArguments();
1665    }
1666
1667    /**
1668     * By default, a mock is thread safe (unless
1669     * {@link #NOT_THREAD_SAFE_BY_DEFAULT} is set). This method can change this
1670     * behavior. Two reasons are known for someone to do that: Performance or
1671     * dead-locking issues.
1672     *
1673     * @param mock
1674     *            the mock to make thread safe
1675     * @param threadSafe
1676     *            If the mock should be thread safe or not
1677     */
1678    public static void makeThreadSafe(Object mock, boolean threadSafe) {
1679        getControl(mock).makeThreadSafe(threadSafe);
1680    }
1681
1682    /**
1683     * Tell that the mock should be used in only one thread. An exception will
1684     * be thrown if that's not the case. This can be useful when mocking an
1685     * object that isn't thread safe to make sure it is used correctly in a
1686     * multithreaded environment. By default, no check is done unless
1687     * {@link #ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT} was set to true.
1688     *
1689     * @param mock
1690     *            the mock
1691     * @param shouldBeUsedInOneThread
1692     *            If the mock should be used in only one thread
1693     */
1694    public static void checkIsUsedInOneThread(Object mock,
1695            boolean shouldBeUsedInOneThread) {
1696        getControl(mock).checkIsUsedInOneThread(shouldBeUsedInOneThread);
1697    }
1698
1699    /**
1700     * Get the current value for an EasyMock property
1701     *
1702     * @param key
1703     *            key for the property
1704     * @return the property value
1705     */
1706    public static String getEasyMockProperty(String key) {
1707        return EasyMockProperties.getInstance().getProperty(key);
1708    }
1709
1710    /**
1711     * Set a property to modify the default EasyMock behavior. These properties
1712     * can also be set as System properties or in easymock.properties. This
1713     * method can then be called to overload them. For details and a list of
1714     * available properties see the EasyMock documentation.
1715     * <p>
1716     * <b>Note:</b> This method is static. Setting a property will change the
1717     * entire EasyMock behavior.
1718     *
1719     * @param key
1720     *            property key
1721     * @param value
1722     *            property value. A null value will remove the property
1723     * @return the previous property value
1724     */
1725    public static String setEasyMockProperty(String key, String value) {
1726        return EasyMockProperties.getInstance().setProperty(key, value);
1727    }
1728
1729    // ///CLOVER:OFF
1730    /** Prevent instantiation but allow inheritance */
1731    protected EasyMock() {
1732    }
1733    // ///CLOVER:ON
1734}
1735