EasyMock 2.3

Clover coverage report - EasyMock 2.3
Coverage timestamp: mar. juil. 10 2007 02:46:04 CEST
FRAMES NO FRAMES

file stats: | LOC: | 147 |  | Methods: | 14
| NCLOC: | 115 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

Invocation.java

100%

100%

100%

100%

1 |  | /*
2 |  | * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
3 |  | * This program is made available under the terms of the MIT License.
4 |  | */
5 |  | package org.easymock.internal;
6 |  |
7 |  | import static java.lang.Character.*;
8 |  |
9 |  | import java.lang.reflect.Method;
10 |  |
11 |  | import org.easymock.ArgumentsMatcher;
12 |  | import org.easymock.internal.matchers.ArrayEquals;
13 |  |
14 |  | public class Invocation {
15 |  |
16 |  | private final Object mock;
17 |  |
18 |  | private final Method method;
19 |  |
20 |  | private final Object[] arguments;
21 |  |
22 | 1036 | public Invocation(Object mock, Method method, Object[] args) {
23 | 1036 | this.mock = mock;
24 | 1036 | this.method = method;
25 | 1036 | this.arguments = expandVarArgs(method.isVarArgs(), args);
26 |  | }
27 |  |
28 | 1036 | private static Object[] expandVarArgs(final boolean isVarArgs,
29 |  | final Object[] args) {
30 | 1036 | if (!isVarArgs || isVarArgs && args[args.length - 1] != null
31 |  | && !args[args.length - 1].getClass().isArray()) {
32 | 994 | return args == null ? new Object[0] : args;
33 |  | }
34 | 42 | Object[] varArgs = ArrayEquals.createObjectArray(args[args.length - 1]);
35 | 42 | final int nonVarArgsCount = args.length - 1;
36 | 42 | final int varArgsCount = varArgs.length;
37 | 42 | Object[] newArgs = new Object[nonVarArgsCount + varArgsCount];
38 | 42 | System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount);
39 | 42 | System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount);
40 | 42 | return newArgs;
41 |  | }
42 |  |
43 | 2974 | public Object getMock() {
44 | 2974 | return mock;
45 |  | }
46 |  |
47 | 4599 | public Method getMethod() {
48 | 4599 | return method;
49 |  | }
50 |  |
51 | 1784 | public Object[] getArguments() {
52 | 1784 | return arguments;
53 |  | }
54 |  |
55 | 649 | public boolean equals(Object o) {
56 | 649 | if (o == null || !o.getClass().equals(this.getClass()))
57 | 2 | return false;
58 |  |
59 | 647 | Invocation other = (Invocation) o;
60 |  |
61 | 647 | return this.mock.equals(other.mock) && this.method.equals(other.method)
62 |  | && this.equalArguments(other.arguments);
63 |  | }
64 |  |
65 | 2 | public int hashCode() {
66 | 2 | throw new UnsupportedOperationException("hashCode() is not implemented");
67 |  | }
68 |  |
69 | 135 | private boolean equalArguments(Object[] arguments) {
70 | 135 | if (this.arguments.length != arguments.length) {
71 | 11 | return false;
72 |  | }
73 | 124 | for (int i = 0; i < this.arguments.length; i++) {
74 | 128 | Object myArgument = this.arguments[i];
75 | 128 | Object otherArgument = arguments[i];
76 |  |
77 | 128 | if (isPrimitiveParameter(i)) {
78 | 60 | if (!myArgument.equals(otherArgument)) {
79 | 24 | return false;
80 |  | }
81 |  | } else {
82 | 68 | if (myArgument != otherArgument) {
83 | 48 | return false;
84 |  | }
85 |  | }
86 |  | }
87 | 52 | return true;
88 |  | }
89 |  |
90 | 128 | private boolean isPrimitiveParameter(int parameterPosition) {
91 | 128 | Class<?>[] parameterTypes = method.getParameterTypes();
92 | 128 | if (method.isVarArgs()) {
93 | 6 | parameterPosition = Math.min(parameterPosition,
94 |  | parameterTypes.length - 1);
95 |  | }
96 | 128 | return parameterTypes[parameterPosition].isPrimitive();
97 |  | }
98 |  |
99 | 107 | public boolean matches(Invocation actual, ArgumentsMatcher matcher) {
100 | 107 | return this.mock.equals(actual.mock)
101 |  | && this.method.equals(actual.method)
102 |  | && matcher.matches(this.arguments, actual.arguments);
103 |  | }
104 |  |
105 | 60 | public String toString(ArgumentsMatcher matcher) {
106 | 60 | return getMockAndMethodName() + "(" + matcher.toString(arguments) + ")";
107 |  | }
108 |  |
109 | 279 | public String getMockAndMethodName() {
110 | 279 | String mockName = mock.toString();
111 | 279 | String methodName = method.getName();
112 | 279 | if (toStringIsDefined(mock) && isJavaIdentifier(mockName)) {
113 | 5 | return mockName + "." + methodName;
114 |  | } else {
115 | 274 | return methodName;
116 |  | }
117 |  | }
118 |  |
119 | 279 | private boolean toStringIsDefined(Object o) {
120 | 279 | try {
121 | 279 | o.getClass().getDeclaredMethod("toString", (Class[]) null)
122 |  | .getModifiers();
123 | 279 | return true;
124 |  | } catch (SecurityException ignored) {
125 |  | // ///CLOVER:OFF
126 |  | return false;
127 |  | // ///CLOVER:ON
128 |  | } catch (NoSuchMethodException shouldNeverHappen) {
129 |  | // ///CLOVER:OFF
130 |  | throw new RuntimeException("The toString() method could not be found!");
131 |  | // ///CLOVER:ON
132 |  | }
133 |  | }
134 |  |
135 | 284 | public static boolean isJavaIdentifier(String mockName) {
136 | 284 | if (mockName.length() == 0 || mockName.indexOf(' ') > -1
137 |  | || !Character.isJavaIdentifierStart(mockName.charAt(0))) {
138 | 273 | return false;
139 |  | }
140 | 11 | for (char c : mockName.substring(1).toCharArray()) {
141 | 46 | if (!isJavaIdentifierPart(c)) {
142 | 2 | return false;
143 |  | }
144 |  | }
145 | 9 | return true;
146 |  | }
147 |  | }