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: | 73 |  | Methods: | 5
| NCLOC: | 54 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

ObjectMethodsFilter.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 java.lang.reflect.InvocationHandler;
8 |  | import java.lang.reflect.Method;
9 |  | import java.lang.reflect.Proxy;
10 |  |
11 |  | public class ObjectMethodsFilter implements InvocationHandler {
12 |  | private final Method equalsMethod;
13 |  |
14 |  | private final Method hashCodeMethod;
15 |  |
16 |  | private final Method toStringMethod;
17 |  |
18 |  | private final MockInvocationHandler delegate;
19 |  |
20 |  | private final String name;
21 |  |
22 | 376 | public ObjectMethodsFilter(Class toMock, MockInvocationHandler delegate,
23 |  | String name) {
24 | 376 | if (name != null && !Invocation.isJavaIdentifier(name)) {
25 | 1 | throw new IllegalArgumentException(String.format("'%s' is not a valid Java identifier.", name));
26 |  |
27 |  | }
28 | 375 | try {
29 | 375 | if (toMock.isInterface()) {
30 | 374 | toMock = Object.class;
31 |  | }
32 | 375 | equalsMethod = toMock.getMethod("equals",
33 |  | new Class[] { Object.class });
34 | 375 | hashCodeMethod = toMock.getMethod("hashCode", (Class[]) null);
35 | 375 | toStringMethod = toMock.getMethod("toString", (Class[]) null);
36 |  | } catch (NoSuchMethodException e) {
37 |  | // ///CLOVER:OFF
38 |  | throw new RuntimeException("An Object method could not be found!");
39 |  | // ///CLOVER:ON
40 |  | }
41 | 375 | this.delegate = delegate;
42 | 375 | this.name = name;
43 |  | }
44 |  |
45 | 3547 | public final Object invoke(Object proxy, Method method, Object[] args)
46 |  | throws Throwable {
47 | 3547 | if (equalsMethod.equals(method)) {
48 | 2242 | return Boolean.valueOf(proxy == args[0]);
49 |  | }
50 | 1305 | if (hashCodeMethod.equals(method)) {
51 | 2 | return new Integer(System.identityHashCode(proxy));
52 |  | }
53 | 1303 | if (toStringMethod.equals(method)) {
54 | 281 | return mockToString(proxy);
55 |  | }
56 | 1022 | return delegate.invoke(proxy, method, args);
57 |  | }
58 |  |
59 | 281 | private String mockToString(Object proxy) {
60 | 281 | return (name != null) ? name : "EasyMock for " + mockType(proxy);
61 |  | }
62 |  |
63 | 277 | private String mockType(Object proxy) {
64 | 277 | if (Proxy.isProxyClass(proxy.getClass()))
65 | 276 | return proxy.getClass().getInterfaces()[0].toString();
66 |  | else
67 | 1 | return proxy.getClass().getSuperclass().toString();
68 |  | }
69 |  |
70 | 117 | public MockInvocationHandler getDelegate() {
71 | 117 | return delegate;
72 |  | }
73 |  | }