From: Tim M. <ma...@us...> - 2003-04-15 22:23:04
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv6206/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment CallMatch.java Mock.java ExpectedCall.java C.java Log Message: Improved error reporting for Proxy's as expectation values Added additional conveniance methods for primitive types Added deprecated expect methods to make library conversion easy Got distracted and have missed XtC completely... Index: CallMatch.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/CallMatch.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- CallMatch.java 11 Apr 2003 13:18:46 -0000 1.1.2.2 +++ CallMatch.java 15 Apr 2003 22:22:57 -0000 1.1.2.3 @@ -16,7 +16,7 @@ public CallMatch( String methodName, Constraint[] constraints, Callable decorated ) { this.methodName = methodName; - this.constraints = (Constraint[])constraints.clone(); + this.constraints = constraints == null ? null : (Constraint[])constraints.clone(); this.decorated = decorated; } @@ -55,5 +55,10 @@ public String getDescription() { return AssertMo.methodToString(methodName, constraints); + } + + // Implemented to aid visualisation in an IDE debugger + public String toString() { + return Mock.className(this.getClass()) + "(" + this.getDescription() + ")"; } } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.16.2.4 retrieving revision 1.16.2.5 diff -u -r1.16.2.4 -r1.16.2.5 --- Mock.java 14 Apr 2003 18:46:52 -0000 1.16.2.4 +++ Mock.java 15 Apr 2003 22:22:58 -0000 1.16.2.5 @@ -36,16 +36,24 @@ } 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 "mock" + name.substring(dotIndex + 1); + return name.substring(dotIndex + 1); } else { - return "mock" + name; + return name; } } + public String getMockName() { + return name; + } + public String toString() { return name; } @@ -57,12 +65,27 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { - return callSequence.call(this, method.getName(), ((args == null) ? new Object[0] : args)); + if(checkingProxyEquality(method, args)) { + return new Boolean(args[0] == this.proxy); + } else if(gettingMockName(method, args)) { + return this.getMockName(); + } else { + Object result = callSequence.call(this, method.getName(), ((args == null) ? new Object[0] : args)); + return result; + } } catch (AssertionFailedError ex) { throw new AssertionFailedError(name + ": " + ex.getMessage()); } } + private boolean checkingProxyEquality(Method method, Object[] args) { + return (method.getName().equals("equals")) && (args.length == 1) && (Proxy.isProxyClass(args[0].getClass())); + } + + private boolean gettingMockName(Method method, Object[] args) { + return (method.getName().equals("getMockName")) && (args.length == 0); + } + public void verify() { try { callSequence.verify(); @@ -71,35 +94,145 @@ } } - public void expect(String methodName, Object equalArg) { - expect(methodName, C.args(C.eq(equalArg))); + public void expect(String methodName) { + expect(methodName, C.NO_ARGS); } + public void expect(String methodName, Object singleEqualArg) { + expect(methodName, C.args(C.eq(singleEqualArg))); + } + public void expect(String methodName, Constraint[] args) { callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createVoidStub()))); } - - public void expectAndReturn(String methodName, Object equalArg, Object result) { - expectAndReturn(methodName, C.args(C.eq(equalArg)), result); + + public void expectAndReturn(String methodName, Object result) { + this.expectAndReturn(methodName, C.NO_ARGS, result); + } + + public void expectAndReturn(String methodName, boolean result) { + this.expectAndReturn(methodName, new Boolean(result)); + } + + public void expectAndReturn(String methodName, long result) { + this.expectAndReturn(methodName, new Long(result)); } + public void expectAndReturn(String methodName, Object singleEqualArg, Object result) { + this.expectAndReturn(methodName, C.args(C.eq(singleEqualArg)), result); + } + + public void expectAndReturn(String methodName, Object singleEqualArg, boolean result) { + this.expectAndReturn(methodName, singleEqualArg, new Boolean(result)); + } + + public void expectAndReturn(String methodName, Object singleEqualArg, long result) { + this.expectAndReturn(methodName, singleEqualArg, new Long(result)); + } + public void expectAndReturn(String methodName, Constraint[] args, Object result) { callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result)))); } + + public void expectAndReturn(String methodName, Constraint[] args, boolean result) { + this.expectAndReturn(methodName, args, new Boolean(result)); + } + + public void expectAndReturn(String methodName, Constraint[] args, long result) { + this.expectAndReturn(methodName, args, new Long(result)); + } + public void expectAndThrow(String methodName, Throwable exception) { + this.expectAndThrow(methodName, C.NO_ARGS, exception); + } + + public void expectAndThrow(String methodName, Object singleEqualArg, Throwable exception) { + this.expectAndThrow(methodName, C.args(C.eq(singleEqualArg)), exception); + } + public void expectAndThrow(String methodName, Constraint[] args, Throwable exception) { callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(exception)))); } - - public void matchAndReturn(String methodName, Object equalArg, Object result) { - matchAndReturn(methodName, C.args(C.eq(equalArg)), result); + + public void matchAndReturn(String methodName, Object result) { + this.matchAndReturn(methodName, C.NO_ARGS, result); } - + + public void matchAndReturn(String methodName, boolean result) { + this.matchAndReturn(methodName, new Boolean(result)); + } + + public void matchAndReturn(String methodName, long result) { + this.matchAndReturn(methodName, new Long(result)); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, Object result) { + this.matchAndReturn(methodName, C.args(C.eq(singleEqualArg)), result); + } + + public void matchAndReturn(String methodName, boolean singleEqualArg, Object result) { + this.matchAndReturn(methodName, new Boolean(singleEqualArg), result); + } + + public void matchAndReturn(String methodName, long singleEqualArg, Object result) { + this.matchAndReturn(methodName, new Long(singleEqualArg), result); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, boolean result) { + this.matchAndReturn(methodName, singleEqualArg, new Boolean(result)); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, long result) { + this.matchAndReturn(methodName, singleEqualArg, new Long(result)); + } + public void matchAndReturn(String methodName, Constraint[] args, Object result) { callSequence.add(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result))); } + + public void matchAndReturn(String methodName, Constraint[] args, boolean result) { + this.matchAndReturn(methodName, args, new Boolean(result)); + } + public void matchAndReturn(String methodName, Constraint[] args, long result) { + this.matchAndReturn(methodName, args, new Long(result)); + } + + public void matchAndThrow(String methodName, Throwable throwable) { + this.matchAndThrow(methodName, C.NO_ARGS, throwable); + } + + public void matchAndThrow(String methodName, Object singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName, C.args(C.eq(singleEqualArg)), throwable); + } + + public void matchAndThrow(String methodName, boolean singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName,new Boolean(singleEqualArg), throwable); + } + + public void matchAndThrow(String methodName, long singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName,new Long(singleEqualArg), throwable); + } + public void matchAndThrow(String methodName, Constraint[] args, Throwable throwable) { callSequence.add(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(throwable))); + } + + /** @deprecated @see OrderedMock + */ + public void expect(String methodName, CallSequence deprecated) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); + } + + /** @deprecated @see OrderedMock + */ + public void expectAndReturn(String methodName, CallSequence deprecated, Object result) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); + } + + /** @deprecated @see OrderedMock + */ + public void expectAndThrow(String methodName, CallSequence deprecated, Throwable throwable) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); } } Index: ExpectedCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/ExpectedCall.java,v retrieving revision 1.2.2.4 retrieving revision 1.2.2.5 diff -u -r1.2.2.4 -r1.2.2.5 --- ExpectedCall.java 11 Apr 2003 15:38:18 -0000 1.2.2.4 +++ ExpectedCall.java 15 Apr 2003 22:22:59 -0000 1.2.2.5 @@ -36,4 +36,9 @@ delegate.verify(); } + + // Implemented to aid visualisation in an IDE debugger + public String toString() { + return Mock.className(this.getClass()) + "(" + this.getDescription() + ")"; + } } Index: C.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/C.java,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- C.java 10 Apr 2003 20:35:59 -0000 1.1.2.3 +++ C.java 15 Apr 2003 22:23:00 -0000 1.1.2.4 @@ -20,6 +20,7 @@ public static final Constraint IS_NOT_ZERO = not(IS_ZERO); public static final Constraint[] ANY_ARGS = null; + public static final Constraint[] NO_ARGS = new Constraint[0]; public static Constraint same( Object o ) { |