Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23471/input/javasrc/biz/xsoftware/mock2/impl
Modified Files:
MockObjectImpl.java CalledMethodImpl.java
MockObjectSuperImpl.java
Log Message:
Fixed some bugs with method not being ignored correctly. general cleanup
Index: CalledMethodImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/CalledMethodImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** CalledMethodImpl.java 25 Mar 2006 08:17:24 -0000 1.3
--- CalledMethodImpl.java 2 May 2006 23:26:34 -0000 1.4
***************
*** 58,64 ****
this.stackTrace = stackTrace;
}
-
-
-
}
--- 58,82 ----
this.stackTrace = stackTrace;
}
+ /**
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ String str = methodName + "(";
+ if(parameters != null)
+ {
+ for(int ii = 0; ii < parameters.length; ii++)
+ {
+ str += parameters[ii];
+ if(ii != parameters.length - 1)
+ {
+ str += ", ";
+ }
+ }
+ }
+ str += ")";
+ return str;
+ }
}
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectImpl.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** MockObjectImpl.java 5 Apr 2006 15:27:08 -0000 1.6
--- MockObjectImpl.java 2 May 2006 23:26:34 -0000 1.7
***************
*** 84,91 ****
return this.realObject;
}
-
-
-
-
}
--- 84,87 ----
Index: MockObjectSuperImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectSuperImpl.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** MockObjectSuperImpl.java 6 Apr 2006 15:25:03 -0000 1.13
--- MockObjectSuperImpl.java 2 May 2006 23:26:34 -0000 1.14
***************
*** 6,17 ****
import java.util.List;
import java.util.Map;
! import java.util.logging.Logger;
import java.util.logging.Level;
import biz.xsoftware.mock2.Behavior;
import biz.xsoftware.mock2.CalledMethod;
import biz.xsoftware.mock2.ExpectFailedException;
- import biz.xsoftware.mock2.MockObject;
import biz.xsoftware.mock2.Messages;
/**
--- 6,18 ----
import java.util.List;
import java.util.Map;
! import java.util.Set;
import java.util.logging.Level;
+ import java.util.logging.Logger;
import biz.xsoftware.mock2.Behavior;
import biz.xsoftware.mock2.CalledMethod;
import biz.xsoftware.mock2.ExpectFailedException;
import biz.xsoftware.mock2.Messages;
+ import biz.xsoftware.mock2.MockObject;
/**
***************
*** 52,65 ****
* A List of Method
*/
! private List<Method> methodList=new LinkedList<Method>();
/**
* a Map of behavior impl which is passed in by expect method
*/
! private Map<String,Behavior> behaviorMap=new HashMap<String,Behavior>();
/**
* A Map of methods which are ignored by tester
*/
! private Map<String,CalledMethod> methodsIgnored=new HashMap<String,CalledMethod>();
--- 53,66 ----
* A List of Method
*/
! private List<Method> methodList = new LinkedList<Method>();
/**
* a Map of behavior impl which is passed in by expect method
*/
! private Map<String,Behavior> behaviorMap = new HashMap<String,Behavior>();
/**
* A Map of methods which are ignored by tester
*/
! private Map<String,CalledMethod> methodsIgnored = new HashMap<String,CalledMethod>();
***************
*** 72,88 ****
}
! public CalledMethod expect(Object returnValue, String methodName,
! Class... vargs) {
return expect(returnValue, methodName, DEFAULT_TIME_OUT, vargs);
}
! public CalledMethod expect(String methodName, Throwable throwable,
! Class... vargs) {
return expect(methodName, throwable, DEFAULT_TIME_OUT, vargs);
}
! public CalledMethod expect(String methodName, Behavior behavior,
! Class... params){
return expect(methodName, behavior, DEFAULT_TIME_OUT, params);
--- 73,86 ----
}
! public CalledMethod expect(Object returnValue, String methodName, Class... vargs) {
return expect(returnValue, methodName, DEFAULT_TIME_OUT, vargs);
}
! public CalledMethod expect(String methodName, Throwable throwable, Class... vargs) {
return expect(methodName, throwable, DEFAULT_TIME_OUT, vargs);
}
! public CalledMethod expect(String methodName, Behavior behavior, Class... params){
return expect(methodName, behavior, DEFAULT_TIME_OUT, params);
***************
*** 90,96 ****
public CalledMethod expect(String methodName, long timeout, Class... vargs) {
! if (methodName == null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! delayTime=timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
if(!methodsIgnored.containsKey(methodName))
--- 88,93 ----
public CalledMethod expect(String methodName, long timeout, Class... vargs) {
! checkPreconditions(methodName);
! delayTime = timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
if(!methodsIgnored.containsKey(methodName))
***************
*** 103,118 ****
}
! public CalledMethod expect(Object returnValue, String methodName,
! long timeout, Class... vargs) {
! if (methodName == null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! delayTime=timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
! if(!methodsIgnored.containsKey(methodName)){
methodsExpected.add(calledMethod);
retVal.put(methodName, returnValue);
! }
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected with return value="+returnValue.toString());
}
return calledMethod;
--- 100,113 ----
}
! public CalledMethod expect(Object returnValue, String methodName, long timeout, Class... vargs) {
! checkPreconditions(methodName);
! delayTime = timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
! // if(!methodsIgnored.containsKey(methodName)){
methodsExpected.add(calledMethod);
retVal.put(methodName, returnValue);
! // }
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected with return value="+returnValue);
}
return calledMethod;
***************
*** 121,126 ****
public CalledMethod expect(String methodName, Throwable throwable,
long timeout, Class... vargs) {
! if (methodName == null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
delayTime=timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
--- 116,120 ----
public CalledMethod expect(String methodName, Throwable throwable,
long timeout, Class... vargs) {
! checkPreconditions(methodName);
delayTime=timeout;
CalledMethod calledMethod = new CalledMethodImpl(methodName);
***************
*** 131,135 ****
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected and expect this method to throw exception="+throwable.toString());
}
--- 125,129 ----
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected and expect this method to throw exception="+throwable);
}
***************
*** 139,145 ****
public CalledMethod expect(String methodName, Behavior behavior,
long timeout, Class... params){
! if(methodName==null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! delayTime=timeout;
CalledMethod calledMethod=new CalledMethodImpl(methodName);
if(!methodsIgnored.containsKey(methodName)){
--- 133,138 ----
public CalledMethod expect(String methodName, Behavior behavior,
long timeout, Class... params){
! checkPreconditions(methodName);
! delayTime = timeout;
CalledMethod calledMethod=new CalledMethodImpl(methodName);
if(!methodsIgnored.containsKey(methodName)){
***************
*** 147,152 ****
behaviorMap.put(methodName,behavior);
try {
! Class c=behavior.getClass();
Method method = c.getMethod(methodName,params);
methodList.add(method);
--- 140,146 ----
behaviorMap.put(methodName,behavior);
+ Class c = null;
try {
! c = behavior.getClass();
Method method = c.getMethod(methodName,params);
methodList.add(method);
***************
*** 154,163 ****
throw new IllegalArgumentException(Messages.METHOD_CAN_NOT_ACCESS);
} catch (NoSuchMethodException e) {
! throw new IllegalArgumentException(methodName+"is not defined in your Behvior Implemetation");
}
}
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected with behavior="+behavior.toString());
}
--- 148,172 ----
throw new IllegalArgumentException(Messages.METHOD_CAN_NOT_ACCESS);
} catch (NoSuchMethodException e) {
! Method[] methods = c.getMethods();
! String message = methodName + "(";
! for(Class param : params)
! {
! message += param + ", ";
! }
! message += ") is not defined in your Behavior Implementation\n" +
! "The following methods with the desired name exist:";
! for(Method method : methods)
! {
! if(method.getName().equals(methodName))
! {
! message += "\n" + method;
! }
! }
! throw new IllegalArgumentException(message);
}
}
if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+methodName+" is expected with behavior="+behavior);
}
***************
*** 167,219 ****
! public void verify(){
!
! if (methodsCalled.size() == 0) {
!
! if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"called methods may be delayed,waitting methods to call...");
! }
! synchronized (this) {
! try {
this.wait(delayTime);
! } catch (InterruptedException e) {
e.getMessage();
}
-
}
}
!
! while (methodsCalled.size() != 0) {
! CalledMethod calledMethod = methodsCalled.remove(0);
! CalledMethodImpl m=new CalledMethodImpl();
! if(methodsExpected.size()!=0){
! m = (CalledMethodImpl) methodsExpected.remove(0);
! while(m.getMethodName()!=calledMethod.getMethodName()){
! if(methodsIgnored.containsKey(m.getMethodName())){
! calledMethod=methodsCalled.remove(0);
! }else
! {
! throw new ExpectFailedException(Messages.ORDER_EXCEPTION);
! }
!
! }
! }else if(methodsIgnored.containsKey(calledMethod.getMethodName())){
! continue;
! }else
! {
! throw new ExpectFailedException(Messages.TOO_MANY_METHODS_CALLED);
! }
!
!
! m.setParameters(calledMethod.getParameters());
! m.setStackTrace(calledMethod.getStackTrace());
!
}
!
}
! protected Object methodCalledImpl(String methodName,
! Object[] parameters) throws Throwable{
methodName = methodName.intern();
--- 176,293 ----
! public void verify()
! {
! timeoutOnEmptyExpect();
!
! // copy this stuff for reporting errors later
! List<CalledMethod> copyMethodsCalled = new LinkedList<CalledMethod>(methodsCalled);
! List<CalledMethod> copyMethodsExpected = new LinkedList<CalledMethod>(methodsExpected);
! Map<String, CalledMethod> copyMethodsIgnored = new HashMap<String, CalledMethod> (methodsIgnored);
+ while(methodsCalled.size() > 0)
+ {
+ log.info("\nmethodsCalled="+ methodsCalled +
+ "\nmethodsExpected=" + methodsExpected);
+
+ CalledMethod calledMethod = methodsCalled.get(0);
+ CalledMethodImpl expectedMethod = null;
+
+ if(methodsExpected.size() > 0)
+ {
+ expectedMethod = (CalledMethodImpl)methodsExpected.get(0);
+
+ if(calledMethod.getMethodName().equals(expectedMethod.getMethodName()))
+ {
+ log.info("called[" + calledMethod + "] == expected[" + expectedMethod + "]... continuing");
+
+ methodsCalled.remove(0);
+ methodsExpected.remove(0);
+ }
+ else
+ {
+ // remove ignored called methods
+ while(!calledMethod.getMethodName().equals(expectedMethod.getMethodName()))
+ {
+ if(methodsIgnored.containsKey(calledMethod.getMethodName()))
+ // TODO: it seems like we need to verify that the _correct_ method was called here
+ // since we now differentiate between methods of the same name, but different parameters
+ // && methodsIgnored.get(calledMethod.getMethodName()).equals(calledMethod))
+ {
+ CalledMethod ignoredMethod = methodsCalled.remove(0);
+ calledMethod = methodsCalled.get(0);
+ log.info("ignored method=" + ignoredMethod);
+ }
+ else
+ {
+ verifyErredOut(Messages.ORDER_EXCEPTION, copyMethodsCalled, copyMethodsExpected, copyMethodsIgnored);
+ }
+ }
+ }
+ }
+ else if(methodsIgnored.containsKey(calledMethod.getMethodName()))
+ {
+ CalledMethod ignoredMethod = methodsCalled.remove(0);
+ log.info("ignored method=" + ignoredMethod);
+ continue;
+ }
+ else
+ {
+ verifyErredOut(Messages.TOO_MANY_METHODS_CALLED + ": " + calledMethod,
+ copyMethodsCalled, copyMethodsExpected, copyMethodsIgnored);
+ }
+ expectedMethod.setParameters(calledMethod.getParameters());
+ expectedMethod.setStackTrace(calledMethod.getStackTrace());
+ }
+ }
+
+ private void timeoutOnEmptyExpect()
+ {
+ if(methodsCalled.size() == 0)
+ {
+ if(log.isLoggable(Level.FINE))
+ {
+ log.log(Level.FINE, "called methods may be delayed, waiting methods to call...");
+ }
+ synchronized(this)
+ {
+ try
+ {
this.wait(delayTime);
! }
! catch(InterruptedException e)
! {
e.getMessage();
}
}
}
! }
!
! private void verifyErredOut(String errorMessage,
! List<CalledMethod> methodsCalled,
! List<CalledMethod> methodsExpected,
! Map<String, CalledMethod> methodsIgnored)
! {
! String message = errorMessage + "\nMethods you expected:";
! for(CalledMethod method : methodsExpected)
! {
! message += "\n" + method;
}
! message += "\n\nMethods you ignored:";
! Set<String> keys = methodsIgnored.keySet();
! for(String key : keys)
! {
! message += "\n" + methodsIgnored.get(key);
! }
! message += "\n\nMethods actually called:";
! for(CalledMethod method : methodsCalled)
! {
! message += "\n" + method;
! }
! message += "\n";
! throw new ExpectFailedException(message);
}
! protected Object methodCalledImpl(String methodName, Object[] parameters) throws Throwable{
! checkPreconditions(methodName);
methodName = methodName.intern();
***************
*** 252,289 ****
}
}
-
return getReturnValue(methodName);
}
private Object getReturnValue(String methodName) {
! if (retVal.containsKey(methodName)) {
! return retVal.get(methodName);
}
return null;
}
! public void addIgnoreMethod(String methodName, Class... vargs) {
! if(methodName==null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! CalledMethod calledMethod=new CalledMethodImpl(methodName);
! methodsIgnored.put(methodName,calledMethod);
!
!
}
! public void removeIgnoreMethod(String methodName, Class... vargs) {
! if(methodName==null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! methodsIgnored.remove(methodName);
}
! public void setDefaultReturnValue(Object returnValue, String methodName,
! Class... vargs) {
! if(methodName==null)
! throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
! retVal.put(methodName,returnValue);
!
!
}
--- 326,370 ----
}
}
return getReturnValue(methodName);
}
private Object getReturnValue(String methodName) {
! String trimmedMethodName = methodName.trim();
! checkPreconditions(trimmedMethodName);
! if (retVal.containsKey(trimmedMethodName)) {
! return retVal.get(trimmedMethodName);
}
return null;
}
! public void addIgnoredMethod(String methodName, Class... vargs) {
! String trimmedMethodName = methodName.trim();
! checkPreconditions(trimmedMethodName);
! CalledMethod calledMethod = new CalledMethodImpl(trimmedMethodName);
! methodsIgnored.put(trimmedMethodName, calledMethod);
}
! /**
! * @see biz.xsoftware.mock2.MockObject#addIgnoredMethod(java.lang.Object, java.lang.String, java.lang.Class...)
! */
! public void addIgnoredMethod(Object returnValue, String methodName, Class... vargs)
! {
! String trimmedMethodName = methodName.trim();
! checkPreconditions(trimmedMethodName);
! CalledMethod calledMethod = new CalledMethodImpl(trimmedMethodName);
! methodsIgnored.put(trimmedMethodName, calledMethod);
! setDefaultReturnValue(returnValue, trimmedMethodName, vargs);
! }
+ public void removeIgnoredMethod(String methodName, Class... vargs) {
+ String trimmedMethodName = methodName.trim();
+ checkPreconditions(trimmedMethodName);
+ methodsIgnored.remove(trimmedMethodName);
}
! public void setDefaultReturnValue(Object returnValue, String methodName, Class... vargs) {
! String trimmedMethodName = methodName.trim();
! checkPreconditions(trimmedMethodName);
! retVal.put(trimmedMethodName, returnValue);
}
***************
*** 292,294 ****
--- 373,382 ----
}
+ private void checkPreconditions(String methodName)
+ {
+ if(methodName == null || "".equals(methodName))
+ {
+ throw new IllegalArgumentException(Messages.METHOD_NAME_CANT_BE_NULL);
+ }
+ }
}
|