From: Tim M. <ma...@us...> - 2003-07-06 22:45:27
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv14872/core/com/mockobjects/dynamic Modified Files: DynamicUtil.java Mock.java Added Files: CoreMock.java Log Message: Move to delegation of core mock functionality - step 1. --- NEW FILE: CoreMock.java --- package com.mockobjects.dynamic; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import com.mockobjects.Verifiable; import junit.framework.AssertionFailedError; public class CoreMock implements InvocationHandler, Verifiable { protected CallableCollection callables; private Object proxy; private String name; public CoreMock(Class mockedClass, String name, CallableCollection callables) { this.name = name; this.callables = callables; this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { mockedClass }, this); } public Object proxy() { return this.proxy; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { ActiveCall activeCall = new ActiveCall(method, args); if (isCheckingEqualityOnProxy(activeCall)) { return new Boolean(args[0] == this.proxy); } else if (isMockNameGetter(activeCall)) { return this.getMockName(); } else { return callables.call(activeCall); } } catch (AssertionFailedError ex) { throw new AssertionFailedError(name + ": " + ex.getMessage()); } } public void verify() { try { callables.verify(); } catch (AssertionFailedError ex) { throw new AssertionFailedError(name + ": " + ex.getMessage()); } } public String toString() { return this.name; } public String getMockName() { return this.name; } protected boolean isCheckingEqualityOnProxy(ActiveCall call) { return (call.getMethodName().equals("equals")) && (call.args.length == 1) && (Proxy.isProxyClass(call.args[0].getClass())); } protected boolean isMockNameGetter(ActiveCall call) { return (call.getMethodName().equals("getMockName")) && (call.args.length == 0); } } Index: DynamicUtil.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DynamicUtil.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DynamicUtil.java 6 Jul 2003 02:25:19 -0000 1.4 +++ DynamicUtil.java 6 Jul 2003 22:45:24 -0000 1.5 @@ -77,7 +77,7 @@ public static String proxyToString(Object element) { if (Proxy.isProxyClass(element.getClass())) { try { - Method mockNameMethod = Mock.class.getDeclaredMethod("getMockName", new Class[0]); + Method mockNameMethod = CoreMock.class.getDeclaredMethod("getMockName", new Class[0]); Object debugableResult = Proxy.getInvocationHandler(element).invoke(element, mockNameMethod, new Object[0]); return debugableResult.toString(); } catch (Throwable e) { Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- Mock.java 6 Jul 2003 02:31:37 -0000 1.26 +++ Mock.java 6 Jul 2003 22:45:24 -0000 1.27 @@ -1,24 +1,16 @@ package com.mockobjects.dynamic; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import junit.framework.AssertionFailedError; -import com.mockobjects.Verifiable; import com.mockobjects.constraint.Constraint; -public class Mock implements InvocationHandler, Verifiable { - private String name; - private Object proxy; +public class Mock extends CoreMock { private CallableFactory callableFactory; - private CallableCollection callables; - - public Mock(CallableFactory callableFactory, CallableCollection callableAddable, Class mockedClass, String name) { - this.name = name; + + public Mock(CallableFactory callableFactory, CallableCollection callables, Class mockedClass, String name) { + super(mockedClass, name, callables); + this.callableFactory = callableFactory; - this.callables = callableAddable; - this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { mockedClass }, this); } public Mock(Class mockedClass, String nonDefaultName) { @@ -64,52 +56,6 @@ } } - public String getMockName() { - return this.name; - } - - public String toString() { - return this.name; - } - - public Object proxy() { - return this.proxy; - } - - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - try { - ActiveCall activeCall = new ActiveCall(method, args); - - if (isCheckingEqualityOnProxy(activeCall)) { - return new Boolean(args[0] == this.proxy); - } else if (isMockNameGetter(activeCall)) { - return this.getMockName(); - } else { - return callables.call(activeCall); - } - } catch (AssertionFailedError ex) { - throw new AssertionFailedError(name + ": " + ex.getMessage()); - } - } - - private boolean isCheckingEqualityOnProxy(ActiveCall call) { - return (call.getMethodName().equals("equals")) && (call.args.length == 1) - && (Proxy.isProxyClass(call.args[0].getClass())); - } - - private boolean isMockNameGetter(ActiveCall call) { - return (call.getMethodName().equals("getMockName")) && (call.args.length == 0); - } - - public void verify() { - try { - callables.verify(); - } catch (AssertionFailedError ex) { - throw new AssertionFailedError(name + ": " + ex.getMessage()); - } - } - public void expect(String methodName) { expect(methodName, C.NO_ARGS); } |