|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
EasyMock.java | 100% | 100% | 100% | 100% |
|
1 | /* | |
2 | * Copyright (c) 2001-2006 OFFIS, Tammo Freese. | |
3 | * This program is made available under the terms of the MIT License. | |
4 | */ | |
5 | package org.easymock; | |
6 | ||
7 | import java.lang.reflect.Proxy; | |
8 | ||
9 | import org.easymock.internal.LastControl; | |
10 | import org.easymock.internal.MocksControl; | |
11 | import org.easymock.internal.ObjectMethodsFilter; | |
12 | import org.easymock.internal.matchers.*; | |
13 | ||
14 | public class EasyMock { | |
15 | ||
16 | /** | |
17 | * Creates a mock object that implements the given interface, order checking | |
18 | * is enabled by default. | |
19 | * | |
20 | * @param <T> | |
21 | * the interface that the mock object should implement. | |
22 | * @param toMock | |
23 | * the class of the interface that the mock object should | |
24 | * implement. | |
25 | * @return the mock object. | |
26 | */ | |
27 | 14 | public static <T> T createStrictMock(Class<T> toMock) { |
28 | 14 | return createStrictControl().createMock(toMock); |
29 | } | |
30 | ||
31 | /** | |
32 | * Creates a mock object that implements the given interface, order checking | |
33 | * is disabled by default. | |
34 | * | |
35 | * @param <T> | |
36 | * the interface that the mock object should implement. | |
37 | * @param toMock | |
38 | * the class of the interface that the mock object should | |
39 | * implement. | |
40 | * @return the mock object. | |
41 | */ | |
42 | 41 | public static <T> T createMock(Class<T> toMock) { |
43 | 41 | return createControl().createMock(toMock); |
44 | } | |
45 | ||
46 | /** | |
47 | * Creates a mock object that implements the given interface, order checking | |
48 | * is disabled by default, and the mock object will return <code>0</code>, | |
49 | * <code>null</code> or <code>false</code> for unexpected invocations. | |
50 | * | |
51 | * @param <T> | |
52 | * the interface that the mock object should implement. | |
53 | * @param toMock | |
54 | * the class of the interface that the mock object should | |
55 | * implement. | |
56 | * @return the mock object. | |
57 | */ | |
58 | 4 | public static <T> T createNiceMock(Class<T> toMock) { |
59 | 4 | return createNiceControl().createMock(toMock); |
60 | } | |
61 | ||
62 | /** | |
63 | * Creates a control, order checking is enabled by default. | |
64 | * | |
65 | * @return the control. | |
66 | */ | |
67 | 47 | public static IMocksControl createStrictControl() { |
68 | 47 | return new MocksControl(MocksControl.MockType.STRICT); |
69 | } | |
70 | ||
71 | /** | |
72 | * Creates a control, order checking is disabled by default. | |
73 | * | |
74 | * @return the control. | |
75 | */ | |
76 | 391 | public static IMocksControl createControl() { |
77 | 391 | return new MocksControl(MocksControl.MockType.DEFAULT); |
78 | } | |
79 | ||
80 | /** | |
81 | * Creates a control, order checking is disabled by default, and the mock | |
82 | * objects created by this control will return <code>0</code>, | |
83 | * <code>null</code> or <code>false</code> for unexpected invocations. | |
84 | * | |
85 | * @return the control. | |
86 | */ | |
87 | 13 | public static IMocksControl createNiceControl() { |
88 | 13 | return new MocksControl(MocksControl.MockType.NICE); |
89 | } | |
90 | ||
91 | /** | |
92 | * Returns the expectation setter for the last expected invocation in the | |
93 | * current thread. | |
94 | * | |
95 | * @param value | |
96 | * the parameter is used to transport the type to the | |
97 | * ExpectationSetter. It allows writing the expected call as | |
98 | * argument, i.e. | |
99 | * <code>expect(mock.getName()).andReturn("John Doe")<code>. | |
100 | * | |
101 | * @return the expectation setter. | |
102 | */ | |
103 | 160 | @SuppressWarnings("unchecked") |
104 | public static <T> IExpectationSetters<T> expect(T value) { | |
105 | 160 | return getControlForLastCall(); |
106 | } | |
107 | ||
108 | /** | |
109 | * Returns the expectation setter for the last expected invocation in the | |
110 | * current thread. This method is used for expected invocations on void | |
111 | * methods. | |
112 | * | |
113 | * @return the expectation setter. | |
114 | */ | |
115 | 257 | @SuppressWarnings("unchecked") |
116 | public static IExpectationSetters<Object> expectLastCall() { | |
117 | 257 | return getControlForLastCall(); |
118 | } | |
119 | ||
120 | 417 | private static IExpectationSetters getControlForLastCall() { |
121 | 417 | MocksControl lastControl = LastControl.lastControl(); |
122 | 417 | if (lastControl == null) { |
123 | 87 | throw new IllegalStateException("no last call on a mock available"); |
124 | } | |
125 | 330 | return lastControl; |
126 | } | |
127 | ||
128 | /** | |
129 | * Expects any boolean argument. For details, see the EasyMock | |
130 | * documentation. | |
131 | * | |
132 | * @return <code>false</code>. | |
133 | */ | |
134 | 1 | public static boolean anyBoolean() { |
135 | 1 | reportMatcher(Any.ANY); |
136 | 1 | return false; |
137 | } | |
138 | ||
139 | /** | |
140 | * Expects any byte argument. For details, see the EasyMock documentation. | |
141 | * | |
142 | * @return <code>0</code>. | |
143 | */ | |
144 | 1 | public static byte anyByte() { |
145 | 1 | reportMatcher(Any.ANY); |
146 | 1 | return 0; |
147 | } | |
148 | ||
149 | /** | |
150 | * Expects any char argument. For details, see the EasyMock documentation. | |
151 | * | |
152 | * @return <code>0</code>. | |
153 | */ | |
154 | 1 | public static char anyChar() { |
155 | 1 | reportMatcher(Any.ANY); |
156 | 1 | return 0; |
157 | } | |
158 | ||
159 | /** | |
160 | * Expects any int argument. For details, see the EasyMock documentation. | |
161 | * | |
162 | * @return <code>0</code>. | |
163 | */ | |
164 | 3 | public static int anyInt() { |
165 | 3 | reportMatcher(Any.ANY); |
166 | 3 | return 0; |
167 | } | |
168 | ||
169 | /** | |
170 | * Expects any long argument. For details, see the EasyMock documentation. | |
171 | * | |
172 | * @return <code>0</code>. | |
173 | */ | |
174 | 1 | public static long anyLong() { |
175 | 1 | reportMatcher(Any.ANY); |
176 | 1 | return 0; |
177 | } | |
178 | ||
179 | /** | |
180 | * Expects any float argument. For details, see the EasyMock documentation. | |
181 | * | |
182 | * @return <code>0</code>. | |
183 | */ | |
184 | 1 | public static float anyFloat() { |
185 | 1 | reportMatcher(Any.ANY); |
186 | 1 | return 0; |
187 | } | |
188 | ||
189 | /** | |
190 | * Expects any double argument. For details, see the EasyMock documentation. | |
191 | * | |
192 | * @return <code>0</code>. | |
193 | */ | |
194 | 1 | public static double anyDouble() { |
195 | 1 | reportMatcher(Any.ANY); |
196 | 1 | return 0; |
197 | } | |
198 | ||
199 | /** | |
200 | * Expects any short argument. For details, see the EasyMock documentation. | |
201 | * | |
202 | * @return <code>0</code>. | |
203 | */ | |
204 | 1 | public static short anyShort() { |
205 | 1 | reportMatcher(Any.ANY); |
206 | 1 | return 0; |
207 | } | |
208 | ||
209 | /** | |
210 | * Expects any Object argument. For details, see the EasyMock documentation. | |
211 | * | |
212 | * @return <code>null</code>. | |
213 | */ | |
214 | 2 | public static Object anyObject() { |
215 | 2 | reportMatcher(Any.ANY); |
216 | 2 | return null; |
217 | } | |
218 | ||
219 | /** | |
220 | * Expects a byte argument greater than or equal to the given value. For | |
221 | * details, see the EasyMock documentation. | |
222 | * | |
223 | * @param value | |
224 | * the given value. | |
225 | * @return <code>0</code>. | |
226 | */ | |
227 | 1 | public static byte geq(byte value) { |
228 | 1 | reportMatcher(new GreaterOrEqual(value)); |
229 | 1 | return 0; |
230 | } | |
231 | ||
232 | /** | |
233 | * Expects a double argument greater than or equal to the given value. For | |
234 | * details, see the EasyMock documentation. | |
235 | * | |
236 | * @param value | |
237 | * the given value. | |
238 | * @return <code>0</code>. | |
239 | */ | |
240 | 1 | public static double geq(double value) { |
241 | 1 | reportMatcher(new GreaterOrEqual(value)); |
242 | 1 | return 0; |
243 | } | |
244 | ||
245 | /** | |
246 | * Expects a float argument greater than or equal to the given value. For | |
247 | * details, see the EasyMock documentation. | |
248 | * | |
249 | * @param value | |
250 | * the given value. | |
251 | * @return <code>0</code>. | |
252 | */ | |
253 | 1 | public static float geq(float value) { |
254 | 1 | reportMatcher(new GreaterOrEqual(value)); |
255 | 1 | return 0; |
256 | } | |
257 | ||
258 | /** | |
259 | * Expects an int argument greater than or equal to the given value. For | |
260 | * details, see the EasyMock documentation. | |
261 | * | |
262 | * @param value | |
263 | * the given value. | |
264 | * @return <code>0</code>. | |
265 | */ | |
266 | 4 | public static int geq(int value) { |
267 | 4 | reportMatcher(new GreaterOrEqual(value)); |
268 | 4 | return 0; |
269 | } | |
270 | ||
271 | /** | |
272 | * Expects a long argument greater than or equal to the given value. For | |
273 | * details, see the EasyMock documentation. | |
274 | * | |
275 | * @param value | |
276 | * the given value. | |
277 | * @return <code>0</code>. | |
278 | */ | |
279 | 1 | public static long geq(long value) { |
280 | 1 | reportMatcher(new GreaterOrEqual(value)); |
281 | 1 | return 0; |
282 | } | |
283 | ||
284 | /** | |
285 | * Expects a short argument greater than or equal to the given value. For | |
286 | * details, see the EasyMock documentation. | |
287 | * | |
288 | * @param value | |
289 | * the given value. | |
290 | * @return <code>0</code>. | |
291 | */ | |
292 | 1 | public static short geq(short value) { |
293 | 1 | reportMatcher(new GreaterOrEqual(value)); |
294 | 1 | return 0; |
295 | } | |
296 | ||
297 | /** | |
298 | * Expects a byte argument less than or equal to the given value. For | |
299 | * details, see the EasyMock documentation. | |
300 | * | |
301 | * @param value | |
302 | * the given value. | |
303 | * @return <code>0</code>. | |
304 | */ | |
305 | 1 | public static byte leq(byte value) { |
306 | 1 | reportMatcher(new LessOrEqual(value)); |
307 | 1 | return 0; |
308 | } | |
309 | ||
310 | /** | |
311 | * Expects a double argument less than or equal to the given value. For | |
312 | * details, see the EasyMock documentation. | |
313 | * | |
314 | * @param value | |
315 | * the given value. | |
316 | * @return <code>0</code>. | |
317 | */ | |
318 | 1 | public static double leq(double value) { |
319 | 1 | reportMatcher(new LessOrEqual(value)); |
320 | 1 | return 0; |
321 | } | |
322 | ||
323 | /** | |
324 | * Expects a float argument less than or equal to the given value. For | |
325 | * details, see the EasyMock documentation. | |
326 | * | |
327 | * @param value | |
328 | * the given value. | |
329 | * @return <code>0</code>. | |
330 | */ | |
331 | 1 | public static float leq(float value) { |
332 | 1 | reportMatcher(new LessOrEqual(value)); |
333 | 1 | return 0; |
334 | } | |
335 | ||
336 | /** | |
337 | * Expects an int argument less than or equal to the given value. For | |
338 | * details, see the EasyMock documentation. | |
339 | * | |
340 | * @param value | |
341 | * the given value. | |
342 | * @return <code>0</code>. | |
343 | */ | |
344 | 4 | public static int leq(int value) { |
345 | 4 | reportMatcher(new LessOrEqual(value)); |
346 | 4 | return 0; |
347 | } | |
348 | ||
349 | /** | |
350 | * Expects a long argument less than or equal to the given value. For | |
351 | * details, see the EasyMock documentation. | |
352 | * | |
353 | * @param value | |
354 | * the given value. | |
355 | * @return <code>0</code>. | |
356 | */ | |
357 | 1 | public static long leq(long value) { |
358 | 1 | reportMatcher(new LessOrEqual(value)); |
359 | 1 | return 0; |
360 | } | |
361 | ||
362 | /** | |
363 | * Expects a short argument less than or equal to the given value. For | |
364 | * details, see the EasyMock documentation. | |
365 | * | |
366 | * @param value | |
367 | * the given value. | |
368 | * @return <code>0</code>. | |
369 | */ | |
370 | 1 | public static short leq(short value) { |
371 | 1 | reportMatcher(new LessOrEqual(value)); |
372 | 1 | return 0; |
373 | } | |
374 | ||
375 | /** | |
376 | * Expects a byte argument greater than the given value. For details, see | |
377 | * the EasyMock documentation. | |
378 | * | |
379 | * @param value | |
380 | * the given value. | |
381 | * @return <code>0</code>. | |
382 | */ | |
383 | 1 | public static byte gt(byte value) { |
384 | 1 | reportMatcher(new GreaterThan(value)); |
385 | 1 | return 0; |
386 | } | |
387 | ||
388 | /** | |
389 | * Expects a double argument greater than the given value. For details, see | |
390 | * the EasyMock documentation. | |
391 | * | |
392 | * @param value | |
393 | * the given value. | |
394 | * @return <code>0</code>. | |
395 | */ | |
396 | 1 | public static double gt(double value) { |
397 | 1 | reportMatcher(new GreaterThan(value)); |
398 | 1 | return 0; |
399 | } | |
400 | ||
401 | /** | |
402 | * Expects a float argument greater than the given value. For details, see | |
403 | * the EasyMock documentation. | |
404 | * | |
405 | * @param value | |
406 | * the given value. | |
407 | * @return <code>0</code>. | |
408 | */ | |
409 | 1 | public static float gt(float value) { |
410 | 1 | reportMatcher(new GreaterThan(value)); |
411 | 1 | return 0; |
412 | } | |
413 | ||
414 | /** | |
415 | * Expects an int argument greater than the given value. For details, see | |
416 | * the EasyMock documentation. | |
417 | * | |
418 | * @param value | |
419 | * the given value. | |
420 | * @return <code>0</code>. | |
421 | */ | |
422 | 3 | public static int gt(int value) { |
423 | 3 | reportMatcher(new GreaterThan(value)); |
424 | 3 | return 0; |
425 | } | |
426 | ||
427 | /** | |
428 | * Expects a long argument greater than the given value. For details, see | |
429 | * the EasyMock documentation. | |
430 | * | |
431 | * @param value | |
432 | * the given value. | |
433 | * @return <code>0</code>. | |
434 | */ | |
435 | 1 | public static long gt(long value) { |
436 | 1 | reportMatcher(new GreaterThan(value)); |
437 | 1 | return 0; |
438 | } | |
439 | ||
440 | /** | |
441 | * Expects a short argument greater than the given value. For details, see | |
442 | * the EasyMock documentation. | |
443 | * | |
444 | * @param value | |
445 | * the given value. | |
446 | * @return <code>0</code>. | |
447 | */ | |
448 | 1 | public static short gt(short value) { |
449 | 1 | reportMatcher(new GreaterThan(value)); |
450 | 1 | return 0; |
451 | } | |
452 | ||
453 | /** | |
454 | * Expects a byte argument less than the given value. For details, see the | |
455 | * EasyMock documentation. | |
456 | * | |
457 | * @param value | |
458 | * the given value. | |
459 | * @return <code>0</code>. | |
460 | */ | |
461 | 1 | public static byte lt(byte value) { |
462 | 1 | reportMatcher(new LessThan(value)); |
463 | 1 | return 0; |
464 | } | |
465 | ||
466 | /** | |
467 | * Expects a double argument less than the given value. For details, see the | |
468 | * EasyMock documentation. | |
469 | * | |
470 | * @param value | |
471 | * the given value. | |
472 | * @return <code>0</code>. | |
473 | */ | |
474 | 1 | public static double lt(double value) { |
475 | 1 | reportMatcher(new LessThan(value)); |
476 | 1 | return 0; |
477 | } | |
478 | ||
479 | /** | |
480 | * Expects a float argument less than the given value. For details, see the | |
481 | * EasyMock documentation. | |
482 | * | |
483 | * @param value | |
484 | * the given value. | |
485 | * @return <code>0</code>. | |
486 | */ | |
487 | 1 | public static float lt(float value) { |
488 | 1 | reportMatcher(new LessThan(value)); |
489 | 1 | return 0; |
490 | } | |
491 | ||
492 | /** | |
493 | * Expects an int argument less than the given value. For details, see the | |
494 | * EasyMock documentation. | |
495 | * | |
496 | * @param value | |
497 | * the given value. | |
498 | * @return <code>0</code>. | |
499 | */ | |
500 | 4 | public static int lt(int value) { |
501 | 4 | reportMatcher(new LessThan(value)); |
502 | 4 | return 0; |
503 | } | |
504 | ||
505 | /** | |
506 | * Expects a long argument less than the given value. For details, see the | |
507 | * EasyMock documentation. | |
508 | * | |
509 | * @param value | |
510 | * the given value. | |
511 | * @return <code>0</code>. | |
512 | */ | |
513 | 1 | public static long lt(long value) { |
514 | 1 | reportMatcher(new LessThan(value)); |
515 | 1 | return 0; |
516 | } | |
517 | ||
518 | /** | |
519 | * Expects a short argument less than the given value. For details, see the | |
520 | * EasyMock documentation. | |
521 | * | |
522 | * @param value | |
523 | * the given value. | |
524 | * @return <code>0</code>. | |
525 | */ | |
526 | 1 | public static short lt(short value) { |
527 | 1 | reportMatcher(new LessThan(value)); |
528 | 1 | return 0; |
529 | } | |
530 | ||
531 | /** | |
532 | * Expects an object implementing the given class. For details, see the | |
533 | * EasyMock documentation. | |
534 | * | |
535 | * @param <T> | |
536 | * the accepted type. | |
537 | * @param clazz | |
538 | * the class of the accepted type. | |
539 | * @return <code>null</code>. | |
540 | */ | |
541 | 3 | public static <T> T isA(Class<T> clazz) { |
542 | 3 | reportMatcher(new InstanceOf(clazz)); |
543 | 3 | return null; |
544 | } | |
545 | ||
546 | /** | |
547 | * Expects a string that contains the given substring. For details, see the | |
548 | * EasyMock documentation. | |
549 | * | |
550 | * @param substring | |
551 | * the substring. | |
552 | * @return <code>null</code>. | |
553 | */ | |
554 | 6 | public static String contains(String substring) { |
555 | 6 | reportMatcher(new Contains(substring)); |
556 | 6 | return null; |
557 | } | |
558 | ||
559 | /** | |
560 | * Expects a boolean that matches both given expectations. | |
561 | * | |
562 | * @param first | |
563 | * placeholder for the first expectation. | |
564 | * @param second | |
565 | * placeholder for the second expectation. | |
566 | * @return <code>false</code>. | |
567 | */ | |
568 | 1 | public static boolean and(boolean first, boolean second) { |
569 | 1 | LastControl.reportAnd(2); |
570 | 1 | return false; |
571 | } | |
572 | ||
573 | /** | |
574 | * Expects a byte that matches both given expectations. | |
575 | * | |
576 | * @param first | |
577 | * placeholder for the first expectation. | |
578 | * @param second | |
579 | * placeholder for the second expectation. | |
580 | * @return <code>0</code>. | |
581 | */ | |
582 | 1 | public static byte and(byte first, byte second) { |
583 | 1 | LastControl.reportAnd(2); |
584 | 1 | return 0; |
585 | } | |
586 | ||
587 | /** | |
588 | * Expects a char that matches both given expectations. | |
589 | * | |
590 | * @param first | |
591 | * placeholder for the first expectation. | |
592 | * @param second | |
593 | * placeholder for the second expectation. | |
594 | * @return <code>0</code>. | |
595 | */ | |
596 | 1 | public static char and(char first, char second) { |
597 | 1 | LastControl.reportAnd(2); |
598 | 1 | return 0; |
599 | } | |
600 | ||
601 | /** | |
602 | * Expects a double that matches both given expectations. | |
603 | * | |
604 | * @param first | |
605 | * placeholder for the first expectation. | |
606 | * @param second | |
607 | * placeholder for the second expectation. | |
608 | * @return <code>0</code>. | |
609 | */ | |
610 | 1 | public static double and(double first, double second) { |
611 | 1 | LastControl.reportAnd(2); |
612 | 1 | return 0; |
613 | } | |
614 | ||
615 | /** | |
616 | * Expects a float that matches both given expectations. | |
617 | * | |
618 | * @param first | |
619 | * placeholder for the first expectation. | |
620 | * @param second | |
621 | * placeholder for the second expectation. | |
622 | * @return <code>0</code>. | |
623 | */ | |
624 | 1 | public static float and(float first, float second) { |
625 | 1 | LastControl.reportAnd(2); |
626 | 1 | return 0; |
627 | } | |
628 | ||
629 | /** | |
630 | * Expects an int that matches both given expectations. | |
631 | * | |
632 | * @param first | |
633 | * placeholder for the first expectation. | |
634 | * @param second | |
635 | * placeholder for the second expectation. | |
636 | * @return <code>0</code>. | |
637 | */ | |
638 | 2 | public static int and(int first, int second) { |
639 | 2 | LastControl.reportAnd(2); |
640 | 2 | return 0; |
641 | } | |
642 | ||
643 | /** | |
644 | * Expects a long that matches both given expectations. | |
645 | * | |
646 | * @param first | |
647 | * placeholder for the first expectation. | |
648 | * @param second | |
649 | * placeholder for the second expectation. | |
650 | * @return <code>0</code>. | |
651 | */ | |
652 | 1 | public static long and(long first, long second) { |
653 | 1 | LastControl.reportAnd(2); |
654 | 1 | return 0; |
655 | } | |
656 | ||
657 | /** | |
658 | * Expects a short that matches both given expectations. | |
659 | * | |
660 | * @param first | |
661 | * placeholder for the first expectation. | |
662 | * @param second | |
663 | * placeholder for the second expectation. | |
664 | * @return <code>0</code>. | |
665 | */ | |
666 | 1 | public static short and(short first, short second) { |
667 | 1 | LastControl.reportAnd(2); |
668 | 1 | return 0; |
669 | } | |
670 | ||
671 | /** | |
672 | * Expects an Object that matches both given expectations. | |
673 | * | |
674 | * @param <T> | |
675 | * the type of the object, it is passed through to prevent casts. | |
676 | * @param first | |
677 | * placeholder for the first expectation. | |
678 | * @param second | |
679 | * placeholder for the second expectation. | |
680 | * @return <code>null</code>. | |
681 | */ | |
682 | 2 | public static <T> T and(T first, T second) { |
683 | 2 | LastControl.reportAnd(2); |
684 | 2 | return null; |
685 | } | |
686 | ||
687 | /** | |
688 | * Expects a boolean that matches one of the given expectations. | |
689 | * | |
690 | * @param first | |
691 | * placeholder for the first expectation. | |
692 | * @param second | |
693 | * placeholder for the second expectation. | |
694 | * @return <code>false</code>. | |
695 | */ | |
696 | 1 | public static boolean or(boolean first, boolean second) { |
697 | 1 | LastControl.reportOr(2); |
698 | 1 | return false; |
699 | } | |
700 | ||
701 | /** | |
702 | * Expects a byte that matches one of the given expectations. | |
703 | * | |
704 | * @param first | |
705 | * placeholder for the first expectation. | |
706 | * @param second | |
707 | * placeholder for the second expectation. | |
708 | * @return <code>0</code>. | |
709 | */ | |
710 | 1 | public static byte or(byte first, byte second) { |
711 | 1 | LastControl.reportOr(2); |
712 | 1 | return 0; |
713 | } | |
714 | ||
715 | /** | |
716 | * Expects a char that matches one of the given expectations. | |
717 | * | |
718 | * @param first | |
719 | * placeholder for the first expectation. | |
720 | * @param second | |
721 | * placeholder for the second expectation. | |
722 | * @return <code>0</code>. | |
723 | */ | |
724 | 1 | public static char or(char first, char second) { |
725 | 1 | LastControl.reportOr(2); |
726 | 1 | return 0; |
727 | } | |
728 | ||
729 | /** | |
730 | * Expects a double that matches one of the given expectations. | |
731 | * | |
732 | * @param first | |
733 | * placeholder for the first expectation. | |
734 | * @param second | |
735 | * placeholder for the second expectation. | |
736 | * @return <code>0</code>. | |
737 | */ | |
738 | 1 | public static double or(double first, double second) { |
739 | 1 | LastControl.reportOr(2); |
740 | 1 | return 0; |
741 | } | |
742 | ||
743 | /** | |
744 | * Expects a float that matches one of the given expectations. | |
745 | * | |
746 | * @param first | |
747 | * placeholder for the first expectation. | |
748 | * @param second | |
749 | * placeholder for the second expectation. | |
750 | * @return <code>0</code>. | |
751 | */ | |
752 | 1 | public static float or(float first, float second) { |
753 | 1 | LastControl.reportOr(2); |
754 | 1 | return 0; |
755 | } | |
756 | ||
757 | /** | |
758 | * Expects an int that matches one of the given expectations. | |
759 | * | |
760 | * @param first | |
761 | * placeholder for the first expectation. | |
762 | * @param second | |
763 | * placeholder for the second expectation. | |
764 | * @return <code>0</code>. | |
765 | */ | |
766 | 2 | public static int or(int first, int second) { |
767 | 2 | LastControl.reportOr(2); |
768 | 2 | return first; |
769 | } | |
770 | ||
771 | /** | |
772 | * Expects a long that matches one of the given expectations. | |
773 | * | |
774 | * @param first | |
775 | * placeholder for the first expectation. | |
776 | * @param second | |
777 | * placeholder for the second expectation. | |
778 | * @return <code>0</code>. | |
779 | */ | |
780 | 1 | public static long or(long first, long second) { |
781 | 1 | LastControl.reportOr(2); |
782 | 1 | return 0; |
783 | } | |
784 | ||
785 | /** | |
786 | * Expects a short that matches one of the given expectations. | |
787 | * | |
788 | * @param first | |
789 | * placeholder for the first expectation. | |
790 | * @param second | |
791 | * placeholder for the second expectation. | |
792 | * @return <code>0</code>. | |
793 | */ | |
794 | 1 | public static short or(short first, short second) { |
795 | 1 | LastControl.reportOr(2); |
796 | 1 | return 0; |
797 | } | |
798 | ||
799 | /** | |
800 | * Expects an Object that matches one of the given expectations. | |
801 | * | |
802 | * @param <T> | |
803 | * the type of the object, it is passed through to prevent casts. | |
804 | * @param first | |
805 | * placeholder for the first expectation. | |
806 | * @param second | |
807 | * placeholder for the second expectation. | |
808 | * @return <code>null</code>. | |
809 | */ | |
810 | 3 | public static <T> T or(T first, T second) { |
811 | 3 | LastControl.reportOr(2); |
812 | 2 | return null; |
813 | } | |
814 | ||
815 | /** | |
816 | * Expects a boolean that does not match the given expectation. | |
817 | * | |
818 | * @param first | |
819 | * placeholder for the expectation. | |
820 | * @return <code>false</code>. | |
821 | */ | |
822 | 1 | public static boolean not(boolean first) { |
823 | 1 | LastControl.reportNot(); |
824 | 1 | return false; |
825 | } | |
826 | ||
827 | /** | |
828 | * Expects a byte that does not match the given expectation. | |
829 | * | |
830 | * @param first | |
831 | * placeholder for the expectation. | |
832 | * @return <code>0</code>. | |
833 | */ | |
834 | 1 | public static byte not(byte first) { |
835 | 1 | LastControl.reportNot(); |
836 | 1 | return 0; |
837 | } | |
838 | ||
839 | /** | |
840 | * Expects a char that does not match the given expectation. | |
841 | * | |
842 | * @param first | |
843 | * placeholder for the expectation. | |
844 | * @return <code>0</code>. | |
845 | */ | |
846 | 1 | public static char not(char first) { |
847 | 1 | LastControl.reportNot(); |
848 | 1 | return 0; |
849 | } | |
850 | ||
851 | /** | |
852 | * Expects a double that does not match the given expectation. | |
853 | * | |
854 | * @param first | |
855 | * placeholder for the expectation. | |
856 | * @return <code>0</code>. | |
857 | */ | |
858 | 1 | public static double not(double first) { |
859 | 1 | LastControl.reportNot(); |
860 | 1 | return 0; |
861 | } | |
862 | ||
863 | /** | |
864 | * Expects a float that does not match the given expectation. | |
865 | * | |
866 | * @param first | |
867 | * placeholder for the expectation. | |
868 | * @return <code>0</code>. | |
869 | */ | |
870 | 1 | public static float not(float first) { |
871 | 1 | LastControl.reportNot(); |
872 | 1 | return first; |
873 | } | |
874 | ||
875 | /** | |
876 | * Expects an int that does not match the given expectation. | |
877 | * | |
878 | * @param first | |
879 | * placeholder for the expectation. | |
880 | * @return <code>0</code>. | |
881 | */ | |
882 | 1 | public static int not(int first) { |
883 | 1 | LastControl.reportNot(); |
884 | 1 | return 0; |
885 | } | |
886 | ||
887 | /** | |
888 | * Expects a long that does not match the given expectation. | |
889 | * | |
890 | * @param first | |
891 | * placeholder for the expectation. | |
892 | * @return <code>0</code>. | |
893 | */ | |
894 | 1 | public static long not(long first) { |
895 | 1 | LastControl.reportNot(); |
896 | 1 | return 0; |
897 | } | |
898 | ||
899 | /** | |
900 | * Expects a short that does not match the given expectation. | |
901 | * | |
902 | * @param first | |
903 | * placeholder for the expectation. | |
904 | * @return <code>0</code>. | |
905 | */ | |
906 | 1 | public static short not(short first) { |
907 | 1 | LastControl.reportNot(); |
908 | 1 | return 0; |
909 | } | |
910 | ||
911 | /** | |
912 | * Expects an Object that does not match the given expectation. | |
913 | * | |
914 | * @param <T> | |
915 | * the type of the object, it is passed through to prevent casts. | |
916 | * @param first | |
917 | * placeholder for the expectation. | |
918 | * @return <code>null</code>. | |
919 | */ | |
920 | 6 | public static <T> T not(T first) { |
921 | 6 | LastControl.reportNot(); |
922 | 5 | return null; |
923 | } | |
924 | ||
925 | /** | |
926 | * Expects a boolean that is equal to the given value. | |
927 | * | |
928 | * @param value | |
929 | * the given value. | |
930 | * @return <code>0</code>. | |
931 | */ | |
932 | 5 | public static boolean eq(boolean value) { |
933 | 5 | reportMatcher(new Equals(value)); |
934 | 5 | return false; |
935 | } | |
936 | ||
937 | /** | |
938 | * Expects a byte that is equal to the given value. | |
939 | * | |
940 | * @param value | |
941 | * the given value. | |
942 | * @return <code>0</code>. | |
943 | */ | |
944 | 5 | public static byte eq(byte value) { |
945 | 5 | reportMatcher(new Equals(value)); |
946 | 5 | return 0; |
947 | } | |
948 | ||
949 | /** | |
950 | * Expects a char that is equal to the given value. | |
951 | * | |
952 | * @param value | |
953 | * the given value. | |
954 | * @return <code>0</code>. | |
955 | */ | |
956 | 5 | public static char eq(char value) { |
957 | 5 | reportMatcher(new Equals(value)); |
958 | 5 | return 0; |
959 | } | |
960 | ||
961 | /** | |
962 | * Expects a double that is equal to the given value. | |
963 | * | |
964 | * @param value | |
965 | * the given value. | |
966 | * @return <code>0</code>. | |
967 | */ | |
968 | 5 | public static double eq(double value) { |
969 | 5 | reportMatcher(new Equals(value)); |
970 | 5 | return 0; |
971 | } | |
972 | ||
973 | /** | |
974 | * Expects a float that is equal to the given value. | |
975 | * | |
976 | * @param value | |
977 | * the given value. | |
978 | * @return <code>0</code>. | |
979 | */ | |
980 | 5 | public static float eq(float value) { |
981 | 5 | reportMatcher(new Equals(value)); |
982 | 5 | return 0; |
983 | } | |
984 | ||
985 | /** | |
986 | * Expects an int that is equal to the given value. | |
987 | * | |
988 | * @param value | |
989 | * the given value. | |
990 | * @return <code>0</code>. | |
991 | */ | |
992 | 11 | public static int eq(int value) { |
993 | 11 | reportMatcher(new Equals(value)); |
994 | 11 | return 0; |
995 | } | |
996 | ||
997 | /** | |
998 | * Expects a long that is equal to the given value. | |
999 | * | |
1000 | * @param value | |
1001 | * the given value. | |
1002 | * @return <code>0</code>. | |
1003 | */ | |
1004 | 5 | public static long eq(long value) { |
1005 | 5 | reportMatcher(new Equals(value)); |
1006 | 5 | return 0; |
1007 | } | |
1008 | ||
1009 | /** | |
1010 | * Expects a short that is equal to the given value. | |
1011 | * | |
1012 | * @param value | |
1013 | * the given value. | |
1014 | * @return <code>0</code>. | |
1015 | */ | |
1016 | 5 | public static short eq(short value) { |
1017 | 5 | reportMatcher(new Equals(value)); |
1018 | 5 | return 0; |
1019 | } | |
1020 | ||
1021 | /** | |
1022 | * Expects an Object that is equal to the given value. | |
1023 | * | |
1024 | * @param value | |
1025 | * the given value. | |
1026 | * @return <code>null</code>. | |
1027 | */ | |
1028 | 12 | public static <T> T eq(T value) { |
1029 | 12 | reportMatcher(new Equals(value)); |
1030 | 12 | return null; |
1031 | } | |
1032 | ||
1033 | /** | |
1034 | * Expects a boolean array that is equal to the given array, i.e. it has to | |
1035 | * have the same length, and each element has to be equal. | |
1036 | * | |
1037 | * @param value | |
1038 | * the given arry. | |
1039 | * @return <code>null</code>. | |
1040 | */ | |
1041 | 1 | public static boolean[] aryEq(boolean[] value) { |
1042 | 1 | reportMatcher(new ArrayEquals(value)); |
1043 | 1 | return null; |
1044 | } | |
1045 | ||
1046 | /** | |
1047 | * Expects a byte array that is equal to the given array, i.e. it has to | |
1048 | * have the same length, and each element has to be equal. | |
1049 | * | |
1050 | * @param value | |
1051 | * the given arry. | |
1052 | * @return <code>null</code>. | |
1053 | */ | |
1054 | 1 | public static byte[] aryEq(byte[] value) { |
1055 | 1 | reportMatcher(new ArrayEquals(value)); |
1056 | 1 | return null; |
1057 | } | |
1058 | ||
1059 | /** | |
1060 | * Expects a char array that is equal to the given array, i.e. it has to | |
1061 | * have the same length, and each element has to be equal. | |
1062 | * | |
1063 | * @param value | |
1064 | * the given arry. | |
1065 | * @return <code>null</code>. | |
1066 | */ | |
1067 | 1 | public static char[] aryEq(char[] value) { |
1068 | 1 | reportMatcher(new ArrayEquals(value)); |
1069 | 1 | return null; |
1070 | } | |
1071 | ||
1072 | /** | |
1073 | * Expects a double array that is equal to the given array, i.e. it has to | |
1074 | * have the same length, and each element has to be equal. | |
1075 | * | |
1076 | * @param value | |
1077 | * the given arry. | |
1078 | * @return <code>null</code>. | |
1079 | */ | |
1080 | 1 | public static double[] aryEq(double[] value) { |
1081 | 1 | reportMatcher(new ArrayEquals(value)); |
1082 | 1 | return null; |
1083 | } | |
1084 | ||
1085 | /** | |
1086 | * Expects a float array that is equal to the given array, i.e. it has to | |
1087 | * have the same length, and each element has to be equal. | |
1088 | * | |
1089 | * @param value | |
1090 | * the given arry. | |
1091 | * @return <code>null</code>. | |
1092 | */ | |
1093 | 1 | public static float[] aryEq(float[] value) { |
1094 | 1 | reportMatcher(new ArrayEquals(value)); |
1095 | 1 | return null; |
1096 | } | |
1097 | ||
1098 | /** | |
1099 | * Expects an int array that is equal to the given array, i.e. it has to | |
1100 | * have the same length, and each element has to be equal. | |
1101 | * | |
1102 | * @param value | |
1103 | * the given arry. | |
1104 | * @return <code>null</code>. | |
1105 | */ | |
1106 | 1 | public static int[] aryEq(int[] value) { |
1107 | 1 | reportMatcher(new ArrayEquals(value)); |
1108 | 1 | return null; |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * Expects a long array that is equal to the given array, i.e. it has to | |
1113 | * have the same length, and each element has to be equal. | |
1114 | * | |
1115 | * @param value | |
1116 | * the given arry. | |
1117 | * @return <code>null</code>. | |
1118 | */ | |
1119 | 1 | public static long[] aryEq(long[] value) { |
1120 | 1 | reportMatcher(new ArrayEquals(value)); |
1121 | 1 | return null; |
1122 | } | |
1123 | ||
1124 | /** | |
1125 | * Expects a short array that is equal to the given array, i.e. it has to | |
1126 | * have the same length, and each element has to be equal. | |
1127 | * | |
1128 | * @param value | |
1129 | * the given arry. | |
1130 | * @return <code>null</code>. | |
1131 | */ | |
1132 | 1 | public static short[] aryEq(short[] value) { |
1133 | 1 | reportMatcher(new ArrayEquals(value)); |
1134 | 1 | return null; |
1135 | } | |
1136 | ||
1137 | /** | |
1138 | * Expects an Object array that is equal to the given array, i.e. it has to | |
1139 | * have the same type, length, and each element has to be equal. | |
1140 | * | |
1141 | * @param <T> | |
1142 | * the type of the array, it is passed through to prevent casts. | |
1143 | * @param value | |
1144 | * the given arry. | |
1145 | * @return <code>null</code>. | |
1146 | */ | |
1147 | 2 | public static <T> T[] aryEq(T[] value) { |
1148 | 2 | reportMatcher(new ArrayEquals(value)); |
1149 | 2 | return null; |
1150 | } | |
1151 | ||
1152 | /** | |
1153 | * Expects null. | |
1154 | * | |
1155 | * @return <code>null</code>. | |
1156 | */ | |
1157 | 3 | public static Object isNull() { |
1158 | 3 | reportMatcher(Null.NULL); |
1159 | 3 | return null; |
1160 | } | |
1161 | ||
1162 | /** | |
1163 | * Expects not null. | |
1164 | * | |
1165 | * @return <code>null</code>. | |
1166 | */ | |
1167 | 5 | public static Object notNull() { |
1168 | 5 | reportMatcher(NotNull.NOT_NULL); |
1169 | 5 | return null; |
1170 | } | |
1171 | ||
1172 | /** | |
1173 | * Expects a string that contains a substring that matches the given regular | |
1174 | * expression. For details, see the EasyMock documentation. | |
1175 | * | |
1176 | * @param regex | |
1177 | * the regular expression. | |
1178 | * @return <code>null</code>. | |
1179 | */ | |
1180 | 2 | public static String find(String regex) { |
1181 | 2 | reportMatcher(new Find(regex)); |
1182 | 2 | return null; |
1183 | } | |
1184 | ||
1185 | /** | |
1186 | * Expects a string that matches the given regular expression. For details, | |
1187 | * see the EasyMock documentation. | |
1188 | * | |
1189 | * @param regex | |
1190 | * the regular expression. | |
1191 | * @return <code>null</code>. | |
1192 | */ | |
1193 | 2 | public static String matches(String regex) { |
1194 | 2 | reportMatcher(new Matches(regex)); |
1195 | 2 | return null; |
1196 | } | |
1197 | ||
1198 | /** | |
1199 | * Expects a string that starts with the given prefix. For details, see the | |
1200 | * EasyMock documentation. | |
1201 | * | |
1202 | * @param prefix | |
1203 | * the prefix. | |
1204 | * @return <code>null</code>. | |
1205 | */ | |
1206 | 2 | public static String startsWith(String prefix) { |
1207 | 2 | reportMatcher(new StartsWith(prefix)); |
1208 | 2 | return null; |
1209 | } | |
1210 | ||
1211 | /** | |
1212 | * Expects a string that ends with the given suffix. For details, see the | |
1213 | * EasyMock documentation. | |
1214 | * | |
1215 | * @param suffix | |
1216 | * the suffix. | |
1217 | * @return <code>null</code>. | |
1218 | */ | |
1219 | 2 | public static String endsWith(String suffix) { |
1220 | 2 | reportMatcher(new EndsWith(suffix)); |
1221 | 2 | return null; |
1222 | } | |
1223 | ||
1224 | /** | |
1225 | * Expects a double that has an absolute difference to the given value that | |
1226 | * is less than the given delta. For details, see the EasyMock | |
1227 | * documentation. | |
1228 | * | |
1229 | * @param value | |
1230 | * the given value. | |
1231 | * @param delta | |
1232 | * the given delta. | |
1233 | * @return <code>0</code>. | |
1234 | */ | |
1235 | 2 | public static double eq(double value, double delta) { |
1236 | 2 | reportMatcher(new EqualsWithDelta(value, delta)); |
1237 | 2 | return 0; |
1238 | } | |
1239 | ||
1240 | /** | |
1241 | * Expects a float that has an absolute difference to the given value that | |
1242 | * is less than the given delta. For details, see the EasyMock | |
1243 | * documentation. | |
1244 | * | |
1245 | * @param value | |
1246 | * the given value. | |
1247 | * @param delta | |
1248 | * the given delta. | |
1249 | * @return <code>0</code>. | |
1250 | */ | |
1251 | 3 | public static float eq(float value, float delta) { |
1252 | 3 | reportMatcher(new EqualsWithDelta(value, delta)); |
1253 | 3 | return 0; |
1254 | } | |
1255 | ||
1256 | /** | |
1257 | * Expects an Object that is the same as the given value. For details, see | |
1258 | * the EasyMock documentation. | |
1259 | * | |
1260 | * @param <T> | |
1261 | * the type of the object, it is passed through to prevent casts. | |
1262 | * @param value | |
1263 | * the given value. | |
1264 | * @return <code>null</code>. | |
1265 | */ | |
1266 | 2 | public static <T> T same(T value) { |
1267 | 2 | reportMatcher(new Same(value)); |
1268 | 2 | return null; |
1269 | } | |
1270 | ||
1271 | /** | |
1272 | * Switches the given mock objects (more exactly: the controls of the mock | |
1273 | * objects) to replay mode. For details, see the EasyMock documentation. | |
1274 | * | |
1275 | * @param mocks | |
1276 | * the mock objects. | |
1277 | */ | |
1278 | 57 | public static void replay(Object... mocks) { |
1279 | 57 | for (Object mock : mocks) { |
1280 | 57 | getControl(mock).replay(); |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /** | |
1285 | * Resets the given mock objects (more exactly: the controls of the mock | |
1286 | * objects). For details, see the EasyMock documentation. | |
1287 | * | |
1288 | * @param mocks | |
1289 | * the mock objects. | |
1290 | */ | |
1291 | 3 | public static void reset(Object... mocks) { |
1292 | 3 | for (Object mock : mocks) { |
1293 | 3 | getControl(mock).reset(); |
1294 | } | |
1295 | } | |
1296 | ||
1297 | /** | |
1298 | * Verifies the given mock objects (more exactly: the controls of the mock | |
1299 | * objects). | |
1300 | * | |
1301 | * @param mocks | |
1302 | * the mock objects. | |
1303 | */ | |
1304 | 42 | public static void verify(Object... mocks) { |
1305 | 42 | for (Object mock : mocks) { |
1306 | 42 | getControl(mock).verify(); |
1307 | } | |
1308 | } | |
1309 | ||
1310 | /** | |
1311 | * Switches order checking of the given mock object (more exactly: the | |
1312 | * control of the mock object) the on and off. For details, see the EasyMock | |
1313 | * documentation. | |
1314 | * | |
1315 | * @param mock | |
1316 | * the mock object. | |
1317 | * @param state | |
1318 | * <code>true</code> switches order checking on, | |
1319 | * <code>false</code> switches it off. | |
1320 | */ | |
1321 | 2 | public static void checkOrder(Object mock, boolean state) { |
1322 | 2 | getControl(mock).checkOrder(state); |
1323 | } | |
1324 | ||
1325 | /** | |
1326 | * Reports an argument matcher. This method is needed to define own argument | |
1327 | * matchers. For details, see the EasyMock documentation. For | |
1328 | * | |
1329 | * @param matcher | |
1330 | */ | |
1331 | 147 | public static void reportMatcher(IArgumentMatcher matcher) { |
1332 | 147 | LastControl.reportMatcher(matcher); |
1333 | } | |
1334 | ||
1335 | 104 | private static MocksControl getControl(Object mock) { |
1336 | 104 | ObjectMethodsFilter handler = (ObjectMethodsFilter) Proxy |
1337 | .getInvocationHandler(mock); | |
1338 | 104 | return handler.getDelegate().getControl(); |
1339 | } | |
1340 | ||
1341 | /** | |
1342 | * Returns the arguments of the current mock method call, if inside an | |
1343 | * <code>IAnswer</code> callback - be careful here, reordering parameters of | |
1344 | * method changes the semantics of your tests. | |
1345 | * | |
1346 | * @return the arguments of the current mock method call. | |
1347 | * @throws IllegalStateException | |
1348 | * if called outside of <code>IAnswer</code> callbacks. | |
1349 | */ | |
1350 | 11 | public static Object[] getCurrentArguments() { |
1351 | 11 | Object[] result = LastControl.getCurrentArguments(); |
1352 | 11 | if (result == null) { |
1353 | 1 | throw new IllegalStateException( |
1354 | "current arguments are only available when executing callback methods"); | |
1355 | } | |
1356 | 10 | return result; |
1357 | } | |
1358 | } |
|