Update of /cvsroot/mocklib/gwtmocklib/input/javasrc/biz/xsoftware/mock/client
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv18350/input/javasrc/biz/xsoftware/mock/client
Added Files:
JsMockObject.java JsMockSuperclass.java JsMockEntry.java
JsBehavior.java JsExpectFailedException.java
JsCalledMethod.java
Log Message:
finishing up gwtmocklib
--- NEW FILE: JsMockObject.java ---
/*
* Created on Jun 7, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.mock.client;
/**
* The interface all mock objects implement. This is the interface used
* by the unit tests once the mock object is created.
*
* @author Dean Hiller
*/
public interface JsMockObject {
/**
* Field used to expect that no methods have been called.
*/
public static String NONE = "No method should have been called";
/**
* Field used to expect any method so expectCall returns as soon as
* any method is called.
*/
public static String ANY = "'Any method'";
/**
* Waits for one and only one method to be called. If any methods are
* called before or after this one(after can sometimes be caught by
* the MockObject when the threading is not synchronous), then
* this call will throw an ExpectFailedException.
*
* @param method The expected method.
* @return An array of params that were passed to the methods called
*/
public JsCalledMethod expect(String method);
/**
* Waits for all the methods to be called. If any of the methods
* are not called or are called out of order, this method throws
* an exception which will fail the test case. For each method,
* this will wait WAIT_TIME milliseconds for each method to be called.
* If the method is not called within this period, an exception will
* be thrown saying 'method' was not called within timeout period.
*
* @param methods The expected method(s) in the correct order.
* @return An array or arrays of params that were passed to the methods called
*/
public JsCalledMethod[] expect(String[] methods);
/**
* Add an exception to throw when a method on the mockObject is called.
* <br/<br/>
* This can be called many times where each time it is called, the
* exception is added to a queue. Each time 'method' is called, it
* checks the queue, if empty, it does not throw an exception.
* <br/<br/>
* Should only pass the type of Throwable that the 'method'
* can throw. If you pass IOException in for a method that
* doesn't have a signature of 'throws IOException', then
* one of the following exceptions will be thrown into
* the sysUnderTest
* <ol>
* <li> UndeclaredThrowableException for MockObjects created with MockCreator</li>
* <li> RuntimeException for Subclasses of MockSuperclass</li>
* </ol>
*
* @param method The method to throw the exception on when it is called.
* @param e The exception to throw on method.
*/
public void addThrowException(String method, Throwable e);
/**
* Add a return value to return when 'method' is called.
* <br></br>
* This can be called multiple times and each call will add
* to a queue. When 'method' is called, it will return
* the first value on the queue. If the queue is null,
* 'method' will return the defaultvalue which is null unless
* setDefaultReturnValue is called on MockObject.
* <br></br>
* Use Integer to return int, Long for long, etc.
*
* @param method The method that when called returns first value on queue
* @param o The object to return that is added to the queue
*/
public void addReturnValue(String method, Object o);
public void addBehavior(String method, JsBehavior behavior);
/**
* When calling expect, the MockObject will ignore this method,
* so it will not result in an exception.
*/
public void addIgnore(String method);
/**
* Removes the method from the ignored methods set.
*/
public void removeIgnore(String method);
/**
* Set the DefaultReturnValue for a 'method'
* @param method The method
*/
public void setDefaultReturnValue(String method, Object o);
public void setExpectTimeout(int timeout);
public int getExpectTimeout();
}
--- NEW FILE: JsMockSuperclass.java ---
package biz.xsoftware.mock.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* This is a super class for mock Objects. It has the following options
* <ol>
* <li>Guarantee order of events and that events happen on one object</li>
* <li>Guarantee events called with order not mattering on one object</li>
* <li>Guarantee order of events is correct between two objects(One mock obj. implements two interfaces)</li>
* </ol>
* This class will also return the parameters that were passed into the MockObject
* so they can be introspected in testing to make sure they were correct.
*
* The MockObject extending this class can also be told to throw exceptions on certain
* methods so we can test error leg behavior.
*
* Example of how to use
* MockActionListener implements ActionListener and extends this class
* The only method in MockActionListener
* is
* <PRE>
* public final static ACTION_METHOD = "actionPerformed method";
* public void actionPerformed(ActionEvent evt) {
* super.methodCalled(ACTION_METHOD, evt);
* }
* </PRE>
*
* In the test, when you expect an ActionEvent, you can call
* <PRE>
* Object o = MockActionListener.expectEvent(ACTION_METHOD);
* ActionEvent evt = (ActionEvent)evt;
* assertNonNull(evt.getSource());
* </PRE>
*
* Another useful behavior is throwing any type of exception using
* setExceptionOnMethod(String method, Throwable e). This can test
* robustness in a system to make sure listeners or services that
* throw exceptions don't affect your system, or at least affect
* your system in the proper way.
*
* @author Dean Hiller (de...@xs...)
*/
public abstract class JsMockSuperclass implements JsMockObject {
/**
* List of the current methods that have been called.
*/
private List methodsCalled = new ArrayList();
/**
* A map of queues, containing either
* 1. objects to return when methods are called
* 2. Behaviors to run which return objects to return
* 3. Exceptions to throw
*/
private Map methodToReturnVal = new HashMap();
/**
* A map of default return values, which are used to return if the
* queue for the method is empty.
*/
private Map methodToDefaultRetVal = new HashMap();
private List ignoredMethods = new ArrayList();
/**
* Default wait time to wait for a method to be called once expectCall is
* called.
*/
public static final int DEFAULT_WAIT_TIME = 10000;
private int waitTime = DEFAULT_WAIT_TIME;
private String id;
/**
* Default constructor of superclass of all mockObjects with a delay of
* 10 seconds. This delay is the amount of time the mock object waits for
* a method to be called when a client calls expectCall.
*/
public JsMockSuperclass() {
}
/**
* The constructor to use to override the default delay({@link #DEFAULT_WAIT_TIME})
* such that the mock object will give methods a longer time to be called
* before timing out to fail the test.
*
* @param delay The amount of time in milliseconds to wait for a method to be
* called.
*/
public JsMockSuperclass(int delay) {
waitTime = delay;
}
public JsMockSuperclass(String id) {
this.id = id;
}
public void setExpectTimeout(int delay) {
this.waitTime = delay;
}
public int getExpectTimeout() {
return waitTime;
}
public void addIgnore(String method)
{
ignoreImpl(new String[] {method});
}
public void removeIgnore(String method)
{
removeIgnoreImpl(new String[] {method});
}
private void removeIgnoreImpl(String[] methods)
{
for(int i = 0; i < methods.length; i++)
{
String method = methods[i];
ignoredMethods.remove(method);
}
}
private void ignoreImpl(String[] methods)
{
addIgnoredMethods(methods);
}
private void addIgnoredMethods(String[] methods) {
if(methods == null)
return;
verifyMethodsExist(methods);
for(int i = 0; i < methods.length; i++)
{
String method = methods[i];
ignoredMethods.add(method);
}
}
/**
* Subclasses should call this method when a method on their interface
* is called. This method is for users to create subclasses and call so
* they don't have to wrap it in a try-catch block.
*
* @param method
* @param parameters
* @return whatever the client has specified using addReturnValue
*/
protected Object methodCalled(String method, Object p) {
try {
Object parameters = p;
if(!(parameters instanceof Object[])) {
parameters = new Object[] {parameters};
}
return methodCalledImpl(method, (Object[])parameters);
} catch (Throwable e) {
if(e instanceof RuntimeException)
throw (RuntimeException)e;
throw new RuntimeException("Sorry, must wrap exception, unwrap in " +
"your mockobject, or have mockObject call methodCalledImpl instead", e);
}
}
protected synchronized Object methodCalledImpl(String method, Object[] parameters) throws Throwable {
//Since no notify and wait can be used in GWT mocklib, intern is not needed and can be commented out.
// method = method.intern();
String params = "";
if(parameters == null) {
params = "no params";
} else {
Object[] array = parameters;
for(int i = 0; i < array.length-1; i++) {
params += array[i]+", ";
}
params += array[array.length-1];
}
System.out.println(id+"method called="+method+"("+params+") on obj="+this);
//sometimes, the contract is for the "code" to give a parameter
//to a library and then modify the parameter after the library
//is done with it. This means the object the "code" gave to
//the MockObject will change out from under the MockObject and be
//corrupted meaning you can't write a test to test the contract
//without cloning the object so it can't be changed.
Object[] newParams = clone(method, parameters);
methodsCalled.add(new JsCalledMethod(method, newParams, new Throwable().fillInStackTrace()));
// this.notifyAll();
return findNextAction(method, parameters);
}
private Object findNextAction(String method, Object[] params) throws Throwable {
List l = (List)methodToReturnVal.get(method);
if(l == null)
{
return methodToDefaultRetVal.get(method);
}
Object retVal = l.remove(0);
if(l.size()<=0)
methodToReturnVal.remove(method);
else if(retVal == null)
methodToDefaultRetVal.get(method);
if(retVal instanceof JsBehavior) {
return ((JsBehavior)retVal).runMethod(params);
} else if(retVal instanceof Throwable) {
Throwable t = (Throwable)retVal;
t.fillInStackTrace();
throw t;
} else
return retVal;
}
// /**
// * @see biz.xsoftware.mock.JsMockObject#expectUnorderedCalls(java.lang.String[])
// */
// public synchronized JsCalledMethod[] expectUnorderedCalls(String[] methods) {
// if(methods == null)
// throw new IllegalArgumentException("methods cannot be null");
// else if(methods.length <= 0)
// throw new IllegalArgumentException("methods.length must be >= 1");
// for(int i = 0; i < methods.length; i++) {
// if(methods[i] == null)
// throw new IllegalArgumentException("None of values in methods " +
// "can be null, yet methods["+i+"] was null");
// }
//
// Map expectedMethods = new HashMap();
// for(int i = 0; i < methods.length; i++) {
// expectedMethods.put(methods[i], new Integer(i));
// }
//
// Set ignorables = createIgnorableMap(ignoredMethods);
// JsCalledMethod[] retVal = new JsCalledMethod[methods.length];
//
// List methodsCalledList = new ArrayList();
// for(int i = 0; i < methods.length; i++) {
// if(ANY.equals(methods[i]))
// throw new IllegalArgumentException("The parameter 'methods' in " +
// "expectUnorderedCalls cannot contain MockSuperclass.ANY(use expectOrderedCalls instead)");
//
// JsCalledMethod o = expectUnignoredCall(ANY, ignorables, methodsCalledList);
// if(o == null) {
// String reason = putTogetherReason(methods, ignorables, methodsCalledList,
// "timed out on next expected method\n");
// throw new JsExpectFailedException("Timed out waiting for a method call\n"
// +reason, retVal, JsExpectFailedException.TIMED_OUT);
// }
// Integer index = (Integer)expectedMethods.remove(o.getMethodName());
// if(index == null) {
// String reason = putTogetherReason(methods, ignorables, methodsCalledList, null);
// throw new JsExpectFailedException(reason, retVal, JsExpectFailedException.UNEXPECTED_CALL_BEFORE);
// }
//
// retVal[index.intValue()] = o;
// }
//
// LeftOverMethods leftOver = getLeftOverMethods(ignorables);
// if(leftOver != null) {
// String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n");
// throw new JsExpectFailedException("There was a method called after your expected methods.\n"+reason, retVal
// , JsExpectFailedException.UNEXPECTED_CALL_AFTER);
// }
//
// return retVal;
// }
private String getHowMethodWasCalled(JsCalledMethod method) {
// Throwable t = method.getHowItWasCalled();
String retVal = "\nThe last method was="+method.getMethodName();
retVal += "\nHere is a stack trace showing ";
retVal += "you how it was called...\n";
retVal += "--------BEGIN="+method.getMethodName()+"---------------\n";
retVal += "THIS DOES NOT WORK IN GWT(so it is commented out)\n";
// StringWriter s = new StringWriter();
// PrintWriter p = new PrintWriter(s);
// t.printStackTrace(p);
// retVal += s.toString();
retVal += "--------END="+method.getMethodName() +"---------------\n";
return retVal;
}
private JsCalledMethod[] cleanup(JsCalledMethod[] methods, int size) {
JsCalledMethod[] retVal = new JsCalledMethod[size];
for(int i = 0; i < retVal.length; i++) {
retVal[i] = methods[i];
}
return retVal;
}
/**
* @see biz.xsoftware.mock.JsMockObject#expectCall(java.lang.String)
*/
public JsCalledMethod expectCall(String method) {
return expectImpl(new String[] {method})[0];
}
/**
* @see biz.xsoftware.mock.JsMockObject#expect(java.lang.String)
*/
public JsCalledMethod expect(String method) {
return expectImpl(new String[] {method})[0];
}
/**
* @see biz.xsoftware.mock.JsMockObject#expect(java.lang.String[])
*/
public synchronized JsCalledMethod[] expect(String[] methods)
{
return expectImpl(methods);
}
private synchronized JsCalledMethod[] expectImpl(String[] methods)
{
return expectOrderedCalls(methods);
}
/**
* @see biz.xsoftware.mock.JsMockObject#expectOrderedCalls(java.lang.String[])
*/
private synchronized JsCalledMethod[] expectOrderedCalls(String[] methods) {
if(methods == null)
throw new IllegalArgumentException("methods cannot be null");
else if(methods.length <= 0)
throw new IllegalArgumentException("methods.length must be >= 1");
for(int i = 0; i < methods.length; i++) {
if(methods[i] == null)
throw new IllegalArgumentException("None of values in methods can be null, yet methods["+i+"] was null");
}
verifyMethodsExist(methods);
Set ignorables = createIgnorableMap(ignoredMethods);
List methodsCalledList = new ArrayList();
JsCalledMethod[] retVal = new JsCalledMethod[methods.length];
int i = 0;
for(; i < methods.length; i++) {
String method = methods[i];
JsCalledMethod o = null;
try {
o = expectUnignoredCall(method, ignorables, methodsCalledList);
} catch(JsExpectFailedException e) {
if(!JsExpectFailedException.UNEXPECTED_ON_NONE.equals(e.getReason())) {
throw e;
}
Object[] leftOver = e.getCalledMethods();
throw new JsExpectFailedException(e.getMessage(), leftOver, e.getReason());
}
if(o == null) {
String reason = putTogetherReason(methods, ignorables, methodsCalledList,
"timed out on next expected method\n");
throw new JsExpectFailedException("Timed out waiting for call="
+method+"\n"+reason, cleanup(retVal, i), JsExpectFailedException.TIMED_OUT);
}
retVal[i] = o;
if(!method.equals(o.getMethodName()) && !ANY.equals(method)) {
String reason = putTogetherReason(methods, ignorables, methodsCalledList, null);
reason += getHowMethodWasCalled(o);
throw new JsExpectFailedException(reason, cleanup(retVal, i), null);
}
}
LeftOverMethods leftOver = getLeftOverMethods(ignorables);
if(leftOver != null) {
String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n");
reason += getHowMethodWasCalled(leftOver.getMethod(0));
JsCalledMethod[] calledOnes = new JsCalledMethod[retVal.length+1];
//System.arraycopy(retVal, 0, calledOnes, 0, retVal.length);
arraycopy(retVal, calledOnes);
calledOnes[retVal.length] = leftOver.getMethod(0);
throw new JsExpectFailedException("There was a method called after your expected methods.\n"+reason, calledOnes
, JsExpectFailedException.UNEXPECTED_CALL_AFTER);
}
return retVal;
}
private void arraycopy(JsCalledMethod[] retVal, JsCalledMethod[] calledOnes) {
for(int i = 0; i < retVal.length; i++) {
calledOnes[i] = retVal[i];
}
}
private void verifyMethodsExist(String[] methods) {
for(int i = 0; i < methods.length; i++) {
verifyMethod(methods[i]);
}
}
private void verifyMethod(String method) {
//verifyMethod can't be done in GWT
return;
// if(MockObject.ANY.equals(method) || MockObject.NONE.equals(method))
// return;
// Class[] classes = getClasses();
// if(classes == null)
// return;
//
// String classNames = "";
// for(int i = 0; i < classes.length; i++) {
// //FINEST LOG.....
// //System.out.println("class="+classes[i].getName());
// if(methodExistInThisClass(method, classes[i]))
// return;
// classNames += "\n"+classes[i];
// }
//
// throw new IllegalArgumentException("method='"+method+"' is not a method on any of the" +
// "\nfollowing Classes/Interfaces"+classNames);
}
// private boolean methodExistInThisClass(String method, Class c) {
// return true;
// //This does not work in GWT testing.....
// Method[] methods = c.getMethods();
// for(int i = 0; i < methods.length; i++) {
// if(log.isLoggable(Level.FINEST))
// log.finest("method in class='"+methods[i].getName()+"' expected='"+method+"'");
// if(method.equals(methods[i].getName()))
// return true;
// }
// return false;
// }
private String putTogetherReason(String[] methods, Set ignorables, List methodsCalled, String lastLine) {
String reason = "\nMethods you expected...\n";
for(int i = 0; i < methods.length; i++) {
reason += "method["+i+"]="+methods[i]+"\n";
}
reason += "Methods you ignored...\n";
//String[] ignored = (String[])ignorables.toArray(new String[0]);
Object[] ignored = ignorables.toArray();
if(ignored.length <= 0)
reason += "no ignored methods\n";
for(int i = 0; i < ignored.length; i++) {
reason += "ignored["+i+"]="+ignored[i]+"\n";
}
reason += "\nMethods that were called...\n";
for(int i = 0; i < methodsCalled.size(); i++) {
if(ignorables.contains(methodsCalled.get(i)))
reason += "method["+i+"](ignored)=";
else
reason += "method["+i+"]=";
reason += methodsCalled.get(i)+"\n";
}
if(lastLine == null)
reason += "(possibly more but we quit after finding unexpected methods)\n";
else
reason += lastLine+"\n";
return reason;
}
protected synchronized JsCalledMethod expectUnignoredCall(String m, Set ignorables, List calledMethods) {
String method = m;
if(method == null)
method = NONE;
//kind of dangerous if used with multiple threads because we might miss
//a bad event coming in, but if you keep using the same listener for every test
//the problem should still manifest itself in a
//different test case which sucks but works.
if(method.equals(NONE)) {
System.out.println("method expected="+NONE+" on obj="+this);
// we have to strip out all of the ignored methods from methodsCalled
//CalledMethod[] methods = (CalledMethod[])methodsCalled.toArray(new CalledMethod[0]);
Object[] methods = methodsCalled.toArray();
for(int i = 0; i < methods.length; i++)
{
JsCalledMethod calledMethod = (JsCalledMethod)methods[i];
if(ignorables.contains(calledMethod.getMethodName()))
{
while(methodsCalled.contains(calledMethod))
{
methodsCalled.remove(calledMethod);
}
}
}
LeftOverMethods leftOver = getLeftOverMethods(ignorables);
if(leftOver != null) {
String reason = "You were expecting no methods to be called, but method(s) not\n"
+"in the ignore list were called earlier. method(s)="+leftOver;
reason += getHowMethodWasCalled(leftOver.getMethod(0));
throw new JsExpectFailedException(reason, leftOver.getMethods(), JsExpectFailedException.UNEXPECTED_ON_NONE);
}
return new JsCalledMethod(NONE, null, null);
}
return waitForUnignoredCall(method, ignorables, calledMethods);
}
private JsCalledMethod waitForUnignoredCall(
String logM, Set ignorables, List calledMethods) {
long waitTime2 = waitTime+50;
// long currentTime;
//only while waitTime is greater than 50 milliseconds
while(waitTime2 >= 50) {
//if there are no methods called yet, since we can't wait like mocklib, throw an exception
//as all testing will have to happen on test thread!!!
if(methodsCalled.size() <= 0) {
return null;
// currentTime = System.currentTimeMillis();
// System.out.println("method expected(not called yet-waiting)="+logM+" on obj="+this+" wait="+waitTime2);
// this.wait(waitTime2);
// long waitedOnWait = System.currentTimeMillis() - currentTime;
// waitTime2 -= waitedOnWait;
}
//check for new methods to be called now...if no non-ignorable methods, then
//continue while loop.
for(int i = 0; i < methodsCalled.size(); i++) {
JsCalledMethod calledMethod = (JsCalledMethod)methodsCalled.remove(0);
calledMethods.add(calledMethod);
if(!ignorables.contains(calledMethod.getMethodName())) {
System.out.println("method expected and was called="+logM+" on obj="+this);
return calledMethod;
}
}
}
//return null means timeout....
return null;
}
private Set createIgnorableMap(List ignorableMethods) {
Set ignorables = new HashSet();
if(ignorableMethods != null) {
for(int i = 0; i < ignorableMethods.size(); i++)
{
String method = (String)ignorableMethods.get(i);
ignorables.add(method);
}
}
return ignorables;
}
private LeftOverMethods getLeftOverMethods(Set ignorables) {
List leftOver = new ArrayList();
for(int i = 0; i < methodsCalled.size(); i++) {
JsCalledMethod o = (JsCalledMethod)methodsCalled.get(i);
if(o != null && !ignorables.contains(o.getMethodName())) {
leftOver.add(o);
}
}
if(leftOver.size() <= 0)
return null;
Object[] m = new Object[0];
//m = (CalledMethod[])leftOver.toArray(m);
m = leftOver.toArray();
return new LeftOverMethods(m);
}
private class LeftOverMethods {
private Object[] leftOver;
public LeftOverMethods(Object[] m) {
leftOver = m;
}
public Object[] getMethods()
{
return leftOver;
}
public int getCalledMethodCount() {
return leftOver.length;
}
public JsCalledMethod getMethod(int index) {
return (JsCalledMethod)leftOver[index];
}
public String toString() {
String retVal = "";
for(int i = 0; i < leftOver.length; i++) {
retVal+="\n"+leftOver[i];
}
return retVal;
}
}
/**
* @see biz.xsoftware.mock.JsMockObject#addThrowException(java.lang.String, java.lang.Throwable)
*/
public synchronized void addThrowException(String method, Throwable e) {
if(method == null)
throw new IllegalArgumentException("method parameter cannot be null");
else if(e == null)
throw new IllegalArgumentException("e parameter to this method cannot be null");
List l = (List)methodToReturnVal.get(method);
if(l == null) {
l = new ArrayList();
methodToReturnVal.put(method, l);
}
l.add(e);
}
/**
* @see biz.xsoftware.mock.JsMockObject#addReturnValue(java.lang.String, java.lang.Object)
*/
public synchronized void addReturnValue(String method, Object o) {
if(method == null)
throw new IllegalArgumentException("method parameter cannot be null");
List l = (List)methodToReturnVal.get(method);
if(l == null) {
l = new ArrayList();
methodToReturnVal.put(method, l);
}
l.add(o);
}
public void setDefaultReturnValue(String method, Object o) {
methodToDefaultRetVal.put(method, o);
}
/**
* This is the method that calls the cloner to clone
* objects like ByteBuffer that can change after
* they are called.
*
* @param o
*/
private Object[] clone(String method, Object[] params) {
if(params == null)
return null;
//check if the next object is a Behavior object
List actions = (List)methodToReturnVal.get(method);
if(actions != null) {
Object action = actions.get(0);
if(action instanceof JsBehavior) {
JsBehavior behavior = (JsBehavior)action;
return behavior.clone(params);
}
}
return params;
}
public void addBehavior(String method, JsBehavior behavior) {
if(method == null)
throw new IllegalArgumentException("method parameter cannot be null");
List l = (List)methodToReturnVal.get(method);
if(l == null) {
l = new ArrayList();
methodToReturnVal.put(method, l);
}
l.add(behavior);
}
}
--- NEW FILE: JsBehavior.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.mock.client;
/**
* An implementation of this class must have the
*/
public interface JsBehavior
{
public Object[] clone(Object[] params);
/**
* This method will be called in place of the interface method you are expecting to be called.
* This is where you can add behavior to the mock object.
*
* @param params
*/
public Object runMethod(Object[] params);
}
--- NEW FILE: JsExpectFailedException.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.mock.client;
/**
* This Exception is thrown when something is expected to happen
* and doesn't. It is basically the assert failing. When
* MockSuperclass.expectEvent is called and the event doesn't come,
* this exception is thrown.
*
* @author Dean Hiller
*/
public class JsExpectFailedException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Expect failed because an event never came and waiting finally timed out.
*/
public static final String TIMED_OUT = "timed out waiting for method";
/**
* Expect failed because a method you did not list in expected methods was called after
* all your expected methods were called.
*/
public static final String UNEXPECTED_CALL_AFTER =
"Another method that was not expected nor ignored was called after all the expected method calls";
/**
* Expect failed because a method you did not list in expected methods was called
* before or during the other methods you did expect.
*/
public static final String UNEXPECTED_CALL_BEFORE =
"Another method that was not expected nor ignored was called before all the expected method calls were called";
/**
* Expect failed because you expected no methods to be called, but a method was called.
*/
public static final String UNEXPECTED_ON_NONE = "Another method was called when no methods should have been called";
private String reason;
private Object[] methods;
private String detailMessage;
public JsExpectFailedException() {}
/**
* Constructor for Expects that fail with a reason and message.
*
* @param message
*/
public JsExpectFailedException(String message, Object[] methods, String reason) {
super(message);
this.detailMessage = message;
this.reason = reason;
this.methods = methods;
}
/**
* Gets the reason expecting the call(s) failed.
*
* Returns either
* <ol>
* <li>{@link #TIMED_OUT}</li>
* <li>{@link #UNEXPECTED_CALL_AFTER}</li>
* <li>{@link #UNEXPECTED_CALL_BEFORE}</li>
* <li>{@link #UNEXPECTED_ON_NONE}</li>
* </ol>
* @return The reason for the failure. One of
*/
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public int retrieveCalledMethodCount() {
return methods.length;
}
/**
* Gives you back the CalledMethods giving you access to the
* parameters that were passed in and the stack traces of how
* they were called.
*
*/
public JsCalledMethod retrieveCalledMethod(int index) {
return (JsCalledMethod)methods[index];
}
/**
* @return
*/
Object[] getCalledMethods()
{
return methods;
}
public void setCalledMethods(Object[] methods) {
this.methods = methods;
}
public String toString() {
return super.toString()+" Subclass:"+detailMessage;
}
}
--- NEW FILE: JsCalledMethod.java ---
/*
* Created on Aug 12, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package biz.xsoftware.mock.client;
import java.util.Arrays;
import java.util.List;
/**
* @author Dean Hiller
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class JsCalledMethod {
private String method;
private Object[] params;
private Throwable howItWasCalled;
public JsCalledMethod(String method, Object[] params, Throwable howItWasCalled) {
this.method = method;
if(params == null)
this.params = new Object[0];
else
this.params = params;
this.howItWasCalled = howItWasCalled;
}
public String getMethodName() {
return method;
}
public int getParameterCount() {
return params.length;
}
public Object getParameter(int index) {
return params[index];
}
/**
*
*/
public Object[] getAllParams() {
return params;
}
public Throwable getHowItWasCalled() {
return howItWasCalled;
}
public String toString() {
List paramList = null;
if(params != null)
paramList = Arrays.asList(params);
return "[method="+method+" params="+paramList+"]";
}
}
--- NEW FILE: JsMockEntry.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.mock.client;
import com.google.gwt.core.client.EntryPoint;
/**
*/
public class JsMockEntry implements EntryPoint
{
/**
* @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
*/
public void onModuleLoad()
{
}
}
|