From: Steve F. <sm...@us...> - 2003-07-09 02:14:03
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv7796/src/core/test/mockobjects/dynamic Modified Files: CallOnceExpectationTest.java CallSequenceTest.java MockCallable.java CallSignatureTest.java CallBagTest.java Added Files: CallableArrayListTest.java TestCallBag.java MockCallableList.java Log Message: Started renaming matches to stubs Changed ordering so that CallBags search backwards Started new, cleaner test class for CallBag --- NEW FILE: CallableArrayListTest.java --- /* * Copyright mockobjects.com 08-Jul-2003 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.Callable; import com.mockobjects.dynamic.CallableArrayList; import com.mockobjects.dynamic.CallableList; import com.mockobjects.dynamic.Invocation; import com.mockobjects.ExpectationSet; import junit.framework.TestCase; public class CallableArrayListTest extends TestCase { private Invocation dummyInvocation = new Invocation("dummy", new Object[0]); public CallableArrayListTest(String name) { super(name); } private CallableArrayList list = new CallableArrayList(); private MockCallable mockCallable = new MockCallable("mock"); private MockCallable otherCallable = new MockCallable("other"); public void testEmptyList() { assertTrue("List isEmpty()", list.isEmpty()); assertEquals("List has no members", 0, list.size()); list.verify(); // should not fail } public void testListHandlesMultipleElements() { mockCallable.setExpectedVerifyCalls(1); otherCallable.setExpectedVerifyCalls(1); list.add(mockCallable); list.add(otherCallable); list.verify(); assertFalse("List is not Empty()", list.isEmpty()); assertEquals("List has 2 members", 2, list.size()); assertEquals("get first", mockCallable, list.get(0)); assertEquals("get second", otherCallable, list.get(1)); mockCallable.verifyExpectations(); otherCallable.verifyExpectations(); } public void testReturnLastMatchingCall() { mockCallable.matches = true; otherCallable.matches = true; list.add(mockCallable); list.add(otherCallable); assertEquals("Should be last match", otherCallable, list.lastMatchingCall(dummyInvocation)); } public void testApplyToAllElements() { final ExpectationSet allElements = new ExpectationSet("all elements"); list.add(mockCallable); list.add(otherCallable); allElements.addExpected(mockCallable); allElements.addExpected(otherCallable); list.apply(new CallableList.Handler() { public Callable handle(int index, Callable callable) { allElements.addActual(callable); return null; } }); allElements.verify(); } } --- NEW FILE: TestCallBag.java --- package test.mockobjects.dynamic; import com.mockobjects.dynamic.CallBag; import com.mockobjects.dynamic.Invocation; import com.mockobjects.util.AssertMo; import junit.framework.Assert; import junit.framework.AssertionFailedError; import junit.framework.TestCase; /** * Copyright mockobjects.com 09-Jul-2003 */ public class TestCallBag extends TestCase { public TestCallBag(String name) { super(name); } private MockCallable mockCallable = new MockCallable("mock callable"); private MockCallableList mockExpected = new MockCallableList(); private MockCallableList mockStubs = new MockCallableList(); private Invocation dummyInvocation = new Invocation("missingMethod", new Object[0]); private CallBag callBag = new CallBag(mockExpected, mockStubs); public void testAddExpectation() { mockExpected.addedCallables.addExpected(mockCallable); mockStubs.addedCallables.setExpectNothing(); callBag.addExpect(mockCallable); verifyAll(); } public void testAddStub() { mockExpected.addedCallables.setExpectNothing(); mockStubs.addedCallables.addExpected(mockCallable); callBag.addMatch(mockCallable); verifyAll(); } public void testResetBothExpectationsAndStubs() { mockExpected.clearCalls.setExpected(1); mockStubs.clearCalls.setExpected(1); callBag.reset(); verifyAll(); } public void testCallFailsWhenNoExpectationsOrStubs() throws Throwable { mockExpected.lastMatchingCall = null; mockStubs.lastMatchingCall = null; mockExpected.isEmpty = true; mockExpected.lastMatchingCallInvocation.setExpected(dummyInvocation); mockStubs.lastMatchingCallInvocation.setExpected(dummyInvocation); try { callBag.call(dummyInvocation); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports empty list", "no methods", ex.getMessage()); verifyAll(); return; } fail("Should fail for a missing item"); } public void testCallReportsMissingMethodWhenNothingMatches() throws Throwable { mockExpected.lastMatchingCall = null; mockStubs.lastMatchingCall = null; mockExpected.applied = mockCallable; mockExpected.lastMatchingCallInvocation.setExpected(dummyInvocation); mockStubs.lastMatchingCallInvocation.setExpected(dummyInvocation); try { callBag.call(dummyInvocation); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports missing method", mockCallable.getDescription(), ex.getMessage()); verifyAll(); return; } fail("Should fail for a missing item"); } public void testCallTriesExpectationsBeforeStubs() throws Throwable { mockExpected.lastMatchingCall = mockCallable; mockStubs.lastMatchingCall = null; mockCallable.setupCallReturn("call result"); mockExpected.lastMatchingCallInvocation.setExpected(dummyInvocation); mockStubs.lastMatchingCallInvocation.setExpectNothing(); mockCallable.callInvocation.setExpected(dummyInvocation); assertEquals("Call should return", "call result", callBag.call(dummyInvocation)); verifyAll(); } public void testCallTriesStubsIfNoExpectations() throws Throwable { mockExpected.lastMatchingCall = null; mockStubs.lastMatchingCall = mockCallable; mockCallable.setupCallReturn("call result"); mockExpected.lastMatchingCallInvocation.setExpected(dummyInvocation); mockStubs.lastMatchingCallInvocation.setExpected(dummyInvocation); mockCallable.callInvocation.setExpected(dummyInvocation); assertEquals("Call should return", "call result", callBag.call(dummyInvocation)); verifyAll(); } public void testCallExceptionsArePropagatedUp() { Throwable throwable = new Throwable(); mockExpected.lastMatchingCall = mockCallable; mockCallable.setupCallThrow(throwable); try { callBag.call(dummyInvocation); } catch (Throwable ex) { Assert.assertEquals("Should have caught throwable", throwable, ex); return; } fail("Should throw throwable"); } public void testVerifiesOnlyExpectationsNotStubs() { mockExpected.verifyCalls.setExpected(1); mockStubs.verifyCalls.setExpectNothing(); callBag.verify(); verifyAll(); } public void testMatchesNotImplemented() { try { callBag.matches(dummyInvocation); } catch (AssertionFailedError unused) { return; } fail("Should have failed because not implemented"); } private void verifyAll() { mockCallable.verifyExpectations(); mockExpected.verifyExpectations(); mockStubs.verifyExpectations(); } } --- NEW FILE: MockCallableList.java --- package test.mockobjects.dynamic; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationValue; import com.mockobjects.ExpectationList; import com.mockobjects.dynamic.Callable; import com.mockobjects.dynamic.CallableList; import com.mockobjects.dynamic.Invocation; import com.mockobjects.util.Verifier; /** * copyright mockobjects.com 09-Jul-2003 */ public class MockCallableList implements CallableList { public ExpectationValue lastMatchingCallInvocation = new ExpectationValue("firstMatchingCallInvocation"); public ExpectationList addedCallables = new ExpectationList("added callables"); public ExpectationCounter clearCalls = new ExpectationCounter("clear calls"); public ExpectationCounter verifyCalls = new ExpectationCounter("verify calls"); public Callable lastMatchingCall; public Callable applied; public Callable apply; public boolean isEmpty; public void add(Callable callable) { addedCallables.addActual(callable); } public boolean isEmpty() { return isEmpty; } public Callable get(int index) { return null; } public int size() { return 0; } public void clear() { clearCalls.inc(); } public Callable firstMatchingCall(Invocation invocation) { return null; } public Callable lastMatchingCall(Invocation invocation) { lastMatchingCallInvocation.setActual(invocation); return lastMatchingCall; } public Callable apply(CallableList.Handler handler) { handler.handle(0, applied); return null; } public void verify() { verifyCalls.inc(); } public void verifyExpectations() { Verifier.verifyObject(this); } } Index: CallOnceExpectationTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallOnceExpectationTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- CallOnceExpectationTest.java 6 Jul 2003 23:24:09 -0000 1.5 +++ CallOnceExpectationTest.java 9 Jul 2003 02:13:59 -0000 1.6 @@ -16,17 +16,13 @@ final String DECORATED_DESCRIPTION = DynamicUtil.methodToString( METHOD_NAME, ARGS ); Mock ignoredMock = null; - MockCallable mockCallable = new MockCallable("mock callable"); + MockCallable mockCallable = new MockCallable(DECORATED_DESCRIPTION); CallOnceExpectation call = new CallOnceExpectation( mockCallable ); public CallOnceExpectationTest(String name) { super(name); } - public void setUp() { - mockCallable.setupGetDescription(DECORATED_DESCRIPTION); - } - public void testDescription() { AssertMo.assertIncludes( "should contain decorated's description", DECORATED_DESCRIPTION, call.getDescription() ); @@ -59,13 +55,13 @@ public void testMatchesDelegated() throws Throwable { mockCallable.setExpectedMatches( METHOD_NAME, ARGS ); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; assertTrue( "returns matches to be true", call.matches( INVOCATION ) ); mockCallable.verifyExpectations(); } public void testCallArgumentsPassedThrough() throws Throwable { - mockCallable.invocation.setExpected(INVOCATION); + mockCallable.callInvocation.setExpected(INVOCATION); mockCallable.setupCallReturn(RESULT); call.call( INVOCATION ); @@ -74,7 +70,7 @@ public void testDoesNotMatchAfterMethodCalled() throws Throwable { - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; mockCallable.setupCallReturn(RESULT); assertTrue( "First time should match", call.matches( INVOCATION )); Index: CallSequenceTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallSequenceTest.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- CallSequenceTest.java 6 Jul 2003 23:24:09 -0000 1.12 +++ CallSequenceTest.java 9 Jul 2003 02:14:00 -0000 1.13 @@ -42,7 +42,7 @@ public void testCallableThrowableThrown() throws Throwable { final Throwable throwable = new DummyThrowable(); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; mockCallable.setupCallThrow(throwable); callSequence.addExpect(mockCallable); @@ -67,7 +67,7 @@ } public void testCallFailsWithTooManyCalls() throws Throwable { - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; mockCallable.setupCallReturn(METHOD_A_RESULT); callSequence.addExpect(mockCallable); callSequence.call(new Invocation("willdefinitelyMatch", new Object[0])); @@ -85,11 +85,11 @@ public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); - methodA.setupAlwaysMatchActiveCall(true); - methodA.invocation.setExpected(METHOD_A_INVOCATION); + methodA.matches = true; + methodA.callInvocation.setExpected(METHOD_A_INVOCATION); methodA.setupCallReturn(METHOD_A_RESULT); - methodB.invocation.setExpectNothing(); + methodB.callInvocation.setExpectNothing(); callSequence.addExpect(methodA); callSequence.addExpect(methodB); @@ -102,10 +102,7 @@ public void testCallPassedToContainedElementsOtherOrderShouldFail() throws Throwable { - methodA.setupAlwaysMatchActiveCall(false); - - methodA.setupGetDescription("***methodA-description****"); - methodB.setupGetDescription("***methodB-description****"); + methodA.matches = false; callSequence.addExpect(methodA); callSequence.addExpect(methodB); @@ -133,7 +130,7 @@ final String result = "result"; mockCallable.setupCallReturn(result); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; callSequence.addExpect(mockCallable); @@ -150,13 +147,11 @@ final Invocation methodCInvocation = new Invocation(methodCName, methodCArgs); methodA.setExpectedMatches(methodCName, methodCArgs); - methodA.setupAlwaysMatchActiveCall(false); - methodA.invocation.setExpectNothing(); - methodA.setupGetDescription("***methodA-description****"); - methodB.invocation.setExpected(methodCInvocation); - methodB.setupAlwaysMatchActiveCall(false); - methodB.invocation.setExpectNothing(); - methodB.setupGetDescription("***methodB-description****"); + methodA.matches = false; + methodA.callInvocation.setExpectNothing(); + methodB.callInvocation.setExpected(methodCInvocation); + methodB.matches = false; + methodB.callInvocation.setExpectNothing(); callSequence.addExpect(methodA); callSequence.addExpect(methodB); Index: MockCallable.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockCallable.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- MockCallable.java 6 Jul 2003 23:24:09 -0000 1.6 +++ MockCallable.java 9 Jul 2003 02:14:00 -0000 1.7 @@ -14,21 +14,19 @@ final public String name; - public ExpectationValue invocation = new ExpectationValue("call") ; + public ExpectationValue callInvocation = new ExpectationValue("call") ; private ReturnValue callResult = new ReturnValue("call.return"); private Throwable callThrow = null; private ExpectationValue matchesMethodName = new ExpectationValue("matches.methodName"); private ExpectationList matchesArgs = new ExpectationList("matches.args"); - private ReturnValue matchesResult = new ReturnValue("matches.return"); + public boolean matches = false; private ExpectationCounter matchesCount = new ExpectationCounter("matches.count"); private ExpectationCounter verifyCount = new ExpectationCounter("verify.count"); private AssertionFailedError verifyError = null; - private ReturnValue toStringResult = new ReturnValue("toString.return"); - public MockCallable(String name) { this.name = name; } @@ -42,7 +40,7 @@ } public Object call(Invocation anInvocation) throws Throwable { - invocation.setActual(anInvocation); + callInvocation.setActual(anInvocation); if( callThrow != null ) { throw callThrow; @@ -60,15 +58,11 @@ matchesCount.setExpected(count); } - public void setupAlwaysMatchActiveCall( boolean result ) { - matchesResult.setValue(result); - } - public boolean matches(Invocation invocation) { matchesMethodName.setActual(invocation.getMethodName()); matchesArgs.addActualMany(invocation.args); matchesCount.inc(); - return matchesResult.getBooleanValue(); + return matches; } public void setExpectedVerifyCalls( int count ) { @@ -93,12 +87,8 @@ Verifier.verifyObject(this); } - public void setupGetDescription( String result ) { - toStringResult.setValue(result); - } - public String getDescription() { - return (String)toStringResult.getValue(); + return name; } public String toString() { Index: CallSignatureTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallSignatureTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallSignatureTest.java 6 Jul 2003 23:24:09 -0000 1.2 +++ CallSignatureTest.java 9 Jul 2003 02:14:00 -0000 1.3 @@ -26,7 +26,7 @@ throws Throwable { final String result = "result"; - mockCallable.invocation.setExpected(INVOCATION); + mockCallable.callInvocation.setExpected(INVOCATION); mockCallable.setupCallReturn(result); callSignature.call(INVOCATION); @@ -72,7 +72,7 @@ public void testMatchesAfterCalls() throws Throwable { mockCallable.setupCallReturn("result"); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; mockConstraintMatcher.setupMatches(true); Index: CallBagTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallBagTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- CallBagTest.java 6 Jul 2003 23:24:09 -0000 1.8 +++ CallBagTest.java 9 Jul 2003 02:14:00 -0000 1.9 @@ -21,8 +21,8 @@ private CallBag callBag = new CallBag(); - private MockCallable methodA = new MockCallable("method a"); - private MockCallable methodB = new MockCallable("method b"); + private MockCallable methodA = new MockCallable("method A"); + private MockCallable methodB = new MockCallable("method B"); private MockCallable mockCallable = new MockCallable("mock callable"); @@ -43,15 +43,14 @@ } public void testCallPassedToContainedElements() throws Throwable { - methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); - methodA.setupAlwaysMatchActiveCall(true); - methodA.invocation.setExpected(METHOD_A_INVOCATION); + methodA.matches = true; methodA.setupCallReturn(METHOD_A_RESULT); - methodB.invocation.setExpectNothing(); + methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); + methodA.callInvocation.setExpected(METHOD_A_INVOCATION); - callBag.addExpect(methodA); callBag.addExpect(methodB); + callBag.addExpect(methodA); assertSame("expected result from method A", METHOD_A_RESULT, callBag.call(METHOD_A_INVOCATION)); @@ -73,14 +72,12 @@ public void testCallPassedToContainedElementsOtherOrder() throws Throwable { - methodA.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS); - methodA.setupAlwaysMatchActiveCall(false); - methodA.invocation.setExpectNothing(); - methodB.invocation.setExpected(METHOD_B_INVOCATION); + methodA.matches = false; + methodB.setupCallReturn(METHOD_B_RESULT); + methodB.matches = true; - methodB.setupCallReturn(METHOD_B_RESULT); + methodB.callInvocation.setExpected(METHOD_B_INVOCATION); methodB.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS); - methodB.setupAlwaysMatchActiveCall(true); callBag.addExpect(methodA); callBag.addExpect(methodB); @@ -96,7 +93,7 @@ final String result = "result"; mockCallable.setupCallReturn(result); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; callBag.addExpect(mockCallable); @@ -107,7 +104,7 @@ throws Throwable { final Throwable throwable = new DummyThrowable(); - mockCallable.setupAlwaysMatchActiveCall(true); + mockCallable.matches = true; mockCallable.setupCallThrow(throwable); callBag.addExpect(mockCallable); @@ -129,13 +126,11 @@ final Invocation methodCInvocation = new Invocation(methodCName, methodCArgs); methodA.setExpectedMatches(methodCName, methodCArgs); - methodA.setupAlwaysMatchActiveCall(false); - methodA.invocation.setExpectNothing(); - methodA.setupGetDescription("***methodA-description****"); - methodB.invocation.setExpected(methodCInvocation); - methodB.setupAlwaysMatchActiveCall(false); - methodB.invocation.setExpectNothing(); - methodB.setupGetDescription("***methodB-description****"); + methodA.matches = false; + methodA.callInvocation.setExpectNothing(); + methodB.callInvocation.setExpected(methodCInvocation); + methodB.matches = false; + methodB.callInvocation.setExpectNothing(); callBag.addExpect(methodA); callBag.addExpect(methodB); |