Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2300/input/javasrc/biz/xsoftware/mock2/impl
Modified Files:
Tag: branchForOffice
MockObjectImpl.java CalledMethodImpl.java
MockObjectSuperImpl.java
Log Message:
make the first test pass, but need to improve the code. the code checked in is really ungraceful
Index: CalledMethodImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/CalledMethodImpl.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -d -r1.1.2.1 -r1.1.2.2
*** CalledMethodImpl.java 13 Jan 2006 16:33:54 -0000 1.1.2.1
--- CalledMethodImpl.java 17 Jan 2006 07:58:27 -0000 1.1.2.2
***************
*** 8,21 ****
private String stackTrace;
! public void setMethodName(String methodName) {
! this.methodName = methodName;
! }
!
! public void setParameters(Object[] parameters) {
! this.parameters = parameters;
! }
!
! public void setStackTrace(String stackTrace) {
! this.stackTrace = stackTrace;
}
--- 8,20 ----
private String stackTrace;
! public CalledMethodImpl(String methodName, Object[]params,String stackTrace){
! this.methodName=methodName;
! if(params==null){
! this.parameters=new Object[0];
! }else
! {
! this.parameters=params;
! }
! this.stackTrace=stackTrace;
}
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectImpl.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -d -r1.1.2.1 -r1.1.2.2
*** MockObjectImpl.java 13 Jan 2006 16:33:54 -0000 1.1.2.1
--- MockObjectImpl.java 17 Jan 2006 07:58:27 -0000 1.1.2.2
***************
*** 2,5 ****
--- 2,6 ----
import java.lang.reflect.InvocationHandler;
+ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
***************
*** 48,57 ****
return callSuperMethod(proxy,method,args);
}
! return null;
}
! private Object callSuperMethod(Object proxy, Method method, Object[] args) {
! // TODO Auto-generated method stub
! return null;
}
--- 49,79 ----
return callSuperMethod(proxy,method,args);
}
!
! Object o=methodCalledImpl(method.getName(),args);
!
! if(o == null) {
! Class c = method.getReturnType();
! if(!Object.class.isAssignableFrom(c) && !"void".equals(c.getName())) {
! throw new RuntimeException("Must call addReturnValue and specify a non-null value as method="+method.getName()+" returns a primitive value");
! }
! }
! return o;
!
}
! private Object callSuperMethod(Object proxy, Method method, Object[] args) throws Throwable{
! try {
! if("equals".equals(method.getName()))
! return new Boolean(proxy == args[0]);
! else if("toString".equals(method.getName()))
! return ""+this;
!
! return method.invoke(this, args);
! } catch(InvocationTargetException e) {
! if(e.getCause() != null)
! throw e.getCause();
! else
! throw e;
! }
}
***************
*** 60,77 ****
}
! public CalledMethod expect(String methodName, Class... vargs) {
! // TODO Auto-generated method stub
! return null;
! }
!
! public CalledMethod expect(Object returnValue, String methodName, Class... vargs) {
! // TODO Auto-generated method stub
! return null;
! }
!
! public CalledMethod expect(String methodName, long timeout, Class... vargs) {
! // TODO Auto-generated method stub
! return null;
! }
}
--- 82,86 ----
}
!
}
Index: MockObjectSuperImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectSuperImpl.java,v
retrieving revision 1.3.2.1
retrieving revision 1.3.2.2
diff -C2 -d -r1.3.2.1 -r1.3.2.2
*** MockObjectSuperImpl.java 13 Jan 2006 16:33:54 -0000 1.3.2.1
--- MockObjectSuperImpl.java 17 Jan 2006 07:58:27 -0000 1.3.2.2
***************
*** 1,4 ****
--- 1,9 ----
package biz.xsoftware.mock2.impl;
+ import java.util.HashMap;
+ import java.util.LinkedList;
+ import java.util.List;
+ import java.util.Map;
+ import java.util.logging.Level;
import java.util.logging.Logger;
***************
*** 7,84 ****
import biz.xsoftware.mock2.MockObject;
! public abstract class MockObjectSuperImpl implements MockObject{
!
!
! private static final Logger logger=Logger.getLogger(MockObjectSuperImpl.class.getName());
! private static final long DEFAULT_TIME_OUT=10000;
! //private static final Logger logger=Logger.getLogger(MockObjectSuperImpl.class.getName());
public CalledMethod expect(String methodName, Class... vargs) {
! return expect(methodName,DEFAULT_TIME_OUT,vargs);
}
-
!
! 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);
! }
public CalledMethod expect(String methodName, long timeout, Class... vargs) {
! if(methodName==null)
throw new IllegalArgumentException("methd name can not be null");
! verify(timeout);
! return null;
}
! public CalledMethod expect(Object returnValue, String methodName, long timeout, Class... vargs) {
// TODO Auto-generated method stub
return null;
}
! public CalledMethod expect(String methodName, Throwable throwable, long timeout, Class... vargs) {
// TODO Auto-generated method stub
return null;
}
! public CalledMethod expect(String methodName, Behavior behavior, long timeout, Class... params) {
// TODO Auto-generated method stub
return null;
}
! public void verify(long timeout) {
// TODO Auto-generated method stub
!
}
public void addIgnoreMethod(String methodName, Class... vargs) {
// TODO Auto-generated method stub
!
}
public void removeIgnoreMethod(String methodName, Class... vargs) {
// TODO Auto-generated method stub
!
}
! public void setDefaultReturnValue(Object returnValue, String methodName, Class... vargs) {
// TODO Auto-generated method stub
!
}
--- 12,111 ----
import biz.xsoftware.mock2.MockObject;
! public abstract class MockObjectSuperImpl implements MockObject {
! private static final Logger logger = Logger.getLogger(MockObjectSuperImpl.class.getName());
+ private static final long DEFAULT_TIME_OUT = 10000;
+ /**
+ * List of the current methods that have been called.
+ */
+ private List<CalledMethod> methodsCalled = new LinkedList<CalledMethod>();
+
+ /**
+ * A map of queues, containing objects to return when methods are called
+ */
+ private Map<String, List<Object>> methodToReturnVal = new HashMap<String, List<Object>>();
+ /**
+ * A map of default return values, which are used to return if the
+ * queue for the method is empty.
+ */
+ private Map<String, Object> methodToDefaultRetVal = new HashMap<String, Object>();
public CalledMethod expect(String methodName, Class... vargs) {
! return expect(methodName, DEFAULT_TIME_OUT, vargs);
}
! 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);
+ }
public CalledMethod expect(String methodName, long timeout, Class... vargs) {
! if (methodName == null)
throw new IllegalArgumentException("methd name can not be null");
! verify(methodName,timeout);
! //waitForCalledMethod(methodName,DEFAULT_TIME_OUT);
! CalledMethod calledMethod=methodsCalled.remove(0);
! return calledMethod;
}
+ private void waitForCalledMethod(String methodName,long timeout) {
+ // TODO Auto-generated method stub
+
+ CalledMethod calledMethod=methodsCalled.remove(0);
+
+
+ }
! public CalledMethod expect(Object returnValue, String methodName,
! long timeout, Class... vargs) {
// TODO Auto-generated method stub
return null;
}
! public CalledMethod expect(String methodName, Throwable throwable,
! long timeout, Class... vargs) {
// TODO Auto-generated method stub
return null;
}
! public CalledMethod expect(String methodName, Behavior behavior,
! long timeout, Class... params) {
// TODO Auto-generated method stub
return null;
}
! public void verify(String methodName,long timeout) {
// TODO Auto-generated method stub
!
}
public void addIgnoreMethod(String methodName, Class... vargs) {
// TODO Auto-generated method stub
!
}
public void removeIgnoreMethod(String methodName, Class... vargs) {
// TODO Auto-generated method stub
!
}
! public void setDefaultReturnValue(Object returnValue, String methodName,
! Class... vargs) {
// TODO Auto-generated method stub
!
}
***************
*** 88,90 ****
--- 115,157 ----
}
+ synchronized protected Object methodCalledImpl(String methodName,
+ Object[] parameters) {
+
+ methodName = methodName.intern();
+ String params = "";
+ if (parameters == null) {
+ params = "no params";
+ } else {
+ Object[] array = (Object[]) parameters;
+ for (int i = 0; i < array.length - 1; i++) {
+ params += array[i] + ", ";
+ }
+ params += array[array.length - 1];
+ }
+
+ if (logger.isLoggable(Level.FINE))
+ logger.log(Level.FINE, "method called=" + methodName + "(" + params
+ + ") on obj=" + this);
+ String stackTrace="method Called="+methodName+"("+params+"on object"+this+")";
+ methodsCalled.add(new CalledMethodImpl(methodName,parameters,stackTrace));
+ this.notifyAll();
+
+ return getReturnValue(methodName);
+ }
+
+ private Object getReturnValue(String methodName){
+
+ List l=(List)methodToReturnVal.get(methodName);
+ if(l != null) {
+ Object retVal = l.remove(0);
+ if(l.size()<= 0)
+ methodToReturnVal.remove(methodName);
+ return retVal;
+ }
+ //next line returns null if setDefaultReturnValue not called by test case...
+ return methodToDefaultRetVal.get(methodName);
+
+
+ }
+
}
|