Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9115/input/javasrc/biz/xsoftware/mock2/impl
Modified Files:
MockObjectImpl.java CalledMethodImpl.java
MockObjectSuperImpl.java
Log Message:
Missing returns on expect or ignore are reported to the user. Incorrect return types fail and are reported. Fixed some bugs with MockObject.verify().
Index: CalledMethodImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/CalledMethodImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** CalledMethodImpl.java 2 May 2006 23:26:34 -0000 1.4
--- CalledMethodImpl.java 5 May 2006 15:29:23 -0000 1.5
***************
*** 2,5 ****
--- 2,6 ----
import biz.xsoftware.mock2.CalledMethod;
+ import biz.xsoftware.mock2.Messages;
public class CalledMethodImpl implements CalledMethod{
***************
*** 37,41 ****
if(parameters==null)
{
! throw new IllegalStateException("MockObject.verify() must be called before accessing and of the methods on CalledMethod object");
}
return parameters;
--- 38,42 ----
if(parameters==null)
{
! throw new IllegalStateException(Messages.VERIFY_NOT_CALLED);
}
return parameters;
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectImpl.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** MockObjectImpl.java 2 May 2006 23:26:34 -0000 1.7
--- MockObjectImpl.java 5 May 2006 15:29:23 -0000 1.8
***************
*** 1,4 ****
--- 1,7 ----
+
+
package biz.xsoftware.mock2.impl;
+
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
***************
*** 9,87 ****
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>();
! private Class[] classes;
private Class c;
private Object realObject;
-
- 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(String id,Class[] interfacesToMock) {
- super(id);
- this.classes=interfacesToMock;
- }
! public MockObjectImpl( String id,Class interfaceToMock, Object realObject) {
! super(id);
! this.c=interfaceToMock;
! this.realObject=realObject;
! }
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
-
- if(isMethodInSuper.contains(method)){
- return callSuperMethod(proxy,method,args);
- }
- Object o=methodCalledImpl(method.getName(),args);
- 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 getC(){
! return this.c;
! }
!
! public Class[] getClasses(){
return this.classes;
}
!
! public Object getRealObject(){
return this.realObject;
}
}
-
--- 12,110 ----
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>();
!
! private Class[] classes;
!
private Class c;
+
private Object realObject;
! static
! {
! addMethodsToSet(MockObject.class);
! addMethodsToSet(Object.class);
! }
!
! private static void addMethodsToSet(Class c)
! {
! Method[] m = c.getMethods();
! for(int i = 0; i < m.length; i++)
! {
! isMethodInSuper.add(m[i]);
! }
! }
! public MockObjectImpl(String id, Class[] interfacesToMock)
! {
! super(id);
! this.classes = interfacesToMock;
! }
!
!
! public MockObjectImpl(String id, Class interfaceToMock, Object realObject)
! {
! super(id);
! this.c = interfaceToMock;
! this.realObject = realObject;
! }
!
!
! public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
! {
! if(isMethodInSuper.contains(method))
! {
! return callSuperMethod(proxy, method, args);
! }
! return methodCalledImpl(method.getName(), args, method.getReturnType());
!
! }
!
!
! 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 getC()
! {
! return this.c;
! }
!
!
! public Class[] getClasses()
! {
return this.classes;
}
!
!
! public Object getRealObject()
! {
return this.realObject;
}
}
Index: MockObjectSuperImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock2/impl/MockObjectSuperImpl.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** MockObjectSuperImpl.java 2 May 2006 23:26:34 -0000 1.14
--- MockObjectSuperImpl.java 5 May 2006 15:29:23 -0000 1.15
***************
*** 23,30 ****
public abstract class MockObjectSuperImpl implements MockObject {
! private static final Logger log=Logger.getLogger(MockObjectSuperImpl.class.getName());
private String id;
! private long delayTime=0;
private static final long DEFAULT_TIME_OUT = 10000;
--- 23,30 ----
public abstract class MockObjectSuperImpl implements MockObject {
! private static final Logger log = Logger.getLogger(MockObjectSuperImpl.class.getName());
private String id;
! private long delayTime=0;
private static final long DEFAULT_TIME_OUT = 10000;
***************
*** 59,62 ****
--- 59,63 ----
*/
private Map<String,Behavior> behaviorMap = new HashMap<String,Behavior>();
+
/**
* A Map of methods which are ignored by tester
***************
*** 73,76 ****
--- 74,81 ----
}
+ public CalledMethod expect(String methodName) {
+ return expect(methodName, DEFAULT_TIME_OUT, new Class[] {});
+ }
+
public CalledMethod expect(Object returnValue, String methodName, Class... vargs) {
return expect(returnValue, methodName, DEFAULT_TIME_OUT, vargs);
***************
*** 104,111 ****
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);
--- 109,116 ----
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);
***************
*** 178,182 ****
public void verify()
{
! timeoutOnEmptyExpect();
// copy this stuff for reporting errors later
--- 183,187 ----
public void verify()
{
! // timeoutOnEmptyExpect();
// copy this stuff for reporting errors later
***************
*** 239,244 ****
--- 244,259 ----
expectedMethod.setStackTrace(calledMethod.getStackTrace());
}
+
+ if(methodsExpected.size() > 0)
+ {
+ verifyErredOut(Messages.EXPECTED_METHOD_NOT_CALLED, copyMethodsCalled, copyMethodsExpected, copyMethodsIgnored);
+ }
}
+ // TODO REMOVE:
+ // dadrox isn't sure what this code was supposed to do. all it _seemed_ to do was make some tests
+ // take a long time to run. dadrox removed the code and fixed all of the tests that seemed to
+ // depend on the delay.
+ // if this is deemed The Right Thing To Do, then this method may be removed.
private void timeoutOnEmptyExpect()
{
***************
*** 288,294 ****
}
! protected Object methodCalledImpl(String methodName, Object[] parameters) throws Throwable{
checkPreconditions(methodName);
methodName = methodName.intern();
String params = "";
--- 303,312 ----
}
! protected Object methodCalledImpl(String methodName, Object[] parameters, Class<?> returnType) throws Throwable{
checkPreconditions(methodName);
+ // REMOVE
+ log.info("return type=" + returnType);
+
methodName = methodName.intern();
String params = "";
***************
*** 296,300 ****
params = "no params";
} else {
! Object[] array = (Object[]) parameters;
for (int i = 0; i < array.length - 1; i++) {
params += array[i] + ", ";
--- 314,318 ----
params = "no params";
} else {
! Object[] array = parameters;
for (int i = 0; i < array.length - 1; i++) {
params += array[i] + ", ";
***************
*** 305,312 ****
String stackTrace = "method Called=" + methodName + ",params=" + params
+ " on object" + this + ")";
! if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+stackTrace);
! }
CalledMethod calledMethod = new CalledMethodImpl(methodName,
parameters, stackTrace);
--- 323,330 ----
String stackTrace = "method Called=" + methodName + ",params=" + params
+ " on object" + this + ")";
! if(log.isLoggable(Level.FINE)){
! log.log(Level.FINE,"ID="+id+" "+stackTrace);
! }
CalledMethod calledMethod = new CalledMethodImpl(methodName,
parameters, stackTrace);
***************
*** 316,320 ****
Throwable t=methodWithException.get(calledMethod.getMethodName());
throw t;
! }
for(int i=0;i<methodList.size();i++){
--- 334,338 ----
Throwable t=methodWithException.get(calledMethod.getMethodName());
throw t;
! }
for(int i=0;i<methodList.size();i++){
***************
*** 326,330 ****
}
}
! return getReturnValue(methodName);
}
--- 344,410 ----
}
}
! Object ret = getReturnValue(methodName);
! if(ret == null && void.class != returnType)
! {
! throw new RuntimeException("You must specify a return value for " +
! "ignored or expected method " + methodName + "()");
! }
! else if(returnType != Void.TYPE && ret != null && (ret.getClass().isPrimitive() || returnType.isPrimitive()))
! {
! handlePrimitiveReturns(methodName, ret, returnType);
! }
! else if(ret != null && ret instanceof MockObject) {}
! else if(ret != null && ret.getClass() != returnType)// && !returnType.isPrimitive())
! {
! throwTypeException(methodName, ret, ret.getClass().getName(), returnType.getName());
! }
! return ret;
! }
!
! private void handlePrimitiveReturns(String methodName, Object ret, Class<?> expectedReturnType)
! {
! Class<?>[] primitiveTypes = {Integer.TYPE, Double.TYPE, Float.TYPE, Boolean.TYPE,
! Character.TYPE, Byte.TYPE, Short.TYPE, Long.TYPE};
! Object[] primitiveClasses = {Integer.class, Double.class, Float.class, Boolean.class,
! Character.class, Byte.class, Short.class, Long.class};
! if(ret.getClass().isPrimitive() && expectedReturnType.isPrimitive())
! {
! for(Class<?> currentType : primitiveTypes)
! {
! if(ret.getClass() == currentType && expectedReturnType != currentType)
! {
! throwTypeException(methodName, ret, ret.getClass().getName(), expectedReturnType.getName());
! }
! }
! }
! else if(ret.getClass().isPrimitive())
! {
! for(int ii = 0; ii < primitiveTypes.length; ii++)
! {
! if(ret.getClass() == primitiveTypes[ii] && expectedReturnType != primitiveClasses[ii])
! {
! throwTypeException(methodName, ret, ret.getClass().getName(), expectedReturnType.getName());
! }
! }
! }
! else
! {
! for(int ii = 0; ii < primitiveTypes.length; ii++)
! {
! if(expectedReturnType == primitiveTypes[ii] && ret.getClass() != primitiveClasses[ii])
! {
! throwTypeException(methodName, ret, ret.getClass().getName(), expectedReturnType.getName());
! }
! }
! }
! }
!
! private void throwTypeException(String methodName, Object returnValue,
! String returnType, String expectedReturnType)
! {
! throw new RuntimeException("You specified an incorrect return type for " +
! "ignored or expected method " + methodName + "()" +
! "\nyou specified: " + returnValue + " of type " +
! returnType + " but should have been of type " + expectedReturnType);
}
***************
*** 345,348 ****
--- 425,432 ----
}
+ public void addIgnoredMethod(String methodName) {
+ addIgnoredMethod(methodName, new Class[] {});
+ }
+
/**
* @see biz.xsoftware.mock2.MockObject#addIgnoredMethod(java.lang.Object, java.lang.String, java.lang.Class...)
|