You can subscribe to this list here.
2001 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
(13) |
Aug
(151) |
Sep
(21) |
Oct
(6) |
Nov
(70) |
Dec
(8) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2002 |
Jan
(47) |
Feb
(66) |
Mar
(23) |
Apr
(115) |
May
(24) |
Jun
(53) |
Jul
(10) |
Aug
(279) |
Sep
(84) |
Oct
(149) |
Nov
(138) |
Dec
(52) |
2003 |
Jan
(22) |
Feb
(20) |
Mar
(29) |
Apr
(106) |
May
(170) |
Jun
(122) |
Jul
(70) |
Aug
(64) |
Sep
(27) |
Oct
(71) |
Nov
(49) |
Dec
(9) |
2004 |
Jan
(7) |
Feb
(38) |
Mar
(3) |
Apr
(9) |
May
(22) |
Jun
(4) |
Jul
(1) |
Aug
(2) |
Sep
(2) |
Oct
|
Nov
(15) |
Dec
(2) |
2005 |
Jan
(1) |
Feb
(1) |
Mar
|
Apr
(1) |
May
(28) |
Jun
(3) |
Jul
(11) |
Aug
(5) |
Sep
(1) |
Oct
(5) |
Nov
(2) |
Dec
(3) |
2006 |
Jan
(8) |
Feb
(3) |
Mar
(8) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Barry K. <bk...@in...> - 2003-05-20 17:37:07
|
Nat Pryce wrote: > There should be a CallOnce decorator that gets added to a call's chain of > decorators by the expect method or perhaps another syntactic sugar method > (expectOnce?). This might not have been implemented yet. Ok, I think I see. Mock.expect() does indeed decorate the call via callFactory.createCallExpectation(), which in turn creates a CallOnceExpectation. Where Mock.match() does /not/ perform this decoration. Thus I assume that match() will allow any number of calls. Cool. |
From: Barry K. <bk...@in...> - 2003-05-20 16:33:50
|
st...@m3... wrote: > Our intention is that for some sort of call object there should be something like methodOne.exepectBefore(methodTwo) assertion. Our view was that most of the time people would either care a lot or not at all about ordering, and that we'd add this mechanism for special cases like yours. Actually, ordering is not the biggest issue, but rather count. For example a strategy/flyweight that adjust a traders buyingpower might use any number of getters zero or more times, but should only adjust the actual buyingpower amount once (since in our case it will send an event which will trigger other actions in the domain model). I don't see how to do this with 0.9 as the Mock is constructed with a single implementation of CallableAddable. Am I missing something? -bk |
From: <st...@m3...> - 2003-05-20 14:55:16
|
Our intention is that for some sort of call object there should be somethin= g like methodOne.exepectBefore(methodTwo) assertion. Our view was that most= of the time people would either care a lot or not at all about ordering, a= nd that we'd add this mechanism for special cases like yours. S. > Just a first observation on the 0.9 changes. It now appears that a mock= =20 > must be order or not across all methods. I tend to setup my mocks to be= =20 > ordered or not on an individual method basis. I only order methods when= =20 > the semantics explicitly require it. But when a mock method(s) needs to= =20 > be ordered and counted, most other methods can be of the match*() type=20 > (ie, any number of calls in any order). >=20 > Is such a setup still possible now that a mock cannot be given a Call? >=20 > I like the idea of not requiring the use of CallSequence, but not the=20 > prevention of its use when necessary. >=20 > Again, I'll covert over to 0.9 and get some concrete experience and=20 > report back and update the twiki. |
From: Barry K. <bk...@in...> - 2003-05-20 12:57:54
|
Just a first observation on the 0.9 changes. It now appears that a mock must be order or not across all methods. I tend to setup my mocks to be ordered or not on an individual method basis. I only order methods when the semantics explicitly require it. But when a mock method(s) needs to be ordered and counted, most other methods can be of the match*() type (ie, any number of calls in any order). Is such a setup still possible now that a mock cannot be given a Call? I like the idea of not requiring the use of CallSequence, but not the prevention of its use when necessary. Again, I'll covert over to 0.9 and get some concrete experience and report back and update the twiki. -bk |
From: Tim M. <ma...@us...> - 2003-05-20 00:05:27
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv10050/core/test/mockobjects/dynamic Modified Files: CallBagTest.java CallSequenceTest.java Added Files: CallOnceExpectationTest.java Removed Files: ExpectedCallTest.java Log Message: Internal class rename - ExpectedCall to more specific CallOnceExpectation --- NEW FILE: CallOnceExpectationTest.java --- package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class CallOnceExpectationTest extends TestCase { final String RESULT = "result!"; final String METHOD_NAME = "methodName"; final Object[] ARGS = { "arg1", "arg2" }; final String DECORATED_DESCRIPTION = DynamicUtil.methodToString( METHOD_NAME, ARGS ); Mock ignoredMock = null; MockCallable mockCallable = new MockCallable(); 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() ); AssertMo.assertIncludes( "should say that decorated call has called status", "called", call.getDescription() ); } public void testDoesNotVerifyIfNotCalled() { try { call.verify(); } catch( AssertionFailedError ex ) { AssertMo.assertIncludes( "should include description of expected call", DECORATED_DESCRIPTION, ex.getMessage() ); return; } fail( "verify did not fail when expected call not called"); } public void testVerifiesIfCalled() throws Throwable { mockCallable.setupCallReturn( RESULT ); mockCallable.setExpectedVerifyCalls(1); call.call( ignoredMock, METHOD_NAME, ARGS ); call.verify(); mockCallable.verifyExpectations(); } public void testMatchesDelegated() throws Throwable { mockCallable.setExpectedMatches( METHOD_NAME, ARGS ); mockCallable.setupMatchesReturn(true); assertTrue( "returns matches to be true", call.matches( METHOD_NAME, ARGS ) ); mockCallable.verifyExpectations(); } public void testCallArgumentsPassedThrough() throws Throwable { mockCallable.setExpectedCall(ignoredMock, METHOD_NAME, ARGS); mockCallable.setupCallReturn(RESULT); call.call( ignoredMock, METHOD_NAME, ARGS ); mockCallable.verifyExpectations(); } public void testDoesNotMatchAfterMethodCalled() throws Throwable { mockCallable.setupMatchesReturn(true); mockCallable.setupCallReturn(RESULT); assertTrue( "First time should match", call.matches(METHOD_NAME, ARGS)); call.call(ignoredMock, METHOD_NAME, ARGS); assertFalse( "Second time should not match", call.matches(METHOD_NAME, ARGS)); } public void testDecoratedResultPassedThrough() throws Throwable { mockCallable.setupCallReturn(RESULT); assertSame( "should return decorated's result", RESULT, call.call( ignoredMock, METHOD_NAME, ARGS ) ); mockCallable.verifyExpectations(); } public void testDecoratedExceptionPassedThrough() throws Throwable { final Throwable exception = new DummyThrowable(); mockCallable.setupCallThrow(exception); try { call.call( ignoredMock, METHOD_NAME, ARGS ); fail("expected decorated's throwable to be thrown"); } catch( DummyThrowable ex ) { // expected } mockCallable.verifyExpectations(); } } Index: CallBagTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallBagTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CallBagTest.java 19 May 2003 23:37:49 -0000 1.3 +++ CallBagTest.java 20 May 2003 00:05:24 -0000 1.4 @@ -63,7 +63,7 @@ Callable anotherMethodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); callSet.addMatch(methodASignature); - callSet.addExpect(new ExpectedCall(anotherMethodASignature)); + callSet.addExpect(new CallOnceExpectation(anotherMethodASignature)); assertSame("expected result from method B, as expect has precendence over match", "result2", callSet.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); Index: CallSequenceTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallSequenceTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CallSequenceTest.java 19 May 2003 23:37:49 -0000 1.7 +++ CallSequenceTest.java 20 May 2003 00:05:24 -0000 1.8 @@ -8,7 +8,7 @@ import com.mockobjects.dynamic.CallSignature; import com.mockobjects.dynamic.CallSequence; import com.mockobjects.dynamic.Callable; -import com.mockobjects.dynamic.ExpectedCall; +import com.mockobjects.dynamic.CallOnceExpectation; import com.mockobjects.dynamic.Mock; import com.mockobjects.dynamic.ReturnStub; @@ -210,7 +210,7 @@ Callable anotherMethodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); callSequence.addMatch(methodASignature); - callSequence.addExpect(new ExpectedCall(anotherMethodASignature)); + callSequence.addExpect(new CallOnceExpectation(anotherMethodASignature)); assertSame("expected result from method B, as expect has precendence over match", "result2", callSequence.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); --- ExpectedCallTest.java DELETED --- |
From: Tim M. <ma...@us...> - 2003-05-20 00:05:27
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv10050/core/com/mockobjects/dynamic Modified Files: DefaultCallFactory.java Added Files: CallOnceExpectation.java Removed Files: ExpectedCall.java Log Message: Internal class rename - ExpectedCall to more specific CallOnceExpectation --- NEW FILE: CallOnceExpectation.java --- package com.mockobjects.dynamic; import junit.framework.*; public class CallOnceExpectation implements Callable { private Callable delegate; private boolean wasCalled = false; public CallOnceExpectation( Callable delegate ) { this.delegate = delegate; } public String getDescription() { return delegate.getDescription() + " [" + (wasCalled ? "" : "not ") + "called]"; } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { wasCalled = true; return delegate.call( mock, methodName, args ); } public boolean matches(String methodName, Object[] args) { return (!wasCalled) && delegate.matches( methodName, args ); } public void verify() { if( !wasCalled ) { throw new AssertionFailedError( delegate.getDescription() + " was expected but not called" ); } delegate.verify(); } // Implemented to aid visualisation in an IDE debugger public String toString() { return Mock.className(this.getClass()) + "(" + this.getDescription() + ")"; } } Index: DefaultCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DefaultCallFactory.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultCallFactory.java 19 May 2003 23:56:23 -0000 1.4 +++ DefaultCallFactory.java 20 May 2003 00:05:24 -0000 1.5 @@ -11,7 +11,7 @@ } public Callable createCallExpectation(Callable call) { - return new ExpectedCall(call); + return new CallOnceExpectation(call); } public Callable createCallSignature(String methodName, ConstraintMatcher constraints, Callable call) { --- ExpectedCall.java DELETED --- |
From: Tim M. <ma...@us...> - 2003-05-19 23:56:26
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3487/core/test/mockobjects/dynamic Modified Files: MockTest.java MockCallFactory.java Log Message: Internal factory method rename from createExpectedCall to createCallExpectation (makes clearer tht factories can have a different CallExpectation policy e.g. CallOnce, or CallMany) Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- MockTest.java 19 May 2003 23:37:49 -0000 1.10 +++ MockTest.java 19 May 2003 23:56:23 -0000 1.11 @@ -55,8 +55,8 @@ mockCallFactory.setupCreateVoidStub(mockVoidStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockVoidStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -71,8 +71,8 @@ mockCallFactory.setupCreateReturnStub(mockReturnStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockReturnStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -87,8 +87,8 @@ mockCallFactory.setupCreateThrowStub(mockThrowStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockThrowStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -103,8 +103,8 @@ mockCallFactory.setupCreateThrowStub(mockThrowStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -119,8 +119,8 @@ mockCallFactory.setupCreateVoidStub(mockVoidStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockVoidStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -135,8 +135,8 @@ mockCallFactory.setupCreateVoidStub(mockVoidStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -150,7 +150,7 @@ mockCallFactory.setupCreateVoidStub(mockVoidStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -164,7 +164,7 @@ mockCallFactory.setupCreateVoidStub(mockVoidStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -180,8 +180,8 @@ mockCallFactory.setupCreateReturnStub(mockReturnStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); @@ -197,8 +197,8 @@ mockCallFactory.setupCreateThrowStub(mockThrowStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); @@ -214,8 +214,8 @@ mockCallFactory.setupCreateReturnStub(mockReturnStub); mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); mockCallFactory.setupCreateCallSignature(mockCallMatch); - mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); - mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch); + mockCallFactory.setupCreateCallExpectation(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); Index: MockCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockCallFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MockCallFactory.java 19 May 2003 23:37:49 -0000 1.3 +++ MockCallFactory.java 19 May 2003 23:56:23 -0000 1.4 @@ -14,9 +14,9 @@ private ExpectationList myCreateThrowStubParameter0Values = new ExpectationList("MockCallFactory.createThrowStub(Throwable) java.lang.Throwable"); private ExpectationCounter myCreateVoidStubCalls = new ExpectationCounter("MockCallFactory.createVoidStub()"); private ReturnValues myActualCreateVoidStubReturnValues = new ReturnValues("MockCallFactory.createVoidStub()", true); - private ExpectationCounter myCreateExpectedCallCalls = new ExpectationCounter("MockCallFactory.createExpectedCall(Callable)"); - private ReturnValues myActualCreateExpectedCallReturnValues = new ReturnValues("MockCallFactory.createExpectedCall(Callable)", true); - private ExpectationList myCreateExpectedCallParameter0Values = new ExpectationList("MockCallFactory.createExpectedCall(Callable) com.mockobjects.dynamic.Callable"); + private ExpectationCounter myCreateCallExpectationCalls = new ExpectationCounter("MockCallFactory.createCallExpectation(Callable)"); + private ReturnValues myActualCreateCallExpectationReturnValues = new ReturnValues("MockCallFactory.createCallExpectation(Callable)", true); + private ExpectationList myCreateCallExpectationParameter0Values = new ExpectationList("MockCallFactory.createCallExpectation(Callable) com.mockobjects.dynamic.Callable"); private ExpectationCounter myCreateCallSignatureCalls = new ExpectationCounter("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)"); private ReturnValues myActualCreateCallSignatureReturnValues = new ReturnValues("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)", true); private ExpectationList myCreateCallSignatureParameter0Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) java.lang.String"); @@ -93,29 +93,29 @@ myActualCreateVoidStubReturnValues.add(arg); } - public void setExpectedCreateExpectedCallCalls(int calls){ - myCreateExpectedCallCalls.setExpected(calls); + public void setExpectedCreateCallExpectationCalls(int calls){ + myCreateCallExpectationCalls.setExpected(calls); } - public void addExpectedCreateExpectedCall(Callable arg0){ - myCreateExpectedCallParameter0Values.addExpected(arg0); + public void addExpectedCreateCallExpectation(Callable arg0){ + myCreateCallExpectationParameter0Values.addExpected(arg0); } - public Callable createExpectedCall(Callable arg0){ - myCreateExpectedCallCalls.inc(); - myCreateExpectedCallParameter0Values.addActual(arg0); - Object nextReturnValue = myActualCreateExpectedCallReturnValues.getNext(); + public Callable createCallExpectation(Callable arg0){ + myCreateCallExpectationCalls.inc(); + myCreateCallExpectationParameter0Values.addActual(arg0); + Object nextReturnValue = myActualCreateCallExpectationReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (Callable) nextReturnValue; } - public void setupExceptionCreateExpectedCall(Throwable arg){ - myActualCreateExpectedCallReturnValues.add(new ExceptionalReturnValue(arg)); + public void setupExceptionCreateCallExpectation(Throwable arg){ + myActualCreateCallExpectationReturnValues.add(new ExceptionalReturnValue(arg)); } - public void setupCreateExpectedCall(Callable arg){ - myActualCreateExpectedCallReturnValues.add(arg); + public void setupCreateCallExpectation(Callable arg){ + myActualCreateCallExpectationReturnValues.add(arg); } public void setExpectedCreateCallSignatureCalls(int calls){ @@ -153,8 +153,8 @@ myCreateThrowStubCalls.verify(); myCreateThrowStubParameter0Values.verify(); myCreateVoidStubCalls.verify(); - myCreateExpectedCallCalls.verify(); - myCreateExpectedCallParameter0Values.verify(); + myCreateCallExpectationCalls.verify(); + myCreateCallExpectationParameter0Values.verify(); myCreateCallSignatureCalls.verify(); myCreateCallSignatureParameter0Values.verify(); myCreateCallSignatureParameter1Values.verify(); |
From: Tim M. <ma...@us...> - 2003-05-19 23:56:26
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3487/core/com/mockobjects/dynamic Modified Files: DefaultCallFactory.java CallFactory.java Mock.java Log Message: Internal factory method rename from createExpectedCall to createCallExpectation (makes clearer tht factories can have a different CallExpectation policy e.g. CallOnce, or CallMany) Index: DefaultCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DefaultCallFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- DefaultCallFactory.java 19 May 2003 23:37:48 -0000 1.3 +++ DefaultCallFactory.java 19 May 2003 23:56:23 -0000 1.4 @@ -10,7 +10,7 @@ return new ThrowStub(exception); } - public Callable createExpectedCall(Callable call) { + public Callable createCallExpectation(Callable call) { return new ExpectedCall(call); } Index: CallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CallFactory.java 19 May 2003 23:37:49 -0000 1.3 +++ CallFactory.java 19 May 2003 23:56:23 -0000 1.4 @@ -1,16 +1,9 @@ -/* - * Created on 07-Apr-2003 - */ package com.mockobjects.dynamic; - -/** - * @author dev - */ public interface CallFactory { Callable createReturnStub( Object result ); Callable createThrowStub( Throwable throwable ); Callable createVoidStub(); - Callable createExpectedCall( Callable call ); + Callable createCallExpectation( Callable call ); Callable createCallSignature( String methodName, ConstraintMatcher constraints, Callable call ); } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- Mock.java 19 May 2003 23:37:49 -0000 1.18 +++ Mock.java 19 May 2003 23:56:23 -0000 1.19 @@ -118,7 +118,7 @@ } public void expect(String methodName, ConstraintMatcher args) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createVoidStub()))); + callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createVoidStub()))); } public void expectAndReturn(String methodName, Object result) { @@ -146,7 +146,7 @@ } public void expectAndReturn(String methodName, ConstraintMatcher args, Object result) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result)))); + callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result)))); } public void expectAndReturn(String methodName, ConstraintMatcher args, boolean result) { @@ -166,7 +166,7 @@ } public void expectAndThrow(String methodName, ConstraintMatcher args, Throwable exception) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(exception)))); + callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(exception)))); } public void matchAndReturn(String methodName, Object result) { |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25099/core/test/mockobjects/dynamic Modified Files: MockTest.java CallBagTest.java MockCallFactory.java CallSequenceTest.java CallMatchTest.java Log Message: Renamed internal class CallMatch to CallSignature - to avoid confusion with MatchAndReturn Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- MockTest.java 18 May 2003 20:59:39 -0000 1.9 +++ MockTest.java 19 May 2003 23:37:49 -0000 1.10 @@ -53,8 +53,8 @@ public void testExpectManyAndVoid() throws Throwable { mockCallFactory.setExpectedCreateVoidStubCalls(1); mockCallFactory.setupCreateVoidStub(mockVoidStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockVoidStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -69,8 +69,8 @@ public void testExpectNoneAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -85,8 +85,8 @@ public void testExpectNoneAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -101,8 +101,8 @@ public void testExpectOneAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -117,8 +117,8 @@ public void testExpectNoneAndVoid() throws Throwable { mockCallFactory.setExpectedCreateVoidStubCalls(1); mockCallFactory.setupCreateVoidStub(mockVoidStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockVoidStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockVoidStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -133,8 +133,8 @@ public void testExpectOneAndVoid() throws Throwable { mockCallFactory.setExpectedCreateVoidStubCalls(1); mockCallFactory.setupCreateVoidStub(mockVoidStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -148,8 +148,8 @@ public void testExpectWithConstraint() throws Throwable { mockCallFactory.setupCreateVoidStub(mockVoidStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -162,8 +162,8 @@ public void testExpectWithConstraintArray() throws Throwable { mockCallFactory.setupCreateVoidStub(mockVoidStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); mockCallableAddable.addExpectedAddExpect(mockExpectedCall); @@ -178,8 +178,8 @@ public void testExpectManyAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -195,8 +195,8 @@ public void testExpectManyAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -212,8 +212,8 @@ public void testExpectOneAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); mockCallFactory.setupCreateExpectedCall(mockExpectedCall); @@ -229,8 +229,8 @@ public void testMatchManyAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); @@ -244,8 +244,8 @@ public void testMatchNoneAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_NOARGANDRETURN_RESULT); @@ -259,8 +259,8 @@ public void testMatchOneAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); @@ -274,8 +274,8 @@ public void testMatchManyAndThrow() throws Throwable { mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); mockCallFactory.setupCreateThrowStub(mockThrowStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); @@ -289,8 +289,8 @@ public void testMatchOneAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); @@ -304,8 +304,8 @@ public void testMatchNoneAndReturn() throws Throwable { mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT); mockCallFactory.setupCreateReturnStub(mockReturnStub); - mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockReturnStub); - mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockReturnStub); + mockCallFactory.setupCreateCallSignature(mockCallMatch); mockCallableAddable.addExpectedAddMatch(mockCallMatch); mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); @@ -396,7 +396,7 @@ boolean IGNORED_RESULT = true; CallStub ret = new ReturnStub(new Boolean(IGNORED_RESULT)); mockCallFactory.setupCreateReturnStub(ret); - mockCallFactory.setupCreateCallMatch(new CallMatch("call",C.anyArgs(1),ret)); + mockCallFactory.setupCreateCallSignature(new CallSignature("call",C.anyArgs(1),ret)); mockCallableAddable.setupCall(new Boolean(false)); mock.matchAndReturn("call", C.anyArgs(1), IGNORED_RESULT); Index: CallBagTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallBagTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallBagTest.java 18 May 2003 20:59:38 -0000 1.2 +++ CallBagTest.java 19 May 2003 23:37:49 -0000 1.3 @@ -59,11 +59,11 @@ public void testExpectOverridesMatch() throws Throwable { - Callable methodAMatch = new CallMatch(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result1")); - Callable anotherMethodAMatch = new CallMatch(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); + Callable methodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result1")); + Callable anotherMethodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); - callSet.addMatch(methodAMatch); - callSet.addExpect(new ExpectedCall(anotherMethodAMatch)); + callSet.addMatch(methodASignature); + callSet.addExpect(new ExpectedCall(anotherMethodASignature)); assertSame("expected result from method B, as expect has precendence over match", "result2", callSet.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); Index: MockCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockCallFactory.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockCallFactory.java 18 May 2003 20:59:38 -0000 1.2 +++ MockCallFactory.java 19 May 2003 23:37:49 -0000 1.3 @@ -17,11 +17,11 @@ private ExpectationCounter myCreateExpectedCallCalls = new ExpectationCounter("MockCallFactory.createExpectedCall(Callable)"); private ReturnValues myActualCreateExpectedCallReturnValues = new ReturnValues("MockCallFactory.createExpectedCall(Callable)", true); private ExpectationList myCreateExpectedCallParameter0Values = new ExpectationList("MockCallFactory.createExpectedCall(Callable) com.mockobjects.dynamic.Callable"); - private ExpectationCounter myCreateCallMatchCalls = new ExpectationCounter("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)"); - private ReturnValues myActualCreateCallMatchReturnValues = new ReturnValues("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)", true); - private ExpectationList myCreateCallMatchParameter0Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) java.lang.String"); - private ExpectationList myCreateCallMatchParameter1Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) com.mockobjects.dynamic.ConstraintMatcher"); - private ExpectationList myCreateCallMatchParameter2Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) com.mockobjects.dynamic.Callable"); + private ExpectationCounter myCreateCallSignatureCalls = new ExpectationCounter("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)"); + private ReturnValues myActualCreateCallSignatureReturnValues = new ReturnValues("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable)", true); + private ExpectationList myCreateCallSignatureParameter0Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) java.lang.String"); + private ExpectationList myCreateCallSignatureParameter1Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) com.mockobjects.dynamic.ConstraintMatcher"); + private ExpectationList myCreateCallSignatureParameter2Values = new ExpectationList("MockCallFactory.createCallMatch(String, ConstraintMatcher, Callable) com.mockobjects.dynamic.Callable"); public void setExpectedCreateReturnStubCalls(int calls){ myCreateReturnStubCalls.setExpected(calls); @@ -118,33 +118,33 @@ myActualCreateExpectedCallReturnValues.add(arg); } - public void setExpectedCreateCallMatchCalls(int calls){ - myCreateCallMatchCalls.setExpected(calls); + public void setExpectedCreateCallSignatureCalls(int calls){ + myCreateCallSignatureCalls.setExpected(calls); } - public void addExpectedCreateCallMatch(String arg0, ConstraintMatcher arg1, Callable arg2){ - myCreateCallMatchParameter0Values.addExpected(arg0); - myCreateCallMatchParameter1Values.addExpectedMany(arg1.getConstraints()); - myCreateCallMatchParameter2Values.addExpected(arg2); - } - - public Callable createCallMatch(String arg0, ConstraintMatcher arg1, Callable arg2){ - myCreateCallMatchCalls.inc(); - myCreateCallMatchParameter0Values.addActual(arg0); - myCreateCallMatchParameter1Values.addActualMany(arg1.getConstraints()); - myCreateCallMatchParameter2Values.addActual(arg2); - Object nextReturnValue = myActualCreateCallMatchReturnValues.getNext(); + public void addExpectedCreateCallSignature(String arg0, ConstraintMatcher arg1, Callable arg2){ + myCreateCallSignatureParameter0Values.addExpected(arg0); + myCreateCallSignatureParameter1Values.addExpectedMany(arg1.getConstraints()); + myCreateCallSignatureParameter2Values.addExpected(arg2); + } + + public Callable createCallSignature(String arg0, ConstraintMatcher arg1, Callable arg2){ + myCreateCallSignatureCalls.inc(); + myCreateCallSignatureParameter0Values.addActual(arg0); + myCreateCallSignatureParameter1Values.addActualMany(arg1.getConstraints()); + myCreateCallSignatureParameter2Values.addActual(arg2); + Object nextReturnValue = myActualCreateCallSignatureReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (Callable) nextReturnValue; } - public void setupExceptionCreateCallMatch(Throwable arg){ - myActualCreateCallMatchReturnValues.add(new ExceptionalReturnValue(arg)); + public void setupExceptionCreateCallSignature(Throwable arg){ + myActualCreateCallSignatureReturnValues.add(new ExceptionalReturnValue(arg)); } - public void setupCreateCallMatch(Callable arg){ - myActualCreateCallMatchReturnValues.add(arg); + public void setupCreateCallSignature(Callable arg){ + myActualCreateCallSignatureReturnValues.add(arg); } public void verify(){ @@ -155,9 +155,9 @@ myCreateVoidStubCalls.verify(); myCreateExpectedCallCalls.verify(); myCreateExpectedCallParameter0Values.verify(); - myCreateCallMatchCalls.verify(); - myCreateCallMatchParameter0Values.verify(); - myCreateCallMatchParameter1Values.verify(); - myCreateCallMatchParameter2Values.verify(); + myCreateCallSignatureCalls.verify(); + myCreateCallSignatureParameter0Values.verify(); + myCreateCallSignatureParameter1Values.verify(); + myCreateCallSignatureParameter2Values.verify(); } } Index: CallSequenceTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallSequenceTest.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- CallSequenceTest.java 18 May 2003 20:59:38 -0000 1.6 +++ CallSequenceTest.java 19 May 2003 23:37:49 -0000 1.7 @@ -5,7 +5,7 @@ import com.mockobjects.dynamic.*; import com.mockobjects.dynamic.C; -import com.mockobjects.dynamic.CallMatch; +import com.mockobjects.dynamic.CallSignature; import com.mockobjects.dynamic.CallSequence; import com.mockobjects.dynamic.Callable; import com.mockobjects.dynamic.ExpectedCall; @@ -206,11 +206,11 @@ public void testExpectOverridesMatch() throws Throwable { - Callable methodAMatch = new CallMatch(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result1")); - Callable anotherMethodAMatch = new CallMatch(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); + Callable methodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result1")); + Callable anotherMethodASignature = new CallSignature(METHOD_A_NAME,METHOD_A_CONSTRAINTS, new ReturnStub("result2")); - callSequence.addMatch(methodAMatch); - callSequence.addExpect(new ExpectedCall(anotherMethodAMatch)); + callSequence.addMatch(methodASignature); + callSequence.addExpect(new ExpectedCall(anotherMethodASignature)); assertSame("expected result from method B, as expect has precendence over match", "result2", callSequence.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); Index: CallMatchTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/CallMatchTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallMatchTest.java 18 May 2003 20:59:38 -0000 1.2 +++ CallMatchTest.java 19 May 2003 23:37:49 -0000 1.3 @@ -14,7 +14,7 @@ Mock mock = new Mock(DummyInterface.class, "mock"); MockCallable mockCallable = new MockCallable(); MockConstraintMatcher mockConstraintMatcher = new MockConstraintMatcher(); - CallMatch callMatch = new CallMatch(METHOD_NAME, mockConstraintMatcher, mockCallable); + CallSignature callSignature = new CallSignature(METHOD_NAME, mockConstraintMatcher, mockCallable); public CallMatchTest(String name) { super(name); @@ -28,7 +28,7 @@ mockCallable.setExpectedCall(mock, METHOD_NAME, arguments); mockCallable.setupCallReturn(result); - callMatch.call(mock, METHOD_NAME, arguments); + callSignature.call(mock, METHOD_NAME, arguments); mockCallable.verifyExpectations(); } @@ -36,7 +36,7 @@ public void testUncalledCallVerifies() { mockCallable.setExpectedVerifyCalls(1); - callMatch.verify(); + callSignature.verify(); mockCallable.verifyExpectations(); } @@ -45,8 +45,8 @@ mockCallable.setExpectedVerifyCalls(1); mockCallable.setupCallReturn("result"); - callMatch.call(mock, "methodName", IGNORED_ARGS); - callMatch.verify(); + callSignature.call(mock, "methodName", IGNORED_ARGS); + callSignature.verify(); mockCallable.verifyExpectations(); } @@ -54,8 +54,8 @@ public void testMultipleCallsSucceed() throws Throwable { mockCallable.setupCallReturn("result"); - callMatch.call(mock, METHOD_NAME, IGNORED_ARGS); - callMatch.call(mock, METHOD_NAME, IGNORED_ARGS); + callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); + callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); mockCallable.verifyExpectations(); } @@ -65,7 +65,7 @@ } public void testCallDoesNotMatchWhenWrongName() throws Throwable { - assertFalse("call does not match", callMatch.matches("anotherName", IGNORED_ARGS)); + assertFalse("call does not match", callSignature.matches("anotherName", IGNORED_ARGS)); mockCallable.verifyExpectations(); } @@ -75,10 +75,10 @@ mockConstraintMatcher.setupMatches(true); - callMatch.call(mock, METHOD_NAME, IGNORED_ARGS); - assertTrue("matches after first call", callMatch.matches(METHOD_NAME, IGNORED_ARGS)); - callMatch.call(mock, METHOD_NAME, IGNORED_ARGS); - assertTrue("matches after further calls", callMatch.matches(METHOD_NAME, IGNORED_ARGS)); + callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); + assertTrue("matches after first call", callSignature.matches(METHOD_NAME, IGNORED_ARGS)); + callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); + assertTrue("matches after further calls", callSignature.matches(METHOD_NAME, IGNORED_ARGS)); mockCallable.verifyExpectations(); } @@ -90,13 +90,13 @@ mockConstraintMatcher.addExpectedMatches(args); mockConstraintMatcher.setupMatches(true); - assertTrue("matches delegated to constraint matcher", callMatch.matches(METHOD_NAME, args)); + assertTrue("matches delegated to constraint matcher", callSignature.matches(METHOD_NAME, args)); mockConstraintMatcher.verify(); } public void testMatchesFailure() throws Throwable { mockConstraintMatcher.setupMatches(false); - assertFalse("Should not match if constraint matcher doesn't", callMatch.matches(METHOD_NAME, IGNORED_ARGS)); + assertFalse("Should not match if constraint matcher doesn't", callSignature.matches(METHOD_NAME, IGNORED_ARGS)); } } |
From: Tim M. <ma...@us...> - 2003-05-19 23:37:52
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25099/examples/com/mockobjects/examples/dynamic Modified Files: SimpleServletTest.java Log Message: Renamed internal class CallMatch to CallSignature - to avoid confusion with MatchAndReturn Index: SimpleServletTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/SimpleServletTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SimpleServletTest.java 18 May 2003 20:59:39 -0000 1.2 +++ SimpleServletTest.java 19 May 2003 23:37:49 -0000 1.3 @@ -72,8 +72,8 @@ // Proposed enhancement to allow individual ordering // -// CallMatch m1 = mockHttpServletResponse.expect( "setContentType", "text/html"); -// CallMatch m2 = mockHttpServletResponse.expectAndReturn( "getWriter", C.args(), contentWriter ); +// CallSignature m1 = mockHttpServletResponse.expect( "setContentType", "text/html"); +// CallSignature m2 = mockHttpServletResponse.expectAndReturn( "getWriter", C.args(), contentWriter ); // m1.expectBefore(m2); SimpleServlet aServlet = new SimpleServlet((Timer)mockTimer.proxy(), (MailSender)mockMailSender.proxy()); |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25099/core/com/mockobjects/dynamic Modified Files: DefaultCallFactory.java CallFactory.java Mock.java Added Files: CallSignature.java Removed Files: CallMatch.java Log Message: Renamed internal class CallMatch to CallSignature - to avoid confusion with MatchAndReturn --- NEW FILE: CallSignature.java --- package com.mockobjects.dynamic; import junit.framework.Assert; public class CallSignature extends Assert implements Callable { private String methodName; private ConstraintMatcher constraints; private Callable delegate; public CallSignature( String methodName, ConstraintMatcher constraints, Callable delegate ) { this.methodName = methodName; this.constraints = constraints; this.delegate = delegate; } public Object call( Mock mock, String methodName, Object[] args ) throws Throwable { return delegate.call( mock, methodName, args ); } public void verify() { delegate.verify(); } public boolean matches(String methodName, Object[] args) { return this.methodName.equals(methodName) && constraints.matches(args); } public String getDescription() { return DynamicUtil.methodToString(methodName, constraints.getConstraints()); } // Implemented to aid visualisation in an IDE debugger public String toString() { return Mock.className(this.getClass()) + "(" + this.getDescription() + ")"; } } Index: DefaultCallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/DefaultCallFactory.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultCallFactory.java 18 May 2003 20:59:35 -0000 1.2 +++ DefaultCallFactory.java 19 May 2003 23:37:48 -0000 1.3 @@ -14,8 +14,8 @@ return new ExpectedCall(call); } - public Callable createCallMatch(String methodName, ConstraintMatcher constraints, Callable call) { - return new CallMatch( methodName, constraints, call ); + public Callable createCallSignature(String methodName, ConstraintMatcher constraints, Callable call) { + return new CallSignature( methodName, constraints, call ); } public Callable createVoidStub() { Index: CallFactory.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallFactory.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CallFactory.java 18 May 2003 20:59:35 -0000 1.2 +++ CallFactory.java 19 May 2003 23:37:49 -0000 1.3 @@ -12,5 +12,5 @@ Callable createThrowStub( Throwable throwable ); Callable createVoidStub(); Callable createExpectedCall( Callable call ); - Callable createCallMatch( String methodName, ConstraintMatcher constraints, Callable call ); + Callable createCallSignature( String methodName, ConstraintMatcher constraints, Callable call ); } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- Mock.java 18 May 2003 20:59:35 -0000 1.17 +++ Mock.java 19 May 2003 23:37:49 -0000 1.18 @@ -118,7 +118,7 @@ } public void expect(String methodName, ConstraintMatcher args) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createVoidStub()))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createVoidStub()))); } public void expectAndReturn(String methodName, Object result) { @@ -146,7 +146,7 @@ } public void expectAndReturn(String methodName, ConstraintMatcher args, Object result) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result)))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result)))); } public void expectAndReturn(String methodName, ConstraintMatcher args, boolean result) { @@ -166,7 +166,7 @@ } public void expectAndThrow(String methodName, ConstraintMatcher args, Throwable exception) { - callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(exception)))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(exception)))); } public void matchAndReturn(String methodName, Object result) { @@ -202,7 +202,7 @@ } public void matchAndReturn(String methodName, ConstraintMatcher args, Object result) { - callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result))); + callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result))); } public void matchAndReturn(String methodName, ConstraintMatcher args, boolean result) { @@ -230,7 +230,7 @@ } public void matchAndThrow(String methodName, ConstraintMatcher args, Throwable throwable) { - callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(throwable))); + callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(throwable))); } /** @deprecated @see OrderedMock --- CallMatch.java DELETED --- |
From: Tim M. <tim...@po...> - 2003-05-19 23:20:10
|
Barry - I have quickly cut and pasted an early message I sent to Vincent that has the basics and put it on the wiki (http://www.mockobjects.com/wiki/UpgradingToDynaMock) - it should be reasonably clear. Maybe as you go through it you can update the mistakes or bits that I have missed. Tim > -----Original Message----- > From: moc...@li... > [mailto:moc...@li...]On Behalf Of > Barry Kaplan > Sent: 16 May 2003 15:24 > To: mockobjects > Subject: Re: [MO-java-dev] dynamic.CallBag which allows any number of > calls > > > > > The branch is very stable - and we will be moving to it asap. I think it > > addresses many of your issues. > > Great. Does a summary of the changes exist anywhere? > > > > ------------------------------------------------------- > Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara > The only event dedicated to issues related to Linux enterprise solutions > www.enterpriselinuxforum.com > > _______________________________________________ > Mockobjects-java-dev mailing list > Moc...@li... > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003 > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.481 / Virus Database: 277 - Release Date: 13/05/2003 |
From: Steve F. <st...@m3...> - 2003-05-18 22:17:21
|
Seconded. Thanks Vincent. S. Tim Mackinnon wrote: > Vincent - > > While we may disagree on some of the smaller details - thanks for sorting > out 08 so we could get the new stuff into the head release. > > Tim |
From: Tim M. <ma...@us...> - 2003-05-18 21:51:38
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv15248/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment Mock.java Log Message: Branch deprecated in favour of CVS Head (changes now merged) Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.16.2.15 retrieving revision 1.16.2.16 diff -u -r1.16.2.15 -r1.16.2.16 --- Mock.java 18 May 2003 20:03:23 -0000 1.16.2.15 +++ Mock.java 18 May 2003 21:51:34 -0000 1.16.2.16 @@ -9,6 +9,9 @@ import com.mockobjects.Verifiable; import com.mockobjects.constraint.Constraint; +/** @deprecated + * Please use the CVS Head - this branch has now been merged into place + */ public class Mock implements InvocationHandler,Verifiable { private String name; private Object proxy; |
From: Tim M. <tim...@po...> - 2003-05-18 21:48:10
|
Vincent - While we may disagree on some of the smaller details - thanks for sorting out 08 so we could get the new stuff into the head release. Tim --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003 |
From: Tim M. <tim...@po...> - 2003-05-18 21:14:10
|
Guys - I bit the bullet and put it in so Vincent has an official baseline for his book. Eclipse made it very easy - unfortunately it was hard to run all of the tests as some of stuff relies on JSDK and Mail api stuff that I don't have. Tim > -----Original Message----- > From: moc...@li... > [mailto:moc...@li...]On Behalf Of > Vincent Massol > Sent: 16 May 2003 20:40 > To: 'Mockobjects-Java-Dev' > Subject: [MO-java-dev] Ready to merge the new Dynamic Mock branch in CVS > HEAD > > > Hi guys, > > It's all yours... :-) > > -Vincent > > > > ------------------------------------------------------- > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003 |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/com/mockobjects/dynamic Modified Files: CallBag.java C.java CallCollection.java CallSequence.java Mock.java Added Files: Callable.java CallStub.java ReturnStub.java FullConstraintMatcher.java CallMatch.java ExpectedCall.java VoidStub.java CallableAddable.java DefaultCallFactory.java ConstraintMatcher.java OrderedMock.java AnyConstraintMatcher.java ThrowStub.java DynamicUtil.java CallFactory.java Removed Files: MockReturnCall.java AbstractMockCall.java MockThrowCall.java MethodMap.java MethodExpectation.java CallCounter.java MockVoidCall.java MockCall.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: CallBag.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallBag.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CallBag.java 4 Apr 2003 11:13:31 -0000 1.1 +++ CallBag.java 18 May 2003 20:59:35 -0000 1.2 @@ -1,23 +1,85 @@ +/* + * Created on 04-Apr-2003 + */ package com.mockobjects.dynamic; -/** - * Represents a collection of calls for which the order in which they are called is not checked but the total number of times is. - */ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; -public class CallBag extends CallCollection { - +public class CallBag extends CallCollection implements Callable, CallableAddable { + private List expectedCalls = new ArrayList(); + private List expectedMatches = new ArrayList(); + public CallBag() { - super(); } - - protected int findCallIndexAndCheckArguments(Object[] args,int callnumber) { - for (int i = 0; i < _expected_constraints.size(); i++) { - if (!hasBeenCalled(i) && checkArguments(i, args)) { - return i; + + public void reset() { + this.expectedCalls.clear(); + this.expectedMatches.clear(); + } + + public Object call(Mock mock, String methodName, Object[] args) + throws Throwable { + + Callable matchingCall = findMatchingCall(methodName, args, this.expectedCalls); + if(matchingCall == null) { + matchingCall = findMatchingCall(methodName, args, this.expectedMatches); + } + if(matchingCall == null) { + throw createUnexpectedCallError(methodName, args); + } + + return matchingCall.call(mock, methodName, args); + + } + + private Callable findMatchingCall(String methodName, Object[] args, List callList) { + for (Iterator call = callList.iterator(); call.hasNext();) { + Callable element = (Callable) call.next(); + + if (element.matches(methodName, args)) { + return element; } } - fail(errorMessage(args)); - return -1; + + return null; + } + + public String getDescription() { + if (this.expectedCalls.isEmpty()) { + return "no methods"; + } else { + StringBuffer buf = new StringBuffer(); + + buf.append("one of:\n"); + + for (Iterator i = this.expectedCalls.iterator(); i.hasNext();) { + buf.append(((Callable) i.next()).getDescription()); + buf.append("\n"); + } + + return buf.toString(); + } + } + + public boolean matches(String methodName, Object[] args) { + throw new Error("not implemented"); + } + + public void verify() { + for (Iterator call = this.expectedCalls.iterator(); call.hasNext();) { + Callable element = (Callable) call.next(); + element.verify(); + } + } + + public void addExpect(Callable call) { + this.expectedCalls.add(call); + } + + public void addMatch(Callable call) { + this.expectedMatches.add(call); } } Index: C.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/C.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- C.java 24 Nov 2002 10:59:29 -0000 1.1 +++ C.java 18 May 2003 20:59:35 -0000 1.2 @@ -19,8 +19,8 @@ public static final Constraint IS_ZERO = eq(new Integer(0)); public static final Constraint IS_NOT_ZERO = not(IS_ZERO); - public static final Constraint[] ANY_ARGS = MockVoidCall.ANY_ARGS; - + public static final ConstraintMatcher NO_ARGS = new FullConstraintMatcher(new Constraint[0]); + public static final ConstraintMatcher ANY_ARGS = new AnyConstraintMatcher(); public static Constraint same( Object o ) { return new IsSame(o); @@ -29,7 +29,15 @@ public static Constraint eq( Object o ) { return new IsEqual(o); } - + + public static ConstraintMatcher eq( Object arg0, Object arg1 ) { + return args(eq(arg0), eq(arg1)); + } + + public static ConstraintMatcher eq( Object arg0, Object arg1, Object arg2 ) { + return args(eq(arg0), eq(arg1), eq(arg2)); + } + public static Constraint eq( int n ) { return new IsEqual( new Integer(n) ); } @@ -89,23 +97,34 @@ public static Constraint isA( Class c ) { return new IsInstanceOf(c); } + + /* Helper methods for succinctly constructing Constraint arrays */ - public static Constraint[] args() { - return Mock.NO_ARGS; + public static ConstraintMatcher args() { + return NO_ARGS; } - public static Constraint[] args(Constraint p) { - return new Constraint[] {p}; + public static ConstraintMatcher args(Constraint p) { + return new FullConstraintMatcher(new Constraint[]{p}); } - public static Constraint[] args(Constraint p1, Constraint p2) { - return new Constraint[] {p1, p2}; + public static ConstraintMatcher args(Constraint p1, Constraint p2) { + return new FullConstraintMatcher(new Constraint[]{p1, p2}); } - public static Constraint[] args(Constraint p1, Constraint p2, Constraint p3) { - return new Constraint[] {p1, p2, p3}; + public static ConstraintMatcher args(Constraint p1, Constraint p2, Constraint p3) { + return new FullConstraintMatcher(new Constraint[]{p1, p2, p3}); } + + public static ConstraintMatcher anyArgs( int argCount) { + Constraint[] constraints = new Constraint[argCount]; + for (int i = 0; i < constraints.length; i++) { + constraints[i] = new IsAnything(); + } + + return new FullConstraintMatcher(constraints); + } } Index: CallCollection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallCollection.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CallCollection.java 4 Apr 2003 11:13:31 -0000 1.1 +++ CallCollection.java 18 May 2003 20:59:35 -0000 1.2 @@ -1,225 +1,19 @@ -/** Created on Oct 31, 2002 by npryce - * Copyright (c) B13media Ltd. - */ package com.mockobjects.dynamic; -import java.util.ArrayList; -import java.util.List; - -import com.mockobjects.Verifiable; -import com.mockobjects.constraint.*; -import com.mockobjects.util.AssertMo; - -/** A {@link MockCall} that expects a sequence of calls, checks their - * arguments and mocks their behaviour. - * A test will fail if too few or too many calls are made. - */ -public abstract class CallCollection extends AssertMo implements MockCall, Verifiable { - protected List _expected_calls = new ArrayList(); - protected List _expected_constraints = new ArrayList(); - public static final String EXPECTED_CALLS="expected calls were:"; - public static final String NO_EXPECTED_CALLS="no expected calls were set"; - public static final String CALLED="called already" ; - public static final String NOT_CALLED="not called"; - public static final String ARGS="\nreceived arguments were "; - - private int numcalls = 0; - - protected List _hasBeenCalled = new ArrayList(); - - public CallCollection() { - super(); - } - - public void expect(MockCall call, Constraint[] constraints) { - _expected_calls.add(call); - _expected_constraints.add(constraints); - _hasBeenCalled.add(Boolean.FALSE); - } - - /* The following were copy-and-pasted from Mock.java with minor editing. - * Is there any way to remove this duplication? - */ - - /** Expect a method call and return a result when it is called. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - */ - public void expectAndReturn(Constraint[] args, Object result) { - expect(new MockReturnCall(args, result), args); - } - - /** Expect a method call and return a result when it is called. - * - * @param arg - * An single object that will be compared with predicate same() - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - */ - public void expectAndReturn(Object arg, Object result) { - expectAndReturn(C.args(C.eq(arg)), result); - } - - /** Expect a method call with no parameters and return a result when it is called. - * - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - */ - public void expectAndReturn(Object result) { - expectAndReturn(Mock.NO_ARGS, result); - } - - /** Expect a call to a method with a void return type. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - */ - public void expectVoid(Constraint[] args) { - expect(new MockVoidCall(args), args); - } - - /** Expect a call to a method with a void return type. - * - * @param method - * The name of the method that will be called. - * @param arg - * An single object that will be compared with predicate eq() - */ - public void expectVoid(Object arg) { - expectVoid(C.args(C.eq(arg))); - } - - /** Expect a call to a method with a void return type and no parameters - */ - public void expectVoid() { - expectVoid(Mock.NO_ARGS); - } - - /** Expect a method call and throw an exception or error when it is called. - * - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param exception - * The exception or error that will be thrown as a result of this call. - */ - public void expectAndThrow(Constraint[] args, Throwable exception) { - expect(new MockThrowCall(args, exception), args); - } - - /** Expect a method call and throw an exception or error when it is called. - * - * @param arg - * An single object that will be compared with predicate eq() - * @param exception - * The exception or error that will be thrown as a result of this call. - */ - public void expectAndThrow(Object arg, Throwable exception) { - expectAndThrow(C.args(C.eq(arg)), exception); - } - - /** Expect a method call with no parameters and throw an exception or error when it is called. - * - * @param exception - * The exception or error that will be thrown as a result of this call. - */ - public void expectAndThrow(Throwable exception) { - expectAndThrow(Mock.NO_ARGS, exception); - } - - public Object call(Object[] args) throws Throwable { - numcalls++; - assertTrue(errorMessage(args), numcalls <= _expected_calls.size()); - - int callIndex = findCallIndexAndCheckArguments(args,numcalls); - MockCall call = (MockCall) _expected_calls.get(callIndex); - _hasBeenCalled.add(callIndex, Boolean.TRUE); - return call.call(args); - } - abstract protected int findCallIndexAndCheckArguments(Object[] args,int callNumber); - - protected boolean checkArguments(int index, Object[] args) { - Constraint[] cons =(Constraint[]) _expected_constraints.get(index); - int cons_count = (cons == null) ? 0 : cons.length; - int arg_count = (args == null) ? 0 : args.length; - if (cons_count != arg_count) { - return false; - } - - for (int i = 0; i < arg_count; i++) { - if (!cons[i].eval(args[i])) { - return false; - } - } - return true; - } - - public void verify() { - assertTrue(errorMessage(null), _expected_calls.size() == numcalls); - } - - public String printConstraint(int i) { - if (i < 0 || i > (_expected_constraints.size() - 1)) { - return ""; - } - - Constraint[] cons = (Constraint[]) _expected_constraints.get(i); - return printArgsArray(cons); - } - - protected String printArgsArray(Object[] array) { - if (array.length == 0) { - return "[no args]"; - } - StringBuffer result = new StringBuffer("["); - for (int j = 0; j < array.length; j++) { - result.append(array[j].toString()); - result.append(','); - } - result.deleteCharAt(result.length() - 1); - result.append(']'); - return result.toString(); - } - - public String errorMessage(Object[] args) { - - StringBuffer result = new StringBuffer(EXPECTED_CALLS); - if (_expected_constraints.size() == 0) { - result = new StringBuffer(NO_EXPECTED_CALLS); - } - for (int i = 0; i < _expected_constraints.size(); i++) { - result.append('\n'); - result.append(printConstraint(i)); - - result.append(" :" + (hasBeenCalled(i) ? CALLED : NOT_CALLED)); - } - if (args != null) { - result.append( ARGS+ printArgsArray(args)); - } - - return result.toString(); - } - - protected boolean hasBeenCalled(int i) { - return ((Boolean) _hasBeenCalled.get(i)).booleanValue(); - } - - +import junit.framework.AssertionFailedError; +abstract public class CallCollection extends Object { + protected AssertionFailedError createUnexpectedCallError(String methodName, Object[] args) { + + StringBuffer buf = new StringBuffer(); + buf.append("Unexpected call: "); + buf.append(DynamicUtil.methodToString(methodName, args)); + buf.append("\n"); + buf.append("Expected "); + buf.append(getDescription()); + return new AssertionFailedError(buf.toString()); + } + abstract protected String getDescription(); } Index: CallSequence.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallSequence.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CallSequence.java 4 Apr 2003 11:13:31 -0000 1.4 +++ CallSequence.java 18 May 2003 20:59:35 -0000 1.5 @@ -1,28 +1,73 @@ package com.mockobjects.dynamic; -/** - * @author chris - * - * Represents a collection of calls on the same method for which the order of the calls is important - */ -public class CallSequence extends CallCollection { +import java.util.ArrayList; +import java.util.Iterator; - /** - * Constructor for CallSequence. - */ - public CallSequence() { - super(); - } +import junit.framework.AssertionFailedError; + +public class CallSequence extends CallCollection implements Callable, CallableAddable { + + private ArrayList expectedCalls = new ArrayList(); + private CallBag matchedCalls = new CallBag(); + int callIndex = 0; - protected int findCallIndexAndCheckArguments(Object[] args,int callnumber) { - int callIndex = callnumber - 1; - if (! checkArguments(callIndex, args) ){ - fail(errorMessage(args)); + public void reset() + { + this.expectedCalls.clear(); + this.matchedCalls.reset(); + } + + public Object call(Mock mock, String methodName, Object[] args) throws Throwable { + if (expectedCalls.size() == 0) throw new AssertionFailedError("no methods defined on mock, received: " + DynamicUtil.methodToString(methodName, args)); + if (callIndex == expectedCalls.size()) throw new AssertionFailedError("mock called too many times, received: " + DynamicUtil.methodToString(methodName, args)); + + Callable nextCall = (Callable)expectedCalls.get(callIndex++); + if (nextCall.matches(methodName, args)) + return nextCall.call(mock, methodName, args); + + try { + return matchedCalls.call(mock, methodName, args); + } catch (AssertionFailedError ex) { + throw createUnexpectedCallError(methodName, args); } - return callIndex; } - + public String getDescription() { + if (expectedCalls.isEmpty()) { + return "no methods"; + } else { + StringBuffer buf = new StringBuffer(); + + buf.append("in sequence:\n"); + + int j=0; + for (Iterator i = expectedCalls.iterator(); i.hasNext();) { + buf.append(((Callable)i.next()).getDescription()); + if (j++==(callIndex-1)) buf.append(" <<< Next Expected Call"); + buf.append("\n"); + } + + return buf.toString(); + } + } + + public boolean matches(String methodName, Object[] args) { + throw new AssertionFailedError("matches() operation not supported in CallSequence"); + } + + public void addExpect(Callable call) { + expectedCalls.add(call); + } + + public void addMatch(Callable call) { + matchedCalls.addMatch(call); + } + public void verify() { + for (Iterator iter = expectedCalls.iterator(); iter.hasNext();) { + Callable callable = (Callable) iter.next(); + callable.verify(); + } + } } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- Mock.java 24 Nov 2002 11:01:00 -0000 1.16 +++ Mock.java 18 May 2003 20:59:35 -0000 1.17 @@ -1,461 +1,276 @@ -/* Copyright (c) 2002 Nat Pryce. All rights reserved. - * - * Created on February 10, 2002, 11:06 PM - */ package com.mockobjects.dynamic; -import com.mockobjects.Verifiable; -import com.mockobjects.constraint.*; - -import junit.framework.Assert; - import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.*; - - -/** A convenient class for creating simple - * <a href="http://www.mockobjects.com">mock objects</a>. - */ -public class Mock - extends Assert - implements InvocationHandler, Verifiable -{ - public static final Object VOID = new Object(); - - /** A convenient constant for defining expectations for methods that - * have no methods. - */ - public static final Constraint[] NO_ARGS = new Constraint[0]; - - - private Class[] _proxy_classes; - private Object _proxy; - private MethodMap methods = new MethodMap(); - private boolean _strict = false; - - private Map _default_results = new HashMap(); +import junit.framework.AssertionFailedError; - /** Creates a new Mock object that mocks the behaviour of - * a single interface. - */ - public Mock( Class interface_to_mock ) { - this( new Class[]{interface_to_mock} ); - } - - /** Creates a new Mock object that mocks the behaviour of - * two interfaces. - */ - public Mock( Class interface1, Class interface2 ) { - this( new Class[]{ interface1, interface2 } ); - } - - /** Creates a new Mock object that mocks the behaviour of - * three interfaces. - */ - public Mock( Class interface1, Class interface2, Class interface3 ) { - this( new Class[] { interface1, interface2, interface3 } ); - } - - /** Creates a new Mock object that mocks the behaviour of - * four interfaces. - */ - public Mock( Class interface1, Class interface2, - Class interface3, Class interface4 ) - { - this( new Class[] { interface1, interface2, interface3, interface4 } ); - } - - /** Creates a new Mock object that mocks the behaviour of - * any number of interfaces. - */ - public Mock( Class[] interfaces_to_mock ) { - _proxy_classes = (Class[])interfaces_to_mock.clone(); - _proxy = createInterface( interfaces_to_mock ); - - setupDefaultResult( byte.class, new Byte((byte)0) ); - setupDefaultResult( short.class, new Short((short)0) ); - setupDefaultResult( int.class, new Integer(0) ); - setupDefaultResult( long.class, new Long(0L) ); - setupDefaultResult( float.class, new Float(0.0f) ); - setupDefaultResult( double.class, new Double(0.0d) ); - setupDefaultResult( boolean.class, Boolean.FALSE ); - setupDefaultResult( char.class, new Character('\0') ); - setupDefaultResult( String.class, "" ); - } - - /** Returns an object that can be cast to any of the interfaces passed - * to this Mock's constructor. This Mock will mock the behaviour of - * calls to the proxy. - */ - public Object proxy() { - return _proxy; - } - - /** Is the mock in strict mode? In strict mode the mock will throw - * {@link junit.framework.AssertionFailedError} exceptions when - * unexpected method calls are made. Otherwise, the mock ignore - * the call or return default arguments. - */ - public boolean isStrict() { - return _strict; - } - - public void setStrict( boolean strict ) { - _strict = strict; - } - - /** Mock the behaviour of a method without requiring that the method - * actually be called. Note: the ExpectedCall may check the arguments - * of the call but should usually not do so. - * - * @param call - * The call to be mocked. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void setup( String method_name, MockCall call ) { - checkMethodName( method_name ); - methods.setupCall( method_name, call ); - } - - /** Set up calls to <var>method</var> to return <var>result</var>, - * but do not define any expectations upon arguments to those calls. - * Arguments of calls to <var>method</var> will be ignored. - * - * @param method_name - * The name of the method that will be called. - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void setupResult( String method_name, Object result ) { - setup( method_name, new MockReturnCall( result ) ); - } - - /** Set up calls to <var>method</var> to throw <var>exception</var>, - * but do not define any expectations upon arguments to those calls. - * Arguments of calls to <var>method</var> will be ignored. - * - * @param method_name - * The name of the method that will be called. - * @param exception - * The exception or error that will be thrown as a result of this call. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void setupThrow( String method_name, Throwable exception ) { - setup( method_name, new MockThrowCall( exception ) ); - } - - /** Set up the value returned by methods that have the given result - * type, if no result or exception has been explicitly defined for - * the called method by {@link #setupResult}, {@link #setupThrow}, - * {@link #expectReturn} or {@link #expectThrow}. - * - * @param result_type - * The result type of methods to be mocked. - * @param result_value - * The default value returned by mocked methods that return values - * of type <var>result_type</var>. - */ - public void setupDefaultResult( Class result_type, Object result_value ) { - _default_results.put( result_type, result_value ); - } - - /** Expect a method call and mock the behaviour of that call. - * - * @param call - * An object describing the expected call and mocking its behaviour. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expect( String method_name, MockCall call ) { - checkMethodName( method_name ); - methods.expectCall( method_name, call ); - } - - /** Expect a method call and return a result when it is called. - * - * @param method - * The name of the method that will be called. - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndReturn( String method, Constraint[] args, Object result ) { - expect( method, new MockReturnCall( args, result ) ); - } - - /** Expect a method call and return a result when it is called. - * - * @param method - * The name of the method that will be called. - * @param arg - * An single object that will be compared with predicate eq() - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndReturn( String method, Object arg, Object result ) { - expectAndReturn( method, C.args(C.eq(arg) ), result); - } - - /** Expect a method call with no parameters and return a result when it is called. - * - * @param method - * The name of the method that will be called. - * @param result - * The result that will be returned from this call. Primitive types - * must be wrapped in the equivalent Java object, and will be unwrapped - * before being returned to the caller of the method. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndReturn( String method, Object result ) { - expectAndReturn( method, NO_ARGS, result ); - } - - /** - * Expect a call to a method with a void return type. - * - * @param method - * The name of the method that will be called. - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectVoid( String method, Constraint[] args ) { - expect( method, new MockVoidCall( args ) ); - } - - /** - * Expect a call to a method with a void return type. - * - * @param method - * The name of the method that will be called. - * @param arg - * An single object that will be compared with predicate eq() - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectVoid(String method, Object arg) { - expectVoid( method, C.args(C.eq(arg)) ); - } - - /** - * Expect a call to a method with a void return type and no parameters - * - * @param method - * The name of the method that will be called. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectVoid(String method) { - expectVoid( method, NO_ARGS ); - } - - /** Expect a method call and throw an exception or error when it is called. - * - * @param method - * The name of the method that will be called. - * @param args - * An array of {@link com.mockobjects.dynamic.Constraint}s that specify the - * epxected arguments of the method call. The length of the array defines - * the expected arity of the method call. - * @param exception - * The exception or error that will be thrown as a result of this call. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndThrow( String method, Constraint[] args, Throwable exception ) { - expect( method, new MockThrowCall( args, exception ) ); - } - - /** Expect a method call and throw an exception or error when it is called. - * - * @param method - * The name of the method that will be called. - * @param arg - * An single object that will be compared with predicate eq() - * @param exception - * The exception or error that will be thrown as a result of this call. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndThrow( String method, Object arg, Throwable exception ) { - expectAndThrow( method, C.args(C.eq(arg)), exception ); - } - - /** Expect a method call with no parameters and throw an exception or error when it is called. - * - * @param method - * The name of the method that will be called. - * @param exception - * The exception or error that will be thrown as a result of this call. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectAndThrow( String method, Throwable exception ) { - expectAndThrow( method, NO_ARGS, exception ); - } - - /** Expect a method not to be called. - * An {@link junit.framework.AssertionFailedError} will be thrown if - * the method is called. - * - * @param methodName - * The name of the method that will not be called. - * @throws junit.framework.AssertionFailedError - * The method name is not the name of a method mocked by this - * Mock object. - */ - public void expectNotCalled( String method_name ) { - checkMethodName( method_name ); - methods.expectNotCalled(method_name); - } - - /** Define an order between two calls. The method named - * <var>subsequent_method</var> <em>must</em> be called after - * the method namd <var>preceding_method</var>, otherwise an - * {@link junit.framework.AssertionFailedError} will be thrown - * when <var>subsequent_method</var> is called. - * - * @throws junit.framework.AssertionFailedError - * Either method name is not the name of a method mocked by this - * Mock object. - */ - public void order( String preceding_method, String subsequent_method ) { - checkMethodName( preceding_method ); - checkMethodName( subsequent_method ); - methods.order( preceding_method, subsequent_method ); - } - - - /** - * @see java.lang.Object#equals(java.lang.Object) - * This version will unpack the inovaction handler if the - * other object is a proxy. - */ - public boolean equals(Object obj) - { - try { - return equals(Proxy.getInvocationHandler(obj)); - } - catch (IllegalArgumentException notAProxy) { - return super.equals(obj); - } - } - - /** Called by the {@link java.lang.reflect.Proxy} to mock the behaviour of an - * invoked method and check expectations. - */ - public Object invoke( Object obj, Method method, Object[] args ) - throws Throwable - { - MethodExpectation expectation = methods.startCall(method.getName()); - try { - return getMatchingMethod(method).invoke( this, args ); - } - catch( NoSuchMethodException ex ) { - return mockCall( expectation, method, args ); - } - } - - protected Object mockCall( Method method, Object[] args ) - throws Throwable - { - return mockCall(methods.startCall(method.getName()), method, args); - } - - protected Object mockCall( MethodExpectation expectation, - Method method, - Object[] args ) - throws Throwable - { - if( expectation.canBeCalled() ) { - return expectation.callAndCheckResult( method, args ); - } else { - assertTrue( "unexpected call to "+method.getName(), !_strict ); - return defaultResult( method.getReturnType() ); - } - } - - - private Method getMatchingMethod(Method method) - throws NoSuchMethodException - { - return getClass().getMethod( method.getName(), method.getParameterTypes() ); - } - - private Object defaultResult(Class return_type) { - return _default_results.get(return_type); - } - - /** - * Fails if not all the expected calls have been made to this mock object. - * @throws junit.framework.AssertionFailedError - * Not all expected calls were made to this mock object. - */ - public void verify() { - methods.verify(); - } +import com.mockobjects.Verifiable; +import com.mockobjects.constraint.Constraint; - private Object createInterface( Class[] interface_classes ) { - return Proxy.newProxyInstance( getClass().getClassLoader(), - interface_classes, - this ); - } - - private void checkMethodName( String method_name ) { - for( int i = 0; i < _proxy_classes.length; i++ ) { - if( hasMethod( _proxy_classes[i], method_name ) ) { - return; - } - } - - fail("method " + method_name + - " is not defined by any of the mocked interfaces" ); - } - - private boolean hasMethod( Class c, String method_name ) { - Method[] methods = c.getMethods(); - for( int i = 0; i < methods.length; i++ ) { - if( methods[i].getName().equals(method_name) ) { - return true; - } - } - return false; - } +public class Mock implements InvocationHandler,Verifiable { + private String name; + private Object proxy; + private CallFactory callFactory; + private CallableAddable callSequence; + + public Mock(CallFactory callFactory, CallableAddable callableAddable, Class mockedClass, String name) { + this.name = name; + this.callFactory = callFactory; + this.callSequence = callableAddable; + this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { mockedClass }, this); + } + + public Mock(Class mockedClass, String nonDefaultName) { + this(new DefaultCallFactory(), new CallBag(), mockedClass, nonDefaultName); + } + + public Mock(Class mockedClass) { + this(mockedClass, mockNameFromClass(mockedClass)); + } + + public void reset() { + this.callSequence.reset(); + } + + public static String mockNameFromClass(Class c) { + return "mock" + className(c); + } + + public static String className(Class c) { + String name = c.getName(); + int dotIndex = name.lastIndexOf('.'); + + if (dotIndex >= 0) { + return name.substring(dotIndex + 1); + } else { + return name; + } + } + + private ConstraintMatcher createConstraintMatcher(Object constraintArg) { + // Can't overload this method as callee had an Object parameter, and java + // doesn't do a secondary dispatch on the true underlying type + + if(constraintArg instanceof Constraint[]) { + // to support possible legacy usage of new Contraint[] {...} + return new FullConstraintMatcher((Constraint[])constraintArg); + } else if(constraintArg instanceof Constraint) { + // to support usage of C.lt(5) type constraints + return C.args((Constraint)constraintArg); + } else { + // normal usage of the overloaded expect/match object parameter + return C.args(C.eq(constraintArg)); + } + } + + public String getMockName() { + return this.name; + } + + public String toString() { + return this.name; + } + + public Object proxy() { + return this.proxy; + } + + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + try { + if(checkingProxyEquality(method, args)) { + return new Boolean(args[0] == this.proxy); + } else if(gettingMockName(method, args)) { + return this.getMockName(); + } else { + Object result = callSequence.call(this, method.getName(), ((args == null) ? new Object[0] : args)); + return result; + } + } catch (AssertionFailedError ex) { + throw new AssertionFailedError(name + ": " + ex.getMessage()); + } + } + + private boolean checkingProxyEquality(Method method, Object[] args) { + return (method.getName().equals("equals")) && (args.length == 1) && (Proxy.isProxyClass(args[0].getClass())); + } + + private boolean gettingMockName(Method method, Object[] args) { + return (method.getName().equals("getMockName")) && (args.length == 0); + } + + public void verify() { + try { + callSequence.verify(); + } catch (AssertionFailedError ex) { + throw new AssertionFailedError(name + ": " + ex.getMessage()); + } + } + + public void expect(String methodName) { + expect(methodName, C.NO_ARGS); + } + + public void expect(String methodName, Object singleEqualArg) { + expect(methodName, createConstraintMatcher(singleEqualArg)); + } + + public void expect(String methodName, ConstraintMatcher args) { + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createVoidStub()))); + } + + public void expectAndReturn(String methodName, Object result) { + this.expectAndReturn(methodName, C.NO_ARGS, result); + } + + public void expectAndReturn(String methodName, boolean result) { + this.expectAndReturn(methodName, new Boolean(result)); + } + + public void expectAndReturn(String methodName, int result) { + this.expectAndReturn(methodName, new Integer(result)); + } + + public void expectAndReturn(String methodName, Object singleEqualArg, Object result) { + this.expectAndReturn(methodName, createConstraintMatcher(singleEqualArg), result); + } + + public void expectAndReturn(String methodName, Object singleEqualArg, boolean result) { + this.expectAndReturn(methodName, singleEqualArg, new Boolean(result)); + } + + public void expectAndReturn(String methodName, Object singleEqualArg, int result) { + this.expectAndReturn(methodName, singleEqualArg, new Integer(result)); + } + + public void expectAndReturn(String methodName, ConstraintMatcher args, Object result) { + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result)))); + } + + public void expectAndReturn(String methodName, ConstraintMatcher args, boolean result) { + this.expectAndReturn(methodName, args, new Boolean(result)); + } + + public void expectAndReturn(String methodName, ConstraintMatcher args, int result) { + this.expectAndReturn(methodName, args, new Integer(result)); + } + + public void expectAndThrow(String methodName, Throwable exception) { + this.expectAndThrow(methodName, C.NO_ARGS, exception); + } + + public void expectAndThrow(String methodName, Object singleEqualArg, Throwable exception) { + this.expectAndThrow(methodName, createConstraintMatcher(singleEqualArg), exception); + } + + public void expectAndThrow(String methodName, ConstraintMatcher args, Throwable exception) { + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(exception)))); + } + + public void matchAndReturn(String methodName, Object result) { + this.matchAndReturn(methodName, C.NO_ARGS, result); + } + + public void matchAndReturn(String methodName, boolean result) { + this.matchAndReturn(methodName, new Boolean(result)); + } + + public void matchAndReturn(String methodName, int result) { + this.matchAndReturn(methodName, new Integer(result)); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, Object result) { + this.matchAndReturn(methodName, createConstraintMatcher(singleEqualArg), result); + } + + public void matchAndReturn(String methodName, boolean singleEqualArg, Object result) { + this.matchAndReturn(methodName, new Boolean(singleEqualArg), result); + } + + public void matchAndReturn(String methodName, int singleEqualArg, Object result) { + this.matchAndReturn(methodName, new Integer(singleEqualArg), result); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, boolean result) { + this.matchAndReturn(methodName, singleEqualArg, new Boolean(result)); + } + + public void matchAndReturn(String methodName, Object singleEqualArg, int result) { + this.matchAndReturn(methodName, singleEqualArg, new Integer(result)); + } + + public void matchAndReturn(String methodName, ConstraintMatcher args, Object result) { + callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result))); + } + + public void matchAndReturn(String methodName, ConstraintMatcher args, boolean result) { + this.matchAndReturn(methodName, args, new Boolean(result)); + } + + public void matchAndReturn(String methodName, ConstraintMatcher args, int result) { + this.matchAndReturn(methodName, args, new Integer(result)); + } + + public void matchAndThrow(String methodName, Throwable throwable) { + this.matchAndThrow(methodName, C.NO_ARGS, throwable); + } + + public void matchAndThrow(String methodName, Object singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName, createConstraintMatcher(singleEqualArg), throwable); + } + + public void matchAndThrow(String methodName, boolean singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName,new Boolean(singleEqualArg), throwable); + } + + public void matchAndThrow(String methodName, int singleEqualArg, Throwable throwable) { + this.matchAndThrow(methodName,new Integer(singleEqualArg), throwable); + } + + public void matchAndThrow(String methodName, ConstraintMatcher args, Throwable throwable) { + callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(throwable))); + } + + /** @deprecated @see OrderedMock + */ + public void expect(String methodName, CallSequence deprecated) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); + } + + /** @deprecated @see OrderedMock + */ + public void expectAndReturn(String methodName, CallSequence deprecated, Object result) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); + } + + /** @deprecated @see OrderedMock + */ + public void expectAndThrow(String methodName, CallSequence deprecated, Throwable throwable) { + throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); + } + + /** @deprecated @see expect + */ + public void expectVoid(String methodName, ConstraintMatcher args) { + this.expect(methodName, args); + } + + /** @deprecated @see expect + */ + public void expectVoid(String methodName, Object equalArg) { + this.expect(methodName,equalArg); + } + + /** @deprecated @see expect + */ + public void expectVoid(String methodName) { + this.expect(methodName); + } + + /** @deprecated Not required, as if methodName is called, you will get a an exception + */ + public void expectNotCalled(String methodName) { + } } --- MockReturnCall.java DELETED --- --- AbstractMockCall.java DELETED --- --- MockThrowCall.java DELETED --- --- MethodMap.java DELETED --- --- MethodExpectation.java DELETED --- --- CallCounter.java DELETED --- --- MockVoidCall.java DELETED --- --- MockCall.java DELETED --- |
From: Tim M. <ma...@us...> - 2003-05-18 21:00:11
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/com/mockobjects Modified Files: AbstractExpectationCollection.java Added Files: ReturnValues.java VoidReturnValues.java ExceptionalReturnValue.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: AbstractExpectationCollection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/AbstractExpectationCollection.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- AbstractExpectationCollection.java 14 May 2003 14:59:34 -0000 1.6 +++ AbstractExpectationCollection.java 18 May 2003 20:59:36 -0000 1.7 @@ -23,6 +23,8 @@ } public void addActualMany(Object[] items) { + if(items == null) return; + for (int i = 0; i < items.length; ++i) { addActual(items[i]); } |
From: Tim M. <ma...@us...> - 2003-05-18 20:59:44
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/com/mockobjects/util Modified Files: AssertMo.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: AssertMo.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/AssertMo.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AssertMo.java 25 Oct 2002 22:06:09 -0000 1.2 +++ AssertMo.java 18 May 2003 20:59:40 -0000 1.3 @@ -1,5 +1,6 @@ package com.mockobjects.util; + import junit.framework.Assert; import junit.framework.AssertionFailedError; import com.mockobjects.Verifiable; |
From: Tim M. <ma...@us...> - 2003-05-18 20:59:44
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/com/mockobjects/constraint Modified Files: IsEqual.java IsLessThan.java IsGreaterThan.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: IsEqual.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint/IsEqual.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- IsEqual.java 24 Nov 2002 11:20:19 -0000 1.2 +++ IsEqual.java 18 May 2003 20:59:40 -0000 1.3 @@ -4,6 +4,9 @@ */ package com.mockobjects.constraint; +import java.util.Arrays; + +import com.mockobjects.dynamic.DynamicUtil; /** Is the value equal to another value, as tested by the * {@link java.lang.Object#equals} method? @@ -12,17 +15,26 @@ { private Object _object; - /** Creates a new instance of IsEqual - */ - public IsEqual( Object o ) { - _object = o; + public IsEqual( Object equalArg) { + if(equalArg instanceof Object[]) { + _object = Arrays.asList((Object[])equalArg); + } else { + _object = equalArg; + } } public boolean eval( Object arg ) { + if(arg instanceof Object[]) { + arg = Arrays.asList((Object[])arg); + } return arg.equals(_object); } - public String toString() { - return "a value equal to <" + _object + ">"; + public String toString() { + return " = " + DynamicUtil.proxyToString(_object); + } + + public boolean equals(Object anObject) { + return eval(anObject); } } Index: IsLessThan.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint/IsLessThan.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- IsLessThan.java 24 Nov 2002 11:20:19 -0000 1.2 +++ IsLessThan.java 18 May 2003 20:59:40 -0000 1.3 @@ -11,8 +11,6 @@ { private Comparable _object; - /** Creates a new instance of IsLessThan - */ public IsLessThan(Comparable o) { _object = o; } Index: IsGreaterThan.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint/IsGreaterThan.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- IsGreaterThan.java 24 Nov 2002 11:20:19 -0000 1.2 +++ IsGreaterThan.java 18 May 2003 20:59:40 -0000 1.3 @@ -5,14 +5,13 @@ package com.mockobjects.constraint; -/** Is the value greater than another {@link java.lang.Comparable} value? +/** + * Is the value greater than another {@link java.lang.Comparable} value? */ public class IsGreaterThan implements Constraint { private Comparable _object; - /** Creates a new instance of IsGreaterThan - */ public IsGreaterThan( Comparable o ) { _object = o; } |
From: Tim M. <ma...@us...> - 2003-05-18 20:59:44
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/constraint In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/test/mockobjects/constraint Modified Files: ConstraintsTest.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: ConstraintsTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/constraint/ConstraintsTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ConstraintsTest.java 12 Nov 2002 17:42:40 -0000 1.1 +++ ConstraintsTest.java 18 May 2003 20:59:40 -0000 1.2 @@ -5,14 +5,16 @@ package test.mockobjects.constraint; import com.mockobjects.constraint.*; -import com.mockobjects.constraint.*; +import com.mockobjects.dynamic.Mock; +import com.mockobjects.util.AssertMo; -import java.util.EventObject; +import test.mockobjects.dynamic.DummyInterface; +import java.util.EventObject; +import junit.framework.*; -public class ConstraintsTest - extends junit.framework.TestCase +public class ConstraintsTest extends TestCase { class True implements Constraint { public boolean eval( Object o ) { return true; } @@ -53,6 +55,38 @@ assertTrue( p.eval( new Integer(1) ) ); assertTrue( !p.eval(i2) ); } + + public void testIsEqualObjectArray() { + String[] s1 = new String[] { "a", "b" }; + String[] s2 = new String[] { "a", "b" }; + String[] s3 = new String[] { "c", "d" }; + String[] s4 = new String[] { "a", "b", "c", "d" }; + + Constraint p = new IsEqual(s1); + + assertTrue( "Should equal itself", p.eval(s1) ); + assertTrue( "Should equal a similar array", p.eval( s2 ) ); + assertTrue( "Should not equal a different array", !p.eval(s3) ); + assertTrue( "Should not equal a different sized array", !p.eval(s4) ); + } + + public void testIsEqualToStringForNestedConstraint() { + assertEquals("Should get an obvious toString to reflect nesting if viewed in a debugger", + " = = NestedConstraint", new IsEqual(new IsEqual("NestedConstraint")).toString()); + } + public void testIsEqualToStringOnProxyArgument() { + // Required for error message reporting + Mock mockDummyInterface = new Mock(DummyInterface.class, "MockName"); + Constraint p = new IsEqual(mockDummyInterface.proxy()); + + AssertMo.assertIncludes("Should get resolved toString() with no expectation error", "MockName", p.toString()); + } + + public void testIsEqualEquals() throws Exception { + assertEquals("Should be equal", new IsEqual("a"), new IsEqual("a")); + assertFalse("Should not be equal - same type different values", new IsEqual("a").equals(new IsEqual("b"))); + assertFalse("Should not be equal - different type", new IsEqual("a").equals("b")); + } public void testIsGreaterThan() { Constraint p = new IsGreaterThan( new Integer(1) ); |
From: Tim M. <ma...@us...> - 2003-05-18 20:59:43
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25351/examples/com/mockobjects/examples/dynamic Added Files: Timer.java SimpleServlet.java SimpleServletTest.java MailSender.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv25351/core/test/mockobjects/dynamic Modified Files: MockTest.java Added Files: ExpectedCallTest.java MockCallable.java CTest.java ConstraintMatcherTest.java DummyInterface.java CallBagTest.java DummyThrowable.java CallMatchTest.java MockCallableAddable.java ErrorMessageExamples.java CallSequenceTest.java StubTest.java MockCallFactory.java DynamicUtilTest.java MockConstraintMatcher.java MockConstraint.java Removed Files: CallCounterTest.java CallCollectionTest.java Log Message: Integration with branch DynamicMockExperiment. If you have problems - you can revert to Version 08. Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- MockTest.java 12 Nov 2002 17:42:38 -0000 1.8 +++ MockTest.java 18 May 2003 20:59:39 -0000 1.9 @@ -1,563 +1,412 @@ -/* Copyright (c) 2002 Nat Pryce. All rights reserved. - * - * Created on February 11, 2002, 12:34 AM +/* + * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import junit.framework.AssertionFailedError; - -import com.mockobjects.dynamic.*; import com.mockobjects.constraint.*; -import com.mockobjects.util.TestCaseMo; +import com.mockobjects.constraint.IsEqual; +import com.mockobjects.dynamic.*; +import com.mockobjects.util.*; + +import junit.framework.*; + + +public class MockTest extends TestCase { + private static final String MOCK_NAME = "Test mock"; + final String METHOD_NOARG_NAME = "noArgMethodVoid"; + final String METHOD_NOARGANDRETURN_NAME = "noArgMethod"; + final String METHOD_NOARGANDRETURN_RESULT = "resultNoArgs"; + final String METHOD_ONEARG_NAME = "oneArgMethod"; + final String METHOD_ONEARG_RESULT = "result1Args"; + final String METHOD_TWOARG_NAME = "twoArgMethod"; + final String METHOD_TWOARG_RESULT = "resultTwoArgs"; + final Throwable METHOD_EXCEPTION = new DummyThrowable("Configured test throwable"); + final Object[] METHOD_NOARG_ARGS = new Object[0]; + final String[] METHOD_ONEARG_ARGS = new String[] { "oneP1" }; + final ConstraintMatcher METHOD_ONEARG_CONSTRAINTS = C.args(C.eq("oneP1")); + final String[] METHOD_TWOARG_ARGS = new String[] { "twoP1", "twoP2" }; + final ConstraintMatcher METHOD_TWOARG_CONSTRAINTS = C.args(C.eq("twoP1"), C.eq("twoP2")); + private DummyInterface proxy; + private Mock mock; + private MockCallFactory mockCallFactory = new MockCallFactory(); + private MockCallable mockCallMatch = new MockCallable(); + private MockCallable mockExpectedCall = new MockCallable(); + private MockCallable mockReturnStub = new MockCallable(); + private MockCallable mockThrowStub = new MockCallable(); + private MockCallable mockVoidStub = new MockCallable(); + private MockCallableAddable mockCallableAddable = new MockCallableAddable(); + + public MockTest(String name) throws Exception { + super(name); + } + + public void setUp() { + mock = new Mock(mockCallFactory, mockCallableAddable, DummyInterface.class, MOCK_NAME); + + try { + proxy = (DummyInterface)mock.proxy(); + } catch (ClassCastException ex) { + fail("proxy is not of expected interface type"); + } + } + + public void testExpectManyAndVoid() throws Throwable { + mockCallFactory.setExpectedCreateVoidStubCalls(1); + mockCallFactory.setupCreateVoidStub(mockVoidStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expect(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectNoneAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expectAndReturn(METHOD_NOARGANDRETURN_NAME, METHOD_NOARGANDRETURN_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectNoneAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expectAndThrow(METHOD_NOARGANDRETURN_NAME, METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectOneAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expectAndThrow(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectNoneAndVoid() throws Throwable { + mockCallFactory.setExpectedCreateVoidStubCalls(1); + mockCallFactory.setupCreateVoidStub(mockVoidStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockVoidStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expect(METHOD_NOARG_NAME); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectOneAndVoid() throws Throwable { + mockCallFactory.setExpectedCreateVoidStubCalls(1); + mockCallFactory.setupCreateVoidStub(mockVoidStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expect(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0]); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectWithConstraint() throws Throwable { + mockCallFactory.setupCreateVoidStub(mockVoidStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expect(METHOD_ONEARG_NAME, new IsEqual(METHOD_ONEARG_ARGS[0])); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectWithConstraintArray() throws Throwable { + mockCallFactory.setupCreateVoidStub(mockVoidStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + + mock.expect(METHOD_ONEARG_NAME, new Constraint[] { new IsEqual(METHOD_ONEARG_ARGS[0])}); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + + public void testExpectManyAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); + + mock.expectAndReturn(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_TWOARG_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectManyAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); + + mock.expectAndThrow(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testExpectOneAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + mockCallFactory.addExpectedCreateExpectedCall(mockCallMatch); + mockCallFactory.setupCreateExpectedCall(mockExpectedCall); + + mockCallableAddable.addExpectedAddExpect(mockExpectedCall); + mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); + + mock.expectAndReturn(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_ONEARG_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchManyAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); + + mock.matchAndReturn(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_TWOARG_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchNoneAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_NOARGANDRETURN_RESULT); + + mock.matchAndThrow(METHOD_NOARG_NAME, METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchOneAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); + + mock.matchAndThrow(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchManyAndThrow() throws Throwable { + mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION); + mockCallFactory.setupCreateThrowStub(mockThrowStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_TWOARG_RESULT); + + mock.matchAndThrow(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_EXCEPTION); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchOneAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); + + mock.matchAndReturn(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_ONEARG_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMatchNoneAndReturn() throws Throwable { + mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT); + mockCallFactory.setupCreateReturnStub(mockReturnStub); + mockCallFactory.addExpectedCreateCallMatch(METHOD_NOARG_NAME, C.NO_ARGS, mockReturnStub); + mockCallFactory.setupCreateCallMatch(mockCallMatch); + + mockCallableAddable.addExpectedAddMatch(mockCallMatch); + mockCallableAddable.setupCall(METHOD_ONEARG_RESULT); + + mock.matchAndReturn(METHOD_NOARG_NAME, METHOD_NOARGANDRETURN_RESULT); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMockAnnotatesAssertionFailedErrors() + throws Throwable { + final String originalMessage = "original message"; + + mockCallableAddable.setupCall(new AssertionFailedError(originalMessage)); + + try { + proxy.noArgMethodVoid(); + } catch (AssertionFailedError err) { + AssertMo.assertIncludes("should contain original message", originalMessage, err.getMessage()); + AssertMo.assertIncludes("should contain mock name", MOCK_NAME, err.getMessage()); + } + } + + public void testMockNameFromClass() throws Exception { + assertEquals("mockString", Mock.mockNameFromClass(String.class)); + } + + public void testMockProxyReturnsConfiguredResult() + throws Throwable { + final String result = "configured result"; + + mockCallableAddable.setupCall(result); + + assertSame("result is returned by mock", result, proxy.oneArgMethod(METHOD_TWOARG_ARGS[0])); + } + + public void testMockProxySendsAllArgument() throws Throwable { + mockCallableAddable.addExpectedCall(mock, METHOD_TWOARG_NAME, METHOD_TWOARG_ARGS); + mockCallableAddable.setupCall("result ignored"); + + proxy.twoArgMethod(METHOD_TWOARG_ARGS[0], METHOD_TWOARG_ARGS[1]); + + mockCallableAddable.verifyExpectations(); + } + + public void testMockProxySendsEmptyArrayWhenNoArguments() + throws Exception { + mockCallableAddable.addExpectedCall(mock, METHOD_NOARG_NAME, METHOD_NOARG_ARGS); + mockCallableAddable.setupCall("result ignored"); + + proxy.noArgMethodVoid(); + + Verifier.verifyObject(this); + mockCallableAddable.verifyExpectations(); + } + + public void testMockProxyThrowsConfiguredExceptions() + throws Throwable { + final Throwable throwable = new DummyThrowable(); + + mockCallableAddable.setupCall(new ThrowStub(throwable)); + + try { + proxy.noArgMethodVoid(); + } catch (Throwable ex) { + assertSame("exception is caught by mock", throwable, ex); + } + } + + public void testMockToStringContainsName() { + AssertMo.assertIncludes("result of toString() should include name", MOCK_NAME, mock.toString()); + } + + public void testMockVerifies() throws Exception { + mockCallableAddable.setExpectedVerifyCalls(1); + mock.verify(); -public class MockTest - extends TestCaseMo -{ - public static interface ExampleInterface { - Object noArgs(); - void voidMethod(); - 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(); - } - - private Mock _mock; - private ExampleInterface _interface; - - - public MockTest( String test ) { - super(test); - } - - public void setUp() { - _mock = new Mock( ExampleInterface.class ); - _interface = (ExampleInterface)_mock.proxy(); - } - - public void tearDown() { - _mock = null; - _interface = null; - } - - public void testNewMockVerifies() { - _mock.verify(); - } - - public void testMockCallToNoArgMethod() { - Object result = new Object(); - - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result ); - - assertSame( result, _interface.noArgs() ); - - _mock.verify(); - } - - public void testMockCallToVoidMethod() { - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - _interface.voidMethod(); - - _mock.verify(); - } - - public void testObjectTypes() { - _mock.expectAndReturn( "objectTypes", - new Constraint[] { new IsEqual( new Integer(1) ) }, - "1" ); - - assertEquals( "1", _interface.objectTypes( new Integer(1) ) ); - _mock.verify(); - } - - public void testPrimitiveTypes() { - _mock.expectAndReturn( "primitiveTypes", - new Constraint[] { new IsEqual( new Integer(2) ), - new IsEqual( new Integer(3) ) }, - new Integer(6) ); - - assertEquals( 6, _interface.primitiveTypes(2,3) ); - _mock.verify(); - } - - public void testLaterExpectationsOverrideEarlierExpectations() { - Object result1 = new Object(); - Object result2 = new Object(); - - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result1 ); - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result2 ); - - assertSame( result2, _interface.noArgs() ); - assertSame( result2, _interface.noArgs() ); - _mock.verify(); - } - - public void testThrow() { - _mock.expectAndThrow( "primitiveTypes", - new Constraint[] { new IsEqual( new Integer(2) ), - new IsEqual( new Integer(3) ) }, - new ArithmeticException("message") ); - - try { - _interface.primitiveTypes( 2, 3 ); - fail( "expected ArithmeticException to be thrown" ); - } - catch( ArithmeticException ex ) { - assertEquals( "message", ex.getMessage() ); - } - - _mock.verify(); - } - - public void testExpectCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.expect( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - assertSame( "wrong buffer", buf, args[0] ); - buf.append("hello"); - return Mock.VOID; - } - } ); - - _interface.sideEffect( buf ); - assertEquals( "hello", buf.toString() ); - _mock.verify(); - } - - public void testFailingExpectationInCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.expect( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - assertSame( "wrong buffer", buf, args[0] ); - buf.append("hello"); - return Mock.VOID; - } - } ); - - assertFails( "should fail with wrong buffer", new Runnable() { - public void run() { - _interface.sideEffect( new StringBuffer() ); - } - } ); - } - - public void testNotAllMethodsCalled() { - _mock.expectVoid("noArgs", Mock.NO_ARGS); - - assertFails("verify did not fail when not all of the expected methods were called", - new Runnable() { - public void run() { _mock.verify(); } - }); - } - - public void testCalledExpectationClearedBySubsequentExpecation() { - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - _interface.voidMethod(); - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - assertFails("verify did not fail when not all of the expected methods were called", - new Runnable() { - public void run() { _mock.verify(); } - }); - } - - public void testUnexpectedMethodThrowsWhenStrict() { - _mock.setStrict(true); - - assertFails("strict mock did not fail when unexpected method was called", - new Runnable() { - public void run() { _interface.voidMethod();} - }); - } - - public void testUnexpectedMethodDoesNotThrowWhenNotStrict() { - 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 testSetupDefaultResult() { - _mock.setupDefaultResult( boolean.class, new Boolean(true) ); - _mock.setupDefaultResult( byte.class, new Byte((byte)1) ); - _mock.setupDefaultResult( char.class, new Character( '2' ) ); - _mock.setupDefaultResult( short.class, new Short( (short)3 ) ); - _mock.setupDefaultResult( int.class, new Integer(4) ); - _mock.setupDefaultResult( long.class, new Long(5L) ); - _mock.setupDefaultResult( float.class, new Float(6.0f) ); - _mock.setupDefaultResult( double.class, new Double(7.0d) ); - _mock.setupDefaultResult( String.class, "8" ); - - assertEquals( true, _interface.booleanResult() ); - assertEquals( (byte)1, _interface.byteResult() ); - assertEquals( '2', _interface.charResult() ); - assertEquals( 3, _interface.shortResult() ); - assertEquals( 4, _interface.intResult() ); - assertEquals( 5L, _interface.longResult() ); - assertEquals( 6.0f, _interface.floatResult(), 0.0f ); - assertEquals( 7.0d, _interface.doubleResult(), 0.0d ); - assertEquals( "8", _interface.stringResult() ); - } - - public void testWrongNumberOfArguments() { - Constraint p = new IsEqual( new Integer(2) ); - Constraint q = new IsAnything(); - - _mock.expectAndReturn( "objectTypes", new Constraint[]{p,q}, "2" ); - - assertFails( "mock did not fail when wrong number of arguments passed", - new Runnable() { - public void run() { _interface.objectTypes( new Integer(1) ); } - }); - } - - public void testArgumentsPassedToNoArgMethod() { - Constraint p = new IsAnything(); - - _mock.expectVoid( "voidMethod", new Constraint[]{p} ); - - try { - _interface.voidMethod(); - } - catch( AssertionFailedError ex ) { - return; - } - - fail( "mock did not fail when arguments passed to void method" ); - } - - public void testPredicateFailure() { - Constraint p = new IsEqual( new Integer(2) ); - - _mock.expectAndReturn( "objectTypes", new Constraint[]{p}, "2" ); - - assertTrue( !p.eval( new Integer(1) ) ); - try { - _interface.objectTypes( new Integer(1) ); - } - catch( AssertionFailedError ex ) { - return; - } - - 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 testExpectNotCalled() { - _mock.expectNotCalled("noArgs"); - try { - _interface.noArgs(); - } - catch( AssertionFailedError ex ) { - return; - } - - fail("AssertionFailedError expected"); - } - - public void testExpectNotCalledClearedBySubsequentExpectation() { - _mock.expectNotCalled("voidMethod"); - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - _interface.voidMethod(); - - _mock.verify(); - } - - public void testSetupResult() { - final String RESULT = "result"; - - _mock.setupResult( "objectTypes", RESULT ); - - assertEquals( RESULT, _interface.objectTypes(null) ); - assertEquals( RESULT, _interface.objectTypes(new Integer(0)) ); - - _mock.verify(); - } - - public void testSetupThrow() { - _mock.setupThrow( "objectTypes", new IllegalArgumentException() ); - - try { - _interface.objectTypes(null); - fail("expected IllegalArgumentException"); - } - catch( IllegalArgumentException ex ) { - // expected - } - - try { - _interface.objectTypes(new Integer(0)); - fail("expected IllegalArgumentException"); - } - catch( IllegalArgumentException ex ) { - // expected - } - - _mock.verify(); - } - - public void testSetupCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.setup( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - buf.append("hello"); - return Mock.VOID; - } - } ); - - _interface.sideEffect( buf ); - assertEquals( "hello", buf.toString() ); - _mock.verify(); - } - - public void testSetupResultDoesNotRequireCall() { - _mock.setupResult( "objectTypes", "result" ); - _mock.verify(); - } - - public void testSetupThrowDoesNotRequireCall() { - _mock.setupThrow( "objectTypes", new RuntimeException() ); - _mock.verify(); - } - - public void testSetupCallWithSideEffectDoesNotRequireCall() { - _mock.setup( "sideEffect", new MockVoidCall() { - public Object call( Object[] args ) throws Throwable { - throw new RuntimeException("should not be called"); - } - } ); - - _mock.verify(); - } - - 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 testErrorWhenMockedVoidMethodReturnsAValue() { - _mock.expectAndReturn( "voidMethod", Mock.NO_ARGS, Boolean.TRUE ); - - try { - _interface.voidMethod(); - } - catch( AssertionFailedError ex ) { - return; - } - fail("should have thrown AssertionFailedError when return from void method"); - } - - public void testEquals() { - assertEquals("Same interface should have same hashcode", _interface.hashCode(), _interface.hashCode()); - assertEquals("Same interface should be equal", _interface, _interface); - } - - public static interface Interface1 { - void method1(); - } - - public static interface Interface2 { - void method2(); - } - - public static interface Interface3 { - void method3(); - } - - public static interface Interface4 { - void method4(); - } - - public static interface Interface5 { - void method5(); - } - - public void testMockTwoInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - } - - public void testMockThreeInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class, - Interface3.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - } - - public void testMockFourInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class, - Interface3.class, Interface4.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - assertInstanceOf( Interface4.class, mock.proxy() ); - } - - public void testMockFiveInterfaces() { - Mock mock = new Mock( new Class[] { - Interface1.class, Interface2.class, Interface3.class, - Interface4.class, Interface5.class } ); - - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - assertInstanceOf( Interface4.class, mock.proxy() ); - assertInstanceOf( Interface5.class, mock.proxy() ); - } - - private static void assertInstanceOf( Class klass, Object o ) { - assertTrue( o + " should be an instance of " + klass, - klass.isInstance(o) ); - } - - public void testFailureWhenExpectMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.expectVoid( "CeciNestPasUneMethode", Mock.NO_ARGS ); - } - } ); - } - - public void testFailureWhenSetupMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.setupResult( "CeciNestPasUneMethode", new Object() ); - } - } ); - } - - public void testFailureWhenExpectNotCalledMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.expectNotCalled( "CeciNestPasUneMethode" ); - } - } ); - } - - public void testFailureWhenOrderMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad first method name", new Runnable() { - public void run() { - mock.order( "CeciNestPasUneMethode", "method2" ); - } - } ); - assertFails( "should fail with bad second method name", new Runnable() { - public void run() { - mock.order( "method1", "CeciNestPasUneMethode" ); - } - } ); - } - - - - public static class DerivedMock - extends Mock - { - public boolean was_called = false; - - public DerivedMock() { - super( ExampleInterface.class ); - } - - public void voidMethod() { - this.was_called = true; - } - - public String objectTypes( Integer n ) throws Throwable { - Method method = getClass().getMethod("objectTypes", new Class[] { Integer.class } ); - return (String)mockCall(method , new Object[]{n} ); - } - }; - - public void testDerivedMockClass() { - DerivedMock mock = new DerivedMock(); - ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( - getClass().getClassLoader(), - new Class[]{ ExampleInterface.class }, - mock ); - - i.voidMethod(); - - assertTrue( "mock method not called on derived class", - mock.was_called ); - mock.verify(); - } - - public void testDerivedClassPassingInvocationToMockCall() { - DerivedMock mock = new DerivedMock(); - ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( - getClass().getClassLoader(), - new Class[]{ ExampleInterface.class }, - mock ); - - mock.expectAndReturn( "objectTypes", - new Constraint[] { new IsAnything() }, - "result" ); - - assertEquals( "result", i.objectTypes( new Integer(1) ) ); - - mock.verify(); - } + mockCallableAddable.verifyExpectations(); + } + + public void testProxyEquality() throws Exception { + assertTrue("Should handle proxy equality without checking expectations", proxy.equals(proxy)); + } + + public void testProxyInEquality() throws Exception { + boolean IGNORED_RESULT = true; + CallStub ret = new ReturnStub(new Boolean(IGNORED_RESULT)); + mockCallFactory.setupCreateReturnStub(ret); + mockCallFactory.setupCreateCallMatch(new CallMatch("call",C.anyArgs(1),ret)); + mockCallableAddable.setupCall(new Boolean(false)); + + mock.matchAndReturn("call", C.anyArgs(1), IGNORED_RESULT); + assertFalse("Should handle proxy inequality by calling through", proxy.equals("not a proxy")); + + Verifier.verifyObject(this); + } + + public void testProxyToString() throws Exception { + assertEquals("Should get a mock name without touching the underlying mock", MOCK_NAME, DynamicUtil.proxyToString(proxy)); + mock.verify(); // should not fail on a proxyToString call + } } --- CallCounterTest.java DELETED --- --- CallCollectionTest.java DELETED --- |
From: Tim M. <tim...@po...> - 2003-05-18 20:13:38
|
Vincent - this example makes no sense at all to me? You probably should have showed the tests that go along with this so we could understand what you are saying. This said, I would tentatively advocate a reset - I thing you are mirroring what Joe said previously in that it can be a pain if you have used setUp to configure a mock and then later just want to do something totally different for one case. I have seen this and so would agree with this change. HOWEVER - YOU ADDED RESET WITHOUT UPDATING THE TESTS!!!! SHAME ON YOU!!!! Part of the reason for rewriting the mock library was to get better test coverage... I would like to get Steve's opinion as well, he is good at enforcing a clean API. Please can we confer on some of these things to try and prevent a massive API buildup. I also want to insist that we don't put comments in the code like you just did. Please read Martin Fowlers refactoring book - rather than have: /** * Resets all expected calls and expected matches. */ Call the method something more explicit in the first place, then we you are using the method in your code, it also won't need a comment above it either - I would suggest: resetAllMatchesAndExpectations. Then you don't need a comment! Tim > -----Original Message----- > From: moc...@li... > [mailto:moc...@li...]On Behalf Of > Vincent Massol > Sent: 17 May 2003 19:55 > To: 'Mockobjects-Java-Dev' > Subject: [MO-java-dev] Resetting dyna mocks - new reset() method > > > Hi, > > I have a requirement for introducing a reset on dyna mocks so that they > can be reused. > > Here's the use case: > > I am setting up a Mock InitialContext factory with: > > NamingManager.setInitialContextFactoryBuilder( > new InitialContextFactoryBuilder() { > public InitialContextFactory > createInitialContextFactory( > Hashtable environment) throws NamingException > { > return new InitialContextFactory() { > public Context getInitialContext(Hashtable env) > throws NamingException > { > // Return the mock context here > return context; > } > }; > } > }); > > The setInitialContextFactoryBuilder() is a static method and can only be > called *ONCE* in the lifetime of the JVM. Which means that it will be > shared between testXXX() tests. Thus the InitialContext mock (in "return > context") also has to be static. > > The second step is to define expectations on the context mock lookup() > method so that we can return mocks. By doing this we are tying the > static context mock with the other per-testXXX() mocks... > > ... which means that if we have 2 test methods setting expectations on > lookup() with the same lookup name, the second test fails.... > > I believe this is a valid use case and that in order to support this use > case, we need to introduce a reset() method in the dynamock Mock class > to rest all expectations. > > I've just committed the patch to support this reset method. If you think > we should not have a reset() method, I can always remove it. > > Thanks > -Vincent > > > > ------------------------------------------------------- > This SF.net email is sponsored by: If flattening out C++ or Java > code to make your application fit in a relational database is painful, > don't do it! Check out ObjectStore. Now part of Progress Software. > http://www.objectstore.net/sourceforge > _______________________________________________ > Mockobjects-java-dev mailing list > Moc...@li... > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003 > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.476 / Virus Database: 273 - Release Date: 24/04/2003 |
From: Tim M. <ma...@us...> - 2003-05-18 20:03:56
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv6780/examples/com/mockobjects/examples/dynamic Modified Files: Tag: DynamicMockExperiment SimpleServletTest.java Log Message: Clearer example using non default mock constructor Index: SimpleServletTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/Attic/SimpleServletTest.java,v retrieving revision 1.1.2.11 retrieving revision 1.1.2.12 diff -u -r1.1.2.11 -r1.1.2.12 --- SimpleServletTest.java 6 May 2003 00:08:50 -0000 1.1.2.11 +++ SimpleServletTest.java 18 May 2003 20:03:53 -0000 1.1.2.12 @@ -56,7 +56,7 @@ public void testDoGet() throws ServletException, IOException { Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); - Mock mockHttpServletResponse = new OrderedMock(HttpServletResponse.class, "response"); + Mock mockHttpServletResponse = new OrderedMock(HttpServletResponse.class, "Response with non-default name"); Mock mockTimer = new Mock(Timer.class); Mock mockMailSender = new Mock(MailSender.class); |