From: Tim M. <ma...@us...> - 2003-04-04 11:13:34
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3249/src/core/test/mockobjects/dynamic Added Files: CallCollectionTest.java Removed Files: CallSequenceTest.java Log Message: CallBag added + refactoring --- NEW FILE: CallCollectionTest.java --- /** Created on Oct 31, 2002 by npryce * Copyright (c) B13media Ltd. */ package test.mockobjects.dynamic; import java.util.List; import junit.framework.AssertionFailedError; import com.mockobjects.constraint.Constraint; import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.CallBag; import com.mockobjects.dynamic.CallCollection; import com.mockobjects.dynamic.CallSequence; import com.mockobjects.dynamic.Mock; import com.mockobjects.dynamic.MockCall; import com.mockobjects.util.TestCaseMo; public class CallCollectionTest extends TestCaseMo { private Mock mock_call_1 = new Mock(MockCall.class); private Mock mock_call_2 = new Mock(MockCall.class); private CallSequence call_list = new CallSequence(); private CallBag call_bag = new CallBag(); private final Object[] ARGS_1 = { "ARG1" }; private final Object RESULT_1 = new Object(); private final Object[] ARGS_2 = { "ARG2" }; private final Object RESULT_2 = new Object(); private Constraint[] CONS_1 = new Constraint[] { C.eq(ARGS_1[0])}; private Constraint[] CONS_2 = new Constraint[] { C.eq(ARGS_2[0])}; private Constraint[] NO_CONSTRAINTS = new Constraint[0]; public CallCollectionTest(String name) { super(name); } public void setUp() { } public void testCallListInitiallyExpectsNoCalls() throws Throwable { call_list.verify(); } public void testCallToEmptyCallListFails() throws Throwable { try { call_list.call(new Object[0]); } catch (AssertionFailedError expected) { assertTrue( "should have nice error message", -1 != expected.getMessage().indexOf(CallCollection.NO_EXPECTED_CALLS)); return; } fail("call to empty call list should fail"); } public void testCallListPassesArgumentsAndReturnsValue() throws Throwable { mock_call_1.expectAndReturn("call", ARGS_1, RESULT_1); call_list.expect((MockCall) mock_call_1.proxy(), CONS_1); mock_call_2.expectAndReturn("call", ARGS_2, RESULT_2); call_list.expect((MockCall) mock_call_2.proxy(), CONS_2); assertSame(RESULT_1, call_list.call(ARGS_1)); assertSame(RESULT_2, call_list.call(ARGS_2)); mock_call_1.verify(); mock_call_2.verify(); call_list.verify(); } public void testCallBagOrderDoesNotMatter() throws Throwable { mock_call_1.expectAndReturn("call", ARGS_1, RESULT_1); call_bag.expect((MockCall) mock_call_1.proxy(), CONS_1); mock_call_2.expectAndReturn("call", ARGS_2, RESULT_2); call_bag.expect((MockCall) mock_call_2.proxy(), CONS_2); assertSame(RESULT_2, call_bag.call(ARGS_2)); assertSame(RESULT_1, call_bag.call(ARGS_1)); mock_call_1.verify(); mock_call_2.verify(); call_bag.verify(); } public void testCallListOrderDoesMatter() throws Throwable { mock_call_1.expectAndReturn("call", ARGS_1, RESULT_1); call_list.expect((MockCall) mock_call_1.proxy(), CONS_1); mock_call_2.expectAndReturn("call", ARGS_2, RESULT_2); call_list.expect((MockCall) mock_call_2.proxy(), CONS_2); try { call_list.call(ARGS_2); } catch (AssertionFailedError expected) { assertTrue( "should have nice error message which refers to the arguments", -1 != expected.getMessage().indexOf((String) ARGS_1[0])); return; } fail("should fail if a call is done in wrong order"); } public void testCallBagOrderDoesNotMatterButNumberOfCallsDoes() throws Throwable { mock_call_1.expectAndReturn("call", ARGS_1, RESULT_1); call_bag.expect((MockCall) mock_call_1.proxy(), CONS_1); mock_call_2.expectAndReturn("call", ARGS_2, RESULT_2); call_bag.expect((MockCall) mock_call_2.proxy(), CONS_2); assertSame(RESULT_2, call_bag.call(ARGS_2)); try { call_bag.call(ARGS_2); } catch (AssertionFailedError expected) { assertTrue( "should have nice error message which refers to the arguments", -1 != expected.getMessage().indexOf((String) ARGS_1[0])); return; } fail("should fail if a call is done too often"); } public void testNicelyFormattedConstraintArrays() throws Throwable { Constraint[] cons1 = new Constraint[] { C.eq("hello"), C.gt(2)}; call_list.expect(CALL, cons1); call_list.expect(CALL, new Constraint[0]); String expected1 = "[a value equal to <hello>,a value greater than <2>]"; assertEquals("should get the nicely formatted message", expected1, call_list.printConstraint(0)); assertEquals("should get no args message if empty constraint array", "[no args]", call_list.printConstraint(1)); assertEquals("should just return empty string if given bad index", "", call_list.printConstraint(2)); } public void testErrorMessage() throws Throwable { Constraint[] cons1 = new Constraint[] { C.eq("hello"), C.gt(2)}; call_list.expect(CALL, cons1); call_list.expect(CALL, new Constraint[0]); call_list.call(new Object[] { "hello", new Integer(3)}); Object[] badargs = new Object[] { "wibble", "foobar" }; String expected1 = "expected calls were:\n[a value equal to <hello>,a value greater than <2>] :called already\n[no args] :not called\nreceived arguments were [wibble,foobar]"; String result = call_list.errorMessage(badargs); assertEquals("should get the nicely formatted message", expected1, result); String expectedWithNullArgs = "expected calls were:\n[a value equal to <hello>,a value greater than <2>] :called already\n[no args] :not called"; assertEquals( "should get the nicely formatted message with null args", expectedWithNullArgs, call_list.errorMessage(null)); } public void testErrorMessageNoCallsSetup() throws Throwable { Object[] badargs = new Object[] { "wibble", "foobar" }; String expected1 = "no expected calls were set\nreceived arguments were [wibble,foobar]"; String result = call_list.errorMessage(badargs); assertEquals("should get the nicely formatted message", expected1, result); } public void testCallListThrowsExceptionValue() throws Throwable { final ExampleException EXCEPTION_1 = new ExampleException(); final ExampleException EXCEPTION_2 = new ExampleException(); mock_call_1.expectAndThrow("call", C.args(C.same(ARGS_1)), EXCEPTION_1); call_list.expect((MockCall) mock_call_1.proxy(), CONS_1); mock_call_2.expectAndThrow("call", C.args(C.eq(ARGS_2)), EXCEPTION_2); call_list.expect((MockCall) mock_call_2.proxy(), CONS_2); try { call_list.call(ARGS_1); fail("expected ExampleException to be thrown"); } catch (ExampleException ex) { assertSame(EXCEPTION_1, ex); } try { call_list.call(ARGS_2); fail("expected ExampleException to be thrown"); } catch (ExampleException ex) { assertSame(EXCEPTION_2, ex); } mock_call_1.verify(); mock_call_2.verify(); call_list.verify(); } static final MockCall CALL = new MockCall() { public Object call(Object[] args) throws Throwable { return Mock.VOID; } }; static final Object[] NO_ARGS = { }; public void testTooManyCalls() throws Throwable { call_list.expect(CALL, NO_CONSTRAINTS); call_list.expect(CALL, NO_CONSTRAINTS); call_list.call(NO_ARGS); call_list.call(NO_ARGS); try { call_list.call(NO_ARGS); } catch (AssertionFailedError expected) { assertTrue("should have nice error message", -1 != expected.getMessage().indexOf(CallCollection.CALLED)); return; } fail("third call should have failed"); } public void testTooFewCalls() throws Throwable { call_list.expect(CALL, NO_CONSTRAINTS); call_list.expect(CALL, NO_CONSTRAINTS); call_list.call(NO_ARGS); try { call_list.verify(); } catch (AssertionFailedError expected) { assertTrue( "should have nice error message", -1 != expected.getMessage().indexOf(CallCollection.EXPECTED_CALLS)); return; } fail("verify should have failed"); } public void testCheckMockVerifiesCallList() throws Throwable { Mock mockList = new Mock(List.class); List aList = (List) mockList.proxy(); call_list.expectAndReturn(new Constraint[] { C.eq("value")}, Boolean.TRUE); call_list.expectVoid(new Constraint[] { C.eq(1), C.eq("value2")}); mockList.expect("add", call_list); aList.add("value"); try { mockList.verify(); } catch (AssertionFailedError expected) { return; } fail("the mock should have verified it's call sequence"); } public void testFirstMatchingCallIsUsedInBag() throws Throwable { Constraint[] greaterThan3 = C.args(C.gt(3)); Constraint[] greaterThan7 = C.args(C.gt(7)); call_bag.expectAndReturn(greaterThan3, RESULT_1); call_bag.expectAndReturn(greaterThan7, RESULT_2); assertSame("should get the first call", RESULT_1, call_bag.call(new Object[] { new Integer(8)})); try { call_bag.call(new Object[] { new Integer(5)}); } catch (AssertionFailedError e) { return; } fail("should have failed to find a matching call for the second call since 5 is not greater than 7"); } private static class ExampleException extends Exception { } } --- CallSequenceTest.java DELETED --- |