1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.lang.reflect.Method; |
8 |
| import java.util.HashMap; |
9 |
| import java.util.Map; |
10 |
| |
11 |
| import junit.framework.AssertionFailedError; |
12 |
| |
13 |
| public class UnorderedBehavior extends AbstractBehavior { |
14 |
| |
15 |
| private Map<Method, ResultListMap> methodBehaviors = new HashMap<Method, ResultListMap>(); |
16 |
| |
17 |
528
| private ResultListMap getMethodBehavior(Method method) {
|
18 |
528
| if (!methodBehaviors.containsKey(method)) {
|
19 |
126
| methodBehaviors.put(method, new ResultListMap(method,
|
20 |
| getMatcher(method))); |
21 |
| } |
22 |
528
| return methodBehaviors.get(method);
|
23 |
| } |
24 |
| |
25 |
154
| public void addExpected(MethodCall call, Result returnValue, Range range) {
|
26 |
154
| ResultListMap behaviors = getMethodBehavior(call.getMethod());
|
27 |
154
| behaviors.addExpected(call.getArguments(), returnValue, range);
|
28 |
| } |
29 |
| |
30 |
12
| protected Result returnValueForUnexpected(Method method) {
|
31 |
12
| return null;
|
32 |
| } |
33 |
| |
34 |
259
| public Result doAddActual(MethodCall methodCall) {
|
35 |
259
| ResultListMap behavior = getMethodBehavior(methodCall.getMethod());
|
36 |
259
| try {
|
37 |
259
| return behavior.addActual(methodCall.getArguments());
|
38 |
| } catch (AssertionFailedErrorWrapper e) { |
39 |
75
| Result defaultBehavior = getDefaultResult(methodCall.getMethod());
|
40 |
75
| if (defaultBehavior != null) {
|
41 |
54
| return defaultBehavior;
|
42 |
| } |
43 |
21
| Result niceBehavior = returnValueForUnexpected(methodCall
|
44 |
| .getMethod()); |
45 |
21
| if (niceBehavior != null) {
|
46 |
9
| return niceBehavior;
|
47 |
| } |
48 |
12
| throw e;
|
49 |
| } |
50 |
| |
51 |
| } |
52 |
| |
53 |
99
| public void doVerify() {
|
54 |
99
| String failureMessage = "";
|
55 |
99
| boolean verifyFailed = false;
|
56 |
| |
57 |
99
| for (Method method : methodBehaviors.keySet()) {
|
58 |
115
| try {
|
59 |
115
| getMethodBehavior(method).verify();
|
60 |
| } catch (AssertionFailedError e) { |
61 |
9
| verifyFailed = true;
|
62 |
9
| failureMessage += e.getMessage();
|
63 |
| } |
64 |
| } |
65 |
| |
66 |
99
| if (!verifyFailed)
|
67 |
90
| return;
|
68 |
9
| throw new AssertionFailedErrorWrapper(new AssertionFailedError(
|
69 |
| failureMessage)); |
70 |
| } |
71 |
| } |