|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package org.easymock.internal; |
|
6 |
| |
|
7 |
| import java.util.ArrayList; |
|
8 |
| import java.util.List; |
|
9 |
| |
|
10 |
| public class UnorderedBehavior { |
|
11 |
| |
|
12 |
| private final List<ExpectedInvocationAndResults> results = new ArrayList<ExpectedInvocationAndResults>(); |
|
13 |
| |
|
14 |
| private final boolean checkOrder; |
|
15 |
| |
|
16 |
228
| public UnorderedBehavior(boolean checkOrder) {
|
|
17 |
228
| this.checkOrder = checkOrder;
|
|
18 |
| } |
|
19 |
| |
|
20 |
385
| public void addExpected(ExpectedInvocation expected, Result result,
|
|
21 |
| Range count) { |
|
22 |
385
| for (ExpectedInvocationAndResults entry : results) {
|
|
23 |
556
| if (entry.getExpectedInvocation().equals(expected)) {
|
|
24 |
39
| entry.getResults().add(result, count);
|
|
25 |
37
| return;
|
|
26 |
| } |
|
27 |
| } |
|
28 |
346
| Results list = new Results();
|
|
29 |
346
| list.add(result, count);
|
|
30 |
346
| results.add(new ExpectedInvocationAndResults(expected, list));
|
|
31 |
| } |
|
32 |
| |
|
33 |
554
| public Result addActual(Invocation actual) {
|
|
34 |
554
| for (ExpectedInvocationAndResults entry : results) {
|
|
35 |
1144
| if (!entry.getExpectedInvocation().matches(actual)) {
|
|
36 |
713
| continue;
|
|
37 |
| } |
|
38 |
431
| Result result = entry.getResults().next();
|
|
39 |
431
| if (result != null) {
|
|
40 |
400
| return result;
|
|
41 |
| } |
|
42 |
| } |
|
43 |
154
| return null;
|
|
44 |
| } |
|
45 |
| |
|
46 |
298
| public boolean verify() {
|
|
47 |
298
| for (ExpectedInvocationAndResults entry : results) {
|
|
48 |
451
| if (!entry.getResults().hasValidCallCount()) {
|
|
49 |
70
| return false;
|
|
50 |
| } |
|
51 |
| } |
|
52 |
228
| return true;
|
|
53 |
| } |
|
54 |
| |
|
55 |
145
| @Override
|
|
56 |
| public String toString() { |
|
57 |
145
| return toString(null);
|
|
58 |
| } |
|
59 |
| |
|
60 |
299
| public String toString(Invocation invocation) {
|
|
61 |
299
| StringBuffer result = new StringBuffer();
|
|
62 |
299
| for (ExpectedInvocationAndResults entry : results) {
|
|
63 |
490
| boolean unordered = !checkOrder;
|
|
64 |
490
| boolean validCallCount = entry.getResults().hasValidCallCount();
|
|
65 |
490
| boolean match = invocation != null
|
|
66 |
| && entry.getExpectedInvocation().matches(invocation); |
|
67 |
| |
|
68 |
490
| if (unordered && validCallCount && !match) {
|
|
69 |
286
| continue;
|
|
70 |
| } |
|
71 |
204
| result.append("\n " + entry.toString());
|
|
72 |
203
| if (match) {
|
|
73 |
30
| result.append(" (+1)");
|
|
74 |
| } |
|
75 |
| } |
|
76 |
298
| return result.toString();
|
|
77 |
| } |
|
78 |
| |
|
79 |
212
| public boolean allowsExpectedInvocation(ExpectedInvocation expected,
|
|
80 |
| boolean checkOrder) { |
|
81 |
212
| if (this.checkOrder != checkOrder) {
|
|
82 |
2
| return false;
|
|
83 |
210
| } else if (results.isEmpty() || !this.checkOrder) {
|
|
84 |
152
| return true;
|
|
85 |
| } else { |
|
86 |
58
| ExpectedInvocation lastMethodCall = results.get(results.size() - 1)
|
|
87 |
| .getExpectedInvocation(); |
|
88 |
58
| return lastMethodCall.equals(expected);
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
| } |