1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.lang.reflect.Method; |
8 |
| |
9 |
| import org.easymock.ArgumentsMatcher; |
10 |
| import org.easymock.internal.matchers.ArrayEquals; |
11 |
| |
12 |
| public class Invocation { |
13 |
| |
14 |
| private final Object mock; |
15 |
| |
16 |
| private final Method method; |
17 |
| |
18 |
| private final Object[] arguments; |
19 |
| |
20 |
993
| public Invocation(Object mock, Method method, Object[] args) {
|
21 |
993
| this.mock = mock;
|
22 |
993
| this.method = method;
|
23 |
993
| this.arguments = expandVarArgs(method.isVarArgs(), args);
|
24 |
| } |
25 |
| |
26 |
993
| private static Object[] expandVarArgs(final boolean isVarArgs,
|
27 |
| final Object[] args) { |
28 |
993
| if (!isVarArgs || isVarArgs && args[args.length - 1] != null
|
29 |
| && !args[args.length - 1].getClass().isArray()) { |
30 |
951
| return args == null ? new Object[0] : args;
|
31 |
| } |
32 |
42
| Object[] varArgs = ArrayEquals.createObjectArray(args[args.length - 1]);
|
33 |
42
| final int nonVarArgsCount = args.length - 1;
|
34 |
42
| final int varArgsCount = varArgs.length;
|
35 |
42
| Object[] newArgs = new Object[nonVarArgsCount + varArgsCount];
|
36 |
42
| System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount);
|
37 |
42
| System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount);
|
38 |
42
| return newArgs;
|
39 |
| } |
40 |
| |
41 |
2838
| public Object getMock() {
|
42 |
2838
| return mock;
|
43 |
| } |
44 |
| |
45 |
4601
| public Method getMethod() {
|
46 |
4601
| return method;
|
47 |
| } |
48 |
| |
49 |
1714
| public Object[] getArguments() {
|
50 |
1714
| return arguments;
|
51 |
| } |
52 |
| |
53 |
619
| public boolean equals(Object o) {
|
54 |
619
| if (o == null || !o.getClass().equals(this.getClass()))
|
55 |
2
| return false;
|
56 |
| |
57 |
617
| Invocation other = (Invocation) o;
|
58 |
| |
59 |
617
| return this.mock.equals(other.mock) && this.method.equals(other.method)
|
60 |
| && this.equalArguments(other.arguments); |
61 |
| } |
62 |
| |
63 |
2
| public int hashCode() {
|
64 |
2
| throw new UnsupportedOperationException("hashCode() is not implemented");
|
65 |
| } |
66 |
| |
67 |
129
| private boolean equalArguments(Object[] arguments) {
|
68 |
129
| if (this.arguments.length != arguments.length) {
|
69 |
11
| return false;
|
70 |
| } |
71 |
118
| for (int i = 0; i < this.arguments.length; i++) {
|
72 |
122
| Object myArgument = this.arguments[i];
|
73 |
122
| Object otherArgument = arguments[i];
|
74 |
| |
75 |
122
| if (isPrimitiveParameter(i)) {
|
76 |
60
| if (!myArgument.equals(otherArgument)) {
|
77 |
24
| return false;
|
78 |
| } |
79 |
| } else { |
80 |
62
| if (myArgument != otherArgument) {
|
81 |
48
| return false;
|
82 |
| } |
83 |
| } |
84 |
| } |
85 |
46
| return true;
|
86 |
| } |
87 |
| |
88 |
122
| private boolean isPrimitiveParameter(int parameterPosition) {
|
89 |
122
| Class<?>[] parameterTypes = method.getParameterTypes();
|
90 |
122
| if (method.isVarArgs()) {
|
91 |
6
| parameterPosition = Math.min(parameterPosition, parameterTypes.length -1);
|
92 |
| } |
93 |
122
| return parameterTypes[parameterPosition].isPrimitive();
|
94 |
| } |
95 |
| |
96 |
107
| public boolean matches(Invocation actual, ArgumentsMatcher matcher) {
|
97 |
107
| return this.mock.equals(actual.mock)
|
98 |
| && this.method.equals(actual.method) |
99 |
| && matcher.matches(this.arguments, actual.arguments); |
100 |
| } |
101 |
| |
102 |
48
| public String toString(ArgumentsMatcher matcher) {
|
103 |
48
| return method.getName() + "(" + matcher.toString(arguments) + ")";
|
104 |
| } |
105 |
| } |