From: Steve F. <sm...@us...> - 2003-08-22 04:30:13
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv5322/src/core/test/mockobjects/dynamic Modified Files: InvocationDispatcherTest.java MockInvokable.java Log Message: Further implementation for InvocationDispatcher Index: InvocationDispatcherTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/InvocationDispatcherTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- InvocationDispatcherTest.java 20 Aug 2003 21:48:24 -0000 1.1 +++ InvocationDispatcherTest.java 20 Aug 2003 22:38:40 -0000 1.2 @@ -16,7 +16,8 @@ private Invocation invocation; private InvocationDispatcher dispatcher; - private MockInvokable invokable = new MockInvokable(); + private MockInvokable invokable1 = new MockInvokable(); + private MockInvokable invokable2 = new MockInvokable(); public void setUp() throws NoSuchMethodException { invocation = new Invocation(getDummyMethod(), null); @@ -36,32 +37,129 @@ fail("expected AssertionFailedError"); } - public void testInvokesOneMatchingInvokable() throws Throwable { + public void testInvokesInvokableThatMatches() throws Throwable { Object result = "invoke result"; - invokable.matchesInvocation.setExpected(invocation); - invokable.matchesResult = true; - invokable.invokeInvocation.setExpected(invocation); - invokable.invokeResult = result; + invokable1.matchesInvocation.setExpected(invocation); + invokable1.matchesResult = true; + invokable1.invokeInvocation.setExpected(invocation); + invokable1.invokeResult = result; - dispatcher.add( invokable ); + dispatcher.add( invokable1 ); dispatcher.dispatch(invocation); - invokable.verify(); + invokable1.verifyExpectations(); } public void testReturnsValueFromInvokable() throws Throwable { Object result = "invoke result"; - invokable.matchesResult = true; - invokable.invokeResult = result; + invokable1.matchesResult = true; + invokable1.invokeResult = result; - dispatcher.add( invokable ); + dispatcher.add( invokable1 ); assertSame( "should be same result", result, dispatcher.dispatch(invocation) ); } + public void testPropagatesExceptionFromInvokable() throws Throwable { + Throwable exception = new Throwable("test throwable"); + + invokable1.matchesResult = true; + invokable1.invokeThrow = exception; + + dispatcher.add( invokable1 ); + + try { + dispatcher.dispatch(invocation); + fail("expected exception"); + } + catch( Throwable t ) { + assertSame( "should be same exception", exception, t ); + } + } + + public void testInvokeFailsWhenNoInvokablesMatch() throws Throwable { + invokable1.matchesResult = false; + invokable2.matchesResult = false; + + dispatcher.add( invokable1 ); + dispatcher.add( invokable2 ); + + try { + dispatcher.dispatch(invocation); + } + catch( DynamicMockError ex ) { + assertSame("should be same invocation", invocation, ex.invocation); + return; + } + fail("expected AssertionFailedError"); + } + + public void testLaterInvokablesOverrideEarlierInvokables() throws Throwable { + invokable1.matchesInvocation.setExpectNothing(); + invokable1.matchesResult = true; + invokable1.invokeInvocation.setExpectNothing(); + + invokable2.matchesInvocation.setExpected(invocation); + invokable2.matchesResult = true; + invokable2.invokeInvocation.setExpected(invocation); + + + dispatcher.add( invokable1 ); + dispatcher.add( invokable2 ); + + dispatcher.dispatch( invocation ); + + verifyInvokables(); + } + + public void testSearchesForMatchInLIFOOrder() throws Throwable { + invokable1.matchesInvocation.setExpected(invocation); + invokable1.matchesResult = true; + invokable1.invokeInvocation.setExpected(invocation); + invokable1.invokeResult = "one"; + + invokable2.matchesInvocation.setExpected(invocation); + invokable2.matchesResult = false; + invokable2.invokeInvocation.setExpectNothing(); + + + dispatcher.add( invokable1 ); + dispatcher.add( invokable2 ); + + assertEquals("Should be invokable1", "one", dispatcher.dispatch( invocation )); + + verifyInvokables(); + } + + public void testVerifiesAllInvokables() { + invokable1.verifyCalls.setExpected(1); + invokable2.verifyCalls.setExpected(1); + + dispatcher.add( invokable1 ); + dispatcher.add( invokable2 ); + + dispatcher.verify(); + + verifyInvokables(); + } + + public void testClearRemovesAllInvokables() throws Throwable { + invokable1.matchesResult = true; + + dispatcher.add( invokable1 ); + + dispatcher.clear(); + testInvokeFailsWhenEmpty(); + } + private Method getDummyMethod() throws NoSuchMethodException { return getClass().getDeclaredMethod("dummyMethod", new Class[0]); } + + private void verifyInvokables() { + invokable1.verifyExpectations(); + invokable2.verifyExpectations(); + } } Index: MockInvokable.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockInvokable.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MockInvokable.java 20 Aug 2003 21:48:24 -0000 1.1 +++ MockInvokable.java 20 Aug 2003 22:38:41 -0000 1.2 @@ -4,29 +4,46 @@ */ package test.mockobjects.dynamic; +import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationValue; -import com.mockobjects.MockObject; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.Invokable; +import com.mockobjects.util.Verifier; -public class MockInvokable extends MockObject implements Invokable { +public class MockInvokable implements Invokable { public boolean matchesResult; public ExpectationValue matchesInvocation = new ExpectationValue("matches.invocation"); public Object invokeResult; public ExpectationValue invokeInvocation = new ExpectationValue("invoke.invocation"); + public Throwable invokeThrow; + public ExpectationCounter verifyCalls = new ExpectationCounter("verify.calls"); + 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); + if (invokeThrow != null) { + throw invokeThrow; + } return invokeResult; } + public void verify() { + verifyCalls.inc(); + } + + public void verifyExpectations() { + Verifier.verifyObject(this); + } + } |