Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv30075/src/core/com/mockobjects/dynamic Modified Files: Invocation.java AbstractCallableCollection.java CallBag.java CallableArrayList.java DefaultCallFactory.java CallableFactory.java VoidStub.java CallOnceExpectation.java CallSequence.java CoreMock.java ReturnStub.java CallableList.java CallableCollection.java DynamicMock.java CallStub.java ThrowStub.java CallSignature.java Removed Files: Callable.java Log Message: First move towards Invokable-based refactoring Index: Invocation.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Invocation.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Invocation.java 6 Jul 2003 23:24:09 -0000 1.1 +++ Invocation.java 20 Aug 2003 21:51:27 -0000 1.2 @@ -1,27 +1,103 @@ -/* - * Copyright mockobjects.com 05-Jul-2003 - */ package com.mockobjects.dynamic; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; -public class Invocation extends Object { - final public String methodName; - final public Object[] args; - - public Invocation(Method method, Object[] args) { - this(method.getName(), args == null ? new Object[0] : args); - } - - public Invocation(String methodName, Object[] args) { - this.methodName = methodName; - this.args = args; - } - public String getMethodName() { - return methodName; - } - public String toString() { - return DynamicUtil.methodToString(getMethodName(), args); - } +/** An object that holds information about a call dispatched to + * a Mock object for err... mocking. + */ +// TODO Simplify this (smgf) +public class Invocation +{ + private String name; + private List parameterTypes; + private Class returnType; + private List parameterValues; + + public Invocation( String name, Class[] parameterTypes, + Class returnType, Object[] parameterValues ) + { + this.name = name; + this.parameterTypes = Arrays.asList(parameterTypes); + this.returnType = returnType; + if( parameterValues == null ) { + this.parameterValues = new ArrayList(0); + } else { + this.parameterValues = Arrays.asList(parameterValues); + } + } + + public Invocation( Method method, Object[] parameterValues ) { + this( method.getName(), method.getParameterTypes(), + method.getReturnType(), parameterValues ); + } + + public String getMethodName() { + return name; + } + + public List getParameterTypes() { + return Collections.unmodifiableList(parameterTypes); + } + + public List getParameterValues() { + return Collections.unmodifiableList(parameterValues); + } + + public Class getReturnType() { + return returnType; + } + + public String toString() { + return DynamicUtil.methodToString( name, parameterValues.toArray() ); + } + + public boolean equals( Object o ) { + if( o instanceof Invocation ) { + return this.equals((Invocation)o); + } else { + return false; + } + } + + public int hashCode() { + return name.hashCode() ^ + listHashCode(parameterTypes) ^ + returnType.hashCode() ^ + listHashCode(parameterValues); + } + + private int listHashCode( List array ) { + int hashCode = 0; + for( Iterator i = array.iterator(); i.hasNext(); ) { + hashCode ^= i.next().hashCode(); + } + return hashCode; + } + + public boolean equals( Invocation call ) { + return call != null + && name.equals(call.name) + && parameterTypes.equals( call.parameterTypes ) + && returnType.equals( call.returnType ) + && parameterValues.equals( call.parameterValues ); + } + + boolean isCheckingEqualityOnProxy() { + return name.equals("equals") + && parameterValues.size() == 1 + && parameterValues.get(0) != null + && Proxy.isProxyClass(parameterValues.get(0).getClass()); + } + + boolean isMockNameGetter() { + return name.equals("getMockName") + && parameterValues.size() == 0; + } } Index: AbstractCallableCollection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/AbstractCallableCollection.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- AbstractCallableCollection.java 9 Aug 2003 13:18:30 -0000 1.5 +++ AbstractCallableCollection.java 20 Aug 2003 21:51:27 -0000 1.6 @@ -17,7 +17,7 @@ } abstract public String getDescription(); - abstract public Object call(Invocation invocation) throws Throwable; + abstract public Object invoke(Invocation invocation) throws Throwable; public boolean matches(Invocation invocation) { throw new AssertionFailedError("matches() operation not supported in CallCollection"); @@ -28,11 +28,11 @@ stubCalls.clear(); } - public void addExpect(Callable call) { + public void addExpect(Invokable call) { expectedCalls.add(call); } - public void addStub(Callable call) { + public void addStub(Invokable call) { stubCalls.add(call); } @@ -40,8 +40,8 @@ expectedCalls.verify(); } - protected Callable lastStubCall(Invocation invocation) throws AssertionFailedError { - Callable foundCall = stubCalls.lastMatchingCall(invocation); + protected Invokable lastStubCall(Invocation invocation) throws AssertionFailedError { + Invokable foundCall = stubCalls.lastMatchingCall(invocation); if (foundCall == null) { throw createUnexpectedCallError(invocation); } Index: CallBag.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallBag.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- CallBag.java 9 Jul 2003 02:14:00 -0000 1.9 +++ CallBag.java 20 Aug 2003 21:51:27 -0000 1.10 @@ -13,13 +13,13 @@ super(expectedCalls, stubCalls); } - public Object call(Invocation invocation) throws Throwable { - Callable foundCall = expectedCalls.lastMatchingCall(invocation); + public Object invoke(Invocation invocation) throws Throwable { + Invokable foundCall = expectedCalls.lastMatchingCall(invocation); if (foundCall == null) { foundCall = lastStubCall(invocation); } - return foundCall.call(invocation); + return foundCall.invoke(invocation); } public String getDescription() { @@ -32,7 +32,7 @@ expectedCalls.apply( new CallableList.Handler() { - public Callable handle(int index, Callable callable) { + public Invokable handle(int index, Invokable callable) { buf.append(callable.getDescription()); buf.append("\n"); return null; Index: CallableArrayList.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallableArrayList.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallableArrayList.java 9 Jul 2003 02:14:00 -0000 1.2 +++ CallableArrayList.java 20 Aug 2003 21:51:27 -0000 1.3 @@ -7,7 +7,7 @@ public class CallableArrayList implements CallableList { private ArrayList callables = new ArrayList(); - public void add(Callable callable) { + public void add(Invokable callable) { callables.add(callable); } @@ -15,8 +15,8 @@ return callables.isEmpty(); } - public Callable get(int index) { - return (Callable)callables.get(index); + public Invokable get(int index) { + return (Invokable)callables.get(index); } public int size() { @@ -27,14 +27,14 @@ callables.clear(); } - public Callable lastMatchingCall(Invocation invocation) { + public Invokable lastMatchingCall(Invocation invocation) { int index = callables.lastIndexOf(new MatcherHack(invocation)); return index > -1 ? get(index) : null; } - public Callable apply(final CallableList.Handler handler) { + public Invokable apply(final CallableList.Handler handler) { for (int i = 0; i < callables.size(); i++) { - Callable result = handler.handle(i, (Callable)callables.get(i)); + Invokable result = handler.handle(i, (Invokable)callables.get(i)); if (null != result) { return result; } @@ -44,7 +44,7 @@ public void verify() { apply(new Handler() { - public Callable handle(int index, Callable callable) { + public Invokable handle(int index, Invokable callable) { callable.verify(); return null; } @@ -54,6 +54,6 @@ private class MatcherHack { private Invocation invocation; public MatcherHack(Invocation invocation) {this.invocation = invocation; } - public boolean equals(Object obj) { return ((Callable)obj).matches(invocation); } + public boolean equals(Object obj) { return ((Invokable)obj).matches(invocation); } } } Index: DefaultCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DefaultCallFactory.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- DefaultCallFactory.java 9 Aug 2003 13:18:15 -0000 1.7 +++ DefaultCallFactory.java 20 Aug 2003 21:51:27 -0000 1.8 @@ -2,35 +2,35 @@ public class DefaultCallFactory implements CallableFactory { - public Callable createCallExpectation(Callable callable) { + public Invokable createCallExpectation(Invokable callable) { return new CallOnceExpectation(callable); } - public Callable createReturnCallable(String methodName, ConstraintMatcher constraints, Object result) { + public Invokable createReturnCallable(String methodName, ConstraintMatcher constraints, Object result) { return createCallSignature(methodName, constraints, createReturnStub(result)); } - public Callable createThrowableCallable(String methodName, ConstraintMatcher constraints, Throwable throwable) { + public Invokable createThrowableCallable(String methodName, ConstraintMatcher constraints, Throwable throwable) { return createCallSignature(methodName, constraints, createThrowStub(throwable)); } - public Callable createVoidCallable(String methodName, ConstraintMatcher constraints) { + public Invokable createVoidCallable(String methodName, ConstraintMatcher constraints) { return createCallSignature(methodName, constraints, createVoidStub()); } - private Callable createCallSignature(String methodName, ConstraintMatcher constraints, Callable callable) { + private Invokable createCallSignature(String methodName, ConstraintMatcher constraints, Invokable callable) { return new CallSignature( methodName, constraints, callable ); } - private Callable createReturnStub(Object result) { + private Invokable createReturnStub(Object result) { return new ReturnStub(result); } - private Callable createThrowStub( Throwable exception ) { + private Invokable createThrowStub( Throwable exception ) { return new ThrowStub(exception); } - private Callable createVoidStub() { + private Invokable createVoidStub() { return new VoidStub(); } Index: CallableFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallableFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CallableFactory.java 6 Jul 2003 02:31:37 -0000 1.1 +++ CallableFactory.java 20 Aug 2003 21:51:27 -0000 1.2 @@ -1,9 +1,9 @@ package com.mockobjects.dynamic; public interface CallableFactory { - Callable createCallExpectation( Callable callable ); + Invokable createCallExpectation( Invokable callable ); - Callable createReturnCallable( String methodName, ConstraintMatcher constraints, Object result ); - Callable createThrowableCallable( String methodName, ConstraintMatcher constraints, Throwable throwable); - Callable createVoidCallable( String methodName, ConstraintMatcher constraints); + Invokable createReturnCallable( String methodName, ConstraintMatcher constraints, Object result ); + Invokable createThrowableCallable( String methodName, ConstraintMatcher constraints, Throwable throwable); + Invokable createVoidCallable( String methodName, ConstraintMatcher constraints); } Index: VoidStub.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/VoidStub.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- VoidStub.java 6 Jul 2003 23:24:09 -0000 1.5 +++ VoidStub.java 20 Aug 2003 21:51:27 -0000 1.6 @@ -10,7 +10,7 @@ return "returns <void>"; } - public Object call(Invocation invocation) throws Throwable { + public Object invoke(Invocation invocation) throws Throwable { return null; } Index: CallOnceExpectation.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallOnceExpectation.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- CallOnceExpectation.java 9 Aug 2003 13:16:17 -0000 1.6 +++ CallOnceExpectation.java 20 Aug 2003 21:51:27 -0000 1.7 @@ -2,11 +2,11 @@ import junit.framework.*; -public class CallOnceExpectation implements Callable { - private Callable delegateCall; +public class CallOnceExpectation implements Invokable { + private Invokable delegateCall; private boolean wasCalled = false; - public CallOnceExpectation( Callable delegateCall ) { + public CallOnceExpectation( Invokable delegateCall ) { this.delegateCall = delegateCall; } @@ -14,9 +14,9 @@ return delegateCall.getDescription() + " [" + (wasCalled ? "" : "not ") + "called]"; } - public Object call(Invocation invocation) throws Throwable { + public Object invoke(Invocation invocation) throws Throwable { wasCalled = true; - return delegateCall.call( invocation ); + return delegateCall.invoke( invocation ); } public boolean matches(Invocation invocation) { Index: CallSequence.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallSequence.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- CallSequence.java 9 Jul 2003 02:14:00 -0000 1.11 +++ CallSequence.java 20 Aug 2003 21:51:27 -0000 1.12 @@ -6,19 +6,19 @@ int callIndex = 0; - public Object call(Invocation invocation) throws Throwable { + public Object invoke(Invocation invocation) throws Throwable { if (expectedCalls.isEmpty()) throw new AssertionFailedError("no methods defined on mock, received: " + invocation.toString()); if (callIndex == expectedCalls.size()) throw new AssertionFailedError("mock called too many times, received: " + invocation.toString()); - Callable callable = expectedCalls.get(callIndex++); + Invokable callable = expectedCalls.get(callIndex++); if (callable.matches(invocation)) { //TODO shouldn't callIndex be incremented here? - return callable.call(invocation); + return callable.invoke(invocation); } - return lastStubCall(invocation).call(invocation); + return lastStubCall(invocation).invoke(invocation); } public String getDescription() { @@ -30,7 +30,7 @@ expectedCalls.apply( new CallableList.Handler() { - public Callable handle(int index, Callable callable) { + public Invokable handle(int index, Invokable callable) { buf.append(callable.getDescription()); if (index == (callIndex - 1)) buf.append(" <<< Next Expected Call"); Index: CoreMock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CoreMock.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- CoreMock.java 11 Aug 2003 21:25:06 -0000 1.8 +++ CoreMock.java 20 Aug 2003 21:51:27 -0000 1.9 @@ -28,12 +28,12 @@ try { Invocation invocation = new Invocation(method, args); - if (isCheckingEqualityOnProxy(invocation)) { + if (invocation.isCheckingEqualityOnProxy()) { return new Boolean(args[0] == this.proxy); - } else if (isMockNameGetter(invocation)) { + } else if (invocation.isMockNameGetter()) { return this.getMockName(); } else { - return invocationMatchers.call(invocation); + return invocationMatchers.invoke(invocation); } } catch (AssertionFailedError ex) { throw new AssertionFailedError(name + ": " + ex.getMessage()); @@ -56,25 +56,14 @@ return this.name; } - public void expect(Callable invocationMatcher) { + public void expect(Invokable invocationMatcher) { invocationMatchers.addExpect(invocationMatcher); } - public void stub(Callable invocationMatcher) { + public void stub(Invokable invocationMatcher) { invocationMatchers.addStub(invocationMatcher); } - private boolean isCheckingEqualityOnProxy(Invocation invocation) { - return invocation.getMethodName().equals("equals") - && invocation.args.length == 1 - && invocation.args[0] != null - && Proxy.isProxyClass(invocation.args[0].getClass()); - } - - private boolean isMockNameGetter(Invocation invocation) { - return (invocation.getMethodName().equals("getMockName")) && (invocation.args.length == 0); - } - public void reset() { invocationMatchers.reset(); } @@ -82,39 +71,9 @@ public static String mockNameFromClass(Class c) { return "mock" + className(c); } - + public static String className(Class c) { String name = c.getName(); - int dotIndex = name.lastIndexOf('.'); - - if (dotIndex >= 0) { - return name.substring(dotIndex + 1); - } else { - return name; - } + return name.substring(name.lastIndexOf('.') + 1); } - - public static class ProxyIsEqual implements Callable { - final Object proxy; - - public ProxyIsEqual(Object proxy) { - this.proxy = proxy; - } - - public String getDescription() { - return "Proxy is equal"; - } - - public Object call(Invocation invocation) throws Throwable { - return new Boolean(invocation.args[0] == proxy); - } - - public boolean matches(Invocation invocation) { - return (invocation.getMethodName().equals("equals")) && (invocation.args.length == 1) && - (Proxy.isProxyClass(invocation.args[0].getClass())); - } - - public void verify() { /* no op */ } - } - } Index: ReturnStub.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/ReturnStub.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ReturnStub.java 6 Jul 2003 23:24:09 -0000 1.6 +++ ReturnStub.java 20 Aug 2003 21:51:27 -0000 1.7 @@ -12,7 +12,7 @@ this.result = result; } - public Object call(Invocation invocation) throws Throwable { + public Object invoke(Invocation invocation) throws Throwable { return result; } Index: CallableList.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallableList.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CallableList.java 9 Jul 2003 02:14:00 -0000 1.4 +++ CallableList.java 20 Aug 2003 21:51:27 -0000 1.5 @@ -9,15 +9,15 @@ public interface CallableList extends Verifiable { public interface Handler { - Callable handle(int index, Callable callable); + Invokable handle(int index, Invokable callable); } - void add(Callable callable); + void add(Invokable callable); boolean isEmpty(); - Callable get(int index); + Invokable get(int index); int size(); void clear(); - Callable lastMatchingCall(Invocation invocation); - Callable apply(final Handler handler); + Invokable lastMatchingCall(Invocation invocation); + Invokable apply(final Handler handler); } Index: CallableCollection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallableCollection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallableCollection.java 9 Aug 2003 13:18:30 -0000 1.2 +++ CallableCollection.java 20 Aug 2003 21:51:27 -0000 1.3 @@ -1,8 +1,8 @@ package com.mockobjects.dynamic; -public interface CallableCollection extends Callable { - void addExpect(Callable call); - void addStub(Callable call); +public interface CallableCollection extends Invokable { + void addExpect(Invokable call); + void addStub(Invokable call); /** * Resets all expected calls and expected matches. Index: DynamicMock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DynamicMock.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DynamicMock.java 7 Jul 2003 01:17:57 -0000 1.2 +++ DynamicMock.java 20 Aug 2003 21:51:27 -0000 1.3 @@ -4,10 +4,13 @@ import com.mockobjects.Verifiable; -public interface DynamicMock extends Verifiable, InvocationHandler { - void expect(Callable invocationMatcher); - void stub(Callable invocationMatcher); - Object proxy(); +public interface DynamicMock + extends Verifiable, InvocationHandler +{ + void expect(Invokable invokable); + void stub(Invokable invokable); + + Object proxy(); void reset(); } Index: CallStub.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallStub.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- CallStub.java 6 Jul 2003 23:24:09 -0000 1.5 +++ CallStub.java 20 Aug 2003 21:51:27 -0000 1.6 @@ -4,7 +4,7 @@ package com.mockobjects.dynamic; public abstract class CallStub - implements Callable + implements Invokable { public boolean matches(Invocation invocation) { return true; Index: ThrowStub.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/ThrowStub.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ThrowStub.java 6 Jul 2003 23:24:09 -0000 1.6 +++ ThrowStub.java 20 Aug 2003 21:51:27 -0000 1.7 @@ -12,7 +12,7 @@ this.throwable = throwable; } - public Object call(Invocation invocation) throws Throwable { + public Object invoke(Invocation invocation) throws Throwable { throw throwable; } Index: CallSignature.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallSignature.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CallSignature.java 9 Aug 2003 13:18:30 -0000 1.7 +++ CallSignature.java 20 Aug 2003 21:51:27 -0000 1.8 @@ -1,21 +1,21 @@ package com.mockobjects.dynamic; -public class CallSignature implements Callable +public class CallSignature implements Invokable { private String methodName; private ConstraintMatcher constraints; - private Callable delegateCall; + private Invokable delegateCall; - public CallSignature( String methodName, ConstraintMatcher constraints, Callable delegateCall ) { + public CallSignature( String methodName, ConstraintMatcher constraints, Invokable delegateCall ) { this.methodName = methodName; this.constraints = constraints; this.delegateCall = delegateCall; } - public Object call( Invocation invocation ) + public Object invoke( Invocation invocation ) throws Throwable { - return delegateCall.call( invocation ); + return delegateCall.invoke( invocation ); } public void verify() { @@ -23,7 +23,8 @@ } public boolean matches(Invocation invocation) { - return this.methodName.equals(invocation.getMethodName()) && constraints.matches(invocation.args); + return this.methodName.equals(invocation.getMethodName()) && + constraints.matches(invocation.getParameterValues().toArray()); } public String getDescription() { --- Callable.java DELETED --- |