From: Nat P. <np...@us...> - 2002-10-31 17:03:50
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory usw-pr-cvs1:/tmp/cvs-serv16430/src/core/com/mockobjects/dynamic Modified Files: P.java Mock.java Added Files: CallCounter.java MockReturnCall.java MockCall.java MockVoidCall.java MockThrowCall.java Removed Files: ExpectedThrow.java ExpectedCall.java ExpectedReturn.java Trainer.java Log Message: Removed Trainer while refactoring Mock class Extracted MockCall interface from ExpectedCall abstract class Moved argument checking into ExpectedCall Changed uses of ExpectedCall to uses of MockCall Renamed ExpectedCall etc. to MockCall etc. Added CallCounter to decorator to show advantages of MockCall interface Cleaned up some javadoc --- NEW FILE: CallCounter.java --- /** Created on Oct 31, 2002 by npryce * Copyright (c) B13media Ltd. */ package com.mockobjects.dynamic; import com.mockobjects.Verifiable; import com.mockobjects.util.AssertMo; public class CallCounter extends AssertMo implements MockCall, Verifiable { private MockCall _call; private int _expected_calls; public CallCounter( int expected_calls, MockCall call ) { _call = call; _expected_calls = expected_calls; } public Object call(Object[] args) throws Throwable { if( _expected_calls == 0 ) { fail( "too many calls" ); } _expected_calls--; return _call.call(args); } public void verify() { if( _expected_calls != 0 ) { fail("expected " + _expected_calls + " additional call(s)" ); } } } --- NEW FILE: MockReturnCall.java --- /* Copyright (c) 2002 Nat Pryce. All rights reserved. * * Created on February 10, 2002, 11:59 PM */ package com.mockobjects.dynamic; import com.mockobjects.dynamic.MockVoidCall; /** An mocked call to a method that returns a result. */ class MockReturnCall extends MockVoidCall { private Object _result; public MockReturnCall( Object result ) { this( ANY_ARGS, result ); } public MockReturnCall( Predicate[] expected_args, Object result ) { super( expected_args ); _result = result; } public Object call( Object[] args ) throws Throwable { super.call(args); return _result; } } --- NEW FILE: MockCall.java --- /** Created on Oct 31, 2002 by npryce * Copyright (c) B13media Ltd. */ package com.mockobjects.dynamic; /** An object that mocks the behaviour of a method call. */ public interface MockCall { Object call( Object[] args ) throws Throwable; } --- NEW FILE: MockVoidCall.java --- /* Copyright (c) 2002 Nat Pryce. All rights reserved. * * Created on February 10, 2002, 11:09 PM */ package com.mockobjects.dynamic; /** An mocked call to a method of that has a void result. */ public class MockVoidCall extends junit.framework.Assert implements MockCall { /** A constant that indicates that the <code>MockVoidCall</code> accepts * any arguments, that is, that the call does not check the arguments * passed to it. */ static final Predicate[] ANY_ARGS = null; private Predicate[] _expected_args; /** Constructs an <code>MockVoidCall</code> that accepts any arguments. * * @param name * The name of the expected method. */ public MockVoidCall() { this( ANY_ARGS ); } /** Constructs a <code>MockVoidCall</code> that checks its arguments. * * @param name * The name of the expected method. * @param expected_args * Predicates that define the valid arguments of the method. */ public MockVoidCall( Predicate[] expected_args ) { _expected_args = expected_args; } public Object call( Object[] args ) throws Throwable { if( isTestingArguments() ) { checkArguments( args ); } return Mock.VOID; } private boolean isTestingArguments() { return _expected_args != null; } private void checkArguments( Object[] args ) { int arg_count = (args == null) ? 0 : args.length; assertEquals( "wrong number of arguments", getArgumentCount(), arg_count ); for( int i = 0; i < arg_count; i++ ) { Object arg = args[i]; checkArgument(i, arg); } } private int getArgumentCount() { return _expected_args.length; } private void checkArgument( int i, Object arg ) { if( !_expected_args[i].eval(arg) ) { fail( "unexpected argument " + (i+1) + ": " + ", expected " + describeArgument(i) + ", was <" + arg + ">" ); } } public String describeArgument( int n ) { if( _expected_args == null ) { return "anything"; } else { return _expected_args[n].toString(); } } } --- NEW FILE: MockThrowCall.java --- /* Copyright (c) 2002 B13media Ltd. All rights reserved. * * Created on February 10, 2002, 11:59 PM */ package com.mockobjects.dynamic; import com.mockobjects.dynamic.MockVoidCall; /** A mocked call to a method results in a thrown exception. */ class MockThrowCall extends MockVoidCall { private Throwable _exception; public MockThrowCall( Throwable exception ) { this( ANY_ARGS, exception ); } public MockThrowCall( Predicate[] expected_args, Throwable exception ) { super( expected_args ); _exception = exception; } public Object call( Object[] args ) throws Throwable { super.call(args); throw _exception; } } Index: P.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/P.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- P.java 25 Oct 2002 21:06:28 -0000 1.3 +++ P.java 31 Oct 2002 17:03:45 -0000 1.4 @@ -18,6 +18,8 @@ public static final Predicate IS_ZERO = eq(new Integer(0)); public static final Predicate IS_NOT_ZERO = not(IS_ZERO); + public static final Predicate[] ANY_ARGS = MockVoidCall.ANY_ARGS; + public static Predicate same( Object o ) { return new IsSame(o); Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Mock.java 27 Oct 2002 22:29:23 -0000 1.5 +++ Mock.java 31 Oct 2002 17:03:45 -0000 1.6 @@ -14,8 +14,7 @@ import java.util.*; -/** - * A convenient class for creating simple +/** A convenient class for creating simple * <a href="http://www.mockobjects.com">mock objects</a>. */ public class Mock @@ -24,13 +23,12 @@ { public static final Object VOID = new Object(); - /** - * A convenient constant for defining expectations for methods that + /** A convenient constant for defining expectations for methods that * have no methods. */ public static final Predicate[] NO_ARGS = new Predicate[0]; - - private String _name; + + private Map _expectations = new HashMap(); private Set _not_called_expectations = new HashSet(); private Map _order_constraints = new HashMap(); @@ -40,12 +38,9 @@ private Map _default_results = new HashMap(); - /** - * Creates a named Mock object, The name will be included in the messages - * of exceptions thrown to indicate violated expectations. + /** Creates a new Mock object. */ - public Mock( String name ) { - _name = name; + public Mock() { setupDefaultResult( byte.class, new Byte((byte)0) ); setupDefaultResult( short.class, new Short((short)0) ); setupDefaultResult( int.class, new Integer(0) ); @@ -57,27 +52,7 @@ setupDefaultResult( String.class, "" ); } - /** - * Creates a Mock object and automatically assigns a name to it. The assigned - * name will include the name of the concrete class and a unique identifier of - * this instance, but will not be particularly user friendly when included in - * error messages. Therefore, it is recommended that you explictly name mock - * objects if you use more than one in the same test case. - */ - public Mock() { - this(null); - _name = super.toString(); - } - - /** - * Returns the Mock's name. - */ - public String toString() { - return _name; - } - - /** - * Is the mock in strict mode? In strict mode the mock will throw + /** Is the mock in strict mode? In strict mode the mock will throw * {@link junit.framework.AssertionFailedError} exceptions when * unexpected method calls are made. Otherwise, the mock ignore * the call or return default arguments. @@ -85,28 +60,25 @@ public boolean isStrict() { return _strict; } - + public void setStrict( boolean strict ) { _strict = strict; } - - /** - * Mock the behaviour of a method without requiring that the method + + /** Mock the behaviour of a method without requiring that the method * actually be called. Note: the ExpectedCall may check the arguments * of the call but should usually not do so. * * @param call * The call to be mocked. */ - public void setup( ExpectedCall call ) { - String methodName = call.getMethodName(); - _expectations.put( methodName, call ); - _not_called_expectations.remove( methodName ); - _called_methods.add( methodName ); + public void setup( String method_name, MockCall call ) { + _expectations.put( method_name, call ); + _not_called_expectations.remove( method_name ); + _called_methods.add( method_name ); } - - /** - * Set up calls to <var>method</var> to return <var>result</var>, + + /** Set up calls to <var>method</var> to return <var>result</var>, * but do not define any expectations upon arguments to those calls. * Arguments of calls to <var>method</var> will be ignored. * @@ -118,11 +90,10 @@ * before being returned to the caller of the method. */ public void setupResult( String method_name, Object result ) { - setup( new ExpectedReturn( method_name, null, result ) ); + setup( method_name, new MockReturnCall( result ) ); } - /** - * Set up calls to <var>method</var> to throw <var>exception</var>, + /** Set up calls to <var>method</var> to throw <var>exception</var>, * but do not define any expectations upon arguments to those calls. * Arguments of calls to <var>method</var> will be ignored. * @@ -132,11 +103,10 @@ * The exception or error that will be thrown as a result of this call. */ public void setupThrow( String method_name, Throwable exception ) { - setup( new ExpectedThrow( method_name, null, exception ) ); + setup( method_name, new MockThrowCall( exception ) ); } - /** - * Set up the value returned by methods that have the given result + /** Set up the value returned by methods that have the given result * type, if no result or exception has been explicitly defined for * the called method by {@link #setupResult}, {@link #setupThrow}, * {@link #expectReturn} or {@link #expectThrow}. @@ -151,21 +121,18 @@ _default_results.put( result_type, result_value ); } - /** - * Expect a method call and mock the behaviour of that call. - * + /** Expect a method call and mock the behaviour of that call. + * * @param call * An object describing the expected call and mocking its behaviour. */ - public void expect( ExpectedCall call ) { - String methodName = call.getMethodName(); - _expectations.put( methodName, call ); - _not_called_expectations.remove( methodName ); - _called_methods.remove( methodName ); + public void expect( String method_name, MockCall call ) { + _expectations.put( method_name, call ); + _not_called_expectations.remove( method_name ); + _called_methods.remove( method_name ); } - - /** - * Expect a method call and return a result when it is called. + + /** Expect a method call and return a result when it is called. * * @param method * The name of the method that will be called. @@ -179,11 +146,10 @@ * before being returned to the caller of the method. */ public void expectReturn( String method, Predicate[] args, Object result ) { - expect( new ExpectedReturn( method, args, result ) ); + expect( method, new MockReturnCall( args, result ) ); } - /** - * Expect a method call and return a result when it is called. + /** Expect a method call and return a result when it is called. * * @param method * The name of the method that will be called. @@ -195,11 +161,10 @@ * before being returned to the caller of the method. */ public void expectReturn( String method, Object arg, Object result ) { - expectReturn(method, P.args(P.eq(arg)), result); + expectReturn( method, P.args(P.eq(arg) ), result); } - /** - * Expect a method call with no parameters and return a result when it is called. + /** Expect a method call with no parameters and return a result when it is called. * * @param method * The name of the method that will be called. @@ -209,7 +174,7 @@ * before being returned to the caller of the method. */ public void expectReturn( String method, Object result ) { - expectReturn(method, NO_ARGS, result ); + expectReturn( method, NO_ARGS, result ); } /** @@ -223,7 +188,7 @@ * the expected arity of the method call. */ public void expectVoid( String method, Predicate[] args ) { - expect( new ExpectedReturn( method, args, VOID ) ); + expect( method, new MockVoidCall( args ) ); } /** @@ -235,7 +200,7 @@ * An single object that will be compared with predicate same() */ public void expectVoid(String method, Object arg) { - expectVoid(method, P.args(P.eq(arg))); + expectVoid( method, P.args(P.eq(arg)) ); } /** @@ -245,11 +210,10 @@ * The name of the method that will be called. */ public void expectVoid(String method) { - expectVoid(method, NO_ARGS); + expectVoid( method, NO_ARGS ); } - /** - * Expect a method call and throw an exception or error when it is called. + /** Expect a method call and throw an exception or error when it is called. * * @param method * The name of the method that will be called. @@ -261,11 +225,10 @@ * The exception or error that will be thrown as a result of this call. */ public void expectThrow( String method, Predicate[] args, Throwable exception ) { - expect( new ExpectedThrow( method, args, exception ) ); + expect( method, new MockThrowCall( args, exception ) ); } - /** - * Expect a method call and throw an exception or error when it is called. + /** Expect a method call and throw an exception or error when it is called. * * @param method * The name of the method that will be called. @@ -278,8 +241,7 @@ expectThrow( method, P.args(P.eq(arg)), exception ); } - /** - * Expect a method call with no parameters and throw an exception or error when it is called. + /** Expect a method call with no parameters and throw an exception or error when it is called. * * @param method * The name of the method that will be called. @@ -289,7 +251,7 @@ public void expectThrow( String method, Throwable exception ) { expectThrow( method, NO_ARGS, exception ); } - + /** * Expect a method not to be called. * An {@link junit.framework.AssertionFailedError} will be thrown if @@ -318,8 +280,7 @@ preceding_calls.add( preceding_method ); } - /** - * Called by the {@link java.lang.reflect.Proxy} to mock the behaviour of an + /** Called by the {@link java.lang.reflect.Proxy} to mock the behaviour of an * invoked method and check expectations. */ public Object invoke( Object obj, Method method, Object[] args ) @@ -335,40 +296,36 @@ return mockCall( method, args ); } } - + protected Object mockCall( Method method, Object[] args ) throws Throwable { String method_name = method.getName(); assertCanBeCalled( method_name ); - + if( _expectations.containsKey( method_name ) ) { - ExpectedCall expected = (ExpectedCall)_expectations.get(method_name); - + MockCall expected = (MockCall)_expectations.get(method_name); + checkCallOrder( method_name ); - if( expected.isTestingArguments() ) { - checkArguments( expected, args ); - } - - return checkResult(method, expected.eval( args )); + return checkResult(method, expected.call( args )); } else if( _strict ) { throw new AssertionFailedError( - _name + ": unexpected call to " + method_name ); + "unexpected call to " + method_name ); } else { return defaultResult( method.getReturnType() ); } } - + private Object checkResult(Method method, Object result) { - if( method.getReturnType() == void.class && VOID != result ) { + if( method.getReturnType() == void.class && result != VOID ) { fail("trying to return " + result + " from void method"); } return result; } - + private void checkCallOrder( String method_name ) { if( _order_constraints.containsKey(method_name) ) { assertMethodsHaveBeenCalled(orderConstraintsFor(method_name) ); @@ -379,24 +336,6 @@ return (Set)_order_constraints.get(method_name); } - private void checkArguments( ExpectedCall expected, Object[] args ) - { - int arg_count = (args == null) ? 0 : args.length; - assertEquals( _name + ": wrong number of arguments to " + - expected.getMethodName() + " method", - expected.getArgumentCount(), arg_count ); - - for( int i = 0; i < arg_count; i++ ) { - Object arg = args[i]; - if( !expected.testArgument( i, arg ) ) { - fail( _name + ": unexpected argument " + (i+1) + - " to " + expected.getMethodName() + " method" + - ", expected " + expected.describeArgument(i) + - ", was " + arg ); - } - } - } - private Object defaultResult(Class return_type) { return _default_results.get(return_type); } @@ -424,22 +363,18 @@ private void assertHasBeenCalled( String method_name ) { if( !_called_methods.contains(method_name) ) { - fail( _name + ": method " + method_name + " was not called" ); + fail( "method " + method_name + " was not called" ); } } private void assertCanBeCalled( String method_name ) { if( _not_called_expectations.contains(method_name) ) { - fail(_name + ": unexpected call to method " + method_name ); + fail( "unexpected call to method " + method_name ); } } - + public Object createInterface( Class interface_class ) { return createInterface(interface_class, this); - } - - public Object getTrainer( Class interfaceClass ) { - return createInterface(interfaceClass, new Trainer( this )); } private Object createInterface(Class interface_class, InvocationHandler handler) { --- ExpectedThrow.java DELETED --- --- ExpectedCall.java DELETED --- --- ExpectedReturn.java DELETED --- --- Trainer.java DELETED --- |