mockobjects-0.09/ 0000755 0001750 0001750 00000000000 10117310270 015365 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/build.properties.sample 0000644 0001750 0001750 00000000730 07371354535 022106 0 ustar crafterm crafterm 0000000 0000000 # Properties related to the user's environment. This file should be provided
# for building mockobjects or the properties need to be specified on the command
# line when starting Ant with the -D switch or from a higher level build file
# Distribution directory (if none is specified here, it will default to
# 'dist'
#dist.dir = e:/tmp/mockobjects-dist
# You can also change your libraries directory. If none is specified here, it
# will default to 'lib'
#lib.dir = lib
mockobjects-0.09/build.xml 0000644 0001750 0001750 00000035143 07662777354 017253 0 ustar crafterm crafterm 0000000 0000000
This class allows a list of objects to be setup which can be used whilst.The * list is check to make sure that all the object in it are used and that none * are left over at the end of a test.
* *For ever sucessive call to nextReturnObject the next object in the list will * returned.
* *If the nextReturnObject method is called and there are no objects in the list * an assertion error will be thrown. If the verify method is called and there * are objects still in the list an assertion error will also be thrown.
*/ public class ReturnObjectList implements Verifiable { private final Vector myObjects = new Vector(); private final String myName; /** * Construct a new empty list * @param aName Label used to identify list */ public ReturnObjectList(String aName) { this.myName = aName; } /** * Add a next object to the end of the list. * @param anObjectToReturn object to be added to the list */ public void addObjectToReturn(Object anObjectToReturn){ myObjects.add(anObjectToReturn); } /** * Add a next boolean to the end of the list. * @param aBooleanToReturn boolean to be added to the list */ public void addObjectToReturn(boolean aBooleanToReturn){ myObjects.add(new Boolean(aBooleanToReturn)); } /** * Add a next integer to the end of the list. * @param anIntegerToReturn integer to be added to the list */ public void addObjectToReturn(int anIntegerToReturn){ myObjects.add(new Integer(anIntegerToReturn)); } /** * Returns the next object from the list. Each object it returned in the * order in which they where added. */ public Object nextReturnObject(){ AssertMo.assertTrue(myName + " has run out of objects.", myObjects.size() > 0); return myObjects.remove(0); } /** * Verify that there are no objects left within the list. */ public void verify() { AssertMo.assertEquals(myName + " has un-used objects.", 0, myObjects.size()); } } mockobjects-0.09/src/core/com/mockobjects/ReturnValue.java 0000644 0001750 0001750 00000006201 07635626401 025324 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects; import com.mockobjects.util.AssertMo; import com.mockobjects.util.Null; /** *The ReturnValue class allows a value to be setup which will then be returned upon a specific
* method call. If value.getValue() is called before value.setValue(value)
* the ReturnValue will raise an error warning that this value has not been set. If the required
* return value is null
the return value can be set like this
* value.setValue(null)
in this case calling value.getValue()
* will return null.
* *
The advantage of this is provide better information to the user of a mock when * interacting with third party code which may expect certain values to have been set.
* * e.g. ** private final ReturnValue value = new ReturnValue("value"); * * public void setupValue(Integer value){ * value.setValue(value); * } * * public Integer getValue(){ * return (Integer)value.getValue(); * } ** @version $Revision: 1.4 $ */ public class ReturnValue { private final String name; private Object value; /** * @param name the name used to identify the ReturnValue when an error is raised */ public ReturnValue(String name) { this.name = name; } /** * @return the value set using setValue * @exception junit.framework.AssertionFailedError throw if setValue has not been called */ public Object getValue() { AssertMo.assertNotNull("The return value \"" + name + "\" has not been set.", value); if(value instanceof Null){ return null; } return value; } /** * @param value value to be returned by getValue. null can be use to force getValue to return null. */ public void setValue(Object value) { if(value==null){ this.value = Null.NULL; }else{ this.value = value; } } /** * @param value value to be returned by getBooleanValue. Calling getValue after this method will return * a Boolean wrapper around the value. */ public void setValue(boolean value){ setValue(new Boolean(value)); } /** * @return the current value converted to a boolean */ public boolean getBooleanValue() { return ((Boolean)getValue()).booleanValue(); } /** * @return the current value converted to an int */ public int getIntValue() { return ((Number)getValue()).intValue(); } /** * @param value value to be returned by getIntValue. Calling getValue after this method will return * a Integer wrapper around the value. */ public void setValue(int value) { setValue(new Integer(value)); } /** * @param value value to be returned by getLongValue. Calling getValue after this method will return * a Long wrapper around the value. */ public void setValue(long value) { setValue(new Long(value)); } /** * @return the current value converted to an long */ public long getLongValue() { return ((Number)getValue()).longValue(); } } mockobjects-0.09/src/core/com/mockobjects/ReturnValues.java 0000644 0001750 0001750 00000002663 07661772070 025522 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects; import junit.framework.*; import java.util.*; /** * Sequence values as required by MockMaker * This is a generic class that should have been introduced to the mockobjects code stream instead of * being separately included in org.mockobjects. * It is possibly similar to a ReturnObjectList? */ public class ReturnValues { private String myName; protected Vector myContents = new Vector(); private boolean myKeepUsingLastReturnValue = false; public ReturnValues() { this("Generate me with a useful name!",true); } public ReturnValues(String name, boolean keepUsingLastReturnValue) { myName = name; myKeepUsingLastReturnValue = keepUsingLastReturnValue; } public ReturnValues(boolean keepUsingLastReturnValue) { this("Generate me with a useful name!", keepUsingLastReturnValue); } public void add(Object element){ myContents.addElement(element); } public void addAll(Collection returnValues){ myContents.addAll(returnValues); } public Object getNext() { if (myContents.isEmpty()) { throw new AssertionFailedError(getClass().getName() + "[" + myName + "] was not setup with enough values"); } return pop(); } public boolean isEmpty() { return myContents.size() == 0; } protected Object pop() { Object result = myContents.firstElement(); boolean shouldNotRemoveElement = myContents.size() == 1 && myKeepUsingLastReturnValue; if (!shouldNotRemoveElement) { myContents.removeElementAt(0); } return result; } } mockobjects-0.09/src/core/com/mockobjects/Verifiable.java 0000644 0001750 0001750 00000000640 07450657026 025123 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects; /** * A Verifiable is an object that can confirm at the end of a unit test that * the correct behvaiour has occurred. * * @see com.mockobjects.util.Verifier Verifier to check all the Verifiables in an object. */ public interface Verifiable { /** * Throw an AssertionFailedException if any expectations have not been met. */ public abstract void verify(); } mockobjects-0.09/src/core/com/mockobjects/VoidReturnValues.java 0000644 0001750 0001750 00000001237 07661772070 026340 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects; /** * Sequence of void values as required by MockMaker * This is a generic class that should have been introduced to the mockobjects code stream instead of * being separately included in org.mockobjects. * It is possibly similar to a ReturnObjectList? */ public class VoidReturnValues extends ReturnValues { public VoidReturnValues() { } public VoidReturnValues(String name, boolean keepUsingLastReturnValue) { super(name, keepUsingLastReturnValue); } public VoidReturnValues(boolean keepUsingLastReturnValue) { super(keepUsingLastReturnValue); } public Object getNext() { return myContents.isEmpty() ? null : pop(); } } mockobjects-0.09/src/core/com/mockobjects/constraint/ 0000755 0001750 0001750 00000000000 10117310266 024356 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/com/mockobjects/constraint/And.java 0000644 0001750 0001750 00000001256 07570132763 025744 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 B13media Ltd. All rights reserved. * * Created on February 10, 2002, 11:49 PM */ package com.mockobjects.constraint; /** Calculates the logical conjunction of two constraints. * Evaluation is shortcut, so that the second constraint is not * called if the first constraint returns
false
.
*/
public class And
implements Constraint
{
Constraint _p1, _p2;
public And(Constraint p1, Constraint p2) {
_p1 = p1;
_p2 = p2;
}
public boolean eval( Object o ) {
return _p1.eval(o) && _p2.eval(o);
}
public String toString() {
return "(" + _p1 + " and " + _p2 + ")";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/Constraint.java 0000644 0001750 0001750 00000000713 07564236615 027367 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:21 PM
*/
package com.mockobjects.constraint;
/** A constraint over acceptable values.
*/
public interface Constraint
{
/** Evaluates the constraint for argument o.
*
* @return
* true
if o meets the constraint,
* false
if it does not.
*/
boolean eval( Object o );
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsAnything.java 0000644 0001750 0001750 00000000652 07564236616 027323 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:33 PM
*/
package com.mockobjects.constraint;
/** A constraint that always returns true
.
*/
public class IsAnything implements Constraint
{
public IsAnything() {
}
public boolean eval(Object o) {
return true;
}
public String toString() {
return "any value";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsCloseTo.java 0000644 0001750 0001750 00000001330 07564236615 027103 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/** Is the value a number equal to a value within some range of
* acceptable error?
*/
public class IsCloseTo implements Constraint
{
private double _error;
private double _value;
public IsCloseTo( double value, double error ) {
_error = error;
_value = value;
}
public boolean eval( Object arg ) {
double arg_value = ((Number)arg).doubleValue();
return Math.abs( (arg_value - _value) ) <= _error;
}
public String toString() {
return "a numeric value within " + _error + " of " + _value;
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsEqual.java 0000644 0001750 0001750 00000001663 07661772074 026615 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
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?
*/
public class IsEqual implements Constraint
{
private Object _object;
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 " = " + DynamicUtil.proxyToString(_object);
}
public boolean equals(Object anObject) {
return eval(anObject);
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsEventFrom.java 0000644 0001750 0001750 00000002455 07570132763 027445 0 ustar crafterm crafterm 0000000 0000000 /** Created on Jun 28, 2002 by npryce
* Copyright (c) B13media Ltd.
*/
package com.mockobjects.constraint;
import java.util.EventObject;
/** Tests if the value is an event announced by a specific object.
*/
public class IsEventFrom
implements Constraint
{
private Class _event_class;
private Object _source;
/** Constructs an IsEventFrom predicate that returns true for any object
* derived from {@link java.util.EventObject} announced by
* source.
*/
public IsEventFrom( Object source ) {
this( EventObject.class, source );
}
/** Constructs an IsEventFrom predicate that returns true for any object
* derived from event_class announced by
* source.
*/
public IsEventFrom( Class event_class, Object source ) {
_event_class = event_class;
_source = source;
}
public boolean eval( Object o ) {
if( o instanceof EventObject ) {
EventObject ev = (EventObject)o;
return _event_class.isInstance(o) && ev.getSource() == _source;
} else {
return false;
}
}
public String toString() {
return "an event of type " + _event_class.getName() +
" from " + _source;
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsGreaterThan.java 0000644 0001750 0001750 00000001104 07661772074 027740 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/**
* Is the value greater than another {@link java.lang.Comparable} value?
*/
public class IsGreaterThan implements Constraint
{
private Comparable _object;
public IsGreaterThan( Comparable o ) {
_object = o;
}
public boolean eval( Object arg ) {
return _object.compareTo(arg) < 0;
}
public String toString() {
return "a value greater than <" + _object + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsInstanceOf.java 0000644 0001750 0001750 00000001364 07564236616 027574 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/** Tests whether the value is an instance of a class.
*/
public class IsInstanceOf implements Constraint
{
private Class _class;
/** Creates a new instance of IsInstanceOf
*
* @param theclass
* The predicate evaluates to true for instances of this class
* or one of its subclasses.
*/
public IsInstanceOf( Class theclass ) {
_class = theclass;
}
public boolean eval( Object arg ) {
return _class.isInstance( arg );
}
public String toString() {
return "an instance of <" + _class.getName() + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsLessThan.java 0000644 0001750 0001750 00000001062 07661772074 027260 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/** Is the value less than another {@link java.lang.Comparable} value?
*/
public class IsLessThan implements Constraint
{
private Comparable _object;
public IsLessThan(Comparable o) {
_object = o;
}
public boolean eval( Object arg ) {
return _object.compareTo(arg) > 0;
}
public String toString() {
return "a value less than <" + _object + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsNot.java 0000644 0001750 0001750 00000001007 07570132763 026270 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/** Calculates the logical negation of a constraint.
*/
public class IsNot implements Constraint
{
private Constraint _predicate;
public IsNot( Constraint p ) {
_predicate = p;
}
public boolean eval( Object arg ) {
return !_predicate.eval(arg);
}
public String toString() {
return "not " + _predicate;
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsNull.java 0000644 0001750 0001750 00000000601 07564236615 026445 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:33 PM
*/
package com.mockobjects.constraint;
/** Is the value null?
*/
public class IsNull implements Constraint
{
public IsNull() {
}
public boolean eval(Object o) {
return o == null;
}
public String toString() {
return "null";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/IsSame.java 0000644 0001750 0001750 00000001255 07570132763 026422 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 10, 2002, 11:35 PM
*/
package com.mockobjects.constraint;
/** Is the value the same object as another value?
*/
public class IsSame implements Constraint
{
private Object _object;
/** Creates a new instance of IsSame
*
* @param o
* The predicate evaluates to true only when the argument is
* this object.
*/
public IsSame(Object o) {
_object = o;
}
public boolean eval( Object arg ) {
return arg == _object;
}
public String toString() {
return "the same object as <" + _object + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/constraint/Or.java 0000644 0001750 0001750 00000001254 07570132763 025620 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 B13media Ltd. All rights reserved.
*
* Created on February 10, 2002, 11:49 PM
*/
package com.mockobjects.constraint;
/** Calculates the logical disjunction of two constraints.
* Evaluation is shortcut, so that the second constraint is not
* called if the first constraint returns true
.
*/
public class Or
implements Constraint
{
Constraint _p1, _p2;
public Or( Constraint p1, Constraint p2 ) {
_p1 = p1;
_p2 = p2;
}
public boolean eval( Object o ) {
return _p1.eval(o) || _p2.eval(o);
}
public String toString() {
return "(" + _p1 + " or " + _p2 + ")";
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/ 0000755 0001750 0001750 00000000000 10117310266 023616 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/com/mockobjects/dynamic/AnyConstraintMatcher.java 0000644 0001750 0001750 00000000411 07661772067 030602 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 20-Apr-03
*/
package com.mockobjects.dynamic;
public class AnyConstraintMatcher implements ConstraintMatcher {
public boolean matches(Object[] args) {
return true;
}
public Object[] getConstraints() {
return new String[] {"ANY"};
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/C.java 0000644 0001750 0001750 00000007236 07661772067 024700 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 14, 2002, 4:02 PM
*/
package com.mockobjects.dynamic;
import com.mockobjects.constraint.*;
/** Convenient factory functions and constants for building predicates.
*/
public abstract class C
{
public static final IsAnything IS_ANYTHING = new IsAnything();
public static final IsNull IS_NULL = new IsNull();
public static final Constraint IS_NOT_NULL = not(IS_NULL);
public static final Constraint IS_TRUE = eq(new Boolean(true));
public static final Constraint IS_FALSE = eq(new Boolean(false));
public static final Constraint IS_ZERO = eq(new Integer(0));
public static final Constraint IS_NOT_ZERO = not(IS_ZERO);
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);
}
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) );
}
public static Constraint eq( long l ) {
return new IsEqual( new Long(l) );
}
public static Constraint eq( double d ) {
return new IsEqual( new Double(d) );
}
public static Constraint gt( int n ) {
return new IsGreaterThan( new Integer(n) );
}
public static Constraint gt( long l ) {
return new IsGreaterThan( new Long(l) );
}
public static Constraint gt( double d ) {
return new IsGreaterThan( new Double(d) );
}
public static Constraint gt( char c ) {
return new IsGreaterThan( new Character(c) );
}
public static Constraint lt( int n ) {
return new IsLessThan( new Integer(n) );
}
public static Constraint lt( long l ) {
return new IsLessThan( new Long(l) );
}
public static Constraint lt( double d ) {
return new IsLessThan( new Double(d) );
}
public static Constraint lt( char c ) {
return new IsLessThan( new Character(c) );
}
public static Constraint not( Constraint p ) {
return new IsNot(p);
}
public static Constraint and( Constraint p1, Constraint p2 ) {
return new And(p1,p2);
}
public static Constraint or( Constraint p1, Constraint p2 ) {
return new Or(p1,p2);
}
public static Constraint isA( Class c ) {
return new IsInstanceOf(c);
}
/* Helper methods for succinctly constructing Constraint arrays
*/
public static ConstraintMatcher args() {
return NO_ARGS;
}
public static ConstraintMatcher args(Constraint p) {
return new FullConstraintMatcher(new Constraint[]{p});
}
public static ConstraintMatcher args(Constraint p1, Constraint p2) {
return new FullConstraintMatcher(new Constraint[]{p1, p2});
}
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);
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallBag.java 0000644 0001750 0001750 00000003662 07661772067 026002 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 04-Apr-2003
*/
package com.mockobjects.dynamic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CallBag extends CallCollection implements Callable, CallableAddable {
private List expectedCalls = new ArrayList();
private List expectedMatches = new ArrayList();
public CallBag() {
}
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;
}
}
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);
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallCollection.java 0000644 0001750 0001750 00000001142 07661772067 027373 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
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();
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallFactory.java 0000644 0001750 0001750 00000000522 07662267447 026712 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
public interface CallFactory {
Callable createReturnStub( Object result );
Callable createThrowStub( Throwable throwable );
Callable createVoidStub();
Callable createCallExpectation( Callable call );
Callable createCallSignature( String methodName, ConstraintMatcher constraints, Callable call );
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallOnceExpectation.java 0000644 0001750 0001750 00000001771 07662270504 030366 0 ustar crafterm crafterm 0000000 0000000 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() + ")";
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallSequence.java 0000644 0001750 0001750 00000004273 07661772067 027060 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
import java.util.ArrayList;
import java.util.Iterator;
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;
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);
}
}
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();
}
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallSignature.java 0000644 0001750 0001750 00000002023 07662265315 027232 0 ustar crafterm crafterm 0000000 0000000 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() + ")";
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallStub.java 0000644 0001750 0001750 00000000377 07661772067 026226 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 07-Apr-2003
*/
package com.mockobjects.dynamic;
/**
* @author dev
*/
public abstract class CallStub
implements Callable
{
public boolean matches(String methodName, Object[] args) {
return true;
}
public void verify() {
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/Callable.java 0000644 0001750 0001750 00000000412 07661772067 026202 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
import com.mockobjects.*;
public interface Callable extends Verifiable {
String getDescription();
Object call( Mock mock, String methodName, Object[] args ) throws Throwable;
boolean matches(String methodName, Object[] args);
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/CallableAddable.java 0000644 0001750 0001750 00000000360 07661772067 027441 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
public interface CallableAddable extends Callable {
void addExpect(Callable call);
void addMatch(Callable call);
/**
* Resets all expected calls and expected matches.
*/
void reset();
} mockobjects-0.09/src/core/com/mockobjects/dynamic/ConstraintMatcher.java 0000644 0001750 0001750 00000000246 07661772067 030140 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 20-Apr-03
*/
package com.mockobjects.dynamic;
public interface ConstraintMatcher {
boolean matches(Object[] args);
Object[] getConstraints();
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/DefaultCallFactory.java 0000644 0001750 0001750 00000001145 07662270504 030205 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
public class DefaultCallFactory implements CallFactory {
public Callable createReturnStub(Object result) {
return new ReturnStub(result);
}
public Callable createThrowStub( Throwable exception ) {
return new ThrowStub(exception);
}
public Callable createCallExpectation(Callable call) {
return new CallOnceExpectation(call);
}
public Callable createCallSignature(String methodName, ConstraintMatcher constraints, Callable call) {
return new CallSignature( methodName, constraints, call );
}
public Callable createVoidStub() {
return new VoidStub();
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/DynamicUtil.java 0000644 0001750 0001750 00000006522 07661772067 026735 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 16-Apr-2003
*/
package com.mockobjects.dynamic;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.List;
public class DynamicUtil {
public static Object[] asObjectArray(Object primitiveArray) {
if (primitiveArray instanceof Object[]) {
return (Object[])primitiveArray;
}
List result = new ArrayList();
if (primitiveArray instanceof boolean[]) {
boolean[] booleanArray = (boolean[]) primitiveArray;
for (int i = 0; i < booleanArray.length; i++) {
result.add(new Boolean(booleanArray[i]));
}
} else if (primitiveArray instanceof char[]) {
char[] charArray = (char[]) primitiveArray;
for (int i = 0; i < charArray.length; i++) {
result.add(new Character(charArray[i]));
}
} else if (primitiveArray instanceof byte[]) {
byte[] byteArray = (byte[]) primitiveArray;
for (int i = 0; i < byteArray.length; i++) {
result.add(new Byte(byteArray[i]));
}
} else if (primitiveArray instanceof short[]) {
short[] shortArray = (short[]) primitiveArray;
for (int i = 0; i < shortArray.length; i++) {
result.add(new Short(shortArray[i]));
}
} else if (primitiveArray instanceof int[]) {
int[] intArray = (int[]) primitiveArray;
for (int i = 0; i < intArray.length; i++) {
result.add(new Integer(intArray[i]));
}
} else if (primitiveArray instanceof long[]) {
long[] longArray = (long[]) primitiveArray;
for (int i = 0; i < longArray.length; i++) {
result.add(new Long(longArray[i]));
}
} else if (primitiveArray instanceof float[]) {
float[] floatArray = (float[]) primitiveArray;
for (int i = 0; i < floatArray.length; i++) {
result.add(new Float(floatArray[i]));
}
} else if (primitiveArray instanceof double[]) {
double[] doulbeArray = (double[]) primitiveArray;
for (int i = 0; i < doulbeArray.length; i++) {
result.add(new Float(doulbeArray[i]));
}
} else {
throw new RuntimeException("Unknown primitive data type for Object[] conversion " + primitiveArray.toString());
}
return result.toArray();
}
public static String proxyToString(Object element) {
if (Proxy.isProxyClass(element.getClass())) {
try {
Method mockNameMethod = Mock.class.getDeclaredMethod("getMockName", new Class[0]);
Object debugableResult = Proxy.getInvocationHandler(element).invoke(element, mockNameMethod, new Object[0]);
return debugableResult.toString();
} catch (Throwable e) {
return element.getClass().getName();
}
}
if (element.getClass().isArray()) {
StringBuffer buf = new StringBuffer();
buf.append("[");
join(asObjectArray(element),buf);
buf.append("]");
return buf.toString();
} else {
return element.toString();
}
}
public static String methodToString(String name, Object[] args) {
StringBuffer buf = new StringBuffer();
buf.append(name);
buf.append("(");
join(args,buf);
buf.append(")");
return buf.toString();
}
public static void join(Object[] elements, StringBuffer buf) {
for (int i = 0; i < elements.length; i++) {
if (i > 0) {
buf.append(", ");
}
Object element = elements[i];
if (element.getClass().isArray()) {
buf.append("[");
join(asObjectArray(element), buf);
buf.append("]");
} else {
buf.append("<");
buf.append(proxyToString(element));
buf.append(">");
}
}
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/FullConstraintMatcher.java 0000644 0001750 0001750 00000001610 07661772067 030757 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 20-Apr-03
*/
package com.mockobjects.dynamic;
import com.mockobjects.constraint.Constraint;
public class FullConstraintMatcher implements ConstraintMatcher {
private Constraint[] constraints;
public FullConstraintMatcher(Constraint[] constraints) {
this.constraints = constraints;
}
public FullConstraintMatcher(Constraint c1) {
this(new Constraint[] {c1});
}
public FullConstraintMatcher(Constraint c1, Constraint c2) {
this(new Constraint[] {c1, c2});
}
public FullConstraintMatcher(Constraint c1, Constraint c2, Constraint c3) {
this(new Constraint[] {c1, c2, c3});
}
public boolean matches(Object[] args) {
if( args.length != constraints.length ) return false;
for (int i = 0; i < args.length; i++) {
if( !constraints[i].eval(args[i]) ) return false;
}
return true;
}
public Object[] getConstraints() {
return constraints;
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/Mock.java 0000644 0001750 0001750 00000022256 07662630527 025401 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import junit.framework.AssertionFailedError;
import com.mockobjects.Verifiable;
import com.mockobjects.constraint.Constraint;
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 (isCheckingEqualityOnProxy(method, args)) {
return new Boolean(args[0] == this.proxy);
} else if (isMockNameGetter(method, args)) {
return this.getMockName();
} else {
return callSequence.call(this, method.getName(), (args == null ? new Object[0] : args));
}
} catch (AssertionFailedError ex) {
throw new AssertionFailedError(name + ": " + ex.getMessage());
}
}
private boolean isCheckingEqualityOnProxy(Method method, Object[] args) {
return (method.getName().equals("equals")) && (args.length == 1) && (Proxy.isProxyClass(args[0].getClass()));
}
private boolean isMockNameGetter(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.createCallExpectation(callFactory.createCallSignature(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.createCallExpectation(callFactory.createCallSignature(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.createCallExpectation(callFactory.createCallSignature(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.createCallSignature(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.createCallSignature(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) {
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/OrderedMock.java 0000644 0001750 0001750 00000000564 07661772067 026711 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 11-Apr-2003
*/
package com.mockobjects.dynamic;
/**
* @author Administrator
*/
public class OrderedMock extends Mock {
public OrderedMock(Class mockedClass) {
this(mockedClass, mockNameFromClass(mockedClass));
}
public OrderedMock(Class mockedClass, String name) {
super(new DefaultCallFactory(), new CallSequence(),mockedClass, name);
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/ReturnStub.java 0000644 0001750 0001750 00000000632 07661772067 026624 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 07-Apr-2003
*/
package com.mockobjects.dynamic;
/**
* @author dev
*/
public class ReturnStub
extends CallStub
{
private Object result;
public ReturnStub( Object result ) {
this.result = result;
}
public Object call(Mock mock, String methodName, Object[] args) throws Throwable {
return result;
}
public String getDescription() {
return "returns <" + result + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/ThrowStub.java 0000644 0001750 0001750 00000000657 07661772067 026457 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 07-Apr-2003
*/
package com.mockobjects.dynamic;
/**
* @author dev
*/
public class ThrowStub
extends CallStub
{
private Throwable throwable;
public ThrowStub( Throwable throwable ) {
this.throwable = throwable;
}
public Object call(Mock mock, String methodName, Object[] args) throws Throwable {
throw throwable;
}
public String getDescription() {
return "throws <" + throwable + ">";
}
}
mockobjects-0.09/src/core/com/mockobjects/dynamic/VoidStub.java 0000644 0001750 0001750 00000000431 07661772067 026243 0 ustar crafterm crafterm 0000000 0000000 /*
* Created on 11-Apr-2003
*/
package com.mockobjects.dynamic;
public class VoidStub extends CallStub {
public String getDescription() {
return "returns null
value.
* The {@link com.mockobjects.util.Null Null} class is used when an
* {@link com.mockobjects.Expectation Expectation} is set to expect nothing.
* * Example usage: *
* public class MockX { * private Expectation... anExpectation = new Expectation...(...); * * public MockX() { * anExpectation.setExpectNothing(); * } * * public void setAnExpectation(Object value) { * anExpectation.setExpected(value); * } * * public void setActual(Object value) { * anExpectation.setActual(value); * } * } ** The act of calling {@link com.mockobjects.Expectation#setExpectNothing() Expectation.setExpectNothing()} * tells the expectation that it should expect no values to change. Since * all {@link com.mockobjects.util.Null Null} objects are equal to themselves, * most expectations set their expected value to an instance of * {@link com.mockobjects.util.Null Null}, and at the same time, set their actual * value to another instance of {@link com.mockobjects.util.Null Null}. * This way, when {@link com.mockobjects.Verifiable#verify() verify()} checks * expectations, they will compare two {@link com.mockobjects.util.Null Null} * objects together, which is guaranteed to succeed. * @author Francois Beausoleil (fbos@users.sourceforge.net) * @version $Id: Null.java,v 1.3 2002/03/28 18:16:54 custommonkey Exp $ */ public class Null { /** * The default description for all {@link com.mockobjects.util.Null Null} * objects. * This String is equal to "
Null
".
*/
public static final String DEFAULT_DESCRIPTION = "Null";
/**
* A default {@link com.mockobjects.util.Null Null} object.
* Instead of always instantiating new {@link com.mockobjects.util.Null Null}
* objects, consider using a reference to this object instead. This way,
* the virtual machine will not be taking the time required to instantiate
* an object everytime it is required.
*/
public static final Null NULL = new Null();
/**
* The description of this {@link com.mockobjects.util.Null Null} object.
*/
final private String myDescription;
/**
* Instantiates a new {@link com.mockobjects.util.Null Null} object with
* the default description.
* @see com.mockobjects.util.Null#DEFAULT_DESCRIPTION
*/
public Null() {
this(DEFAULT_DESCRIPTION);
}
/**
* Instantiates a new {@link com.mockobjects.util.Null Null} object and
* sets it's description.
* @param description
*/
public Null(String description) {
super();
myDescription = description;
}
/**
* Determines equality between two objects.
* {@link com.mockobjects.util.Null Null} objects are only equal to
* another instance of themselves.
* @param other
*/
public boolean equals(Object other) {
return other instanceof Null;
}
/**
* Returns this {@link com.mockobjects.util.Null Null} object's hashCode.
* All {@link com.mockobjects.util.Null Null} return the same
* hashCode value.
*/
public int hashCode() {
return 0;
}
/**
* Returns a string representation of this {@link com.mockobjects.util.Null Null}
* object.
* This merely returns the string passed to the constructor initially.
*/
public String toString() {
return myDescription;
}
}
mockobjects-0.09/src/core/com/mockobjects/util/SuiteBuilder.java 0000644 0001750 0001750 00000002624 07331064400 026416 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.util;
import junit.framework.*;
import java.lang.reflect.*;
/**
* Singleton to fill in a JUnit Test composite for use in a suite method.
*/
public class SuiteBuilder {
public static TestSuite buildTest(Class allTestsClass) {
return buildTest(allTestsClass, new ErrorLogger());
}
public static TestSuite buildTest(
Class allTestsClass,
ErrorLogger logger) {
TestSuite result = new TestSuite();
Method[] methods = allTestsClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method m = methods[i];
String name = m.getName();
if (isAddMethod(m)) {
try {
Object[] args = new Object[] { result };
m.invoke(allTestsClass, args);
} catch (Exception ex) {
logger.error("Error creating Test from " + name, ex);
}
}
}
return result;
}
public static boolean isAddMethod(Method m) {
String name = m.getName();
Class[] parameters = m.getParameterTypes();
Class returnType = m.getReturnType();
return parameters.length == 1
&& name.startsWith("add")
&& returnType.equals(Void.TYPE)
&& Modifier.isPublic(m.getModifiers())
&& Modifier.isStatic(m.getModifiers());
}
}
mockobjects-0.09/src/core/com/mockobjects/util/TestCaseMo.java 0000644 0001750 0001750 00000001313 07560517670 026037 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.util;
import junit.framework.*;
import junit.awtui.TestRunner;
import com.mockobjects.Verifiable;
/**
* Provides a level of indirection from TestCase so you can accomodate
* JUnit interface changes (like the change from 2.x to 3.1)
*/
public abstract class TestCaseMo extends TestCase {
public TestCaseMo(String name) {
super(name);
}
public void assertVerifyFails(Verifiable aVerifiable) {
AssertMo.assertVerifyFails(aVerifiable);
}
public void assertFails(String message, Runnable runnable) {
AssertMo.assertFails(message, runnable);
}
public static void start(String[] testNames) {
TestRunner.main(testNames);
}
}
mockobjects-0.09/src/core/com/mockobjects/util/Verifier.java 0000644 0001750 0001750 00000006057 07545626754 025624 0 ustar crafterm crafterm 0000000 0000000 package com.mockobjects.util;
import com.mockobjects.Verifiable;
import java.lang.reflect.Field;
import java.util.Vector;
import junit.framework.Assert;
/**
* Helper class to verify all {@link com.mockobjects.Expectation Expectation}s
* of an object.
* The {@link com.mockobjects.util.Verifier Verifier} class provides two static
* methods to verify objects:
* * Example usage:
* Verifying all expectations on one object at a time:
*
* public class MockX implements Verifiable { * private Expectation... anExpectation = new Expectation...(...); * private Expectation... aSecondExpectation = new Expectation...(...); * * public void verify() { * Verifier.verifyObject(this); * } * } ** This example shows how most mocks implement * {@link com.mockobjects.Verifiable Verifiable}, i.e.: by delegation. * @see com.mockobjects.Expectation * @see com.mockobjects.Verifiable * @version $Id: Verifier.java,v 1.5 2002/09/29 16:44:28 smgf Exp $ */ public class Verifier { private static Vector myProcessingObjects = new Vector(); /** * Verifies all the fields of type Verifiable in the given object, including * those inherited from superclasses. * @param anObject The object to be verified. */ static synchronized public void verifyObject(Object anObject) { verifyFieldsForClass(anObject, anObject.getClass(), myProcessingObjects); } static private void verifyFieldsForClass(Object anObject, Class aClass, Vector alreadyProcessed) { if (alreadyProcessed.contains(anObject) || isBaseObjectClass(aClass)) { return; } verifyFieldsForClass(anObject, aClass.getSuperclass(), alreadyProcessed); try { alreadyProcessed.addElement(anObject); Field[] fields = aClass.getDeclaredFields(); for (int i = 0; i < fields.length; ++i) { verifyField(fields[i], anObject, alreadyProcessed); } } finally { alreadyProcessed.removeElement(anObject); } } static private void verifyField(Field aField, Object anObject, Vector alreadyProcessed) { try { aField.setAccessible(true); Object fieldObject = aField.get(anObject); if (isVerifiable(fieldObject) && ! alreadyProcessed.contains(fieldObject)) { ((Verifiable)fieldObject).verify(); } } catch (IllegalAccessException e) { Assert.fail("Could not access field " + aField.getName()); } } private static boolean isVerifiable(Object anObject) { return anObject instanceof Verifiable; } private static boolean isBaseObjectClass(Class aClass) { return aClass.equals(Object.class); } } mockobjects-0.09/src/core/com/mockobjects/util/package.html 0000644 0001750 0001750 00000000162 07450657026 025445 0 ustar crafterm crafterm 0000000 0000000 A collection of utilities for the MockObjects framework. mockobjects-0.09/src/core/test/ 0000755 0001750 0001750 00000000000 10117310266 020070 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/test/mockobjects/ 0000755 0001750 0001750 00000000000 10117310266 022373 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/test/mockobjects/constraint/ 0000755 0001750 0001750 00000000000 10117310266 024557 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/test/mockobjects/constraint/ConstraintsTest.java 0000644 0001750 0001750 00000015461 07661772074 030623 0 ustar crafterm crafterm 0000000 0000000 /* Copyright (c) 2002 Nat Pryce. All rights reserved. * * Created on February 10, 2002, 11:24 PM */ package test.mockobjects.constraint; import com.mockobjects.constraint.*; import com.mockobjects.dynamic.Mock; import com.mockobjects.util.AssertMo; import test.mockobjects.dynamic.DummyInterface; import java.util.EventObject; import junit.framework.*; public class ConstraintsTest extends TestCase { class True implements Constraint { public boolean eval( Object o ) { return true; } } class False implements Constraint { public boolean eval( Object o ) { return false; } } /** Creates a new instance of Test_Predicates */ public ConstraintsTest( String test ) { super(test); } public void testIsNull() { Constraint p = new IsNull(); assertTrue( p.eval(null) ); assertTrue( !p.eval(new Object()) ); } public void testIsSame() { Object o1 = new Object(); Object o2 = new Object(); Constraint p = new IsSame(o1); assertTrue( p.eval(o1) ); assertTrue( !p.eval(o2) ); } public void testIsEqual() { Integer i1 = new Integer(1); Integer i2 = new Integer(2); Constraint p = new IsEqual(i1); assertTrue( p.eval(i1) ); 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) ); assertTrue( !p.eval( new Integer(0) ) ); assertTrue( !p.eval( new Integer(1) ) ); assertTrue( p.eval( new Integer(2) ) ); } public void testIsLessThan() { Constraint p = new IsLessThan( new Integer(1) ); assertTrue( p.eval( new Integer(0) ) ); assertTrue( !p.eval( new Integer(1) ) ); assertTrue( !p.eval( new Integer(2) ) ); } public void testIsAnything() { Constraint p = new IsAnything(); assertTrue( p.eval(null) ); assertTrue( p.eval( new Object() ) ); } public void testIsInstanceOf() { Constraint p = new IsInstanceOf( Number.class ); assertTrue( p.eval( new Integer(1) ) ); assertTrue( p.eval( new Double(1.0) ) ); assertTrue( !p.eval("a string") ); assertTrue( !p.eval(null) ); } public void testIsNot() { Constraint p = new IsNot( new True() ); assertTrue( !p.eval(null) ); assertTrue( !p.eval( new Object() ) ); } public void testAnd() { Object o = new Object(); assertTrue( new And( new True(), new True() ).eval(o) ); assertTrue( !new And( new False(), new True() ).eval(o) ); assertTrue( !new And( new True(), new False() ).eval(o) ); assertTrue( !new And( new False(), new False() ).eval(o) ); } public void testOr() { Object o = new Object(); assertTrue( new Or( new True(), new True() ).eval(o) ); assertTrue( new Or( new False(), new True() ).eval(o) ); assertTrue( new Or( new True(), new False() ).eval(o) ); assertTrue( !new Or( new False(), new False() ).eval(o) ); } public void testIsEventFrom() { Object o = new Object(); EventObject ev = new EventObject(o); EventObject ev2 = new EventObject( new Object() ); Constraint p = new IsEventFrom(o); assertTrue( p.eval(ev) ); assertTrue( "p should eval to false for an event not from o", !p.eval(ev2) ); assertTrue( "p should eval to false for objects that are not events", !p.eval(o) ); } private static class DerivedEvent extends EventObject { public DerivedEvent( Object source ) { super(source); } } public void testIsEventSubtypeFrom() { Object o = new Object(); DerivedEvent good_ev = new DerivedEvent(o); DerivedEvent wrong_source = new DerivedEvent(new Object()); EventObject wrong_type = new EventObject(o); EventObject wrong_source_and_type = new EventObject(new Object()); Constraint p = new IsEventFrom( DerivedEvent.class, o ); assertTrue( p.eval(good_ev) ); assertTrue( "p should eval to false for an event not from o", !p.eval(wrong_source) ); assertTrue( "p should eval to false for an event of the wrong type", !p.eval(wrong_type) ); assertTrue( "p should eval to false for an event of the wrong type "+ "and from the wrong source", !p.eval(wrong_source_and_type) ); } public void testIsCloseTo() { Constraint p = new IsCloseTo( 1.0, 0.5 ); assertTrue( p.eval( new Double(1.0) ) ); assertTrue( p.eval( new Double(0.5) ) ); assertTrue( p.eval( new Double(1.5) ) ); assertTrue( p.eval( new Float(1.0) ) ); assertTrue( p.eval( new Integer(1) ) ); assertTrue( "number too large", !p.eval( new Double(2.0) ) ); assertTrue( "number too small", !p.eval( new Double(0.0) ) ); try { p.eval("wrong type"); fail("ClassCastException expected for wrong type of argument"); } catch( ClassCastException ex ) { // expected } } } mockobjects-0.09/src/core/test/mockobjects/AllTests.java 0000644 0001750 0001750 00000003747 07555102466 025021 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.util.SuiteBuilder; import com.mockobjects.util.TestCaseMo; import junit.framework.Test; import junit.framework.TestSuite; /** * JUnit test case for AllTests */ public class AllTests extends TestCaseMo { private static final Class THIS = AllTests.class; public AllTests(String name) { super(name); } public static void addTestAssertMo(TestSuite suite) { suite.addTest(TestAssertMo.suite()); } public static void addTestExpectationCounter(TestSuite suite) { suite.addTest(TestExpectationCounter.suite()); } public static void addTestExpectationList(TestSuite suite) { suite.addTest(TestExpectationList.suite()); } public static void addTestExpectationMap(TestSuite suite) { suite.addTestSuite(TestExpectationMap.class); } public static void addTestExpectationSegment(TestSuite suite) { suite.addTest(TestExpectationSegment.suite()); } public static void addTestExpectationSet(TestSuite suite) { suite.addTest(TestExpectationSet.suite()); } public static void addTestExpectationValue(TestSuite suite) { suite.addTest(TestExpectationValue.suite()); } public static void addTestExpectationDoubleValue(TestSuite suite) { suite.addTest(TestExpectationDoubleValue.suite()); } public static void addTestMapEntry(TestSuite suite) { suite.addTest(TestMapEntry.suite()); } public static void addTestNull(TestSuite suite) { suite.addTestSuite(TestNull.class); } public static void addTestVerifier(TestSuite suite) { suite.addTest(TestVerifier.suite()); } public static void addTestReturnObjectList(TestSuite suite) { suite.addTestSuite(TestReturnObjectList.class); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return SuiteBuilder.buildTest(THIS); } } mockobjects-0.09/src/core/test/mockobjects/AutoTestSuite.java 0000644 0001750 0001750 00000007300 07560517670 026040 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import java.io.File; import java.lang.reflect.Modifier; import java.util.StringTokenizer; import junit.framework.*; /** A TestSuite containing all test classes found on the class path in a * hierarchy that matches the class-path and package structure of the system. */ public class AutoTestSuite extends TestSuite { /** Constructs a SystemTestSuite by finding all the test classes and * building the test hierarchy. */ public AutoTestSuite() { super("System Tests"); String class_path= System.getProperty("java.class.path"); String separator= System.getProperty("path.separator"); StringTokenizer path_elements = new StringTokenizer( class_path, separator ); while( path_elements.hasMoreTokens() ) { File root = new File( path_elements.nextToken() ); if( root.isDirectory() ) { TestSuite sub_suite = new AutoTestSuite( root.toString(), root, root ); if( sub_suite.testCount() > 0 ) addTest( sub_suite ); } } } private AutoTestSuite( String name, File root, File dir ) { super( name ); File[] contents = dir.listFiles(); for( int i = 0; i < contents.length; i++ ) { File f = contents[i]; if( f.isDirectory() ) { addNonEmptySuite(new AutoTestSuite( f.getName(), root, f )); } else if( isTestClass(f) ) { try { Class test_class = fileToClass( root, f ); if( isInstantiable(test_class) ) { addNonEmptySuite(new TestSuite(test_class)); } } catch( ClassNotFoundException ex ) { System.err.println("failed to load class " + f ); ex.printStackTrace(); // Continue adding other classes to the test suite } } } } private void addNonEmptySuite(TestSuite suite) { if( suite.testCount() > 0 ) { addTest( suite ); } } private boolean isInstantiable( Class c ) { int mods = c.getModifiers(); return !Modifier.isAbstract(mods) && !Modifier.isInterface(mods) && Modifier.isPublic(mods); } /** Is `f' a class-file containing a test case? */ protected boolean isTestClass( File f ) { String name = f.getName(); return name.endsWith("Test.class") || ( name.endsWith(".class") && name.startsWith("Test") && !isFilenameOfInnerClass(name) ); } private boolean isFilenameOfInnerClass(String name) { return name.indexOf('$') >= 0; } private Class fileToClass( File root, File f ) throws ClassNotFoundException { String class_name = pathToClassName( root.toString(), f.toString() ); return Class.forName( class_name ); } private String pathToClassName( String root, String f ) { int root_len = root.length(); if( !root.endsWith("/") ) root_len++; int tail_len = f.length() - ".class".length(); return f.substring( root_len, tail_len ).replace('/','.'); } /** Constructs and returns a SystemTestSuite. */ public static TestSuite suite() { return new AutoTestSuite(); } public static void main( String[] args ) { junit.swingui.TestRunner.main( new String[]{ AutoTestSuite.class.getName() } ); } } mockobjects-0.09/src/core/test/mockobjects/TestAssertMo.java 0000644 0001750 0001750 00000012247 07557063615 025662 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.util.AssertMo; import com.mockobjects.util.TestCaseMo; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; import java.util.Vector; public class TestAssertMo extends TestCaseMo { private static final Class THIS = TestAssertMo.class; public TestAssertMo(String name) { super(name); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testAssertExcludes() { AssertMo.assertExcludes( "Should not include substring", "dog", "The quick brown fox"); } public void testAssertExcludesFails() { Throwable result = null; try { AssertMo.assertExcludes( "Should fail on exclude", "fox", "The quick brown fox"); } catch (AssertionFailedError ex) { result = ex; } assertTrue("Should get an exception", result != null); } public void testAssertIncludes() { AssertMo.assertIncludes( "Should include a substring", "fox", "The quick brown fox"); } public void testAssertIncludesFails() { Throwable result = null; try { AssertMo.assertIncludes( "Should fail if no include", "dog", "The quick brown fox"); } catch (AssertionFailedError ex) { result = ex; } assertTrue("Should get an exception", result != null); } public void testAssertStartsWith() { AssertMo.assertStartsWith( "Should start with fox", "fox", "fox quick brown"); } public void testAssertStartsWithFails() { Throwable result = null; try { AssertMo.assertStartsWith( "Should fail if it doesn't start with fox", "fox", "The quick brown fox"); } catch (AssertionFailedError ex) { result = ex; } assertTrue("Should get an exception", result != null); } public void testDifferentArrays() { Object[] anExpectedArray = new Object[] { "one", new Integer(2)}; Object[] anActualArray = new Object[] { "two", new Integer(2)}; boolean threwException = false; try { AssertMo.assertEquals( "Should be expected value", anExpectedArray, anActualArray); } catch (AssertionFailedError ignoredException) { threwException = true; } assertTrue("should have thrown assertion failure", threwException); } public void testDifferentLengthArrays() { Object[] anExpectedArray = new Object[] { "one", new Integer(2)}; Object[] anActualArray = new Object[] { "one" }; boolean threwException = false; try { AssertMo.assertEquals( "Should be expected value", anExpectedArray, anActualArray); } catch (AssertionFailedError ignoredException) { threwException = true; } assertTrue("should have thrown assertion failure", threwException); } public void testDifferentObjectArrays() { Object[] anExpectedArray = new Object[] { "one", new Integer(2)}; Object[] anActualArray = new Object[] { new Integer(2), new Vector()}; boolean threwException = false; try { AssertMo.assertEquals( "Should be expected value", anExpectedArray, anActualArray); } catch (AssertionFailedError ignoredException) { threwException = true; } assertTrue("should have thrown assertion failure", threwException); } public void testEqualArrays() { Object[] anExpectedArray = new Object[] { "one", new Integer(2)}; Object[] anActualArray = new Object[] { "one", new Integer(2)}; AssertMo.assertEquals( "Should be expected value", anExpectedArray, anActualArray); } public void testEqualEmptyArrays() { Object[] anExpectedArray = new Object[0]; Object[] anActualArray = new Object[0]; AssertMo.assertEquals( "Should be expected value", anExpectedArray, anActualArray); } public void testFailureCheckerWithFailure() { AssertMo.assertFails("Test Description", new Runnable() { public void run() { fail("Should not be propagated"); } }); } public void testFailureCheckerWithoutFailure() { final String TEST_MESSAGE = "Test Description"; try { AssertMo.assertFails(TEST_MESSAGE, new Runnable() { public void run() {} }); } catch (AssertionFailedError expected) { assertEquals(TEST_MESSAGE, expected.getMessage()); return; } fail("Should have thrown an exception"); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationCollection.java 0000644 0001750 0001750 00000011665 07555102466 030423 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import java.util.*; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.util.*; public abstract class TestExpectationCollection extends TestCaseMo { ExpectationCollection myExpectation; public TestExpectationCollection(String name) { super(name); } public void testEmpty() { myExpectation.verify(); } public void testFailImmediately() { myExpectation.addExpected("A"); myExpectation.addExpected("B"); myExpectation.addActual("A"); try { myExpectation.addActual("C"); } catch (AssertionFailedError ex) { return; } fail("Should have failed immediately"); } public void testFailImmediatelyAddingTooMany() { myExpectation.addExpected("A"); myExpectation.addActual("A"); try { myExpectation.addActual("C"); } catch (AssertionFailedError ex) { return; } fail("Should have failed immediately"); } public void testFailOnSizes() { myExpectation.addExpected("A"); myExpectation.addExpected("B"); myExpectation.addActual("A"); myExpectation.addActual("B"); try { myExpectation.addActual("C"); } catch (AssertionFailedError ex) { return; } fail("Should have failed immediately"); } public void testFailOnVerify() { myExpectation.setFailOnVerify(); myExpectation.addExpectedMany(new String[] { "A", "B" }); myExpectation.addActualMany(new String[] { "C", "A" }); assertVerifyFails(myExpectation); } public void testFlushActual() { myExpectation.addActual("a value"); myExpectation.setExpectNothing(); myExpectation.verify(); } public void testHasExpectations() { assertTrue( "Should not have any expectations", !myExpectation.hasExpectations()); myExpectation.addExpected("item"); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testHasExpectationsForAddingManyArray() { assertTrue( "Should not have any expectations", !myExpectation.hasExpectations()); myExpectation.addExpectedMany(new Object[0]); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testHasExpectationsForAddingManyVector() { assertTrue( "Should not have any expectations", !myExpectation.hasExpectations()); myExpectation.addExpectedMany(new Vector().elements()); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testHasNoExpectations() { myExpectation.addActual("a value"); assertTrue("Has no expectations", !myExpectation.hasExpectations()); } public void testManyFromEnumeration() { Vector expectedItems = new Vector(); expectedItems.addElement("A"); expectedItems.addElement("B"); Vector actualItems = (Vector) expectedItems.clone(); myExpectation.addExpectedMany(expectedItems.elements()); myExpectation.addActualMany(actualItems.elements()); myExpectation.verify(); } public void testManyFromIterator() { Vector expectedItems = new Vector(); expectedItems.addElement("A"); expectedItems.addElement("B"); Vector actualItems = (Vector) expectedItems.clone(); myExpectation.addExpectedMany(expectedItems.iterator()); myExpectation.addActualMany(actualItems.iterator()); myExpectation.verify(); } public void testMultiFailureFromEnumeration() { Vector expectedItems = new Vector(); expectedItems.addElement("A"); expectedItems.addElement("B"); Vector actualItems = new Vector(); actualItems.addElement("A"); actualItems.addElement("C"); myExpectation.addExpectedMany(expectedItems.elements()); myExpectation.setFailOnVerify(); myExpectation.addActualMany(actualItems.elements()); assertVerifyFails(myExpectation); } public void testMultiFailureFromIterator() { Vector expectedItems = new Vector(); expectedItems.addElement("A"); expectedItems.addElement("B"); Vector actualItems = new Vector(); actualItems.addElement("A"); actualItems.addElement("C"); myExpectation.addExpectedMany(expectedItems.iterator()); myExpectation.setFailOnVerify(); myExpectation.addActualMany(actualItems.iterator()); assertVerifyFails(myExpectation); } public void testMultiFailureSizes() { myExpectation.addExpectedMany(new String[] { "A", "B" }); myExpectation.setFailOnVerify(); myExpectation.addActualMany(new String[] { "A", "B", "C" }); assertVerifyFails(myExpectation); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationCounter.java 0000644 0001750 0001750 00000004653 07555102466 027746 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.util.*; /** * JUnit test case for TestExpectationCounter */ public class TestExpectationCounter extends TestCaseMo { private static final Class THIS = TestExpectationCounter.class; public TestExpectationCounter(String name) { super(name); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testExpectNothing() { ExpectationCounter e = new ExpectationCounter(""); e.setExpectNothing(); assertTrue("Has expectation", e.hasExpectations()); e.verify(); } public void testExpectNothingFailure() { ExpectationCounter e = new ExpectationCounter(""); e.setExpectNothing(); assertTrue("Has expectation", e.hasExpectations()); try { e.inc(); } catch (AssertionFailedError ex) { return; } fail("Should have failed immediately"); } public void testFailImmediately() { ExpectationCounter aCounter = new ExpectationCounter("a test counter"); aCounter.setExpected(1); aCounter.inc(); try { aCounter.inc(); } catch (AssertionFailedError ex) { return; } fail("Should have failed immediately"); } public void testFailOnVerify() { ExpectationCounter aCounter = new ExpectationCounter("a test counter"); aCounter.setExpected(1); aCounter.setFailOnVerify(); aCounter.inc(); aCounter.inc(); assertVerifyFails(aCounter); } public void testFailure() { ExpectationCounter e = new ExpectationCounter(""); e.setExpected(1); assertVerifyFails(e); } public void testFlushActual() { ExpectationCounter e = new ExpectationCounter(""); e.inc(); e.setExpected(1); e.inc(); e.verify(); } public void testHasNoExpectations() { ExpectationCounter aCounter = new ExpectationCounter("a test counter"); aCounter.inc(); assertTrue("Has no expectations", !aCounter.hasExpectations()); } public void testSuccess() { ExpectationCounter e = new ExpectationCounter(""); e.setExpected(1); e.inc(); e.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationDoubleValue.java 0000644 0001750 0001750 00000004354 07555102466 030534 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.util.*; public class TestExpectationDoubleValue extends TestCaseMo { private static final Class THIS = TestExpectationDoubleValue.class; private ExpectationDoubleValue myExpectation = new ExpectationDoubleValue("ExpectationDoubleValue for testing"); public TestExpectationDoubleValue(String name) { super(name); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testExpectNothing() { myExpectation.setExpectNothing(); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testExpectNothingFail() { myExpectation.setExpectNothing(); try { myExpectation.setActual(100.0); fail("Should fail fast"); } catch (AssertionFailedError ex) { // expected } } public void testFailOnVerify() { myExpectation.setExpected( 0.0, 0.0 ); myExpectation.setFailOnVerify(); myExpectation.setActual(1.0); assertVerifyFails(myExpectation); } public void testFlushActual() { myExpectation.setActual(10); myExpectation.setExpectNothing(); myExpectation.verify(); } public void testHasNoExpectations() { myExpectation.setActual(0.0); assertTrue( "Has no expectations", !myExpectation.hasExpectations()); } public void testFailOutsideError() { myExpectation.setExpected( 100.0, 1.0 ); try { myExpectation.setActual(102.0); fail("Should fail fast on double"); } catch (AssertionFailedError ex) { //expected } } public void testPassOnError() { myExpectation.setExpected( 100.0, 1.0 ); myExpectation.setActual(101.0); myExpectation.verify(); } public void testPassWithinError() { myExpectation.setExpected( 100.0, 1.0 ); myExpectation.setActual(100); myExpectation.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationList.java 0000644 0001750 0001750 00000001523 07570131754 027232 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.Test; import junit.framework.TestSuite; import com.mockobjects.ExpectationList; public class TestExpectationList extends TestExpectationCollection { private static final Class THIS = TestExpectationList.class; public TestExpectationList(String name) { super(name); myExpectation = new ExpectationList(name); } public void lookAtTheSuperclassForTests() { } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testSorted() { myExpectation.addExpected("A"); myExpectation.addExpected("B"); myExpectation.addActual("A"); myExpectation.addActual("B"); myExpectation.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationMap.java 0000644 0001750 0001750 00000005101 07570131754 027030 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.awtui.TestRunner; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.util.*; /** * JUnit test case for TestExpectationMap */ public class TestExpectationMap extends TestCaseMo { private static final Class THIS = TestExpectationMap.class; public TestExpectationMap(String name) { super(name); } public static void main(String[] args) { TestRunner.run(THIS); } public void testExpectMissingEntry() { ExpectationMap map = new ExpectationMap("map"); map.addExpectedMissing("key"); assertEquals("one entry", null, map.get("key")); map.verify(); } public void testExpectNullEntry() { ExpectationMap map = new ExpectationMap("map"); try { map.addExpected("key", null); assertEquals("one entry", null, map.get("key")); map.verify(); } catch (NullPointerException ex) { AssertMo.assertStartsWith( "Should be JDK 1.1.7A", "1.1", System.getProperty("java.version")); } } public void testExpectOneEntry() { ExpectationMap map = new ExpectationMap("map"); map.addExpected("key", "value"); assertEquals("one entry", "value", map.get("key")); map.verify(); } public void testExpectTwoEntries() { ExpectationMap map = new ExpectationMap("map"); map.addExpected("key", "value"); map.addExpected("key1", "value1"); assertEquals("two entries", "value", map.get("key")); assertEquals("two entries", "value1", map.get("key1")); map.verify(); } public void testFailOneEntry() { try { ExpectationMap map = new ExpectationMap("map"); map.setExpectNothing(); map.get("key"); } catch (AssertionFailedError ex) { return; } fail("should fail one entry"); } public void testFailOnVerify() { ExpectationMap map = new ExpectationMap("map"); map.setExpectNothing(); map.setFailOnVerify(); map.get("key"); try { map.verify(); } catch (AssertionFailedError ex) { return; } fail("should fail one entry"); } public void testOverwriteEntry() { ExpectationMap map = new ExpectationMap("map"); map.addExpected("key", "value"); map.addExpected("key", "value1"); assertEquals("overwrite entry", "value1", map.get("key")); map.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationSegment.java 0000644 0001750 0001750 00000004567 07570131754 027734 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; import com.mockobjects.ExpectationSegment; import com.mockobjects.util.TestCaseMo; public class TestExpectationSegment extends TestCaseMo { private static final Class THIS = TestExpectationSegment.class; private ExpectationSegment myExpectation; public TestExpectationSegment(String name) { super(name); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public void setUp() { myExpectation = new ExpectationSegment("Expectation segment"); } public static Test suite() { return new TestSuite(THIS); } public void testExpectNothing() { myExpectation.setExpectNothing(); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testExpectNothingFail() { myExpectation.setExpectNothing(); boolean hasThrownException = false; try { myExpectation.setActual("some string"); } catch (AssertionFailedError ex) { hasThrownException = true; } assertTrue("Should fail fast", hasThrownException); } public void testFailOnVerify() { myExpectation.setExpected("a segment"); myExpectation.setFailOnVerify(); myExpectation.setActual("string without stuff"); assertVerifyFails(myExpectation); } public void testFailsImmediately() { boolean hasThrownException = false; myExpectation.setExpected("inner"); try { myExpectation.setActual("String not containing segment"); } catch (AssertionFailedError expected) { hasThrownException = true; } assertTrue("Should have thrown exception", hasThrownException); } public void testFlushActual() { myExpectation.setActual("a string"); myExpectation.setExpectNothing(); myExpectation.verify(); } public void testHasNoExpectations() { myExpectation.setActual("a string"); assertTrue("Has no expectations", !myExpectation.hasExpectations()); } public void testPasses() { myExpectation.setExpected("inner"); myExpectation.setActual("String containing inner segment"); myExpectation.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationSet.java 0000644 0001750 0001750 00000004305 07660455516 027060 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.ExpectationSet; import com.mockobjects.MapEntry; import junit.framework.Test; import junit.framework.TestSuite; import java.util.ArrayList; import java.util.Vector; public class TestExpectationSet extends TestExpectationCollection { private static final Class THIS = TestExpectationSet.class; public TestExpectationSet(String name) { super(name); myExpectation = new ExpectationSet(name); } public void lookAtTheSuperclassForTests() { } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testMultiUnsorted() { myExpectation.addExpectedMany(new String[] { "A", "B" }); myExpectation.addActualMany(new String[] { "A", "B" }); myExpectation.verify(); } public void testChangingHashcode() { final Vector value = new Vector(); myExpectation.addExpected(new MapEntry("key", value)); myExpectation.addActual(new MapEntry("key", value)); value.add(getName()); myExpectation.verify(); } public void testChanginHashcodeImediateCheck() { final Vector value = new Vector(); myExpectation.addExpected(new MapEntry("key", value)); value.add(getName()); myExpectation.addActual(new MapEntry("key", value)); myExpectation.verify(); } public void testMultiUnsortedSet() { myExpectation.addExpectedMany(new String[] { "A", "B" }); myExpectation.addActualMany(new String[] { "A", "B", "A", "B" }); myExpectation.verify(); } public void testUnsorted() { myExpectation.addExpected("A"); myExpectation.addExpected("B"); myExpectation.addActual("B"); myExpectation.addActual("A"); myExpectation.verify(); } public void testUnsortedSet() { myExpectation.addExpected("A"); myExpectation.addExpected("B"); myExpectation.addActual("A"); myExpectation.addActual("B"); myExpectation.addActual("A"); myExpectation.addActual("B"); myExpectation.verify(); } } mockobjects-0.09/src/core/test/mockobjects/TestExpectationValue.java 0000644 0001750 0001750 00000011035 07555102466 027373 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.util.*; /** * JUnit test case for TestExpectationValue */ public class TestExpectationValue extends TestCaseMo { private static final Class THIS = TestExpectationValue.class; private ExpectationValue myExpectation = new ExpectationValue("ExpectationValue for testing"); public TestExpectationValue(String name) { super(name); } public static void main(String[] args) { start(new String[] { THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testBooleanFail() { myExpectation.setExpected(true); boolean testPasses = false; try { myExpectation.setActual(false); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on boolean", testPasses); } public void testBooleanPass() { myExpectation.setExpected(true); myExpectation.setActual(true); myExpectation.verify(); } public void testExpectNothing() { myExpectation.setExpectNothing(); assertTrue("Should have an expectation", myExpectation.hasExpectations()); } public void testExpectNothingFail() { myExpectation.setExpectNothing(); boolean testPasses = false; try { myExpectation.setActual("another object"); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on object", testPasses); } public void testFailOnVerify() { myExpectation.setExpected("string object"); myExpectation.setFailOnVerify(); myExpectation.setActual("another object"); assertVerifyFails(myExpectation); } public void testFlushActual() { myExpectation.setActual(10); myExpectation.setExpectNothing(); myExpectation.verify(); } public void testHasNoExpectations() { myExpectation.setActual("a value"); assertTrue("Has no expectations", !myExpectation.hasExpectations()); } public void testIntFail() { myExpectation.setExpected(100); boolean testPasses = false; try { myExpectation.setActual(150); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on int", testPasses); } public void testIntPass() { myExpectation.setExpected(100); myExpectation.setActual(100); myExpectation.verify(); } public void testLongFail() { myExpectation.setExpected(100L); boolean testPasses = false; try { myExpectation.setActual(150L); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on long", testPasses); } public void testLongPass() { myExpectation.setExpected(100L); myExpectation.setActual(100L); myExpectation.verify(); } public void testDoubleFail() { myExpectation.setExpected(100.0); boolean testPasses = false; try { myExpectation.setActual(150.0); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on double", testPasses); } public void testDoublePass() { myExpectation.setExpected(100.0); myExpectation.setActual(100.0); myExpectation.verify(); } public void testNullFail() { myExpectation.setExpected(null); boolean testPasses = false; try { myExpectation.setActual("another object"); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on object", testPasses); } public void testNullPass() { myExpectation.setExpected(null); myExpectation.setActual(null); myExpectation.verify(); } public void testObject() { myExpectation.setExpected("string object"); myExpectation.setActual("string object"); myExpectation.verify(); } public void testObjectFail() { myExpectation.setExpected("string object"); boolean testPasses = false; try { myExpectation.setActual("another object"); } catch (AssertionFailedError ex) { testPasses = true; } assertTrue("Should fail fast on object", testPasses); } } mockobjects-0.09/src/core/test/mockobjects/TestMapEntry.java 0000644 0001750 0001750 00000004051 07555102466 025652 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.MapEntry; import com.mockobjects.util.TestCaseMo; import junit.framework.Test; import junit.framework.TestSuite; /** * JUnit test case for TestMapEntry */ public class TestMapEntry extends TestCaseMo { public TestMapEntry(String name) { super(name); } public static void main(String[] args) { start(new String[]{TestMapEntry.class.getName()}); } public static Test suite() { return new TestSuite(TestMapEntry.class); } public void testEquals() { assertEquals( "Should be expected value", new MapEntry("A", "2"), new MapEntry("A", "2")); assertTrue( "Should not be equal", !new MapEntry("A", "2").equals(new MapEntry("A", "1"))); assertTrue( "Should not be equal", !new MapEntry("A", "2").equals(new MapEntry("B", "2"))); assertEquals( "Should be equal with null value", new MapEntry("A", null), new MapEntry("A", null)); assertEquals( "Should be equal with null key", new MapEntry(null, "A"), new MapEntry(null, "A")); assertEquals( "Should be equal byte arrays", new MapEntry("A", "A".getBytes()), new MapEntry("A", "A".getBytes())); assertTrue( "Should not be equal byte arrays", !new MapEntry("A", "AB".getBytes()).equals(new MapEntry("A", "A".getBytes()))); assertTrue( "Should not be equal byte arrays", !new MapEntry("A", "A".getBytes()).equals(new MapEntry("A", "AB".getBytes()))); assertTrue( "Should not be equal byte arrays", !new MapEntry("A", null).equals(new MapEntry("A", "AB".getBytes()))); } public void testHashCode() { assertEquals( "Should be equal hashcodes", new MapEntry("A", "A".getBytes()).hashCode(), new MapEntry("A", "A".getBytes()).hashCode()); } } mockobjects-0.09/src/core/test/mockobjects/TestNull.java 0000644 0001750 0001750 00000002413 07570131754 025024 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.Test; import junit.framework.TestSuite; import com.mockobjects.util.Null; import com.mockobjects.util.TestCaseMo; /** * JUnit test case for TestMapEntry */ public class TestNull extends TestCaseMo { public TestNull(String name) { super(name); } public static void main(String[] args) { start(new String[] { TestNull.class.getName()}); } public static Test suite() { return new TestSuite(TestNull.class); } public void testEquals() { assertEquals("Should be same value", new Null(), new Null()); assertEquals("Should be same hashCode", new Null().hashCode(), new Null().hashCode()); assertEquals("Should be same value", new Null("one"), new Null("two")); assertEquals("Should be same hashCode", new Null("one").hashCode(), new Null("two").hashCode()); // Compare with other objects to assert that they are not equal assertEquals("Not equal to something else", false, new Null("one").equals("one")); assertEquals("Not equal to something else", false, new Null().equals(new Integer(2))); } public void testDescription() { assertEquals("Description", "what it is", new Null("what it is").toString()); } } mockobjects-0.09/src/core/test/mockobjects/TestReturnObjectBag.java 0000644 0001750 0001750 00000005460 07651477217 027146 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.util.TestCaseMo; import com.mockobjects.ReturnObjectBag; import junit.framework.AssertionFailedError; public class TestReturnObjectBag extends TestCaseMo { private final ReturnObjectBag bag = new ReturnObjectBag(getName()); private static final String KEY1 = "key1"; private static final String KEY2 = "key2"; private static final short SHORT_KEY1 = 1; private static final short SHORT_KEY2 = 2; private static final String VALUE_ONE = "one"; private static final String VALUE_TWO = "two"; public TestReturnObjectBag(String name) { super(name); } public static void main(String[] args) { start(new String[] { TestReturnObjectBag.class.getName()}); } public void testLeftoverObjectFails() { bag.putObjectToReturn(KEY1, VALUE_ONE); assertVerifyFails(bag); } public void testEmptyList() { bag.verify(); } public void testReturnSucceeds() { bag.putObjectToReturn(KEY1, VALUE_ONE); bag.putObjectToReturn(KEY2, VALUE_TWO); assertEquals("Should be first result", VALUE_ONE, bag.getNextReturnObject(KEY1)); assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(KEY2)); bag.verify(); } public void testReturnInt() { bag.putObjectToReturn(KEY1, 1); assertEquals("Should be 1", 1, bag.getNextReturnInt(KEY1)); bag.verify(); } public void testReturnBoolean() { bag.putObjectToReturn(KEY1, true); assertEquals("Should be true", true, bag.getNextReturnBoolean(KEY1)); bag.verify(); } public void testShortKey(){ bag.putObjectToReturn(SHORT_KEY1, VALUE_ONE); bag.putObjectToReturn(SHORT_KEY2, VALUE_TWO); assertEquals("Should be first result", VALUE_ONE, bag.getNextReturnObject(SHORT_KEY1)); assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(SHORT_KEY2)); bag.verify(); } public void testNoListForKey(){ try { bag.getNextReturnObject(KEY1); fail("AssertionFiledError not thrown"); } catch (AssertionFailedError e) { assertEquals(getName() + " does not contain key1", e.getMessage()); } } public void testNullKey(){ bag.putObjectToReturn(null, VALUE_ONE); assertEquals(VALUE_ONE, bag.getNextReturnObject(null)); } public void testTooManyReturns() { bag.putObjectToReturn(KEY1, VALUE_ONE); bag.getNextReturnObject(KEY1); try { bag.getNextReturnObject(KEY1); fail("AssertionFiledError not thrown"); } catch (AssertionFailedError e) { assertEquals(getName() + ".key1 has run out of objects.", e.getMessage()); } } } mockobjects-0.09/src/core/test/mockobjects/TestReturnObjectList.java 0000644 0001750 0001750 00000002274 07570131754 027361 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import junit.framework.AssertionFailedError; import com.mockobjects.ReturnObjectList; import com.mockobjects.util.TestCaseMo; public class TestReturnObjectList extends TestCaseMo { private ReturnObjectList list = new ReturnObjectList("test"); public TestReturnObjectList(String name) { super(name); } public static void main(String[] args) { start(new String[] { TestReturnObjectList.class.getName()}); } public void testLeftoverObjectFails() { list.addObjectToReturn("one"); assertVerifyFails(list); } public void testEmptyList() { list.verify(); } public void testReturnSucceeds() { list.addObjectToReturn("one"); list.addObjectToReturn("two"); assertEquals("Should be first result", "one", list.nextReturnObject()); assertEquals("Should be second result", "two", list.nextReturnObject()); list.verify(); } public void testTooManyReturns() { try{ list.nextReturnObject(); fail("Error should have been raised"); } catch(AssertionFailedError expected){ } } } mockobjects-0.09/src/core/test/mockobjects/TestReturnValue.java 0000644 0001750 0001750 00000002560 07635626402 026372 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.util.TestCaseMo; import com.mockobjects.ReturnValue; import junit.framework.AssertionFailedError; public class TestReturnValue extends TestCaseMo { private final ReturnValue value = new ReturnValue(getName()); public void testGetNull(){ value.setValue(null); assertTrue(value.getValue()==null); } public void testGetValue(){ value.setValue(this); assertEquals(this, value.getValue()); } public void testGetBooleanValue(){ value.setValue(true); assertTrue(value.getBooleanValue()); } public void testIntValue(){ value.setValue(13); assertEquals(13, value.getIntValue()); } public void testLongValue(){ long now = System.currentTimeMillis(); value.setValue(now); assertEquals(now, value.getLongValue()); value.getIntValue(); } public void testValueNotSet() { try { value.getValue(); fail("Error not thrown"); } catch (AssertionFailedError e) { assertEquals("The return value \"" + getName() + "\" has not been set.", e.getMessage()); } } public TestReturnValue(String name) { super(name); } public static void main(String[] args) { start(new String[] { TestReturnValue.class.getName()}); } } mockobjects-0.09/src/core/test/mockobjects/TestVerifier.java 0000644 0001750 0001750 00000006232 07555102466 025671 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects; import com.mockobjects.ExpectationValue; import com.mockobjects.MockObject; import com.mockobjects.util.TestCaseMo; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; public class TestVerifier extends TestCaseMo { private static final Class THIS = TestVerifier.class; class OneVerifiable extends MockObject { private ExpectationValue myValue = new ExpectationValue("should fail"); private int unusedField; public OneVerifiable() { myValue.setFailOnVerify(); myValue.setExpected("good"); } public void setValue(String aValue) { myValue.setActual(aValue); } } class InheritVerifiable extends OneVerifiable { } class LoopingVerifiable extends MockObject { LoopingVerifiable myRef = this; boolean inVerify = false; LoopingVerifiable() { super(); } public void setRef(LoopingVerifiable aRef) { myRef = aRef; } public void verify() { assertTrue("Looping verification detected", !inVerify); inVerify = true; super.verify(); inVerify = false; } } public TestVerifier(String name) { super(name); } public static void main(String[] args) { start(new String[]{THIS.getName()}); } public static Test suite() { return new TestSuite(THIS); } public void testInheritVerifiableFails() { InheritVerifiable inheritVerifiable = new InheritVerifiable(); inheritVerifiable.setValue("bad"); boolean hasThrownException = false; try { inheritVerifiable.verify(); } catch (AssertionFailedError ex) { hasThrownException = true; } assertTrue("Should have thrown exception", hasThrownException); } public void testInheritVerifiablePasses() { InheritVerifiable inheritVerifiable = new InheritVerifiable(); inheritVerifiable.setValue("good"); inheritVerifiable.verify(); } public void testNoVerifiables() { class NoVerifiables extends MockObject { } new NoVerifiables().verify(); } public void testOneVerifiableFails() { OneVerifiable oneVerifiable = new OneVerifiable(); oneVerifiable.setValue("bad"); boolean hasThrownException = false; try { oneVerifiable.verify(); } catch (AssertionFailedError ex) { hasThrownException = true; } assertTrue("Should have thrown exception", hasThrownException); } public void testOneVerifiablePasses() { OneVerifiable oneVerifiable = new OneVerifiable(); oneVerifiable.setValue("good"); oneVerifiable.verify(); } public void testNoLoopVerifySingleLevel() { new LoopingVerifiable().verify(); } public void testNoLoopVerifyMultiLevel() { LoopingVerifiable a = new LoopingVerifiable(); LoopingVerifiable b = new LoopingVerifiable(); a.setRef(b); b.setRef(a); a.verify(); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/ 0000755 0001750 0001750 00000000000 10117310266 024017 5 ustar crafterm crafterm 0000000 0000000 mockobjects-0.09/src/core/test/mockobjects/dynamic/CTest.java 0000644 0001750 0001750 00000000564 07661772071 025731 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 30-Apr-03 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.C; import junit.framework.TestCase; public class CTest extends TestCase { public CTest(String name) { super(name); } public void testEqObjectArray() { assertTrue("Should be equals for object arrays", C.eq(new String[]{"a", "b"}).eval(new String[]{"a","b"})); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/CallBagTest.java 0000644 0001750 0001750 00000013277 07662270504 027034 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class CallBagTest extends TestCase { final String METHOD_A_NAME = "methodA"; final String METHOD_A_RESULT = "resultA"; final String METHOD_B_NAME = "methodB"; final String METHOD_B_RESULT = "resultB"; final Throwable METHOD_A_EXCEPTION = new DummyThrowable("Configured test throwable"); final String[] METHOD_A_ARGS = new String[] { "a1", "a2" }; final ConstraintMatcher METHOD_A_CONSTRAINTS = C.args(C.eq("a1"), C.eq("a2")); final String[] METHOD_B_ARGS = new String[] { "b1", "b2" }; private CallBag callSet = new CallBag(); private Mock unusedMock = null; private MockCallable methodA = new MockCallable(); private MockCallable methodB = new MockCallable(); private MockCallable mockCallable = new MockCallable(); public CallBagTest(String name) { super(name); } public void testCallFailsOnEmptySet() throws Throwable { try { callSet.call(unusedMock, "missingMethod", new Object[0]); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports empty set in error message", "no methods", ex.getMessage()); return; } fail("Should fail for a missing item"); } public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); methodA.setupMatchesReturn(true); methodA.setExpectedCall(unusedMock, METHOD_A_NAME, METHOD_A_ARGS); methodA.setupCallReturn(METHOD_A_RESULT); methodB.setExpectedCallCount(0); callSet.addExpect(methodA); callSet.addExpect(methodB); assertSame("expected result from method A", METHOD_A_RESULT, callSet.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); methodA.verifyExpectations(); methodB.verifyExpectations(); } public void testExpectOverridesMatch() throws Throwable { 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(methodASignature); 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)); } public void testCallPassedToContainedElementsOtherOrder() throws Throwable { methodA.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount(0); methodB.setExpectedCall(unusedMock, METHOD_B_NAME, METHOD_B_ARGS); methodB.setupCallReturn(METHOD_B_RESULT); methodB.setExpectedMatches(METHOD_B_NAME, METHOD_B_ARGS); methodB.setupMatchesReturn(true); callSet.addExpect(methodA); callSet.addExpect(methodB); assertSame("expected result from method B", METHOD_B_RESULT, callSet.call(unusedMock, METHOD_B_NAME, METHOD_B_ARGS)); methodA.verifyExpectations(); methodB.verifyExpectations(); } public void testConfiguredResultReturned() throws Throwable { final String result = "result"; mockCallable.setupCallReturn(result); mockCallable.setupMatchesReturn(true); callSet.addExpect(mockCallable); assertSame("result is returned by mock", result, callSet.call(unusedMock, "method", new Object[0])); } public void testCallableThrowableThrown() throws Throwable { final Throwable throwable = new DummyThrowable(); mockCallable.setupMatchesReturn(true); mockCallable.setupCallThrow(throwable); callSet.addExpect(mockCallable); try { callSet.call(unusedMock, "hello", new String[0]); } catch (Throwable ex) { assertSame("exception is caught by mock", throwable, ex); } } public void testEmptySetVerifies() throws Exception { callSet.verify(); } public void testFailureIfNoElementMatches() throws Throwable { final String methodCName = "methodC"; final String[] methodCArgs = { "c1", "c2" }; methodA.setExpectedMatches(methodCName, methodCArgs); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount(0); methodA.setupGetDescription("***methodA-description****"); methodB.setExpectedCall(unusedMock, methodCName, methodCArgs); methodB.setupMatchesReturn(false); methodB.setExpectedCallCount(0); methodB.setupGetDescription("***methodB-description****"); callSet.addExpect(methodA); callSet.addExpect(methodB); try { callSet.call(unusedMock, methodCName, methodCArgs); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("method name is in error message", "methodC", ex.getMessage()); AssertMo.assertIncludes("argument is in error message (1)", methodCArgs[0], ex.getMessage()); AssertMo.assertIncludes("argument is in error message (2)", methodCArgs[1], ex.getMessage()); AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), ex.getMessage()); AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), ex.getMessage()); return; } fail("Should fail for a missing item"); } public void testVerifiesIfAllContainedElementsVerify() throws Throwable { methodA.setExpectedVerifyCalls(1); methodB.setExpectedVerifyCalls(1); callSet.addExpect(methodA); callSet.addExpect(methodB); callSet.verify(); methodA.verifyExpectations(); methodB.verifyExpectations(); } public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { methodA.setExpectedVerifyCalls(1); methodA.setupVerifyThrow(new AssertionFailedError("verify failed")); callSet.addExpect(methodA); try { callSet.verify(); } catch (AssertionFailedError ex) { methodA.verifyExpectations(); return; } fail("Should have got a failure for contained element failing"); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/CallMatchTest.java 0000644 0001750 0001750 00000006373 07662265315 027402 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import junit.framework.TestCase; import com.mockobjects.dynamic.*; public class CallMatchTest extends TestCase { private static final Object[] IGNORED_ARGS = new Object[0]; final String METHOD_NAME = "methodName"; Mock mock = new Mock(DummyInterface.class, "mock"); MockCallable mockCallable = new MockCallable(); MockConstraintMatcher mockConstraintMatcher = new MockConstraintMatcher(); CallSignature callSignature = new CallSignature(METHOD_NAME, mockConstraintMatcher, mockCallable); public CallMatchTest(String name) { super(name); } public void testCallArgumentsArePropagatedToDecorated() throws Throwable { final Object[] arguments = IGNORED_ARGS; final String result = "result"; mockCallable.setExpectedCall(mock, METHOD_NAME, arguments); mockCallable.setupCallReturn(result); callSignature.call(mock, METHOD_NAME, arguments); mockCallable.verifyExpectations(); } public void testUncalledCallVerifies() { mockCallable.setExpectedVerifyCalls(1); callSignature.verify(); mockCallable.verifyExpectations(); } public void testCallSuccessVerifies() throws Throwable { mockCallable.setExpectedVerifyCalls(1); mockCallable.setupCallReturn("result"); callSignature.call(mock, "methodName", IGNORED_ARGS); callSignature.verify(); mockCallable.verifyExpectations(); } public void testMultipleCallsSucceed() throws Throwable { mockCallable.setupCallReturn("result"); callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); callSignature.call(mock, METHOD_NAME, IGNORED_ARGS); mockCallable.verifyExpectations(); } public void testCallFailure() throws Throwable { // na } public void testCallDoesNotMatchWhenWrongName() throws Throwable { assertFalse("call does not match", callSignature.matches("anotherName", IGNORED_ARGS)); mockCallable.verifyExpectations(); } public void testMatchesAfterCalls() throws Throwable { mockCallable.setupCallReturn("result"); mockCallable.setupMatchesReturn(true); mockConstraintMatcher.setupMatches(true); 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(); } public void testMatchesDelegatesToContraintMatcher() throws Throwable { final String[] args = new String[] { "a1", "a2" }; mockConstraintMatcher.addExpectedMatches(args); mockConstraintMatcher.setupMatches(true); 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", callSignature.matches(METHOD_NAME, IGNORED_ARGS)); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/CallOnceExpectationTest.java 0000644 0001750 0001750 00000006113 07662270504 031422 0 ustar crafterm crafterm 0000000 0000000 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(); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/CallSequenceTest.java 0000644 0001750 0001750 00000016561 07662270504 030112 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 14-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.CallSignature; import com.mockobjects.dynamic.CallSequence; import com.mockobjects.dynamic.Callable; import com.mockobjects.dynamic.CallOnceExpectation; import com.mockobjects.dynamic.Mock; import com.mockobjects.dynamic.ReturnStub; import com.mockobjects.util.AssertMo; import junit.framework.AssertionFailedError; import junit.framework.TestCase; public class CallSequenceTest extends TestCase { final String METHOD_A_NAME = "methodA"; final String METHOD_A_RESULT = "resultA"; final String METHOD_B_NAME = "methodB"; final String METHOD_B_RESULT = "resultB"; final Throwable METHOD_A_EXCEPTION = new DummyThrowable("Configured test throwable"); final String[] METHOD_A_ARGS = new String[] { "a1", "a2" }; final ConstraintMatcher METHOD_A_CONSTRAINTS = C.args(C.eq("a1"), C.eq("a2")); final String[] METHOD_B_ARGS = new String[] { "b1", "b2" }; private CallSequence callSequence = new CallSequence(); private Mock unusedMock = null; private MockCallable methodA = new MockCallable(); private MockCallable methodB = new MockCallable(); private MockCallable mockCallable = new MockCallable(); public CallSequenceTest(String name) { super(name); } public void testCallableThrowableThrown() throws Throwable { final Throwable throwable = new DummyThrowable(); mockCallable.setupMatchesReturn(true); mockCallable.setupCallThrow(throwable); callSequence.addExpect(mockCallable); try { callSequence.call(unusedMock, "hello", new String[0]); } catch (Throwable ex) { assertSame("exception is caught by mock", throwable, ex); } } public void testCallFailsOnEmptyList() throws Throwable { try { callSequence.call(unusedMock, "missingMethod", new Object[0]); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports empty set in error message", "no methods", ex.getMessage()); return; } fail("Should fail for a missing item"); } public void testCallFailsWithTooManyCalls() throws Throwable { mockCallable.setupMatchesReturn(true); mockCallable.setupCallReturn(METHOD_A_RESULT); callSequence.addExpect(mockCallable); callSequence.call(unusedMock, "willdefinitelyMatch", new Object[0]); try { callSequence.call(unusedMock, "oneMethodTooMany", new Object[0]); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports one method call too many", "too many", ex.getMessage()); return; } fail("Should fail for calling too many times"); } public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); methodA.setupMatchesReturn(true); methodA.setExpectedCall(unusedMock, METHOD_A_NAME, METHOD_A_ARGS); methodA.setupCallReturn(METHOD_A_RESULT); methodB.setExpectedCallCount(0); callSequence.addExpect(methodA); callSequence.addExpect(methodB); assertSame("expected result from method A", METHOD_A_RESULT, callSequence.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); methodA.verifyExpectations(); methodB.verifyExpectations(); } public void testCallPassedToContainedElementsOtherOrderShouldFail() throws Throwable { methodA.setupMatchesReturn(false); methodA.setupGetDescription("***methodA-description****"); methodB.setupGetDescription("***methodB-description****"); callSequence.addExpect(methodA); callSequence.addExpect(methodB); try { assertSame("expected result from method B", METHOD_B_RESULT, callSequence.call(unusedMock, METHOD_B_NAME, METHOD_B_ARGS)); } catch (AssertionFailedError ex) { String message = ex.getMessage(); AssertMo.assertIncludes("Should have expected error message", "Unexpected call: methodB", message); AssertMo.assertIncludes("Should have arguments in error message (1)", METHOD_B_ARGS[0], message); AssertMo.assertIncludes("Should have arguments in error message (2)", METHOD_B_ARGS[1], message); AssertMo.assertIncludes("Should have Index pointer next to correct item", methodA.getDescription() + " <<<", message); AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), message); AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), message); AssertMo.assertTrue("Method A should be before method B", message.indexOf(methodA.getDescription()) < message.indexOf(methodB.getDescription())); return; } fail("Should fail due to wrong order"); } public void testConfiguredResultReturned() throws Throwable { final String result = "result"; mockCallable.setupCallReturn(result); mockCallable.setupMatchesReturn(true); callSequence.addExpect(mockCallable); assertSame("result is returned by mock", result, callSequence.call(unusedMock, "method", new Object[0])); } public void testEmptySetVerifies() throws Exception { callSequence.verify(); } public void testFailureIfNoElementMatches() throws Throwable { final String methodCName = "methodC"; final String[] methodCArgs = { "c1", "c2" }; methodA.setExpectedMatches(methodCName, methodCArgs); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount(0); methodA.setupGetDescription("***methodA-description****"); methodB.setExpectedCall(unusedMock, methodCName, methodCArgs); methodB.setupMatchesReturn(false); methodB.setExpectedCallCount(0); methodB.setupGetDescription("***methodB-description****"); callSequence.addExpect(methodA); callSequence.addExpect(methodB); try { callSequence.call(unusedMock, methodCName, methodCArgs); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("method name is in error message", "methodC", ex.getMessage()); AssertMo.assertIncludes("argument is in error message (1)", methodCArgs[0], ex.getMessage()); AssertMo.assertIncludes("argument is in error message (2)", methodCArgs[1], ex.getMessage()); AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), ex.getMessage()); AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), ex.getMessage()); return; } fail("Should fail for a missing item"); } public void testVerifiesIfAllContainedElementsVerify() throws Throwable { methodA.setExpectedVerifyCalls(1); methodB.setExpectedVerifyCalls(1); callSequence.addExpect(methodA); callSequence.addExpect(methodB); callSequence.verify(); methodA.verifyExpectations(); methodB.verifyExpectations(); } public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { methodA.setExpectedVerifyCalls(1); methodA.setupVerifyThrow(new AssertionFailedError("verify failed")); callSequence.addExpect(methodA); try { callSequence.verify(); } catch (AssertionFailedError ex) { methodA.verifyExpectations(); return; } fail("Should have got a failure for contained element failing"); } public void testExpectOverridesMatch() throws Throwable { 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(methodASignature); 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)); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/ConstraintMatcherTest.java 0000644 0001750 0001750 00000006604 07661772071 031200 0 ustar crafterm crafterm 0000000 0000000 package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import com.mockobjects.dynamic.ConstraintMatcher; import junit.framework.TestCase; public class ConstraintMatcherTest extends TestCase { public ConstraintMatcherTest(String name) { super(name); } public void testNoMatchWhenTooManyArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint("constraint1", args[0], true) }; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertFalse("Should not match if too many arguments", constraintMatcher.matches(args)); } public void testNoMatchWhenTooFewArguments() throws Throwable { String[] args = { "arg1" }; MockConstraint[] constraints = { new MockConstraint("constraint1", args[0], true), new MockConstraint("constraint2", args[0], true) }; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertFalse("Should not match if too few arguments", constraintMatcher.matches(args)); } public void testNoMatchWhenConstraintIsViolated() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint("constraintA", args[0], true), new MockConstraint("constraintB", args[1], false), new MockConstraint("constraintC", args[2], true) }; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertFalse("Should not match", constraintMatcher.matches(args)); } public void testNoMatchWithNoArgumentsAndCalleeHasArguments() throws Throwable { String[] args = new String[] { "arg1", "arg2" }; MockConstraint[] constraints = new MockConstraint[0]; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertFalse("Should not match", constraintMatcher.matches(args)); } public void testMatchWithNoArguments() throws Throwable { String[] args = new String[0]; MockConstraint[] constraints = new MockConstraint[0]; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertTrue("Should match", constraintMatcher.matches(args)); } public void testMatchAndArgumentsCheckedAgainstConstraints() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint constraintA = new MockConstraint("constraintA", args[0], true); MockConstraint constraintB = new MockConstraint("constraintB", args[1], true); MockConstraint constraintC = new MockConstraint("constraintC", args[2], true); MockConstraint[] constraints = { constraintA, constraintB, constraintC }; ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints); assertTrue("Should match", constraintMatcher.matches(args)); constraintA.verify(); constraintB.verify(); constraintC.verify(); } public void testMatchWithArgument() throws Throwable { String[] args = { "argA" }; MockConstraint constraintA = new MockConstraint("constraintA", args[0], true); ConstraintMatcher constraintMatcher = new FullConstraintMatcher(new MockConstraint[] { constraintA }); assertTrue("Should match", constraintMatcher.matches(args)); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/DummyInterface.java 0000644 0001750 0001750 00000000464 07661772072 027623 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public interface DummyInterface { public String twoArgMethod( String arg1, String arg2 ) throws Throwable; public String oneArgMethod( String arg1); public void noArgMethodVoid(); public String noArgMethod(); } mockobjects-0.09/src/core/test/mockobjects/dynamic/DummyThrowable.java 0000644 0001750 0001750 00000000367 07661772072 027654 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public class DummyThrowable extends Throwable { public DummyThrowable() { super(); } public DummyThrowable(String message) { super(message); } } mockobjects-0.09/src/core/test/mockobjects/dynamic/DynamicUtilTest.java 0000644 0001750 0001750 00000003754 07661772073 027777 0 ustar crafterm crafterm 0000000 0000000 /* * Created on 16-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.DynamicUtil; import com.mockobjects.dynamic.Mock; import com.mockobjects.util.AssertMo; import junit.framework.TestCase; /** * @author e2x */ public class DynamicUtilTest extends TestCase { public DynamicUtilTest(String name) { super(name); } public void testMethodToStringWithoutProxyArg() throws Exception { String[] args = new String[] {"arg1", "arg2" }; String result = DynamicUtil.methodToString("methodName", args); AssertMo.assertIncludes("Should contain method name", "methodName", result); AssertMo.assertIncludes("Should contain firstArg", "arg1", result); AssertMo.assertIncludes("Should contain second Arg", "arg2", result); } public void testMethodToStringWithStringArray() throws Exception { Object[] args = new Object[] { new String[] {"arg1","arg2"}}; String result = DynamicUtil.methodToString("methodName", args); AssertMo.assertIncludes("Should contain method name", "methodName", result); AssertMo.assertIncludes("Should contain args as an array", "[