Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
70   213   41   3,68
30   157   0,59   19
19     2,16  
1    
2,5% of code in this file is excluded from these metrics.
 
  Invocation       Line # 33 70 2,5% 41 0 100% 1.0
 
  (653)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.internal;
18   
19    import static java.lang.Character.*;
20   
21    import java.io.IOException;
22    import java.io.Serializable;
23    import java.lang.reflect.Array;
24    import java.lang.reflect.Method;
25    import java.util.ArrayList;
26    import java.util.Collection;
27   
28    import org.easymock.internal.matchers.Captures;
29   
30    /**
31    * @author OFFIS, Tammo Freese
32    */
 
33    public class Invocation implements Serializable {
34   
35    private static final long serialVersionUID = 1604995470419943411L;
36   
37    private final Object mock;
38   
39    private transient Method method;
40   
41    private final Object[] arguments;
42   
43    private final Collection<Captures<?>> currentCaptures = new ArrayList<Captures<?>>(0);
44   
 
45  3265 toggle public Invocation(final Object mock, final Method method, final Object[] args) {
46  3266 this.mock = mock;
47  3266 this.method = method;
48  3266 this.arguments = expandVarArgs(method.isVarArgs(), args);
49    }
50   
 
51  3266 toggle private static Object[] expandVarArgs(final boolean isVarArgs, final Object[] args) {
52  3266 if (!isVarArgs) {
53  3138 return args == null ? new Object[0] : args;
54    }
55  128 if (args[args.length - 1] == null) {
56  6 return args;
57    }
58  122 final Object[] varArgs = createObjectArray(args[args.length - 1]);
59  122 final int nonVarArgsCount = args.length - 1;
60  122 final int varArgsCount = varArgs.length;
61  122 final Object[] newArgs = new Object[nonVarArgsCount + varArgsCount];
62  122 System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount);
63  122 System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount);
64  122 return newArgs;
65    }
66   
 
67  122 toggle private static Object[] createObjectArray(final Object array) {
68  122 if (array instanceof Object[]) {
69  54 return (Object[]) array;
70    }
71  68 final Object[] result = new Object[Array.getLength(array)];
72  170 for (int i = 0; i < Array.getLength(array); i++) {
73  102 result[i] = Array.get(array, i);
74    }
75  68 return result;
76    }
77   
 
78  4676 toggle public Object getMock() {
79  4676 return mock;
80    }
81   
 
82  9630 toggle public Method getMethod() {
83  9630 return method;
84    }
85   
 
86  3408 toggle public Object[] getArguments() {
87  3408 return arguments;
88    }
89   
 
90  1684 toggle @Override
91    public boolean equals(final Object o) {
92  1684 if (o == null || !o.getClass().equals(this.getClass())) {
93  4 return false;
94    }
95   
96  1680 final Invocation other = (Invocation) o;
97   
98  1680 return this.mock.equals(other.mock) && this.method.equals(other.method)
99    && this.equalArguments(other.arguments);
100    }
101   
 
102  2 toggle @Override
103    public int hashCode() {
104  2 throw new UnsupportedOperationException("hashCode() is not implemented");
105    }
106   
 
107  198 toggle @Override
108    public String toString() {
109  198 return getMockAndMethodName() + "(" + ArgumentToString.argumentsToString(arguments) + ")";
110    }
111   
 
112  418 toggle private boolean equalArguments(final Object[] arguments) {
113  418 if (this.arguments.length != arguments.length) {
114  28 return false;
115    }
116  600 for (int i = 0; i < this.arguments.length; i++) {
117  422 final Object myArgument = this.arguments[i];
118  422 final Object otherArgument = arguments[i];
119   
120  422 if (isPrimitiveParameter(i)) {
121  226 if (!myArgument.equals(otherArgument)) {
122  104 return false;
123    }
124    } else {
125  196 if (myArgument != otherArgument) {
126  108 return false;
127    }
128    }
129    }
130  178 return true;
131    }
132   
 
133  422 toggle private boolean isPrimitiveParameter(int parameterPosition) {
134  422 final Class<?>[] parameterTypes = method.getParameterTypes();
135  422 if (method.isVarArgs()) {
136  28 parameterPosition = Math.min(parameterPosition, parameterTypes.length - 1);
137    }
138  422 return parameterTypes[parameterPosition].isPrimitive();
139    }
140   
 
141  444 toggle public String getMockAndMethodName() {
142  444 final String mockName = mock.toString();
143  444 final String methodName = method.getName();
144  444 if (toStringIsDefined(mock) && isJavaIdentifier(mockName)) {
145  50 return mockName + "." + methodName;
146    } else {
147  394 return methodName;
148    }
149    }
150   
 
151  104 toggle public void addCapture(final Captures<Object> capture, final Object value) {
152  104 capture.setPotentialValue(value);
153  104 currentCaptures.add(capture);
154    }
155   
 
156  1560 toggle public void validateCaptures() {
157  1560 for (final Captures<?> c : currentCaptures) {
158  100 c.validateCapture();
159    }
160    }
161   
 
162  3654 toggle public void clearCaptures() {
163  3654 for (final Captures<?> c : currentCaptures) {
164  100 c.setPotentialValue(null);
165    }
166  3654 currentCaptures.clear();
167    }
168   
 
169  444 toggle private boolean toStringIsDefined(final Object o) {
170  444 try {
171  444 o.getClass().getDeclaredMethod("toString", (Class[]) null).getModifiers();
172  444 return true;
173    } catch (final SecurityException ignored) {
174    // ///CLOVER:OFF
175    return false;
176    // ///CLOVER:ON
177    } catch (final NoSuchMethodException ignored) {
178    // ///CLOVER:OFF
179    return false;
180    // ///CLOVER:ON
181    }
182    }
183   
 
184  528 toggle public static boolean isJavaIdentifier(final String mockName) {
185  528 if (mockName.length() == 0 || mockName.indexOf(' ') > -1
186    || !Character.isJavaIdentifierStart(mockName.charAt(0))) {
187  392 return false;
188    }
189  136 for (final char c : mockName.substring(1).toCharArray()) {
190  586 if (!isJavaIdentifierPart(c)) {
191  4 return false;
192    }
193    }
194  132 return true;
195    }
196   
 
197  18 toggle private void readObject(final java.io.ObjectInputStream stream) throws IOException,
198    ClassNotFoundException {
199  18 stream.defaultReadObject();
200  18 try {
201  18 method = ((MethodSerializationWrapper) stream.readObject()).getMethod();
202    } catch (final NoSuchMethodException e) {
203    // ///CLOVER:OFF
204    throw new IOException(e.toString());
205    // ///CLOVER:ON
206    }
207    }
208   
 
209  18 toggle private void writeObject(final java.io.ObjectOutputStream stream) throws IOException {
210  18 stream.defaultWriteObject();
211  18 stream.writeObject(new MethodSerializationWrapper(method));
212    }
213    }