|
From: Nat P. <np...@us...> - 2002-10-16 13:56:45
|
Update of /cvsroot/mockobjects/nat/jmock/source/com/b13media/mock
In directory usw-pr-cvs1:/tmp/cvs-serv13215/source/com/b13media/mock
Modified Files:
ExpectedCall.java ExpectedReturn.java Mock.java
ExpectedThrow.java
Log Message:
Added facility to setup behaviour of methods with side effect.
Setup methods do not have to be called.
Expected methods clear the method-called flag.
Index: ExpectedCall.java
===================================================================
RCS file: /cvsroot/mockobjects/nat/jmock/source/com/b13media/mock/ExpectedCall.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ExpectedCall.java 16 Oct 2002 12:56:21 -0000 1.3
+++ ExpectedCall.java 16 Oct 2002 13:56:41 -0000 1.4
@@ -10,12 +10,34 @@
abstract class ExpectedCall
extends junit.framework.Assert
{
+ /** A constant that indicates that an <code>ExpectedCall</code> accepts
+ * any arguments, that is, that the call does not check the arguments
+ * passed to it.
+ */
+ public static final Predicate[] ANY_ARGS = null;
+
+
private String _name;
private Predicate[] _expected_args;
- public ExpectedCall( String name,
- Predicate[] expected_args )
- {
+
+ /** Constructs an <code>ExpectedCall</code> that accepts any arguments.
+ *
+ * @param name
+ * The name of the expected method.
+ */
+ public ExpectedCall( String name ) {
+ this( name, ANY_ARGS );
+ }
+
+ /** Constructs an <code>ExpectedCall</code> that checks its arguments.
+ *
+ * @param name
+ * The name of the expected method.
+ * @param expected_args
+ * Predicates that define the valid arguments of the method.
+ */
+ public ExpectedCall( String name, Predicate[] expected_args ) {
_name = name;
_expected_args = expected_args;
}
Index: ExpectedReturn.java
===================================================================
RCS file: /cvsroot/mockobjects/nat/jmock/source/com/b13media/mock/ExpectedReturn.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- ExpectedReturn.java 22 May 2002 10:02:49 -0000 1.1.1.1
+++ ExpectedReturn.java 16 Oct 2002 13:56:42 -0000 1.2
@@ -11,6 +11,9 @@
{
private Object _result;
+ public ExpectedReturn( String name, Object result ) {
+ this( name, ANY_ARGS, result );
+ }
public ExpectedReturn( String name,
Predicate[] expected_args,
Index: Mock.java
===================================================================
RCS file: /cvsroot/mockobjects/nat/jmock/source/com/b13media/mock/Mock.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Mock.java 16 Oct 2002 13:12:56 -0000 1.6
+++ Mock.java 16 Oct 2002 13:56:42 -0000 1.7
@@ -33,13 +33,14 @@
private String _name;
private Map _expectations = new HashMap();
- private Set _expectations_not_called = new HashSet();
+ private Set _not_called_expectations = new HashSet();
private Map _order_constraints = new HashMap();
private Set _called_methods = new HashSet();
private boolean _strict = false;
private Map _default_results = new HashMap();
+
/** Creates a named Mock object, The name will be included in the messages
* of exceptions thrown to indicate violated expectations.
*/
@@ -86,32 +87,45 @@
_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.
+ */
+ public void setup( ExpectedCall call ) {
+ _expectations.put( call.getMethodName(), call );
+ _not_called_expectations.remove( call.getMethodName() );
+ _called_methods.add( call.getMethodName() );
+ }
+
/** 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
+ * @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.
*/
- public void setupResult( String method, Object result ) {
- expectReturn( method, null, result );
+ public void setupResult( String method_name, Object result ) {
+ setup( new ExpectedReturn( method_name, null, 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
+ * @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.
*/
- public void setupThrow( String method, Throwable exception ) {
- expectThrow( method, null, exception );
+ public void setupThrow( String method_name, Throwable exception ) {
+ setup( new ExpectedThrow( method_name, null, exception ) );
}
/** Set up the value returned by methods that have the given result
@@ -136,7 +150,8 @@
*/
public void expect( ExpectedCall call ) {
_expectations.put( call.getMethodName(), call );
- _expectations_not_called.remove( call.getMethodName() );
+ _not_called_expectations.remove( call.getMethodName() );
+ _called_methods.remove( call.getMethodName() );
}
/** Expect a method call and return a result when it is called.
@@ -194,7 +209,7 @@
* The name of the method that will not be called.
*/
public void expectNotCalled( String method ) {
- _expectations_not_called.add(method);
+ _not_called_expectations.add(method);
}
/** Define an order between two calls. The method named
@@ -311,7 +326,7 @@
}
private void assertCanBeCalled( String method_name ) {
- if( _expectations_not_called.contains(method_name) ) {
+ if( _not_called_expectations.contains(method_name) ) {
fail(_name + ": unexpected call to method " + method_name );
}
}
Index: ExpectedThrow.java
===================================================================
RCS file: /cvsroot/mockobjects/nat/jmock/source/com/b13media/mock/ExpectedThrow.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- ExpectedThrow.java 22 May 2002 10:02:49 -0000 1.1.1.1
+++ ExpectedThrow.java 16 Oct 2002 13:56:42 -0000 1.2
@@ -12,6 +12,10 @@
private Throwable _exception;
+ public ExpectedThrow( String name, Throwable exception ) {
+ this( name, ANY_ARGS, exception );
+ }
+
public ExpectedThrow( String name,
Predicate[] expected_args,
Throwable exception )
|