From: Nat P. <np...@us...> - 2002-10-11 14:58:11
|
Update of /cvsroot/mockobjects/nat/jmock/test/com/b13media/mock In directory usw-pr-cvs1:/tmp/cvs-serv19938/test/com/b13media/mock Modified Files: Test_Predicates.java Test_Mock.java Log Message: Mock does not care about order of method calls unless explicitly specified. Mock synthesises results for unexpected methods, unless explicitly told to throw errors. New predicates: IsEventFrom and Matches (regex matching of strings). Index: Test_Predicates.java =================================================================== RCS file: /cvsroot/mockobjects/nat/jmock/test/com/b13media/mock/Test_Predicates.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- Test_Predicates.java 22 May 2002 10:03:00 -0000 1.1.1.1 +++ Test_Predicates.java 11 Oct 2002 14:57:35 -0000 1.2 @@ -4,6 +4,8 @@ */ package com.b13media.mock; +import java.util.EventObject; + public class Test_Predicates @@ -99,5 +101,29 @@ assertTrue( new Or( new False(), new True() ).eval(o) ); assertTrue( new Or( new True(), new False() ).eval(o) ); assertTrue( !new Or( new False(), new False() ).eval(o) ); + } + + public void testIsEventFrom() { + Object o = new Object(); + EventObject ev = new EventObject(o); + EventObject ev2 = new EventObject( new Object() ); + + Predicate p = new IsEventFrom(o); + + assertTrue( p.eval(ev) ); + assertTrue( "p should eval to false for an event not from o", + !p.eval(ev2) ); + assertTrue( "p should eval to false for objects that are not events", + !p.eval(o) ); + } + + public void testMatches() { + Predicate p = new Matches("^hello, (.*)\\.$"); + + assertTrue( p.eval("hello, world.") ); + assertTrue( p.eval("hello, mum.") ); + assertTrue( !p.eval("hello world.") ); + assertTrue( !p.eval("hello, world") ); + assertTrue( !p.eval("goodbye, world.") ); } } Index: Test_Mock.java =================================================================== RCS file: /cvsroot/mockobjects/nat/jmock/test/com/b13media/mock/Test_Mock.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- Test_Mock.java 22 May 2002 10:03:00 -0000 1.1.1.1 +++ Test_Mock.java 11 Oct 2002 14:57:35 -0000 1.2 @@ -4,6 +4,7 @@ */ package com.b13media.mock; +import java.lang.reflect.Method; import java.lang.reflect.Proxy; import junit.framework.AssertionFailedError; @@ -17,6 +18,21 @@ String objectTypes( Integer i ); int primitiveTypes( int x, int y ); void sideEffect( StringBuffer buf ); + + // These are used to test default return values + boolean booleanResult(); + byte byteResult(); + char charResult(); + short shortResult(); + int intResult(); + long longResult(); + float floatResult(); + double doubleResult(); + String stringResult(); + + // Used to test ordered calls + void first(); + void second(); } public static class DerivedMock @@ -33,7 +49,10 @@ } public String objectTypes( Integer n ) throws Throwable { - return (String)mockCall( "objectTypes", new Object[]{n} ); + Method method = getClass().getMethod("objectTypes", + new Class[] { Integer.class } ); + + return (String)mockCall( method , new Object[]{n} ); } }; @@ -48,10 +67,8 @@ public void setUp() { _mock = new Mock(); - _interface = (ExampleInterface)Proxy.newProxyInstance( - getClass().getClassLoader(), - new Class[]{ ExampleInterface.class }, - _mock ); + _interface = + (ExampleInterface)_mock.createInterface( ExampleInterface.class ); } public void tearDown() { @@ -71,7 +88,7 @@ public void testMockCallToNoArgMethod() { Object result = new Object(); - _mock.expectReturn( "noArgs", new Predicate[0], result ); + _mock.expectReturn( "noArgs", Mock.NO_ARGS, result ); assertSame( result, _interface.noArgs() ); @@ -79,7 +96,7 @@ } public void testMockCallToVoidMethod() { - _mock.expectVoid( "voidMethod", new Predicate[0] ); + _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); _interface.voidMethod(); @@ -105,14 +122,14 @@ _mock.verify(); } - public void testMultipleCalls() { + public void testLaterExpectationsOverrideEarlierExpectations() { Object result1 = new Object(); Object result2 = new Object(); - _mock.expectReturn( "noArgs", new Predicate[0], result1 ); - _mock.expectReturn( "noArgs", new Predicate[0], result2 ); + _mock.expectReturn( "noArgs", Mock.NO_ARGS, result1 ); + _mock.expectReturn( "noArgs", Mock.NO_ARGS, result2 ); - assertSame( result1, _interface.noArgs() ); + assertSame( result2, _interface.noArgs() ); assertSame( result2, _interface.noArgs() ); _mock.verify(); } @@ -152,7 +169,7 @@ } public void testNotAllMethodsCalled() { - _mock.expectVoid( "noArg", new Predicate[0] ); + _mock.expectVoid( "noArg", Mock.NO_ARGS ); try { _mock.verify(); @@ -161,20 +178,41 @@ return; } - fail( "verify did fail when all methods were not called" ); + fail( "verify did not fail when all methods were not called" ); } - public void testInvalidMethod() { - _mock.expectVoid( "noArg", new Predicate[0] ); + public void testUnexpectedMethodThrowsWhenStrict() { + _mock.setStrict(true); try { - _interface.objectTypes( new Integer(1) ); + _interface.voidMethod(); } catch( AssertionFailedError ex ) { return; } - fail( "mock did not fail when unexpected method was called" ); + fail( "strict mock did not fail when unexpected method was called" ); + } + + public void testUnexpectedMethodDoesNotThrowsWhenNotStrict() { + try { + _interface.voidMethod(); + } + catch( AssertionFailedError ex ) { + fail( "unstrict mock failed when unexpected method was called" ); + } + } + + public void testDefaultResultFromUnexpectedCalls() { + assertEquals( false, _interface.booleanResult() ); + assertEquals( 0, _interface.byteResult() ); + assertEquals( '\0', _interface.charResult() ); + assertEquals( 0, _interface.shortResult() ); + assertEquals( 0, _interface.intResult() ); + assertEquals( 0L, _interface.longResult() ); + assertEquals( 0.0f, _interface.floatResult(), 0.0f ); + assertEquals( 0.0d, _interface.doubleResult(), 0.0d ); + assertEquals( "", _interface.stringResult() ); } public void testWrongNumberOfArguments() { @@ -222,6 +260,32 @@ } fail( "mock did not fail when predicate returned false" ); + } + + public void testErrorWhenOrderedMethodsCalledInWrongOrder() { + _mock.expectVoid( "first", Mock.NO_ARGS ); + _mock.expectVoid( "second", Mock.NO_ARGS ); + + _mock.order( "first", "second" ); + + try { + _interface.second(); + } + catch( AssertionFailedError ex ) { + return; + } + + fail( "mock did not enforce order of calls" ); + } + + public void testNoErrorWhenOrderedMethodsCalledInCorrectOrder() { + _mock.expectVoid( "first", Mock.NO_ARGS ); + _mock.expectVoid( "second", Mock.NO_ARGS ); + + _mock.order( "first", "second" ); + + _interface.first(); + _interface.second(); } public void testDerivedMockClass() { |