Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24439/input/javasrc/biz/xsoftware/mock
Modified Files:
MockObjectImpl.java
Log Message:
fix mocklib bug.
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/MockObjectImpl.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** MockObjectImpl.java 25 Aug 2006 22:31:55 -0000 1.9
--- MockObjectImpl.java 1 Sep 2006 16:31:43 -0000 1.10
***************
*** 51,135 ****
Class returnType = method.getReturnType();
! if(o == null) {
! if(returnType == null || void.class != returnType)
! {
! String methodArgs = "";
! Class<?>[] parameterTypes = method.getParameterTypes();
! for(int ii = 0; ii < parameterTypes.length; ii++)
! {
! Class arg = method.getParameterTypes()[ii];
! methodArgs += arg.getName();
! if(ii < parameterTypes.length -1)
! methodArgs += ", ";
! }
! throw new RuntimeException("\nYou must specify a return value of type " +
! returnType +
! " for ignored or expected method:\n" +
! " " + method.getName() + "(" + methodArgs + ")");
! }
! else if(!Object.class.isAssignableFrom(returnType) && !"void".equals(returnType.getName())) {
! throw new RuntimeException("Must call addReturnValue and " +
! "specify a non-null value as method="+method.getName()+" returns a primitive value");
! }
! }
! else if(returnType != Void.TYPE && (o.getClass().isPrimitive() || returnType.isPrimitive()))
! {
! handlePrimitiveReturns(method.getName(), o, returnType);
! }
! else if(o instanceof MockObject) {}
! else if(o.getClass() != returnType)// && !returnType.isPrimitive())
! {
! throwTypeException(method.getName(), o, o.getClass().getName(), returnType.getName());
}
return o;
}
!
!
! 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);
! }
/**
--- 51,142 ----
Class returnType = method.getReturnType();
! if(returnType == null)
! throw new RuntimeException("This is a bug or something");
!
! String methodString = getCleanMethodString(method);
! if(returnType.equals(void.class)) {
! //The return type is void
! if(o != null)
! throw new IllegalArgumentException("You are trying to return something on a method that returns void. Method="+methodString+" value you tried to return="+o);
! } else {
! //Return type is not void...(could be primitive or Object)
!
! if(o == null) {
! if(returnType.isPrimitive())
! throw new IllegalArgumentException("Must call addReturnValue and " +
! "specify a non-null value as method="+methodString+" returns a primitive value");
! } else if(!returnType.isInstance(o))
! throw new IllegalArgumentException("You specified an incorrect return type on method\n="+methodString+"\n"
! +"You specified a return type of="+o.getClass()+" which needs to be or extend type="+returnType);
}
+
return o;
}
! /**
! * @param method
! * @return
! */
! private String getCleanMethodString(Method method)
{
! String retType = method.getReturnType().getName();
! String methodArgs = retType+" "+method.getName()+"(";
! Class<?>[] parameterTypes = method.getParameterTypes();
! for(int ii = 0; ii < parameterTypes.length; ii++)
{
! Class arg = method.getParameterTypes()[ii];
! methodArgs += arg.getName();
! if(ii < parameterTypes.length -1)
! methodArgs += ", ";
}
+ return methodArgs+")";
}
+
! // 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);
! // }
/**
|