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
31   112   17   4,43
14   72   0,55   7
7     2,43  
1    
3,7% of code in this file is excluded from these metrics.
 
  ObjectMethodsFilter       Line # 28 31 3,7% 17 0 100% 1.0
 
  (769)
 
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 java.io.IOException;
20    import java.io.Serializable;
21    import java.lang.reflect.InvocationHandler;
22    import java.lang.reflect.Method;
23    import java.lang.reflect.Proxy;
24   
25    /**
26    * @author OFFIS, Tammo Freese
27    */
 
28    public class ObjectMethodsFilter implements InvocationHandler, Serializable {
29   
30    private static final long serialVersionUID = -2120581954279966998L;
31   
32    private transient Method equalsMethod;
33   
34    private transient Method hashCodeMethod;
35   
36    private transient Method toStringMethod;
37   
38    private final MockInvocationHandler delegate;
39   
40    private final String name;
41   
 
42  998 toggle public ObjectMethodsFilter(Class<?> toMock, final MockInvocationHandler delegate, final String name) {
43  998 if (name != null && !Invocation.isJavaIdentifier(name)) {
44  2 throw new IllegalArgumentException(String.format("'%s' is not a valid Java identifier.", name));
45   
46    }
47  996 try {
48  996 if (toMock.isInterface()) {
49  730 toMock = Object.class;
50    }
51  996 equalsMethod = toMock.getMethod("equals", new Class[] { Object.class });
52  996 hashCodeMethod = toMock.getMethod("hashCode", (Class[]) null);
53  996 toStringMethod = toMock.getMethod("toString", (Class[]) null);
54    } catch (final NoSuchMethodException e) {
55    // ///CLOVER:OFF
56    throw new RuntimeException("An Object method could not be found!");
57    // ///CLOVER:ON
58    }
59  996 this.delegate = delegate;
60  996 this.name = name;
61    }
62   
 
63  7620 toggle public final Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
64  7620 if (equalsMethod.equals(method)) {
65  3838 return Boolean.valueOf(proxy == args[0]);
66    }
67  3782 if (hashCodeMethod.equals(method)) {
68  36 return Integer.valueOf(System.identityHashCode(proxy));
69    }
70  3746 if (toStringMethod.equals(method)) {
71  510 return mockToString(proxy);
72    }
73  3236 return delegate.invoke(proxy, method, args);
74    }
75   
 
76  510 toggle private String mockToString(final Object proxy) {
77  510 return (name != null) ? name : "EasyMock for " + mockType(proxy);
78    }
79   
 
80  412 toggle private String mockType(final Object proxy) {
81  412 if (Proxy.isProxyClass(proxy.getClass())) {
82  320 return proxy.getClass().getInterfaces()[0].toString();
83    } else {
84  92 return proxy.getClass().getSuperclass().toString();
85    }
86    }
87   
 
88  1280 toggle public MockInvocationHandler getDelegate() {
89  1280 return delegate;
90    }
91   
 
92  24 toggle private void readObject(final java.io.ObjectInputStream stream) throws IOException,
93    ClassNotFoundException {
94  24 stream.defaultReadObject();
95  24 try {
96  24 toStringMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
97  24 equalsMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
98  24 hashCodeMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
99    } catch (final NoSuchMethodException e) {
100    // ///CLOVER:OFF
101    throw new IOException(e.toString());
102    // ///CLOVER:ON
103    }
104    }
105   
 
106  24 toggle private void writeObject(final java.io.ObjectOutputStream stream) throws IOException {
107  24 stream.defaultWriteObject();
108  24 stream.writeObject(new MethodSerializationWrapper(toStringMethod));
109  24 stream.writeObject(new MethodSerializationWrapper(equalsMethod));
110  24 stream.writeObject(new MethodSerializationWrapper(hashCodeMethod));
111    }
112    }