Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory usw-pr-cvs1:/tmp/cvs-serv7280/src/core/com/mockobjects/dynamic Modified Files: MockReturnCall.java Mock.java AbstractMockCall.java MockVoidCall.java MockThrowCall.java Log Message: Added more checks to catch wrong method names earlier and give meaningful failure messages Refactoring: renamed expect... methods in Mock class to match Joe's better naming convention Fixed and extended Javadoc comments. Index: MockReturnCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MockReturnCall.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockReturnCall.java 6 Nov 2002 18:57:21 -0000 1.2 +++ MockReturnCall.java 7 Nov 2002 17:45:04 -0000 1.3 @@ -14,12 +14,23 @@ { private Object _result; + /** Constructs a <code>MockReturnCall</code> that accepts any arguments. + * + * @param result + * The value returned when the method is called. + */ public MockReturnCall( Object result ) { this( ANY_ARGS, result ); } - public MockReturnCall( Predicate[] expected_args, Object result ) - { + /** Constructs a <code>MockReturnCall</code> that checks its arguments. + * + * @param expected_args + * Predicates that define the valid arguments of the method. + * @param result + * The value returned when the method is called. + */ + public MockReturnCall( Predicate[] expected_args, Object result ) { super( expected_args ); _result = result; } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- Mock.java 7 Nov 2002 13:04:21 -0000 1.12 +++ Mock.java 7 Nov 2002 17:45:05 -0000 1.13 @@ -6,6 +6,7 @@ import com.mockobjects.Verifiable; import junit.framework.Assert; +import junit.framework.AssertionFailedError; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -111,6 +112,9 @@ * * @param call * The call to be mocked. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void setup( String method_name, MockCall call ) { checkMethodName( method_name ); @@ -127,6 +131,9 @@ * The result that will be returned from this call. Primitive types * must be wrapped in the equivalent Java object, and will be unwrapped * before being returned to the caller of the method. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void setupResult( String method_name, Object result ) { setup( method_name, new MockReturnCall( result ) ); @@ -140,6 +147,9 @@ * The name of the method that will be called. * @param exception * The exception or error that will be thrown as a result of this call. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void setupThrow( String method_name, Throwable exception ) { setup( method_name, new MockThrowCall( exception ) ); @@ -164,6 +174,9 @@ * * @param call * An object describing the expected call and mocking its behaviour. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void expect( String method_name, MockCall call ) { checkMethodName( method_name ); @@ -182,8 +195,11 @@ * The result that will be returned from this call. Primitive types * must be wrapped in the equivalent Java object, and will be unwrapped * before being returned to the caller of the method. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectReturn( String method, Predicate[] args, Object result ) { + public void expectAndReturn( String method, Predicate[] args, Object result ) { expect( method, new MockReturnCall( args, result ) ); } @@ -197,9 +213,12 @@ * The result that will be returned from this call. Primitive types * must be wrapped in the equivalent Java object, and will be unwrapped * before being returned to the caller of the method. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectReturn( String method, Object arg, Object result ) { - expectReturn( method, P.args(P.eq(arg) ), result); + public void expectAndReturn( String method, Object arg, Object result ) { + expectAndReturn( method, P.args(P.eq(arg) ), result); } /** Expect a method call with no parameters and return a result when it is called. @@ -210,9 +229,12 @@ * The result that will be returned from this call. Primitive types * must be wrapped in the equivalent Java object, and will be unwrapped * before being returned to the caller of the method. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectReturn( String method, Object result ) { - expectReturn( method, NO_ARGS, result ); + public void expectAndReturn( String method, Object result ) { + expectAndReturn( method, NO_ARGS, result ); } /** @@ -224,6 +246,9 @@ * An array of {@link com.mockobjects.dynamic.Predicate}s that specify the * epxected arguments of the method call. The length of the array defines * the expected arity of the method call. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void expectVoid( String method, Predicate[] args ) { expect( method, new MockVoidCall( args ) ); @@ -236,6 +261,9 @@ * The name of the method that will be called. * @param arg * An single object that will be compared with predicate eq() + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void expectVoid(String method, Object arg) { expectVoid( method, P.args(P.eq(arg)) ); @@ -246,6 +274,9 @@ * * @param method * The name of the method that will be called. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ public void expectVoid(String method) { expectVoid( method, NO_ARGS ); @@ -261,8 +292,11 @@ * the expected arity of the method call. * @param exception * The exception or error that will be thrown as a result of this call. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectThrow( String method, Predicate[] args, Throwable exception ) { + public void expectAndThrow( String method, Predicate[] args, Throwable exception ) { expect( method, new MockThrowCall( args, exception ) ); } @@ -274,9 +308,12 @@ * An single object that will be compared with predicate eq() * @param exception * The exception or error that will be thrown as a result of this call. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectThrow( String method, Object arg, Throwable exception ) { - expectThrow( method, P.args(P.eq(arg)), exception ); + public void expectAndThrow( String method, Object arg, Throwable exception ) { + expectAndThrow( method, P.args(P.eq(arg)), exception ); } /** Expect a method call with no parameters and throw an exception or error when it is called. @@ -285,30 +322,42 @@ * The name of the method that will be called. * @param exception * The exception or error that will be thrown as a result of this call. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectThrow( String method, Throwable exception ) { - expectThrow( method, NO_ARGS, exception ); + public void expectAndThrow( String method, Throwable exception ) { + expectAndThrow( method, NO_ARGS, exception ); } - /** - * Expect a method not to be called. + /** Expect a method not to be called. * An {@link junit.framework.AssertionFailedError} will be thrown if * the method is called. * * @param methodName * The name of the method that will not be called. + * @throws junit.framework.AssertionFailedError + * The method name is not the name of a method mocked by this + * Mock object. */ - public void expectNotCalled( String methodName ) { - methods.expectNotCalled(methodName); + public void expectNotCalled( String method_name ) { + checkMethodName( method_name ); + methods.expectNotCalled(method_name); } - - /** - * Define an order between two calls. The method named + + /** Define an order between two calls. The method named * <var>subsequent_method</var> <em>must</em> be called after * the method namd <var>preceding_method</var>, otherwise an - * {@link junit.framework.AssertionFailedError} will be thrown. + * {@link junit.framework.AssertionFailedError} will be thrown + * when <var>subsequent_method</var> is called. + * + * @throws junit.framework.AssertionFailedError + * Either method name is not the name of a method mocked by this + * Mock object. */ public void order( String preceding_method, String subsequent_method ) { + checkMethodName( preceding_method ); + checkMethodName( subsequent_method ); methods.order( preceding_method, subsequent_method ); } @@ -357,7 +406,7 @@ if( expectation.canBeCalled() ) { return expectation.callAndCheckResult( method, args ); } else { - assertTrue( "Unexpected call to " + method.getName(), + assertTrue( "unexpected call to " + method.getName(), !_strict ); return defaultResult( method.getReturnType() ); } Index: AbstractMockCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/AbstractMockCall.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractMockCall.java 6 Nov 2002 18:57:21 -0000 1.1 +++ AbstractMockCall.java 7 Nov 2002 17:45:05 -0000 1.2 @@ -5,13 +5,15 @@ package com.mockobjects.dynamic; -/** An mocked call to a method of that has a void result. +/** An abstract mocked method call that checks its arguments against + * expectations, but leaves the result of the call (returned value, + * thrown exception or side effect) to derived classes. */ public abstract class AbstractMockCall extends junit.framework.Assert implements MockCall { - /** A constant that indicates that the <code>MockVoidCall</code> accepts + /** A constant that indicates that the mocked call accepts * any arguments, that is, that the call does not check the arguments * passed to it. */ @@ -21,14 +23,13 @@ private Predicate[] _expected_args; - /** Constructs an <code>MockVoidCall</code> that accepts any arguments. - * The name of the expected method. + /** Constructs an <code>AbstractMockCall</code> that accepts any arguments. */ public AbstractMockCall() { this( ANY_ARGS ); } - /** Constructs a <code>MockVoidCall</code> that checks its arguments. + /** Constructs a <code>AbstractMockCall</code> that checks its arguments. * * @param expected_args * Predicates that define the valid arguments of the method. @@ -53,7 +54,8 @@ private void checkArguments( Object[] args ) { int arg_count = (args == null) ? 0 : args.length; - assertEquals( "wrong number of arguments", getArgumentCount(), arg_count ); + assertEquals( "wrong number of arguments", + getArgumentCount(), arg_count ); for( int i = 0; i < arg_count; i++ ) { checkArgument( i, _expected_args[i], args[i]); @@ -65,13 +67,13 @@ } private void checkArgument( int i, Predicate expectedArg, Object arg ) { - assertTrue("argument " + (i+1) + "\n" + - "expected " + expectedArg + "\n" + - " was " + arg + "", + assertTrue( "argument " + (i+1) + "\n" + + "expected " + expectedArg + "\n" + + " was " + arg + "", expectedArg.eval(arg) ); } - public String describeArgument( int n ) { + private String describeArgument( int n ) { if( _expected_args == null ) { return "anything"; } else { Index: MockVoidCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MockVoidCall.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MockVoidCall.java 6 Nov 2002 18:57:21 -0000 1.3 +++ MockVoidCall.java 7 Nov 2002 17:45:06 -0000 1.4 @@ -5,15 +5,22 @@ package com.mockobjects.dynamic; -/** An mocked call to a method of that has a void result. +/** A mocked call to a method of that has a void result. */ public class MockVoidCall extends AbstractMockCall { + /** Constructs a <code>MockVoidCall</code> that accepts any arguments. + */ public MockVoidCall() { this( ANY_ARGS ); } + /** Constructs a <code>MockVoidCall</code> that checks its arguments. + * + * @param expected_args + * Predicates that define the valid arguments of the method. + */ public MockVoidCall( Predicate[] expected_args ) { super( expected_args ); } Index: MockThrowCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MockThrowCall.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockThrowCall.java 6 Nov 2002 18:57:21 -0000 1.2 +++ MockThrowCall.java 7 Nov 2002 17:45:06 -0000 1.3 @@ -6,7 +6,7 @@ import com.mockobjects.dynamic.MockVoidCall; -/** A mocked call to a method results in a thrown exception. +/** A mocked call to a method that results in a thrown exception. */ class MockThrowCall extends AbstractMockCall @@ -14,10 +14,24 @@ private Throwable _exception; + /** Constructs a <code>MockThrowCall</code> that accepts any arguments. + * + * @param exception + * The exception or error that will be thrown when this method + * is called. + */ public MockThrowCall( Throwable exception ) { this( ANY_ARGS, exception ); } + /** Constructs a <code>MockThrowCall</code> that checks its arguments. + * + * @param expected_args + * Predicates that define the valid arguments of the method. + * @param exception + * The exception or error that will be thrown when this method + * is called. + */ public MockThrowCall( Predicate[] expected_args, Throwable exception ) { |