Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv7561/input/javasrc/biz/xsoftware/mock
Modified Files:
MockObject.java MockSuperclass.java
Added Files:
Behavior.java
Log Message:
add behavior to mock library.
--- NEW FILE: Behavior.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.mock;
/**
* An implementation of this class must have the
*/
public interface Behavior
{
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
* @return
*/
public Object runMethod(Object[] params);
}
Index: MockSuperclass.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/MockSuperclass.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** MockSuperclass.java 19 Feb 2006 19:20:52 -0000 1.6
--- MockSuperclass.java 10 Jun 2006 12:41:03 -0000 1.7
***************
*** 62,71 ****
*/
private List<CalledMethod> methodsCalled = new LinkedList<CalledMethod>();
/**
! * A map of queues containing exceptions to throw on certain methods.
! */
! private Map<String, List<Throwable>> methodToException = new HashMap<String, List<Throwable>>();
! /**
! * A map of queues, containing objects to return when methods are called
*/
private Map<String, List<Object>> methodToReturnVal = new HashMap<String, List<Object>>();
--- 62,71 ----
*/
private List<CalledMethod> methodsCalled = new LinkedList<CalledMethod>();
+
/**
! * 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<String, List<Object>> methodToReturnVal = new HashMap<String, List<Object>>();
***************
*** 172,210 ****
//corrupted meaning you can't write a test to test the contract
//without cloning the object so it can't be changed.
! parameters = clone(parameters);
! methodsCalled.add(new CalledMethod(method, parameters, new Throwable().fillInStackTrace()));
this.notifyAll();
! //throw exception last so clients can still verify this method was called
! //so they have confidence the exception was actually thrown.
! throwExceptionIfNeeded(method);
!
! return getReturnValue(method);
! }
!
! private Object getReturnValue(String method) {
! List l = (List)methodToReturnVal.get(method);
! if(l != null) {
! Object retVal = l.remove(0);
! if(l.size()<= 0)
! methodToReturnVal.remove(method);
! return retVal;
! }
! //next line returns null if setDefaultReturnValue not called by test case...
! return methodToDefaultRetVal.get(method);
}
! private void throwExceptionIfNeeded(String method) throws Throwable {
! List l = (List)methodToException.get(method);
! if(l == null)
! return;
! Throwable t = (Throwable)l.remove(0);
! if(l.size() <= 0)
! methodToException.remove(method);
!
! t.fillInStackTrace();
! log.fine("throw exception on method="+method+" exc="+t);
! throw (Throwable)t;
}
--- 172,200 ----
//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 CalledMethod(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 null;
!
! Object retVal = l.remove(0);
! if(l.size()<=0)
! methodToReturnVal.remove(method);
!
! if(retVal instanceof Behavior) {
! return ((Behavior)retVal).runMethod(params);
! } else if(retVal instanceof Throwable) {
! Throwable t = (Throwable)retVal;
! t.fillInStackTrace();
! throw t;
! } else
! return retVal;
}
***************
*** 535,542 ****
else if(e == null)
throw new IllegalArgumentException("e parameter to this method cannot be null");
! List<Throwable> l = methodToException.get(method);
if(l == null) {
! l = new ArrayList<Throwable>();
! methodToException.put(method, l);
}
l.add(e);
--- 525,532 ----
else if(e == null)
throw new IllegalArgumentException("e parameter to this method cannot be null");
! List<Object> l = methodToReturnVal.get(method);
if(l == null) {
! l = new ArrayList<Object>();
! methodToReturnVal.put(method, l);
}
l.add(e);
***************
*** 569,575 ****
* @return
*/
! private Object[] clone(Object[] params) {
if(params == null)
return null;
if(cloner == null)
return params;
--- 559,577 ----
* @return
*/
! private Object[] clone(String method, Object[] params) {
if(params == null)
return null;
+
+ //check if the next object is a Behavior object
+ List<Object> actions = methodToReturnVal.get(method);
+ if(actions != null) {
+ Object action = actions.get(0);
+ if(action instanceof Behavior) {
+ Behavior behavior = (Behavior)action;
+ return behavior.clone(params);
+ }
+ }
+
+
if(cloner == null)
return params;
***************
*** 585,587 ****
--- 587,600 ----
cloner = c;
}
+
+ public void addBehavior(String method, Behavior behavior) {
+ if(method == null)
+ throw new IllegalArgumentException("method parameter cannot be null");
+ List<Object> l = methodToReturnVal.get(method);
+ if(l == null) {
+ l = new ArrayList<Object>();
+ methodToReturnVal.put(method, l);
+ }
+ l.add(behavior);
+ }
}
Index: MockObject.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/MockObject.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockObject.java 27 Jun 2005 03:40:32 -0000 1.3
--- MockObject.java 10 Jun 2006 12:41:03 -0000 1.4
***************
*** 7,10 ****
--- 7,11 ----
package biz.xsoftware.mock;
+
/**
* The interface all mock objects implement. This is the interface used
***************
*** 113,116 ****
--- 114,119 ----
public int getExpectTimeout();
+
+ public void addBehavior(String string, Behavior behavior);
}
|