Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv26046/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment CallableAddable.java Mock.java CallBag.java CallSequence.java Log Message: expectAndReturn has a higher precendence than matchAndReturn. This was previously unclear and could give strange results. this can be used to provide default method return values which can then be overridden in specific tests where there needs to be a precise expectation. AddableCallable interface has seperate addExpect and addMatch methods which open the door to simpler testing and less reliance on the DefaultCallFactory (this hasn't been implemented) Index: CallableAddable.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/CallableAddable.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- CallableAddable.java 14 Apr 2003 08:46:27 -0000 1.1.2.1 +++ CallableAddable.java 17 Apr 2003 16:17:27 -0000 1.1.2.2 @@ -4,5 +4,6 @@ package com.mockobjects.dynamic; public interface CallableAddable extends Callable { - public abstract void add(Callable call); + void addExpect(Callable call); + void addMatch(Callable call); } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.16.2.9 retrieving revision 1.16.2.10 diff -u -r1.16.2.9 -r1.16.2.10 --- Mock.java 16 Apr 2003 22:47:06 -0000 1.16.2.9 +++ Mock.java 17 Apr 2003 16:17:28 -0000 1.16.2.10 @@ -105,7 +105,7 @@ } public void expect(String methodName, Constraint[] args) { - callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createVoidStub()))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createVoidStub()))); } public void expectAndReturn(String methodName, Object result) { @@ -133,7 +133,7 @@ } public void expectAndReturn(String methodName, Constraint[] args, Object result) { - callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result)))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result)))); } public void expectAndReturn(String methodName, Constraint[] args, boolean result) { @@ -153,7 +153,7 @@ } public void expectAndThrow(String methodName, Constraint[] args, Throwable exception) { - callSequence.add(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(exception)))); + callSequence.addExpect(callFactory.createExpectedCall(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(exception)))); } public void matchAndReturn(String methodName, Object result) { @@ -189,7 +189,7 @@ } public void matchAndReturn(String methodName, Constraint[] args, Object result) { - callSequence.add(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result))); + callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createReturnStub(result))); } public void matchAndReturn(String methodName, Constraint[] args, boolean result) { @@ -217,7 +217,7 @@ } public void matchAndThrow(String methodName, Constraint[] args, Throwable throwable) { - callSequence.add(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(throwable))); + callSequence.addMatch(callFactory.createCallMatch(methodName, args, callFactory.createThrowStub(throwable))); } /** @deprecated @see OrderedMock Index: CallBag.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallBag.java,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- CallBag.java 16 Apr 2003 22:47:58 -0000 1.1.2.3 +++ CallBag.java 17 Apr 2003 16:17:30 -0000 1.1.2.4 @@ -10,24 +10,39 @@ public class CallBag extends CallCollection implements Callable, CallableAddable { private List expectedCalls = new ArrayList(); + private List expectedMatches = new ArrayList(); public CallBag() { } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { - for (Iterator call = expectedCalls.iterator(); call.hasNext();) { - Callable element = (Callable)call.next(); + + Callable matchingCall = findMatchingCall(methodName, args, expectedCalls); + if(matchingCall == null) { + matchingCall = findMatchingCall(methodName, args, 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.call(mock, methodName, args); + return element; } } - throw createUnexpectedCallError(methodName, args); + return null; } - public String getDescription() { + public String getDescription() { if (expectedCalls.isEmpty()) { return "no methods"; } else { @@ -36,7 +51,7 @@ buf.append("one of:\n"); for (Iterator i = expectedCalls.iterator(); i.hasNext();) { - buf.append(((Callable)i.next()).getDescription()); + buf.append(((Callable) i.next()).getDescription()); buf.append("\n"); } @@ -50,12 +65,16 @@ public void verify() { for (Iterator call = expectedCalls.iterator(); call.hasNext();) { - Callable element = (Callable)call.next(); + Callable element = (Callable) call.next(); element.verify(); } } - public void add(Callable call) { + public void addExpect(Callable call) { expectedCalls.add(call); + } + + public void addMatch(Callable call) { + expectedMatches.add(call); } } Index: CallSequence.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/CallSequence.java,v retrieving revision 1.4.2.6 retrieving revision 1.4.2.7 diff -u -r1.4.2.6 -r1.4.2.7 --- CallSequence.java 16 Apr 2003 22:47:58 -0000 1.4.2.6 +++ CallSequence.java 17 Apr 2003 16:17:30 -0000 1.4.2.7 @@ -11,6 +11,7 @@ public class CallSequence extends CallCollection implements Callable, CallableAddable { private ArrayList expectedCalls = new ArrayList(); + private CallBag matchedCalls = new CallBag(); int callIndex = 0; public CallSequence() { @@ -25,7 +26,11 @@ if (nextCall.matches(methodName, args)) return nextCall.call(mock, methodName, args); - throw createUnexpectedCallError(methodName, args); + try { + return matchedCalls.call(mock, methodName, args); + } catch (AssertionFailedError ex) { + throw createUnexpectedCallError(methodName, args); + } } public String getDescription() { @@ -51,8 +56,12 @@ throw new AssertionFailedError("matches() operation not supported in CallSequence"); } - public void add(Callable call) { + public void addExpect(Callable call) { expectedCalls.add(call); + } + + public void addMatch(Callable call) { + matchedCalls.addMatch(call); } public void verify() { |