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.Iterator; |
10 |
| import java.util.List; |
11 |
| |
12 |
| import org.easymock.ArgumentsMatcher; |
13 |
| import org.easymock.IArgumentMatcher; |
14 |
| import org.easymock.internal.matchers.Equals; |
15 |
| |
16 |
| public class ExpectedInvocation { |
17 |
| |
18 |
| private final Invocation invocation; |
19 |
| |
20 |
| private final ArgumentsMatcher matcher; |
21 |
| |
22 |
| private final List<IArgumentMatcher> matchers; |
23 |
| |
24 |
436
| public ExpectedInvocation(Invocation invocation,
|
25 |
| List<IArgumentMatcher> matchers) { |
26 |
436
| this(invocation, matchers, null);
|
27 |
| } |
28 |
| |
29 |
475
| private ExpectedInvocation(Invocation invocation,
|
30 |
| List<IArgumentMatcher> matchers, ArgumentsMatcher matcher) { |
31 |
475
| this.invocation = invocation;
|
32 |
475
| this.matcher = matcher;
|
33 |
475
| this.matchers = (matcher == null) ? createMissingMatchers(invocation,
|
34 |
| matchers) : null; |
35 |
| } |
36 |
| |
37 |
437
| private List<IArgumentMatcher> createMissingMatchers(Invocation invocation,
|
38 |
| List<IArgumentMatcher> matchers) { |
39 |
437
| if (matchers != null) {
|
40 |
113
| if (matchers.size() != invocation.getArguments().length) {
|
41 |
1
| throw new IllegalStateException(""
|
42 |
| + invocation.getArguments().length |
43 |
| + " matchers expected, " + matchers.size() |
44 |
| + " recorded."); |
45 |
| } |
46 |
| ; |
47 |
112
| return matchers;
|
48 |
| } |
49 |
324
| List<IArgumentMatcher> result = new ArrayList<IArgumentMatcher>();
|
50 |
324
| for (Object argument : invocation.getArguments()) {
|
51 |
350
| result.add(new Equals(argument));
|
52 |
| } |
53 |
324
| return result;
|
54 |
| } |
55 |
| |
56 |
616
| public boolean equals(Object o) {
|
57 |
616
| if (o == null || !this.getClass().equals(o.getClass()))
|
58 |
1
| return false;
|
59 |
| |
60 |
615
| ExpectedInvocation other = (ExpectedInvocation) o;
|
61 |
615
| return this.invocation.equals(other.invocation)
|
62 |
| && ((this.matcher == null && other.matcher == null) || (this.matcher != null && this.matcher |
63 |
| .equals(other.matcher))) |
64 |
| && ((this.matchers == null && other.matchers == null) || (this.matchers != null && this.matchers |
65 |
| .equals(other.matchers))); |
66 |
| } |
67 |
| |
68 |
1
| public int hashCode() {
|
69 |
1
| return invocation.hashCode();
|
70 |
| } |
71 |
| |
72 |
1526
| public boolean matches(Invocation actual) {
|
73 |
1526
| return matchers != null ? this.invocation.getMock().equals(
|
74 |
| actual.getMock()) |
75 |
| && this.invocation.getMethod().equals(actual.getMethod()) |
76 |
| && matches(actual.getArguments()) : this.invocation.matches( |
77 |
| actual, matcher); |
78 |
| } |
79 |
| |
80 |
766
| private boolean matches(Object[] arguments) {
|
81 |
766
| if (arguments.length != matchers.size()) {
|
82 |
20
| return false;
|
83 |
| } |
84 |
746
| for (int i = 0; i < arguments.length; i++) {
|
85 |
822
| if (!matchers.get(i).matches(arguments[i])) {
|
86 |
250
| return false;
|
87 |
| } |
88 |
| } |
89 |
496
| return true;
|
90 |
| } |
91 |
| |
92 |
208
| public String toString() {
|
93 |
208
| return matchers != null ? myToString() : invocation.toString(matcher);
|
94 |
| } |
95 |
| |
96 |
198
| private String myToString() {
|
97 |
198
| StringBuffer result = new StringBuffer();
|
98 |
198
| result.append(getMethod().getName());
|
99 |
198
| result.append("(");
|
100 |
198
| for (Iterator<IArgumentMatcher> it = matchers.iterator(); it.hasNext();) {
|
101 |
224
| it.next().appendTo(result);
|
102 |
221
| if (it.hasNext()) {
|
103 |
51
| result.append(", ");
|
104 |
| } |
105 |
| } |
106 |
195
| result.append(")");
|
107 |
195
| return result.toString();
|
108 |
| } |
109 |
| |
110 |
1317
| public Method getMethod() {
|
111 |
1317
| return invocation.getMethod();
|
112 |
| } |
113 |
| |
114 |
38
| public ExpectedInvocation withMatcher(ArgumentsMatcher matcher) {
|
115 |
38
| return new ExpectedInvocation(invocation, null, matcher);
|
116 |
| } |
117 |
| } |