1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.util.ArrayList; |
8 |
| import java.util.List; |
9 |
| |
10 |
| import junit.framework.AssertionFailedError; |
11 |
| |
12 |
| public class OrderedBehavior extends AbstractBehavior { |
13 |
| |
14 |
| private List<ExpectedMethodCall> methodCalls = new ArrayList<ExpectedMethodCall>(); |
15 |
| |
16 |
| private List<ResultList> resultLists = new ArrayList<ResultList>(); |
17 |
| |
18 |
| private int position = 0; |
19 |
| |
20 |
64
| public void addExpected(MethodCall methodCall, Result result, Range range) {
|
21 |
64
| ExpectedMethodCall matchableMethodCall = new ExpectedMethodCall(
|
22 |
| methodCall.getMethod(), methodCall.getArguments(), |
23 |
| getMatcher(methodCall.getMethod())); |
24 |
| |
25 |
64
| if (!lastMethodCallEqualTo(matchableMethodCall)) {
|
26 |
62
| methodCalls.add(matchableMethodCall);
|
27 |
62
| resultLists.add(new ResultList());
|
28 |
| } |
29 |
64
| ResultList resultList = (ResultList) resultLists
|
30 |
| .get(resultLists.size() - 1); |
31 |
64
| resultList.add(result, range);
|
32 |
| } |
33 |
| |
34 |
64
| private boolean lastMethodCallEqualTo(ExpectedMethodCall matchableMethodCall) {
|
35 |
64
| if (methodCalls.isEmpty()) {
|
36 |
30
| return false;
|
37 |
| } |
38 |
34
| ExpectedMethodCall lastMethodCall = methodCalls
|
39 |
| .get(methodCalls.size() - 1); |
40 |
34
| return lastMethodCall.equals(matchableMethodCall);
|
41 |
| } |
42 |
| |
43 |
61
| public Result doAddActual(MethodCall methodCall) {
|
44 |
61
| List<ExpectedMethodCall> matchedCalls = new ArrayList<ExpectedMethodCall>();
|
45 |
61
| List<ResultList> matchedResultLists = new ArrayList<ResultList>();
|
46 |
| |
47 |
61
| int tooManyCallsPosition = -1;
|
48 |
| |
49 |
61
| for (; position < methodCalls.size(); position++) {
|
50 |
84
| ExpectedMethodCall expectedCall = methodCalls.get(position);
|
51 |
84
| ResultList resultList = resultLists.get(position);
|
52 |
| |
53 |
84
| if (expectedCall.matches(methodCall)) {
|
54 |
55
| Result result = resultList.next();
|
55 |
55
| if (result != null) {
|
56 |
50
| return result;
|
57 |
| } |
58 |
| |
59 |
5
| matchedCalls.add(expectedCall);
|
60 |
5
| matchedResultLists.add(resultList);
|
61 |
5
| tooManyCallsPosition = matchedCalls.size() - 1;
|
62 |
5
| continue;
|
63 |
| } else { |
64 |
29
| if (resultList.hasValidCallCount()) {
|
65 |
20
| continue;
|
66 |
| } |
67 |
| |
68 |
9
| matchedCalls.add(expectedCall);
|
69 |
9
| matchedResultLists.add(resultList);
|
70 |
9
| break;
|
71 |
| } |
72 |
| } |
73 |
11
| Result defaultBehavior = getDefaultResult(methodCall.getMethod());
|
74 |
11
| if (defaultBehavior != null) {
|
75 |
3
| return defaultBehavior;
|
76 |
| } |
77 |
8
| throw new AssertionFailedErrorWrapper(new AssertionFailedError(
|
78 |
| createFailureMessage(methodCall, matchedCalls, |
79 |
| matchedResultLists, tooManyCallsPosition))); |
80 |
| } |
81 |
| |
82 |
8
| private String createFailureMessage(MethodCall methodCall,
|
83 |
| List<ExpectedMethodCall> matchedCalls, |
84 |
| List<ResultList> matchedResultLists, int tooManyCallsPosition) { |
85 |
8
| String failureMessage = "";
|
86 |
8
| if (tooManyCallsPosition == -1) {
|
87 |
5
| failureMessage += "\n " + methodCall.toString(getMatcher(methodCall.getMethod())) + ": "
|
88 |
| + new Range(2).expectedAndActual(1).replace('2', '0'); |
89 |
| } |
90 |
7
| for (int i = 0; i < matchedCalls.size(); i++) {
|
91 |
9
| ExpectedMethodCall call = matchedCalls.get(i);
|
92 |
9
| ResultList list = matchedResultLists.get(i);
|
93 |
9
| failureMessage += "\n " + call.toString() + ": ";
|
94 |
9
| int count = list.getCallCount()
|
95 |
9
| + (i == tooManyCallsPosition ? 1 : 0);
|
96 |
9
| failureMessage += list.getMessage(count);
|
97 |
| } |
98 |
7
| return failureMessage;
|
99 |
| } |
100 |
| |
101 |
20
| public void doVerify() {
|
102 |
20
| String failureMessage = "";
|
103 |
20
| boolean verifyFailed = false;
|
104 |
| |
105 |
20
| for (int i = position; i < methodCalls.size(); i++) {
|
106 |
23
| ExpectedMethodCall call = methodCalls.get(i);
|
107 |
23
| ResultList list = resultLists.get(i);
|
108 |
| |
109 |
23
| failureMessage += "\n " + call.toString() + ": "
|
110 |
| + list.getMessage(); |
111 |
| |
112 |
22
| if (list.hasValidCallCount()) {
|
113 |
18
| continue;
|
114 |
| } |
115 |
4
| verifyFailed = true;
|
116 |
| } |
117 |
19
| if (verifyFailed) {
|
118 |
3
| throw new AssertionFailedErrorWrapper(new AssertionFailedError(
|
119 |
| failureMessage)); |
120 |
| } |
121 |
| } |
122 |
| } |