Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14392/input/javasrc/biz/xsoftware/mock2/impl
Modified Files:
MockObjectSuperImpl.java MockObjectFactoryImpl.java
CalledMethodImpl.java MockObjectImpl.java
Log Message:
merge brachForOffice to HEAD
Index: CalledMethodImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/CalledMethodImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** CalledMethodImpl.java 3 Jan 2006 13:29:54 -0000 1.1
--- CalledMethodImpl.java 16 Feb 2006 15:37:34 -0000 1.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,25 ----
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;
}
!
! public CalledMethodImpl(String methodName){
! this.methodName=methodName;
!
}
***************
*** 27,30 ****
--- 31,38 ----
public Object[] getParameters() {
// TODO Auto-generated method stub
+ if(parameters==null)
+ {
+ throw new IllegalStateException("MockObject.verify() must be called before accessing and of the methods on CalledMethod object");
+ }
return parameters;
}
***************
*** 34,37 ****
--- 42,59 ----
return 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;
+ }
+
+
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** MockObjectImpl.java 13 Jan 2006 14:48:31 -0000 1.1
--- MockObjectImpl.java 16 Feb 2006 15:37:34 -0000 1.2
***************
*** 2,13 ****
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MockObjectImpl extends MockObjectSuperImpl implements
InvocationHandler {
!
public MockObjectImpl(Class[] interfacesToMock) {
! // TODO Auto-generated constructor stub
}
--- 2,38 ----
import java.lang.reflect.InvocationHandler;
+ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+ import java.util.HashSet;
+ import java.util.Set;
+ import biz.xsoftware.mock2.MockObject;
+ /**
+ * this class implements the interface InvocationHandler,defind a
+ * certain MockObject and return a calledMethod instance to the
+ * test cases
+ * @author Jay
+ *
+ */
public class MockObjectImpl extends MockObjectSuperImpl implements
InvocationHandler {
! private static Set<Method> isMethodInSuper=new HashSet<Method>();
! Class[]classes;
!
! static {
! Class c=MockObject.class;
! Method[] m=c.getMethods();
! for(int i=0;i<m.length;i++){
! isMethodInSuper.add(m[i]);
! }
! c=Object.class;
! m=c.getMethods();
! for(int i=0;i<m.length;i++){
! isMethodInSuper.add(m[i]);
! }
! }
public MockObjectImpl(Class[] interfacesToMock) {
! this.classes=interfacesToMock;
}
***************
*** 19,24 ****
throws Throwable {
// TODO Auto-generated method stub
! return null;
}
}
--- 44,85 ----
throws Throwable {
// TODO Auto-generated method stub
! if(isMethodInSuper.contains(method)){
! 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;
+ }
+ }
+
+ public Class[] getClasses(){
+ return this.classes;
+ }
+
+
+
}
+
Index: MockObjectFactoryImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectFactoryImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MockObjectFactoryImpl.java 13 Jan 2006 15:45:00 -0000 1.2
--- MockObjectFactoryImpl.java 16 Feb 2006 15:37:34 -0000 1.3
***************
*** 15,19 ****
}
! ClassLoader cl = MockObjectFactory.class.getClassLoader();
MockObjectImpl impl = new MockObjectImpl(interfacesToMock);
Object o = Proxy.newProxyInstance(cl, interfacesPlusMock, impl);
--- 15,19 ----
}
! ClassLoader cl = MockObjectFactoryImpl.class.getClassLoader();
MockObjectImpl impl = new MockObjectImpl(interfacesToMock);
Object o = Proxy.newProxyInstance(cl, interfacesPlusMock, impl);
***************
*** 27,31 ****
interfacePlusMock[0] = MockObject.class;
! ClassLoader cl = MockObjectFactory.class.getClassLoader();
MockObjectImpl impl = new MockObjectImpl(interfaceToMock, realObject);
Object o = Proxy.newProxyInstance(cl, interfacePlusMock, impl);
--- 27,31 ----
interfacePlusMock[0] = MockObject.class;
! ClassLoader cl = MockObjectFactoryImpl.class.getClassLoader();
MockObjectImpl impl = new MockObjectImpl(interfaceToMock, realObject);
Object o = Proxy.newProxyInstance(cl, interfacePlusMock, impl);
Index: MockObjectSuperImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectSuperImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockObjectSuperImpl.java 13 Jan 2006 14:48:31 -0000 1.3
--- MockObjectSuperImpl.java 16 Feb 2006 15:37:34 -0000 1.4
***************
*** 1,75 ****
package biz.xsoftware.mock2.impl;
import biz.xsoftware.mock2.Behavior;
import biz.xsoftware.mock2.CalledMethod;
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 NO_TIME_OUT=0;
! public CalledMethod expect(String methodName, Class... vargs) {
! return expect(methodName,NO_TIME_OUT,vargs);
! }
! public CalledMethod expect(Object returnValue, String methodName, Class... vargs) {
! return expect(returnValue,methodName,NO_TIME_OUT,vargs);
! }
! public CalledMethod expect(String methodName, Throwable throwable, Class... vargs) {
!
! return expect(methodName,throwable,NO_TIME_OUT,vargs);
! }
! public CalledMethod expect(String methodName, Behavior behavior, Class... params) {
!
! return expect(methodName,behavior,NO_TIME_OUT,params);
! }
! public CalledMethod expect(String methodName, long timeout, Class... vargs) {
!
! 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() {
! // 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
!
! }
! public Object instance() {
! // TODO Auto-generated method stub
! return null;
! }
}
--- 1,186 ----
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;
+
import biz.xsoftware.mock2.Behavior;
import biz.xsoftware.mock2.CalledMethod;
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 int Index=0;
! /**
! * List of the current methods that have been called.
! */
! private List<CalledMethod> methodsCalled = new LinkedList<CalledMethod>();
! /**
! * List of the methods been called ,and store them
! */
! Map retVal=new HashMap();
! /**
! * List of the methods that have been expected
! */
! private List<CalledMethod> methodsExpected = 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");
! CalledMethod calledMethod = new CalledMethodImpl(methodName);
! methodsExpected.add(calledMethod);
! return calledMethod;
! }
!
! public CalledMethod expect(Object returnValue, String methodName,
! long timeout, Class... vargs) {
! if(methodName==null)
! throw new IllegalArgumentException("method name can not be null");
! CalledMethod calledMethod=new CalledMethodImpl(methodName);
! methodsExpected.add(calledMethod);
! retVal.put(methodName,returnValue);
! return calledMethod;
! }
!
! 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() {
!
! if (methodsCalled.size() == 0) {
! synchronized(this){
! try {
! this.wait(DEFAULT_TIME_OUT);
! } catch (InterruptedException e) {
! // TODO Auto-generated catch block
! e.printStackTrace();
! }
! }
! }
!
! while (methodsCalled.size() != 0) {
! CalledMethod calledMethod = methodsCalled.remove(0);
! CalledMethodImpl m = (CalledMethodImpl) methodsExpected.remove(0);
! m.setParameters(calledMethod.getParameters());
! m.setStackTrace(calledMethod.getStackTrace());
!
! }
!
! }
!
! 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) {
!
!
! }
!
! public Object instance() {
! // TODO Auto-generated method stub
! return null;
! }
!
! 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];
! }
!
! String stackTrace = "method Called=" + methodName + "(" + params
! + "on object" + this + ")";
! // if(methodsExpected.size()!=0){
! CalledMethod calledMethod = new CalledMethodImpl(methodName,
! parameters, stackTrace);
!
! methodsCalled.add(calledMethod);
!
! // }
!
! return getReturnValue(methodName);
! }
!
! private Object getReturnValue(String methodName) {
! if(retVal.size()==0){
! String returnValue="No return value is set";
! setDefaultReturnValue(returnValue,methodName);
! }
! if(retVal.containsKey(methodName)){
! return retVal.get(methodName);
! }
!
! return null;
!
!
! }
}
|