Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3129/src/core/test/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment ExpectedCallTest.java MockTest.java CallSetTest.java Added Files: Tag: DynamicMockExperiment CallMatchTest.java MockCallFactory.java Log Message: Basic implementation - adds syntactic sugar for permitAndReturn, expectAndReturn etc. - separated CallMatch from ExpectedCall Still needs Sequence, SingleCall, CallCount support --- NEW FILE: CallMatchTest.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class CallMatchTest extends TestCase { final String methodName = "methodName"; Mock mock = new Mock(DummyInterface.class,"mock"); MockCallMocker mockDecorated = new MockCallMocker(); CallMatch call = new CallMatch( methodName, new Constraint[0], mockDecorated ); public CallMatchTest(String name) { super(name); } public void testCallArgumentsArePropagatedToDecorated() throws Throwable { final Object[] arguments = new Object[0]; final String result = "result"; mockDecorated.setExpectedCall( mock, methodName, arguments ); mockDecorated.setupCallReturn( result ); call.call(mock, methodName, arguments); mockDecorated.verifyExpectations(); } public void testUncalledCallVerifies() { mockDecorated.setExpectedVerifyCalls(1); call.verify(); mockDecorated.verifyExpectations(); } public void testSuccessfulCallVerifies() throws Throwable { mockDecorated.setExpectedVerifyCalls(1); mockDecorated.setupCallReturn("result"); call.call( mock, "methodName", new Object[0] ); call.verify(); mockDecorated.verifyExpectations(); } public void testMultipleCallsSucceed() throws Throwable { mockDecorated.setupCallReturn("result"); call.call( mock, methodName, new Object[0] ); call.call( mock, methodName, new Object[0] ); mockDecorated.verifyExpectations(); } public void testArgumentsCheckedAgainstConstraints() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setupCallReturn("result"); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); call.call( mock, methodName, args ); for( int i = 0; i < constraints.length; i++ ) { constraints[i].verify(); } mockDecorated.verifyExpectations(); } public void testWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should show expected number of arguments", Integer.toString(constraints.length), ex.getMessage()); AssertMo.assertIncludes("Should show actual number of arguments", Integer.toString(args.length), ex.getMessage()); AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); mockDecorated.verifyExpectations(); } public void testConstraintFailure() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); AssertMo.assertIncludes("Should show index of failed argument", "1", ex.getMessage() ); AssertMo.assertIncludes("Should show expected arguments", AssertMo.join(constraints), ex.getMessage()); AssertMo.assertIncludes("Should show actual arguments", AssertMo.join(args), ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); mockDecorated.verifyExpectations(); } public void testCallMatchesArguments() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setExpectedMatches( methodName, args ); mockDecorated.setupMatchesReturn(true); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertTrue( "call matches", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchIfDecoratedDoesNotMatch() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setExpectedMatches( methodName, args ); mockDecorated.setupMatchesReturn(false); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match when decorated does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenWrongName() throws Throwable { CallMatch call = new CallMatch( methodName, new Constraint[0], mockDecorated ); assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testMatchesAfterCalls() throws Throwable { mockDecorated.setupCallReturn("result"); mockDecorated.setupMatchesReturn(true); CallMatch call = new CallMatch(methodName, C.args(), mockDecorated ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after first call", call.matches( methodName, new Object[0]) ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after further calls", call.matches( methodName, new Object[0]) ); mockDecorated.verifyExpectations(); } } --- NEW FILE: MockCallFactory.java --- package test.mockobjects.dynamic; import mockmaker.ReturnValues; import mockmaker.VoidReturnValues; import mockmaker.ExceptionalReturnValue; import com.mockobjects.*; import com.mockobjects.dynamic.CallFactory; import com.mockobjects.dynamic.CallMocker; import com.mockobjects.constraint.Constraint; public class MockCallFactory implements CallFactory{ private ExpectationCounter myCreateReturnStubCalls = new ExpectationCounter("MockCallFactory.createReturnStub(Object)"); private ReturnValues myActualCreateReturnStubReturnValues = new ReturnValues("MockCallFactory.createReturnStub(Object)", true); private ExpectationList myCreateReturnStubParameter0Values = new ExpectationList("MockCallFactory.createReturnStub(Object) java.lang.Object"); private ExpectationCounter myCreateThrowStubCalls = new ExpectationCounter("MockCallFactory.createThrowStub(Throwable)"); private ReturnValues myActualCreateThrowStubReturnValues = new ReturnValues("MockCallFactory.createThrowStub(Throwable)", true); private ExpectationList myCreateThrowStubParameter0Values = new ExpectationList("MockCallFactory.createThrowStub(Throwable) java.lang.Throwable"); private ExpectationCounter myCreateExpectedCallCalls = new ExpectationCounter("MockCallFactory.createExpectedCall(CallMocker)"); private ReturnValues myActualCreateExpectedCallReturnValues = new ReturnValues("MockCallFactory.createExpectedCall(CallMocker)", true); private ExpectationList myCreateExpectedCallParameter0Values = new ExpectationList("MockCallFactory.createExpectedCall(CallMocker) com.mockobjects.dynamic.CallMocker"); private ExpectationCounter myCreateCallMatchCalls = new ExpectationCounter("MockCallFactory.createCallMatch(String, Constraint[], CallMocker)"); private ReturnValues myActualCreateCallMatchReturnValues = new ReturnValues("MockCallFactory.createCallMatch(String, Constraint[], CallMocker)", true); private ExpectationList myCreateCallMatchParameter0Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) java.lang.String"); private ExpectationList myCreateCallMatchParameter1Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) com.mockobjects.constraint.Constraint"); private ExpectationList myCreateCallMatchParameter2Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) com.mockobjects.dynamic.CallMocker"); public void setExpectedCreateReturnStubCalls(int calls){ myCreateReturnStubCalls.setExpected(calls); } public void addExpectedCreateReturnStub(Object arg0){ myCreateReturnStubParameter0Values.addExpected(arg0); } public CallMocker createReturnStub(Object arg0){ myCreateReturnStubCalls.inc(); myCreateReturnStubParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateReturnStubReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateReturnStub(Throwable arg){ myActualCreateReturnStubReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateReturnStub(CallMocker arg){ myActualCreateReturnStubReturnValues.add(arg); } public void setExpectedCreateThrowStubCalls(int calls){ myCreateThrowStubCalls.setExpected(calls); } public void addExpectedCreateThrowStub(Throwable arg0){ myCreateThrowStubParameter0Values.addExpected(arg0); } public CallMocker createThrowStub(Throwable arg0){ myCreateThrowStubCalls.inc(); myCreateThrowStubParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateThrowStubReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateThrowStub(Throwable arg){ myActualCreateThrowStubReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateThrowStub(CallMocker arg){ myActualCreateThrowStubReturnValues.add(arg); } public void setExpectedCreateExpectedCallCalls(int calls){ myCreateExpectedCallCalls.setExpected(calls); } public void addExpectedCreateExpectedCall(CallMocker arg0){ myCreateExpectedCallParameter0Values.addExpected(arg0); } public CallMocker createExpectedCall(CallMocker arg0){ myCreateExpectedCallCalls.inc(); myCreateExpectedCallParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateExpectedCallReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateExpectedCall(Throwable arg){ myActualCreateExpectedCallReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateExpectedCall(CallMocker arg){ myActualCreateExpectedCallReturnValues.add(arg); } public void setExpectedCreateCallMatchCalls(int calls){ myCreateCallMatchCalls.setExpected(calls); } public void addExpectedCreateCallMatch(String arg0, Constraint[] arg1, CallMocker arg2){ myCreateCallMatchParameter0Values.addExpected(arg0); myCreateCallMatchParameter1Values.addExpected(arg1); myCreateCallMatchParameter2Values.addExpected(arg2); } public CallMocker createCallMatch(String arg0, Constraint[] arg1, CallMocker arg2){ myCreateCallMatchCalls.inc(); myCreateCallMatchParameter0Values.addActual(arg0); myCreateCallMatchParameter1Values.addActual(arg1); myCreateCallMatchParameter2Values.addActual(arg2); Object nextReturnValue = myActualCreateCallMatchReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateCallMatch(Throwable arg){ myActualCreateCallMatchReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateCallMatch(CallMocker arg){ myActualCreateCallMatchReturnValues.add(arg); } public void verify(){ myCreateReturnStubCalls.verify(); myCreateReturnStubParameter0Values.verify(); myCreateThrowStubCalls.verify(); myCreateThrowStubParameter0Values.verify(); myCreateExpectedCallCalls.verify(); myCreateExpectedCallParameter0Values.verify(); myCreateCallMatchCalls.verify(); myCreateCallMatchParameter0Values.verify(); myCreateCallMatchParameter1Values.verify(); myCreateCallMatchParameter2Values.verify(); } } Index: ExpectedCallTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/ExpectedCallTest.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- ExpectedCallTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.1 +++ ExpectedCallTest.java 7 Apr 2003 17:38:38 -0000 1.1.2.2 @@ -1,189 +1,102 @@ /* - * Created on 04-Apr-2003 - * - * To change this generated comment go to - * Window>Preferences>Java>Code Generation>Code and Comments + * Created on 07-Apr-2003 */ package test.mockobjects.dynamic; -import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; +/** + * @author dev + */ public class ExpectedCallTest extends TestCase { + final String result = "result!"; final String methodName = "methodName"; - Mock mock = new Mock(DummyInterface.class,"mock"); - ExpectedCall call; + final Object[] args = { "arg1", "arg2" }; + final String decoratedDescription = AssertMo.methodToString( methodName, args ); + + Mock ignoredMock = null; + MockCallMocker mockDecorated = new MockCallMocker(); + ExpectedCall call = new ExpectedCall( mockDecorated ); public ExpectedCallTest(String name) { super(name); } + + public void setUp() { + mockDecorated.setupGetDescription(decoratedDescription); + } - public void testCallArgumentsArePropagatedToDecorated() throws Throwable { - final Object[] arguments = new Object[0]; - final String result = "result"; - - MockCallMocker mockDecorated = new MockCallMocker(); - mockDecorated.setExpectedCall( mock, methodName, arguments ); - mockDecorated.setupCallReturn( result ); - - ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], mockDecorated ); - - call.call(mock, methodName, arguments); - - mockDecorated.verifyExpectations(); + public void testDescription() { + AssertMo.assertIncludes( "should contain decorated's description", + decoratedDescription, call.getDescription() ); + AssertMo.assertIncludes( "should say that decorated call is mandatory", + "mandatory", call.getDescription() ); } - public void testUncalledCallDoesNotVerify() { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); - + public void testDoesNotVerifyIfNotCalled() { try { call.verify(); } catch( AssertionFailedError ex ) { - AssertMo.assertIncludes("should include call name", methodName, ex.getMessage() ); + AssertMo.assertIncludes( "should include description of expected call", + decoratedDescription, ex.getMessage() ); return; } - fail("expected verify to fail"); - } - - public void testSuccessfulCallVerifies() throws Throwable { - ExpectedCall call = new ExpectedCall("methodName", C.args(), new ReturnStub("ignored") ); - - call.call( mock, "methodName", new Object[0] ); - call.verify(); - } - - public void testMultipleCallsSucceed() throws Throwable { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); - call.call( mock, methodName, new Object[0] ); - call.call( mock, methodName, new Object[0] ); + fail( "verify did not fail when expected call not called"); } - public void testArgumentsCheckedAgainstConstraints() throws Throwable { - String[] args = { "arg1", "arg2", "arg3" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[2], true ) - }; - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); + public void testVerifiesIfCalled() throws Throwable { + mockDecorated.setupCallReturn( result ); + mockDecorated.setExpectedVerifyCalls(1); - call.call( mock, methodName, args ); + call.call( ignoredMock, methodName, args ); + call.verify(); - for( int i = 0; i < constraints.length; i++ ) { - constraints[i].verify(); - } + mockDecorated.verifyExpectations(); } - public void testWrongNumberOfArguments() throws Throwable { - String[] args = { "arg1", "arg2" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[1], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - boolean passed = true; - try { - call.call( mock, methodName, args ); - passed = false; - } catch (AssertionFailedError ex) { - AssertMo.assertIncludes("Should show expected number of arguments", - Integer.toString(constraints.length), ex.getMessage()); - AssertMo.assertIncludes("Should show actual number of arguments", - Integer.toString(args.length), ex.getMessage()); - AssertMo.assertIncludes("Should include the method name", - methodName, ex.getMessage()); - } - - assertTrue("Should fail is call doesn't give an exception", passed); + public void testMatchesDelegated() throws Throwable { + mockDecorated.setExpectedMatches( methodName, args ); + mockDecorated.setupMatchesReturn(true); + assertTrue( "returns matches to be true", call.matches( methodName, args ) ); + mockDecorated.verifyExpectations(); } - public void testConstraintFailure() throws Throwable { - String[] args = { "argA", "argB", "argC" }; - MockConstraint[] constraints = { - new MockConstraint( "constraintA", args[0], true ), - new MockConstraint( "constraintB", args[1], false ), - new MockConstraint( "constraintC", args[2], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - boolean passed = true; - try { - call.call( mock, methodName, args ); - passed = false; - } catch (AssertionFailedError ex) { - AssertMo.assertIncludes("Should include the method name", - methodName, ex.getMessage()); - AssertMo.assertIncludes("Should show index of failed argument", - "1", ex.getMessage() ); - AssertMo.assertIncludes("Should show expected arguments", - AssertMo.join(constraints), ex.getMessage()); - AssertMo.assertIncludes("Should show actual arguments", - AssertMo.join(args), ex.getMessage()); - } + public void testCallArgumentsPassedThrough() throws Throwable { + mockDecorated.setExpectedCall(ignoredMock, methodName, args); + mockDecorated.setupCallReturn(result); - assertTrue("Should fail is call doesn't give an exception", passed); + call.call( ignoredMock, methodName, args ); + mockDecorated.verifyExpectations(); } - - public void testCallMatchesArguments() throws Throwable { - String[] args = { "arg1", "arg2", "arg3" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[2], true ) - }; - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); + public void testDecoratedResultPassedThrough() throws Throwable { + mockDecorated.setupCallReturn(result); - assertTrue( "call matches", call.matches( methodName, args) ); - } - - public void testCallDoesNotMatchWhenWrongName() throws Throwable { - ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], new ReturnStub("ignored") ); - assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); + assertSame( "should return decorated's result", + result, call.call( ignoredMock, methodName, args ) ); + mockDecorated.verifyExpectations(); } - - public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { - String[] args = { "arg1", "arg2" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[1], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - assertFalse( "call does not match", call.matches( methodName, args) ); - } - public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { - String[] args = { "argA", "argB", "argC" }; - MockConstraint[] constraints = { - new MockConstraint( "constraintA", args[0], true ), - new MockConstraint( "constraintB", args[1], false ), - new MockConstraint( "constraintC", args[2], true ) - }; + public void testDecoratedExceptionPassedThrough() throws Throwable { + final Throwable exception = new DummyThrowable(); - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - assertFalse( "call does not match", call.matches( methodName, args) ); - } - - public void testMatchesAfterCalls() throws Throwable { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); + mockDecorated.setupCallThrow(exception); - call.call( mock, methodName, new Object[0] ); - assertTrue( "matches after first call", call.matches( methodName, new Object[0]) ); - call.call( mock, methodName, new Object[0] ); - assertTrue( "matches after further calls", call.matches( methodName, new Object[0]) ); + try { + call.call( ignoredMock, methodName, args ); + fail("expected decorated's throwable to be thrown"); + } + catch( DummyThrowable ex ) { + // expected + } + + mockDecorated.verifyExpectations(); } } Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.8.2.2 retrieving revision 1.8.2.3 diff -u -r1.8.2.2 -r1.8.2.3 --- MockTest.java 7 Apr 2003 14:26:57 -0000 1.8.2.2 +++ MockTest.java 7 Apr 2003 17:38:46 -0000 1.8.2.3 @@ -55,7 +55,7 @@ mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); mockMethod.setupCallReturn( "result ignored" ); - aMock.expect( mockMethod ); + aMock.add( mockMethod ); proxy.aMethod( expectedArgs[0], expectedArgs[1] ); @@ -72,7 +72,7 @@ mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); mockMethod.setupCallReturn( "result ignored" ); - aMock.expect( mockMethod ); + aMock.add( mockMethod ); proxy.aMethodWithNoArguments(); @@ -82,7 +82,7 @@ public void testCallMockerResultReturnedByProxy() throws Throwable { final String result = "result"; - aMock.expect( new ReturnStub(result) ); + aMock.add( new ReturnStub(result) ); assertSame( "result is returned by mock", result, proxy.aMethod( "hello", "world" ) ); @@ -91,7 +91,7 @@ public void testCallMockerThrowableThrownThroughProxy() throws Throwable { final Throwable throwable = new DummyThrowable(); - aMock.expect( new ThrowStub(throwable) ); + aMock.add( new ThrowStub(throwable) ); try { proxy.aMethod( "hello", "world" ); @@ -106,7 +106,7 @@ final String originalMessage = "original message"; final AssertionFailedError error = new AssertionFailedError(originalMessage); - aMock.expect( new ThrowStub(error) ); + aMock.add( new ThrowStub(error) ); try { proxy.aMethod( "hello", "world" ); Index: CallSetTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/CallSetTest.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- CallSetTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.2 +++ CallSetTest.java 7 Apr 2003 17:38:49 -0000 1.1.2.3 @@ -3,6 +3,7 @@ */ package test.mockobjects.dynamic; +import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; @@ -12,13 +13,21 @@ * @author dev */ public class CallSetTest extends TestCase { - private CallSet callSet = new CallSet(); + private MockCallFactory mockCallFactory = new MockCallFactory(); + private CallSet callSet = new CallSet(mockCallFactory); + private MockCallMocker mockReturnStub = new MockCallMocker(); + private MockCallMocker mockCallMatch = new MockCallMocker(); + private MockCallMocker mockExpectedCall = new MockCallMocker(); + private MockCallMocker mockThrowStub = new MockCallMocker(); + private Mock unusedMock = null; private MockCallMocker methodA = new MockCallMocker(); private MockCallMocker methodB = new MockCallMocker(); final String methodAName = "methodA"; final String[] methodAArgs = new String[] { "a1", "a2" }; + final Constraint[] methodAConstraints = C.args( C.eq("a1"), C.eq("a2") ); final String methodAResult = "resultA"; + final Throwable methodAException = new DummyThrowable("Configured test throwable"); final String methodBName = "methodB"; final String[] methodBArgs = new String[] { "b1", "b2" }; @@ -36,7 +45,7 @@ public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { methodA.setExpectedVerifyCalls(1); methodA.setupVerifyThrow( new AssertionFailedError("verify failed") ); - callSet.expect( methodA ); + callSet.add( methodA ); try { callSet.verify(); @@ -51,14 +60,94 @@ methodA.setExpectedVerifyCalls(1); methodB.setExpectedVerifyCalls(1); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); callSet.verify(); methodA.verifyExpectations(); methodB.verifyExpectations(); } + public void testExpectAndReturn() throws Throwable{ + mockExpectedCall.setupMatchesReturn(true); + mockExpectedCall.setupCallReturn(methodAResult); + + mockCallFactory.addExpectedCreateReturnStub( methodAResult ); + mockCallFactory.setupCreateReturnStub( mockReturnStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockReturnStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + mockCallFactory.addExpectedCreateExpectedCall( mockCallMatch ); + mockCallFactory.setupCreateExpectedCall( mockExpectedCall ); + + callSet.expectAndReturn( methodAName, methodAConstraints, methodAResult ); + + assertSame( "should be result of calling expected call", + methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); + + Verifier.verifyObject(this); + } + + public void testPermitAndReturn() throws Throwable{ + mockCallMatch.setupMatchesReturn(true); + mockCallMatch.setupCallReturn(methodAResult); + + mockCallFactory.addExpectedCreateReturnStub( methodAResult ); + mockCallFactory.setupCreateReturnStub( mockReturnStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockReturnStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + + callSet.permitAndReturn( methodAName, methodAConstraints, methodAResult ); + + assertSame( "should be result of calling expected call", + methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); + + Verifier.verifyObject(this); + } + + public void testExpectAndThrow() throws Throwable{ + mockExpectedCall.setupMatchesReturn(true); + mockExpectedCall.setupCallThrow(methodAException); + + mockCallFactory.addExpectedCreateThrowStub(methodAException); + mockCallFactory.setupCreateThrowStub( mockThrowStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockThrowStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + mockCallFactory.addExpectedCreateExpectedCall( mockCallMatch ); + mockCallFactory.setupCreateExpectedCall( mockExpectedCall ); + + callSet.expectAndThrow( methodAName, methodAConstraints, methodAException ); + + try { + callSet.call( unusedMock, methodAName, methodAArgs ); + } catch (DummyThrowable ex) { + Verifier.verifyObject(this); + return; + } + + fail("Should have thrown configured exception"); + } + + public void testPermitAndThrow() throws Throwable{ + mockCallMatch.setupMatchesReturn(true); + mockCallMatch.setupCallThrow(methodAException); + + mockCallFactory.addExpectedCreateThrowStub(methodAException); + mockCallFactory.setupCreateThrowStub( mockThrowStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockThrowStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + + callSet.permitAndThrow( methodAName, methodAConstraints, methodAException ); + + try { + callSet.call( unusedMock, methodAName, methodAArgs ); + } catch (DummyThrowable ex) { + Verifier.verifyObject(this); + return; + } + + fail("Should have thrown configured exception"); + } + public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches( methodAName, methodAArgs ); methodA.setupMatchesReturn(true); @@ -67,8 +156,8 @@ methodB.setExpectedCallCount( 0 ); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); assertSame( "expected result from method A", methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); @@ -87,8 +176,8 @@ methodB.setExpectedMatches( methodBName, methodBArgs ); methodB.setupMatchesReturn(true); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); assertSame( "expected result from method B", methodBResult, callSet.call( unusedMock, methodBName, methodBArgs ) ); @@ -104,14 +193,14 @@ methodA.setExpectedMatches( methodCName, methodCArgs ); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount( 0 ); - methodA.setupGetDescription("***methodA-toString****"); + methodA.setupGetDescription("***methodA-description****"); methodB.setExpectedCall( unusedMock, methodCName, methodCArgs ); methodB.setupMatchesReturn(false); methodB.setExpectedCallCount( 0 ); - methodB.setupGetDescription("***methodB-toString****"); + methodB.setupGetDescription("***methodB-description****"); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); try { callSet.call( unusedMock, methodCName, methodCArgs ); @@ -124,9 +213,9 @@ methodCArgs[1], ex.getMessage() ); AssertMo.assertIncludes( "shows set contents (A)", - methodA.toString(), ex.getMessage() ); + methodA.getDescription(), ex.getMessage() ); AssertMo.assertIncludes( "shows set contents (B)", - methodB.toString(), ex.getMessage() ); + methodB.getDescription(), ex.getMessage() ); return; } |