From: Steve F. <sm...@us...> - 2003-09-23 23:04:37
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/support In directory sc8-pr-cvs1:/tmp/cvs-serv3211/src/core/test/mockobjects/dynamic/support Added Files: MockInvokableFactory.java MockConstraint.java MockInvokable.java MockConstraintMatcher.java MockCallable.java MockInvocationDispatcher.java Log Message: Moved mocks to support testing of dynamic mock into support package for clarity. --- NEW FILE: MockInvokableFactory.java --- /* * Copyright mockobjects.com 11-Sep-2003 * */ package test.mockobjects.dynamic.support; import com.mockobjects.ExpectationList; import com.mockobjects.ExpectationValue; import com.mockobjects.MockObject; import com.mockobjects.dynamic.InvokableFactory; import com.mockobjects.dynamic.ConstraintMatcher; import com.mockobjects.dynamic.Invokable; public class MockInvokableFactory extends MockObject implements InvokableFactory { public Invokable createCallExpectationResult; public Invokable createReturnCallableResult; public Invokable createThrowableCallableResult; public Invokable createVoidCallableResult; public ExpectationValue createCallExpectation = new ExpectationValue("createCallExpectation"); public ExpectationList parameters = new ExpectationList("parameters"); public Invokable createCallExpectation(Invokable invokable) { createCallExpectation.setActual(invokable); return createCallExpectationResult; } public void expectCreateReturnCallable( String methodName, ConstraintMatcher constraints, Object result) { parameters.addExpectedMany( new Object[] { "createReturnCallable", methodName, constraints, result }); } public Invokable createReturnCallable( String methodName, ConstraintMatcher constraints, Object result) { parameters.addActualMany( new Object[] { "createReturnCallable", methodName, constraints, result }); return createReturnCallableResult; } public void expectCreateThrowableCallable( String methodName, ConstraintMatcher constraints, Throwable throwable) { parameters.addExpectedMany( new Object[] { "createThrowableCallable", methodName, constraints, throwable }); } public Invokable createThrowableCallable( String methodName, ConstraintMatcher constraints, Throwable throwable) { parameters.addActualMany( new Object[] { "createThrowableCallable", methodName, constraints, throwable }); return createThrowableCallableResult; } public void expectCreateVoidCallable( String methodName, ConstraintMatcher constraints) { parameters.addExpectedMany( new Object[] { "createVoidCallable", methodName, constraints }); } public Invokable createVoidCallable( String methodName, ConstraintMatcher constraints) { parameters.addActualMany( new Object[] { "createVoidCallable", methodName, constraints }); return createVoidCallableResult; } } --- NEW FILE: MockConstraint.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic.support; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.constraint.*; /** * @author dev */ public class MockConstraint extends Assert implements Constraint, Verifiable { private String description; private Object expectedArg; private boolean result; private boolean wasChecked = false; public MockConstraint( String description, Object expectedArg, boolean result ) { this.description = description; this.expectedArg = expectedArg; this.result = result; } public String toString() { return description; } public boolean eval( Object arg ) { assertSame( "Should be expected argument", expectedArg, arg ); wasChecked = true; return result; } public void verify() { assertTrue( description + " should have been checked", wasChecked ); } } --- NEW FILE: MockInvokable.java --- /* * Created on 20-Aug-2003 * Copyright mockobjects.com */ package test.mockobjects.dynamic.support; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationValue; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.Invokable; import com.mockobjects.util.Verifier; public class MockInvokable implements Invokable { public boolean matchesResult; public ExpectationValue matchesInvocation = new ExpectationValue("matches.invocation"); public Object invokeResult; public ExpectationValue invokeInvocation = new ExpectationValue("invoke.invocation"); public Throwable invokeThrow; public ExpectationCounter verifyCalls = new ExpectationCounter("verify.calls"); public String getDescription() { return null; } public boolean matches(Invocation invocation) { matchesInvocation.setActual(invocation); return matchesResult; } public Object invoke(Invocation invocation) throws Throwable { invokeInvocation.setActual(invocation); if (invokeThrow != null) { throw invokeThrow; } return invokeResult; } public void verify() { verifyCalls.inc(); } public void verifyExpectations() { Verifier.verifyObject(this); } } --- NEW FILE: MockConstraintMatcher.java --- package test.mockobjects.dynamic.support; import com.mockobjects.*; import com.mockobjects.dynamic.ConstraintMatcher; public class MockConstraintMatcher implements ConstraintMatcher{ private ExpectationCounter myMatchesCalls = new ExpectationCounter("MockConstraintMatcher.matches(Object[])"); private ReturnValues myActualMatchesReturnValues = new ReturnValues("MockConstraintMatcher.matches(Object[])", true); private ExpectationList myMatchesParameter0Values = new ExpectationList("MockConstraintMatcher.matches(Object[]) java.lang.Object"); private ExpectationCounter myGetConstraintsCalls = new ExpectationCounter("MockConstraintMatcher.getConstraints()"); private ReturnValues myActualGetConstraintsReturnValues = new ReturnValues("MockConstraintMatcher.getConstraints()", true); public void setExpectedMatchesCalls(int calls){ myMatchesCalls.setExpected(calls); } public void addExpectedMatches(Object[] arg0){ myMatchesParameter0Values.addExpectedMany(arg0); } public boolean matches(Object[] arg0){ myMatchesCalls.inc(); myMatchesParameter0Values.addActualMany(arg0); Object nextReturnValue = myActualMatchesReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return ((Boolean) nextReturnValue).booleanValue(); } public void setupExceptionMatches(Throwable arg){ myActualMatchesReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupMatches(boolean arg){ myActualMatchesReturnValues.add(new Boolean(arg)); } public void setExpectedGetConstraintsCalls(int calls){ myGetConstraintsCalls.setExpected(calls); } public Object[] getConstraints(){ myGetConstraintsCalls.inc(); Object nextReturnValue = myActualGetConstraintsReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (Object[]) nextReturnValue; } public void setupExceptionGetConstraints(Throwable arg){ myActualGetConstraintsReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupGetConstraints(Object[] arg){ myActualGetConstraintsReturnValues.add(arg); } public void verify(){ myMatchesCalls.verify(); myMatchesParameter0Values.verify(); myGetConstraintsCalls.verify(); } } --- NEW FILE: MockCallable.java --- package test.mockobjects.dynamic.support; import junit.framework.AssertionFailedError; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationList; import com.mockobjects.ExpectationValue; import com.mockobjects.ReturnValue; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.Invokable; import com.mockobjects.util.Verifier; public class MockCallable implements Invokable { final public String name; public ExpectationValue callInvocation = new ExpectationValue("call") ; private ReturnValue callResult = new ReturnValue("call.return"); private Throwable callThrow = null; private ExpectationValue matchesMethodName = new ExpectationValue("matches.methodName"); private ExpectationList matchesArgs = new ExpectationList("matches.args"); public boolean matches = false; private ExpectationCounter matchesCount = new ExpectationCounter("matches.count"); private ExpectationCounter verifyCount = new ExpectationCounter("verify.count"); private AssertionFailedError verifyError = null; public MockCallable(String name) { this.name = name; } public void setupCallReturn( Object result ) { callResult.setValue(result); } public void setupCallThrow( Throwable thrown ) { callThrow = thrown; } public Object invoke(Invocation anInvocation) throws Throwable { callInvocation.setActual(anInvocation); if( callThrow != null ) { throw callThrow; } else { return callResult.getValue(); } } public void setExpectedMatches( String methodName, Object[] args ) { matchesMethodName.setExpected(methodName); matchesArgs.addExpectedMany(args); } public void setExpectedMatchesCount(int count) { matchesCount.setExpected(count); } public boolean matches(Invocation invocation) { matchesMethodName.setActual(invocation.getMethodName()); matchesArgs.addActualMany(invocation.getParameterValues().toArray()); matchesCount.inc(); return matches; } public void setExpectedVerifyCalls( int count ) { verifyCount.setExpected(count); } public void setupVerifyThrow( AssertionFailedError err ) { verifyError = err; } /** @deprecated to avoid calling verify instead of verifyExpectations */ public void verify() { verifyCount.inc(); if( verifyError != null ) throw verifyError; } /** We have to rename 'verify' because we want to mock the behaviour of the * verify method itself. */ public void verifyExpectations() { Verifier.verifyObject(this); } public String getDescription() { return name; } public String toString() { return "MockCallable " + name; } } --- NEW FILE: MockInvocationDispatcher.java --- /* * 11-Sep-2003 * Copyright mockobjects.com */ package test.mockobjects.dynamic.support; import junit.framework.AssertionFailedError; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationValue; import com.mockobjects.MockObject; import com.mockobjects.dynamic.Invocation; import com.mockobjects.dynamic.InvocationDispatcher; import com.mockobjects.dynamic.Invokable; public class MockInvocationDispatcher extends MockObject implements InvocationDispatcher { public ExpectationValue dispatchInvocation = new ExpectationValue("dispatchInvocation"); public Object dispatchResult; public Throwable dispatchThrowable; public ExpectationValue addInvokable = new ExpectationValue("addInvokable"); public ExpectationCounter clearCalls = new ExpectationCounter("clear calls"); public ExpectationCounter verifyCalls = new ExpectationCounter("verify calls"); public AssertionFailedError verifyFailure; public void add(Invokable invokable) { addInvokable.setActual(invokable); } public void clear() { clearCalls.inc(); } public Object dispatch(Invocation invocation) throws Throwable { dispatchInvocation.setActual(invocation); if (null != dispatchThrowable) { throw dispatchThrowable; } return dispatchResult; } /** * @deprecated Use verifyExpectations to verify this object */ public void verify() { verifyCalls.inc(); if (null != verifyFailure) { throw verifyFailure; } } public void verifyExpectations() { super.verify(); } } |