|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package org.easymock.internal; |
|
6 |
| |
|
7 |
| import java.lang.reflect.Method; |
|
8 |
| import java.util.ArrayList; |
|
9 |
| import java.util.List; |
|
10 |
| |
|
11 |
| import junit.framework.AssertionFailedError; |
|
12 |
| |
|
13 |
| import org.easymock.ArgumentsMatcher; |
|
14 |
| |
|
15 |
| public class ResultListMap { |
|
16 |
| |
|
17 |
| private final List<ArgumentsEntry> results = new ArrayList<ArgumentsEntry>(); |
|
18 |
| |
|
19 |
| private final ArgumentsMatcher matcher; |
|
20 |
| |
|
21 |
| private final Method method; |
|
22 |
| |
|
23 |
| private static class ArgumentsEntry { |
|
24 |
| ExpectedMethodCall matchableArguments; |
|
25 |
| |
|
26 |
| ResultList resultList; |
|
27 |
| |
|
28 |
130
| ArgumentsEntry(ExpectedMethodCall key, ResultList value) {
|
|
29 |
130
| this.matchableArguments = key;
|
|
30 |
130
| this.resultList = value;
|
|
31 |
| } |
|
32 |
| |
|
33 |
484
| public ExpectedMethodCall getMatchableArguments() {
|
|
34 |
484
| return matchableArguments;
|
|
35 |
| } |
|
36 |
| |
|
37 |
382
| public ResultList getResultList() {
|
|
38 |
382
| return resultList;
|
|
39 |
| } |
|
40 |
| } |
|
41 |
| |
|
42 |
126
| public ResultListMap(Method method, ArgumentsMatcher matcher) {
|
|
43 |
126
| this.method = method;
|
|
44 |
126
| this.matcher = matcher;
|
|
45 |
| } |
|
46 |
| |
|
47 |
154
| public void addExpected(Object[] expected, Result result, Range count) {
|
|
48 |
154
| ExpectedMethodCall matchableArguments = new ExpectedMethodCall(method,
|
|
49 |
| expected, matcher); |
|
50 |
| |
|
51 |
154
| for (ArgumentsEntry entry : results) {
|
|
52 |
58
| if (entry.getMatchableArguments().equals(matchableArguments)) {
|
|
53 |
24
| entry.getResultList().add(result, count);
|
|
54 |
22
| return;
|
|
55 |
| } |
|
56 |
| } |
|
57 |
130
| ResultList list = new ResultList();
|
|
58 |
130
| list.add(result, count);
|
|
59 |
130
| results.add(new ArgumentsEntry(matchableArguments, list));
|
|
60 |
| } |
|
61 |
| |
|
62 |
259
| public Result addActual(Object[] arguments) {
|
|
63 |
259
| boolean matched = false;
|
|
64 |
| |
|
65 |
259
| for (ArgumentsEntry entry : results) {
|
|
66 |
262
| ExpectedMethodCall matchableArguments = entry
|
|
67 |
| .getMatchableArguments(); |
|
68 |
| |
|
69 |
262
| if (matchableArguments.matches(method, arguments)) {
|
|
70 |
194
| matched = true;
|
|
71 |
194
| Result result = entry.getResultList().next();
|
|
72 |
194
| if (result != null) {
|
|
73 |
184
| return result;
|
|
74 |
| } |
|
75 |
| } |
|
76 |
| } |
|
77 |
| |
|
78 |
75
| throw new AssertionFailedErrorWrapper(new AssertionFailedError(
|
|
79 |
| createFailureMessage(arguments, matched))); |
|
80 |
| } |
|
81 |
| |
|
82 |
75
| private String createFailureMessage(Object[] actual, boolean matched) {
|
|
83 |
75
| StringBuffer result = new StringBuffer();
|
|
84 |
| |
|
85 |
75
| if (!matched) {
|
|
86 |
67
| result.append("\n ");
|
|
87 |
67
| result.append(new MethodCall(method, actual).toString(matcher));
|
|
88 |
67
| result.append(": ");
|
|
89 |
67
| result.append(new Range(7).expectedAndActual(1).replace('7', '0'));
|
|
90 |
| } |
|
91 |
| |
|
92 |
75
| for (ArgumentsEntry entry : results) {
|
|
93 |
54
| ExpectedMethodCall matchableArguments = entry
|
|
94 |
| .getMatchableArguments(); |
|
95 |
54
| ResultList list = (ResultList) entry.getResultList();
|
|
96 |
| |
|
97 |
54
| if (list.hasValidCallCount()
|
|
98 |
| && !matchableArguments.matches(method, actual)) { |
|
99 |
34
| continue;
|
|
100 |
| } |
|
101 |
| |
|
102 |
20
| int count = list.getCallCount();
|
|
103 |
| |
|
104 |
20
| if (matched && matchableArguments.matches(method, actual)) {
|
|
105 |
8
| count += 1;
|
|
106 |
8
| matched = false;
|
|
107 |
| } |
|
108 |
20
| result.append("\n ");
|
|
109 |
20
| result.append(matchableArguments.toString());
|
|
110 |
20
| result.append(": ");
|
|
111 |
20
| result.append(list.getMessage(count));
|
|
112 |
| } |
|
113 |
| |
|
114 |
75
| return result.toString();
|
|
115 |
| } |
|
116 |
| |
|
117 |
115
| public void verify() {
|
|
118 |
115
| String failureMessage = "";
|
|
119 |
115
| boolean verifyFailed = false;
|
|
120 |
| |
|
121 |
115
| for (ArgumentsEntry entry : results) {
|
|
122 |
110
| ExpectedMethodCall matchableArguments = entry
|
|
123 |
| .getMatchableArguments(); |
|
124 |
110
| ResultList list = (ResultList) entry.getResultList();
|
|
125 |
| |
|
126 |
110
| if (list.hasValidCallCount()) {
|
|
127 |
100
| continue;
|
|
128 |
| } |
|
129 |
10
| verifyFailed = true;
|
|
130 |
10
| failureMessage += "\n " + matchableArguments.toString() + ": "
|
|
131 |
| + list.getMessage(); |
|
132 |
| } |
|
133 |
115
| if (verifyFailed) {
|
|
134 |
9
| throw new AssertionFailedError(failureMessage);
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| } |