Clover Coverage Report - EasyMock 2.4
Coverage timestamp: mer. juil. 2 2008 02:17:38 CEST
31   98   16   4,43
14   73   0,52   7
7     2,29  
1    
 
 
  ObjectMethodsFilter       Line # 13 31 16 100% 1.0
 
  (269)
 
1    /*
2    * Copyright (c) 2001-2008 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 java.io.IOException;
8    import java.io.Serializable;
9    import java.lang.reflect.InvocationHandler;
10    import java.lang.reflect.Method;
11    import java.lang.reflect.Proxy;
12   
 
13    public class ObjectMethodsFilter implements InvocationHandler, Serializable {
14   
15    private static final long serialVersionUID = -2120581954279966998L;
16   
17    private transient Method equalsMethod;
18   
19    private transient Method hashCodeMethod;
20   
21    private transient Method toStringMethod;
22   
23    private final MockInvocationHandler delegate;
24   
25    private final String name;
26   
 
27  400 toggle public ObjectMethodsFilter(Class<?> toMock, MockInvocationHandler delegate,
28    String name) {
29  400 if (name != null && !Invocation.isJavaIdentifier(name)) {
30  1 throw new IllegalArgumentException(String.format("'%s' is not a valid Java identifier.", name));
31   
32    }
33  399 try {
34  399 if (toMock.isInterface()) {
35  398 toMock = Object.class;
36    }
37  399 equalsMethod = toMock.getMethod("equals",
38    new Class[] { Object.class });
39  399 hashCodeMethod = toMock.getMethod("hashCode", (Class[]) null);
40  399 toStringMethod = toMock.getMethod("toString", (Class[]) null);
41    } catch (NoSuchMethodException e) {
42    // ///CLOVER:OFF
43    throw new RuntimeException("An Object method could not be found!");
44    // ///CLOVER:ON
45    }
46  399 this.delegate = delegate;
47  399 this.name = name;
48    }
49   
 
50  3825 toggle public final Object invoke(Object proxy, Method method, Object[] args)
51    throws Throwable {
52  3825 if (equalsMethod.equals(method)) {
53  2397 return Boolean.valueOf(proxy == args[0]);
54    }
55  1428 if (hashCodeMethod.equals(method)) {
56  9 return Integer.valueOf(System.identityHashCode(proxy));
57    }
58  1419 if (toStringMethod.equals(method)) {
59  298 return mockToString(proxy);
60    }
61  1121 return delegate.invoke(proxy, method, args);
62    }
63   
 
64  298 toggle private String mockToString(Object proxy) {
65  298 return (name != null) ? name : "EasyMock for " + mockType(proxy);
66    }
67   
 
68  294 toggle private String mockType(Object proxy) {
69  294 if (Proxy.isProxyClass(proxy.getClass()))
70  293 return proxy.getClass().getInterfaces()[0].toString();
71    else
72  1 return proxy.getClass().getSuperclass().toString();
73    }
74   
 
75  160 toggle public MockInvocationHandler getDelegate() {
76  160 return delegate;
77    }
78   
 
79  4 toggle private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
80  4 stream.defaultReadObject();
81  4 try {
82  4 toStringMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
83  4 equalsMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
84  4 hashCodeMethod = ((MethodSerializationWrapper) stream.readObject()).getMethod();
85    } catch (NoSuchMethodException e) {
86    // ///CLOVER:OFF
87    throw new IOException(e.toString());
88    // ///CLOVER:ON
89    }
90    }
91   
 
92  4 toggle private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
93  4 stream.defaultWriteObject();
94  4 stream.writeObject(new MethodSerializationWrapper(toStringMethod));
95  4 stream.writeObject(new MethodSerializationWrapper(equalsMethod));
96  4 stream.writeObject(new MethodSerializationWrapper(hashCodeMethod));
97    }
98    }