From: Steve F. <sm...@us...> - 2002-11-05 16:04:38
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory usw-pr-cvs1:/tmp/cvs-serv23805/src/core/com/mockobjects/dynamic Modified Files: Mock.java Added Files: MethodMap.java MethodExpectation.java Log Message: Refactored MethodExpectation and MethodMap out of Mock Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- Mock.java 1 Nov 2002 15:35:40 -0000 1.7 +++ Mock.java 5 Nov 2002 16:04:32 -0000 1.8 @@ -6,7 +6,6 @@ import com.mockobjects.Verifiable; import junit.framework.Assert; -import junit.framework.AssertionFailedError; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -27,12 +26,8 @@ * have no methods. */ public static final Predicate[] NO_ARGS = new Predicate[0]; - - - private Map _expectations = new HashMap(); - private Set _not_called_expectations = new HashSet(); - private Map _order_constraints = new HashMap(); - private Set _called_methods = new HashSet(); + + protected MethodMap methods = new MethodMap(); private boolean _strict = false; private Map _default_results = new HashMap(); @@ -73,9 +68,7 @@ * The call to be mocked. */ public void setup( String method_name, MockCall call ) { - _expectations.put( method_name, call ); - _not_called_expectations.remove( method_name ); - _called_methods.add( method_name ); + methods.setupCall(method_name, call); } /** Set up calls to <var>method</var> to return <var>result</var>, @@ -127,9 +120,7 @@ * An object describing the expected call and mocking its behaviour. */ public void expect( String method_name, MockCall call ) { - _expectations.put( method_name, call ); - _not_called_expectations.remove( method_name ); - _called_methods.remove( method_name ); + methods.expectCall(method_name, call); } /** Expect a method call and return a result when it is called. @@ -257,11 +248,11 @@ * An {@link junit.framework.AssertionFailedError} will be thrown if * the method is called. * - * @param method + * @param methodName * The name of the method that will not be called. */ - public void expectNotCalled( String method ) { - _not_called_expectations.add(method); + public void expectNotCalled( String methodName ) { + methods.expectNotCalled(methodName); } /** @@ -271,13 +262,7 @@ * {@link junit.framework.AssertionFailedError} will be thrown. */ public void order( String preceding_method, String subsequent_method ) { - Set preceding_calls = orderConstraintsFor(subsequent_method); - if( null == preceding_calls) { - preceding_calls = new HashSet(); - _order_constraints.put( subsequent_method, preceding_calls ); - } - - preceding_calls.add( preceding_method ); + methods.order(preceding_method, subsequent_method); } /** Called by the {@link java.lang.reflect.Proxy} to mock the behaviour of an @@ -286,54 +271,37 @@ public Object invoke( Object obj, Method method, Object[] args ) throws Throwable { - _called_methods.add( method.getName() ); - + MethodExpectation expectation = methods.startCall(method.getName()); try { - return getClass().getMethod( - method.getName(), method.getParameterTypes() ).invoke( this, args ); + return getMatchingMethod(method).invoke( this, args ); } catch( NoSuchMethodException ex ) { - return mockCall( method, args ); + return mockCall(expectation, method, args ); } } - - protected Object mockCall( Method method, Object[] args ) + + protected Object mockCall(Method method, Object[] args ) throws Throwable { - String method_name = method.getName(); - - assertCanBeCalled( method_name ); - - if( _expectations.containsKey( method_name ) ) { - MockCall expected = (MockCall)_expectations.get(method_name); - - checkCallOrder( method_name ); - return checkResult(method, expected.call( args )); - - } else if( _strict ) { - throw new AssertionFailedError( - "unexpected call to " + method_name ); - } else { - return defaultResult( method.getReturnType() ); - } + return mockCall(methods.startCall(method.getName()), method, args); } - - private Object checkResult(Method method, Object result) { - if( method.getReturnType() == void.class && result != VOID ) { - fail("trying to return " + result + " from void method"); + + protected Object mockCall(MethodExpectation expectation, Method method, Object[] args ) + throws Throwable + { + if( expectation.canBeCalled() ) { + return expectation.callAndCheckResult( method, args ); } - return result; + assertTrue("Unexpected call to " + method.getName(), ! _strict); + + return defaultResult( method.getReturnType() ); } - private void checkCallOrder( String method_name ) { - if( _order_constraints.containsKey(method_name) ) { - assertMethodsHaveBeenCalled(orderConstraintsFor(method_name) ); - } - } - - private Set orderConstraintsFor(String method_name) { - return (Set)_order_constraints.get(method_name); + private Method getMatchingMethod(Method method) + throws NoSuchMethodException + { + return getClass().getMethod( method.getName(), method.getParameterTypes() ); } private Object defaultResult(Class return_type) { @@ -346,33 +314,9 @@ * Not all expected calls were made to this mock object. */ public void verify() { - assertAllExpectedMethodsCalled(); - } - - private void assertAllExpectedMethodsCalled() { - - assertMethodsHaveBeenCalled( _expectations.keySet() ); - } - - private void assertMethodsHaveBeenCalled( Set method_names ) { - Iterator i = method_names.iterator(); - while( i.hasNext() ) { - assertHasBeenCalled( (String)i.next() ); - } - } - - private void assertHasBeenCalled( String method_name ) { - if( !_called_methods.contains(method_name) ) { - fail( "method " + method_name + " was not called" ); - } + methods.verify(); } - private void assertCanBeCalled( String method_name ) { - if( _not_called_expectations.contains(method_name) ) { - fail( "unexpected call to method " + method_name ); - } - } - public Object createInterface( Class interface_class ) { return createInterface(interface_class, this); } |