|
From: Nat P. <np...@us...> - 2002-11-06 15:54:38
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic
In directory usw-pr-cvs1:/tmp/cvs-serv31865/src/core/com/mockobjects/dynamic
Modified Files:
Mock.java MethodMap.java MethodExpectation.java
Log Message:
Fixed tests broken by MethodExpectation class
Replaced createInterface methods with interface references passed to constructor, and the proxy() method to "cast" the mock to its proxy.
Index: Mock.java
===================================================================
RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Mock.java 5 Nov 2002 16:37:54 -0000 1.10
+++ Mock.java 6 Nov 2002 15:54:35 -0000 1.11
@@ -26,16 +26,51 @@
* have no methods.
*/
public static final Predicate[] NO_ARGS = new Predicate[0];
-
- protected MethodMap methods = new MethodMap();
+
+
+ private Object _proxy;
+ private MethodMap methods = new MethodMap();
private boolean _strict = false;
-
+
private Map _default_results = new HashMap();
- /** Creates a new Mock object.
+ /** 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() {
+ public Mock( Class[] interfaces_to_mock ) {
+ _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) );
@@ -46,7 +81,15 @@
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
@@ -262,7 +305,7 @@
* {@link junit.framework.AssertionFailedError} will be thrown.
*/
public void order( String preceding_method, String subsequent_method ) {
- methods.order(preceding_method, subsequent_method);
+ methods.order( preceding_method, subsequent_method );
}
@@ -280,8 +323,7 @@
return super.equals(obj);
}
}
-
-
+
/** Called by the {@link java.lang.reflect.Proxy} to mock the behaviour of an
* invoked method and check expectations.
*/
@@ -293,26 +335,28 @@
return getMatchingMethod(method).invoke( this, args );
}
catch( NoSuchMethodException ex ) {
- return mockCall(expectation, method, args );
+ return mockCall( expectation, method, args );
}
}
-
- protected Object mockCall(Method method, Object[] 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 )
+
+ 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() );
}
-
- assertTrue("Unexpected call to " + method.getName(), ! _strict);
-
- return defaultResult( method.getReturnType() );
}
@@ -335,13 +379,9 @@
methods.verify();
}
- public Object createInterface( Class interface_class ) {
- return createInterface(interface_class, this);
- }
-
- private Object createInterface(Class interface_class, InvocationHandler handler) {
- return Proxy.newProxyInstance( interface_class.getClassLoader(),
- new Class[]{ interface_class },
- handler );
+ private Object createInterface( Class[] interface_classes ) {
+ return Proxy.newProxyInstance( getClass().getClassLoader(),
+ interface_classes,
+ this );
}
}
Index: MethodMap.java
===================================================================
RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MethodMap.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MethodMap.java 5 Nov 2002 16:04:33 -0000 1.2
+++ MethodMap.java 6 Nov 2002 15:54:35 -0000 1.3
@@ -9,7 +9,9 @@
import java.util.HashMap;
import java.util.Iterator;
-public class MethodMap implements Verifiable {
+public class MethodMap
+ implements Verifiable
+{
private HashMap map = new HashMap();
public void setupCall(String methodName, MockCall call) {
Index: MethodExpectation.java
===================================================================
RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/MethodExpectation.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MethodExpectation.java 5 Nov 2002 16:37:54 -0000 1.3
+++ MethodExpectation.java 6 Nov 2002 15:54:35 -0000 1.4
@@ -8,7 +8,9 @@
import com.mockobjects.ExpectationValue;
import com.mockobjects.Verifiable;
-public class MethodExpectation implements Verifiable {
+public class MethodExpectation
+implements Verifiable
+{
final public String name;
private ExpectationValue wasCalled;
@@ -62,7 +64,14 @@
}
private void checkResultType(Method method, Object result) {
- Assert.assertTrue("trying to return " + result + " from void method " + method.getName(),
- (result == Mock.VOID) == (method.getReturnType() == void.class));
+ if( method.getReturnType() == void.class ) {
+ Assert.assertTrue( "trying to return " + result +
+ " from void method " + method.getName(),
+ result == Mock.VOID );
+ } else {
+ Assert.assertTrue( "returning nothing from non-void method " +
+ method.getName(),
+ result != Mock.VOID );
+ }
}
}
|