Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv15882/input/javasrc/biz/xsoftware/mock
Modified Files:
MockSuperclass.java CalledMethod.java MockObjectImpl.java
Behavior.java
Log Message:
fixed a bug in ignore... ported the examples
Index: Behavior.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/Behavior.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Behavior.java 10 Jun 2006 12:41:03 -0000 1.1
--- Behavior.java 7 Aug 2006 15:52:45 -0000 1.2
***************
*** 16,20 ****
*
* @param params
- * @return
*/
public Object runMethod(Object[] params);
--- 16,19 ----
Index: MockSuperclass.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/MockSuperclass.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** MockSuperclass.java 4 Aug 2006 16:20:52 -0000 1.9
--- MockSuperclass.java 7 Aug 2006 15:52:45 -0000 1.10
***************
*** 121,129 ****
public void ignore(String method)
{
! ignore(method);
}
public void ignore(String ... methods)
{
setIgnoredMethods(methods);
}
--- 121,134 ----
public void ignore(String method)
{
! ignoreImpl(new String[] {method});
}
public void ignore(String ... methods)
{
+ ignoreImpl(methods);
+ }
+
+ private void ignoreImpl(String[] methods)
+ {
setIgnoredMethods(methods);
}
***************
*** 293,297 ****
*/
public CalledMethod expectCall(String method) {
! return expect(new String[] {method})[0];
}
--- 298,302 ----
*/
public CalledMethod expectCall(String method) {
! return expectImpl(new String[] {method})[0];
}
***************
*** 300,304 ****
*/
public CalledMethod expect(String method) {
! return expect(new String[] {method})[0];
}
--- 305,309 ----
*/
public CalledMethod expect(String method) {
! return expectImpl(new String[] {method})[0];
}
***************
*** 308,311 ****
--- 313,321 ----
public synchronized CalledMethod[] expect(String ... methods)
{
+ return expectImpl(methods);
+ }
+
+ private synchronized CalledMethod[] expectImpl(String[] methods)
+ {
return expectOrderedCalls(methods);
}
Index: CalledMethod.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/CalledMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** CalledMethod.java 26 Jun 2005 12:20:14 -0000 1.1
--- CalledMethod.java 7 Aug 2006 15:52:45 -0000 1.2
***************
*** 44,48 ****
/**
*
- * @return
*/
public Object[] getAllParams() {
--- 44,47 ----
***************
*** 53,57 ****
return howItWasCalled;
}
! public String toString() {
List paramList = null;
if(params != null)
--- 52,57 ----
return howItWasCalled;
}
! @Override
! public String toString() {
List paramList = null;
if(params != null)
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/mock/MockObjectImpl.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** MockObjectImpl.java 4 Aug 2006 16:20:52 -0000 1.7
--- MockObjectImpl.java 7 Aug 2006 15:52:45 -0000 1.8
***************
*** 48,61 ****
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;
}
/**
*
--- 48,136 ----
Object o = methodCalledImpl(method.getName(), args);
!
! 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.getCanonicalName() +
! " 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);
+ }
+
/**
*
***************
*** 64,68 ****
* @param m This is the method that was invoked
* @param args These are the arguments that were passed to the method
- * @return
* @throws Throwable
*/
--- 139,142 ----
***************
*** 78,83 ****
if(e.getCause() != null)
throw e.getCause();
! else
! throw e;
}
}
--- 152,156 ----
if(e.getCause() != null)
throw e.getCause();
! throw e;
}
}
***************
*** 85,89 ****
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
! public Class[] getClasses() {
return classes;
}
--- 158,163 ----
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
! @Override
! public Class[] getClasses() {
return classes;
}
|