From: Tim M. <ma...@us...> - 2003-04-04 11:13:34
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3249/src/core/com/mockobjects/dynamic Modified Files: MethodExpectation.java CallSequence.java Added Files: CallBag.java CallCollection.java Log Message: CallBag added + refactoring --- NEW FILE: CallBag.java --- package com.mockobjects.dynamic; /** * Represents a collection of calls for which the order in which they are called is not checked but the total number of times is. */ public class CallBag extends CallCollection { public CallBag() { super(); } protected int findCallIndexAndCheckArguments(Object[] args,int callnumber) { for (int i = 0; i < _expected_constraints.size(); i++) { if (!hasBeenCalled(i) && checkArguments(i, args)) { return i; } } fail(errorMessage(args)); return -1; } } --- NEW FILE: CallCollection.java --- /** Created on Oct 31, 2002 by npryce * Copyright (c) B13media Ltd. */ package com.mockobjects.dynamic; import java.util.ArrayList; import java.util.List; import com.mockobjects.Verifiable; import com.mockobjects.constraint.*; import com.mockobjects.util.AssertMo; /** A {@link MockCall} that expects a sequence of calls, checks their * arguments and mocks their behaviour. * A test will fail if too few or too many calls are made. */ public abstract class CallCollection extends AssertMo implements MockCall, Verifiable { protected List _expected_calls = new ArrayList(); protected List _expected_constraints = new ArrayList(); public static final String EXPECTED_CALLS="expected calls were:"; public static final String NO_EXPECTED_CALLS="no expected calls were set"; public static final String CALLED="called already" ; public static final String NOT_CALLED="not called"; public static final String ARGS="\nreceived arguments were "; private int numcalls = 0; protected List _hasBeenCalled = new ArrayList(); public CallCollection() { super(); } public void expect(MockCall call, Constraint[] constraints) { _expected_calls.add(call); _expected_constraints.add(constraints); _hasBeenCalled.add(Boolean.FALSE); } /* The following were copy-and-pasted from Mock.java with minor editing. * Is there any way to remove this duplication? */ /** Expect a method call and return a result when it is called. * * @param args * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the * epxected arguments of the method call. The length of the array defines * the expected arity of the method call. * @param result * 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. */ public void expectAndReturn(Constraint[] args, Object result) { expect(new MockReturnCall(args, result), args); } /** Expect a method call and return a result when it is called. * * @param arg * An single object that will be compared with predicate same() * @param result * 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. */ public void expectAndReturn(Object arg, Object result) { expectAndReturn(C.args(C.eq(arg)), result); } /** Expect a method call with no parameters and return a result when it is called. * * @param result * 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. */ public void expectAndReturn(Object result) { expectAndReturn(Mock.NO_ARGS, result); } /** Expect a call to a method with a void return type. * * @param args * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the * epxected arguments of the method call. The length of the array defines * the expected arity of the method call. */ public void expectVoid(Constraint[] args) { expect(new MockVoidCall(args), args); } /** Expect a call to a method with a void return type. * * @param method * The name of the method that will be called. * @param arg * An single object that will be compared with predicate eq() */ public void expectVoid(Object arg) { expectVoid(C.args(C.eq(arg))); } /** Expect a call to a method with a void return type and no parameters */ public void expectVoid() { expectVoid(Mock.NO_ARGS); } /** Expect a method call and throw an exception or error when it is called. * * @param args * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the * epxected arguments of the method call. The length of the array defines * the expected arity of the method call. * @param exception * The exception or error that will be thrown as a result of this call. */ public void expectAndThrow(Constraint[] args, Throwable exception) { expect(new MockThrowCall(args, exception), args); } /** Expect a method call and throw an exception or error when it is called. * * @param arg * 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. */ public void expectAndThrow(Object arg, Throwable exception) { expectAndThrow(C.args(C.eq(arg)), exception); } /** Expect a method call with no parameters and throw an exception or error when it is called. * * @param exception * The exception or error that will be thrown as a result of this call. */ public void expectAndThrow(Throwable exception) { expectAndThrow(Mock.NO_ARGS, exception); } public Object call(Object[] args) throws Throwable { numcalls++; assertTrue(errorMessage(args), numcalls <= _expected_calls.size()); int callIndex = findCallIndexAndCheckArguments(args,numcalls); MockCall call = (MockCall) _expected_calls.get(callIndex); _hasBeenCalled.add(callIndex, Boolean.TRUE); return call.call(args); } abstract protected int findCallIndexAndCheckArguments(Object[] args,int callNumber); protected boolean checkArguments(int index, Object[] args) { Constraint[] cons =(Constraint[]) _expected_constraints.get(index); int cons_count = (cons == null) ? 0 : cons.length; int arg_count = (args == null) ? 0 : args.length; if (cons_count != arg_count) { return false; } for (int i = 0; i < arg_count; i++) { if (!cons[i].eval(args[i])) { return false; } } return true; } public void verify() { assertTrue(errorMessage(null), _expected_calls.size() == numcalls); } public String printConstraint(int i) { if (i < 0 || i > (_expected_constraints.size() - 1)) { return ""; } Constraint[] cons = (Constraint[]) _expected_constraints.get(i); return printArgsArray(cons); } protected String printArgsArray(Object[] array) { if (array.length == 0) { return "[no args]"; } StringBuffer result = new StringBuffer("["); for (int j = 0; j < array.length; j++) { result.append(array[j].toString()); result.append(','); } result.deleteCharAt(result.length() - 1); result.append(']'); return result.toString(); } public String errorMessage(Object[] args) { StringBuffer result = new StringBuffer(EXPECTED_CALLS); if (_expected_constraints.size() == 0) { result = new StringBuffer(NO_EXPECTED_CALLS); } for (int i = 0; i < _expected_constraints.size(); i++) { result.append('\n'); result.append(printConstraint(i)); result.append(" :" + (hasBeenCalled(i) ? CALLED : NOT_CALLED)); } if (args != null) { result.append( ARGS+ printArgsArray(args)); } return result.toString(); } protected boolean hasBeenCalled(int i) { return ((Boolean) _hasBeenCalled.get(i)).booleanValue(); } } Index: MethodExpectation.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MethodExpectation.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MethodExpectation.java 24 Nov 2002 11:03:28 -0000 1.5 +++ MethodExpectation.java 4 Apr 2003 11:13:31 -0000 1.6 @@ -55,9 +55,12 @@ prerequisites.verify(); } - public void verify() { - wasCalled.verify(); - } + public void verify() { + wasCalled.verify(); + if (mockCall instanceof Verifiable) { + ((CallCollection) mockCall).verify(); + } + } private void initializeWasCalled() { wasCalled = new ExpectationValue(name); Index: CallSequence.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallSequence.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CallSequence.java 24 Nov 2002 11:02:38 -0000 1.3 +++ CallSequence.java 4 Apr 2003 11:13:31 -0000 1.4 @@ -1,147 +1,28 @@ -/** Created on Oct 31, 2002 by npryce - * Copyright (c) B13media Ltd. - */ package com.mockobjects.dynamic; -import java.util.ArrayList; -import java.util.List; - -import com.mockobjects.Verifiable; -import com.mockobjects.constraint.*; -import com.mockobjects.util.AssertMo; - - -/** A {@link MockCall} that expects a sequence of calls, checks their - * arguments and mocks their behaviour. - * A test will fail if too few or too many calls are made. +/** + * @author chris + * + * Represents a collection of calls on the same method for which the order of the calls is important */ -public class CallSequence - extends AssertMo - implements MockCall, Verifiable -{ - List _expected_calls = new ArrayList(); - - public CallSequence() { - super(); - } - - public void expect( MockCall call ) { - _expected_calls.add(call); - } - - /* The following were copy-and-pasted from Mock.java with minor editing. - * Is there any way to remove this duplication? - */ - - /** Expect a method call and return a result when it is called. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param result - * 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. - */ - public void expectReturn( Constraint[] args, Object result ) { - expect( new MockReturnCall( args, result ) ); - } - - /** Expect a method call and return a result when it is called. - * - * @param arg - * An single object that will be compared with predicate same() - * @param result - * 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. - */ - public void expectReturn( Object arg, Object result ) { - expectReturn( C.args(C.eq(arg) ), result); - } - - /** Expect a method call with no parameters and return a result when it is called. - * - * @param result - * 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. - */ - public void expectReturn( Object result ) { - expectReturn( Mock.NO_ARGS, result ); - } - - /** Expect a call to a method with a void return type. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - */ - public void expectVoid( Constraint[] args ) { - expect( new MockVoidCall( args ) ); - } - - /** Expect a call to a method with a void return type. - * - * @param method - * The name of the method that will be called. - * @param arg - * An single object that will be compared with predicate eq() - */ - public void expectVoid( Object arg ) { - expectVoid( C.args(C.eq(arg)) ); - } +public class CallSequence extends CallCollection { - /** Expect a call to a method with a void return type and no parameters - */ - public void expectVoid() { - expectVoid( Mock.NO_ARGS ); - } + /** + * Constructor for CallSequence. + */ + public CallSequence() { + super(); + } + + protected int findCallIndexAndCheckArguments(Object[] args,int callnumber) { + int callIndex = callnumber - 1; + if (! checkArguments(callIndex, args) ){ + fail(errorMessage(args)); + } + return callIndex; + } + - /** Expect a method call and throw an exception or error when it is called. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param exception - * The exception or error that will be thrown as a result of this call. - */ - public void expectThrow( Constraint[] args, Throwable exception ) { - expect( new MockThrowCall( args, exception ) ); - } - /** Expect a method call and throw an exception or error when it is called. - * - * @param arg - * 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. - */ - public void expectThrow( Object arg, Throwable exception ) { - expectThrow( C.args(C.eq(arg)), exception ); - } - /** Expect a method call with no parameters and throw an exception or error when it is called. - * - * @param exception - * The exception or error that will be thrown as a result of this call. - */ - public void expectThrow( Throwable exception ) { - expectThrow( Mock.NO_ARGS, exception ); - } - - - public Object call(Object[] args) throws Throwable { - assertTrue("Too many calls", 0 != _expected_calls.size()); - - MockCall call = (MockCall)_expected_calls.remove(0); - return call.call( args ); - } - - public void verify() { - assertEquals("Too few calls", 0, _expected_calls.size()); - } } |