From: Steve F. <sm...@us...> - 2003-08-22 04:56:53
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv29581/src/core/test/mockobjects/dynamic Added Files: InvocationDispatcherTest.java MockInvokable.java InvocationTest.java Log Message: New classes for Invocation-based refactoring --- NEW FILE: InvocationDispatcherTest.java --- /* * Created on 20-Aug-2003 * Copyright mockobjects.com */ package test.mockobjects.dynamic; import java.lang.reflect.Method; import junit.framework.TestCase; import com.mockobjects.dynamic.DynamicMockError; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.InvocationDispatcher; public class InvocationDispatcherTest extends TestCase { private Invocation invocation; private InvocationDispatcher dispatcher; private MockInvokable invokable = new MockInvokable(); public void setUp() throws NoSuchMethodException { invocation = new Invocation(getDummyMethod(), null); dispatcher = new InvocationDispatcher(); } public void dummyMethod() {}; public void testInvokeFailsWhenEmpty() throws Throwable { try { dispatcher.dispatch(invocation); } catch( DynamicMockError ex ) { assertSame("should be same invocation", invocation, ex.invocation); return; } fail("expected AssertionFailedError"); } public void testInvokesOneMatchingInvokable() throws Throwable { Object result = "invoke result"; invokable.matchesInvocation.setExpected(invocation); invokable.matchesResult = true; invokable.invokeInvocation.setExpected(invocation); invokable.invokeResult = result; dispatcher.add( invokable ); dispatcher.dispatch(invocation); invokable.verify(); } public void testReturnsValueFromInvokable() throws Throwable { Object result = "invoke result"; invokable.matchesResult = true; invokable.invokeResult = result; dispatcher.add( invokable ); assertSame( "should be same result", result, dispatcher.dispatch(invocation) ); } private Method getDummyMethod() throws NoSuchMethodException { return getClass().getDeclaredMethod("dummyMethod", new Class[0]); } } --- NEW FILE: MockInvokable.java --- /* * Created on 20-Aug-2003 * Copyright mockobjects.com */ package test.mockobjects.dynamic; import com.mockobjects.ExpectationValue; import com.mockobjects.MockObject; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.Invokable; public class MockInvokable extends MockObject implements Invokable { public boolean matchesResult; public ExpectationValue matchesInvocation = new ExpectationValue("matches.invocation"); public Object invokeResult; public ExpectationValue invokeInvocation = new ExpectationValue("invoke.invocation"); public String getDescription() { return null; } public boolean matches(Invocation invocation) { matchesInvocation.setActual(invocation); return matchesResult; } public Object invoke(Invocation invocation) throws Throwable { invokeInvocation.setActual(invocation); return invokeResult; } } --- NEW FILE: InvocationTest.java --- /** Created on Jun 17, 2003 by npryce * Copyright (c) B13media Ltd. */ package test.mockobjects.dynamic; import java.lang.reflect.Method; import java.util.Arrays; import junit.framework.TestCase; import com.mockobjects.dynamic.Invocation; public class InvocationTest extends TestCase { public String exampleMethod( int number, boolean flag ) { return "hello, world"; } final String METHOD_NAME = "exampleMethod"; final Class[] ARG_TYPES = { int.class, boolean.class }; final Class RETURN_TYPE = String.class; final Object[] ARG_VALUES = { new Integer(0), Boolean.TRUE }; public InvocationTest(String name) { super(name); } public void testCanBeConstructedWithExplicitCallDetails() { Invocation call = new Invocation( METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); assertEquals( "name", METHOD_NAME, call.getMethodName() ); assertEquals( "parameter types", Arrays.asList(ARG_TYPES), call.getParameterTypes() ); assertEquals( "return type", RETURN_TYPE, call.getReturnType() ); assertEquals( "argument values", Arrays.asList(ARG_VALUES), call.getParameterValues() ); } public void testCanBeConstructedFromAMethodObject() throws Exception { Method method = getClass().getMethod( METHOD_NAME, ARG_TYPES ); Invocation call = new Invocation( method, ARG_VALUES ); assertEquals( "name", method.getName(), call.getMethodName() ); assertEquals( "parameter types", Arrays.asList(method.getParameterTypes()), call.getParameterTypes() ); assertEquals( "return type", method.getReturnType(), call.getReturnType() ); assertEquals( "argument values", Arrays.asList(ARG_VALUES), call.getParameterValues() ); } public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() { Invocation call = new Invocation( METHOD_NAME, new Class[0], RETURN_TYPE, null ); assertEquals( "expected no parameters values", 0, call.getParameterValues().size() ); } public void testTestsForEqualityOnMethodSignatureAndArguments() { Invocation call1 = new Invocation( METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); Invocation call2 = new Invocation( METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); Invocation differentName = new Invocation( "other" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); Invocation differentReturnType = new Invocation( "other" + METHOD_NAME, ARG_TYPES, int.class, ARG_VALUES ); Invocation differentArgTypes = new Invocation( "other" + METHOD_NAME, new Class[]{double.class}, RETURN_TYPE, ARG_VALUES ); Invocation differentArgValues = new Invocation( "other" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, new Object[] { new Integer(1), Boolean.FALSE } ); assertTrue( "should be equal to itself", call1.equals(call1) ); assertTrue( "identical calls should be equal", call1.equals(call2) ); assertFalse( "should not be equal to object that is not an ActiveCall", call1.equals(new Object()) ); assertFalse( "should not be equal to null", call1.equals(null) ); assertFalse( "should not be equal if different name", call1.equals(differentName) ); assertFalse( "should not be equal if different parameter types", call1.equals(differentArgTypes) ); assertFalse( "should not be equal if different return type", call1.equals(differentReturnType) ); assertFalse( "should not be equal if different argumentValues", call1.equals(differentArgValues) ); } public void testFollowsEqualsHashcodeProtocol() { Invocation call1 = new Invocation( METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); Invocation call2 = new Invocation( METHOD_NAME, ARG_TYPES, RETURN_TYPE, ARG_VALUES ); assertEquals( "should have equal hash codes", call1.hashCode(), call2.hashCode() ); } } |