1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import junit.framework.AssertionFailedError;
5
6 import java.lang.reflect.Method;
7 import java.lang.reflect.Proxy;
8
9 public class CoreMock implements DynamicMock {
10 private InvocationDispatcher invocationDispatcher;
11 private Object proxy;
12 private String name;
13
14 public CoreMock(Class mockedClass, String name, InvocationDispatcher invocationDispatcher) {
15 this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{mockedClass}, this);
16 this.name = name;
17 this.invocationDispatcher = invocationDispatcher;
18
19 // callables.addStub(new ProxyIsEqual(this.mock));
20 }
21
22 public Object proxy() {
23 return this.proxy;
24 }
25
26 public Object invoke(Object proxy, Method method, Object[] args)
27 throws Throwable {
28 Invocation invocation = new Invocation(method, args);
29 try {
30 if (invocation.isCheckingEqualityOnProxy()) {
31 return new Boolean(args[0] == this.proxy);
32 } else if (invocation.isMockNameGetter()) {
33 return this.getMockName();
34 } else {
35 return invocationDispatcher.dispatch(invocation);
36 }
37 } catch (AssertionFailedError ex) {
38 DynamicMockError error = new DynamicMockError(invocation, name + ": " + ex.getMessage());
39 error.fillInStackTrace();
40 throw error;
41 }
42 }
43
44 public void verify() {
45 try {
46 invocationDispatcher.verify();
47 } catch (AssertionFailedError ex) {
48 throw new AssertionFailedError(name + ": " + ex.getMessage());
49 }
50 }
51
52 public String toString() {
53 return this.name;
54 }
55
56 public String getMockName() {
57 return this.name;
58 }
59
60 public void add(Invokable invokable) {
61 invocationDispatcher.add(invokable);
62 }
63
64 public void reset() {
65 invocationDispatcher.clear();
66 }
67
68 public static String mockNameFromClass(Class c) {
69 return "mock" + className(c);
70 }
71
72 public static String className(Class c) {
73 String name = c.getName();
74 return name.substring(name.lastIndexOf('.') + 1);
75 }
76 }
This page was automatically generated by Maven