Update of /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv16469/input/javasrc/biz/xsoftware/mock
Modified Files:
ExpectFailedException.java CalledMethod.java
MockObjectImpl.java MockSuperclass.java MockObject.java
MockObjectFactory.java Cloner.java
Added Files:
Behavior.java
Log Message:
clean up mocklib 1. It is not 1.4 compatible and is pretty much the final mocklib1
Index: MockObjectFactory.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/MockObjectFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockObjectFactory.java 24 Jun 2005 12:57:00 -0000 1.3
--- MockObjectFactory.java 10 Sep 2006 18:01:12 -0000 1.4
***************
*** 16,19 ****
--- 16,41 ----
public abstract class MockObjectFactory {
+ private MockObjectFactory() {}
+
+ /**
+ * Creates a MockObject based on your interface. One way to use it
+ * for a mock listener is below.<br/><br/>
+ * See examples for other non-listener mock objects.
+ * <PRE>
+ * MockSuperclass mockActionList = MockObjectFactory.createMock(ActionListener.class);
+ * button.addActionListener((ActionListener)mockActionList);
+ * button.setPressed(true);
+ * Object o = mockActionList.expectEvent("actionPerformed");
+ * ActionEvent evt = (ActionEvent)o;
+ * assertEquals(evt.getSource(), button);
+ * </PRE>
+ * @param theInterface you want the mockObject to implement.
+ * @param id The object id used in the logs so you can differentiate mockobjects
+ * @return The mockObject that can receive events, and JUnit test
+ * cases can call expect event on.
+ */
+ public static MockObject createMock(String id, Class theInterface) {
+ return createMock(new Class[] { theInterface });
+ }
/**
***************
*** 36,40 ****
return createMock(new Class[] { theInterface });
}
!
/**
* Creates a MockObject based on your interface. One way to use it
--- 58,62 ----
return createMock(new Class[] { theInterface });
}
!
/**
* Creates a MockObject based on your interface. One way to use it
***************
*** 57,60 ****
--- 79,105 ----
*/
public static MockObject createMock(Class[] interfaces) {
+ return createMock("(no id specified)", interfaces);
+ }
+
+ /**
+ * Creates a MockObject based on your interface. One way to use it
+ * for mock listeners is below(This can help guarantee order between
+ * two different listeners).<br/><br/>
+ * See examples for other non-listener mock objects.
+ * <PRE>
+ * Class[] c = new Class[] {ActionListener.class, WindowListener.class};
+ * MockSuperclass mockList = MockObjectFactory.createMock(c);
+ * button.addActionListener((ActionListener)mockList);
+ * window.addWindowListener((WindowListener)mockList);
+ * button.setPressed(true);
+ * Object o = mockList.expectEvent("actionPerformed");
+ * ActionEvent evt = (ActionEvent)o;
+ * assertEquals(evt.getSource(), button);
+ * </PRE>
+ * @param interfaces The interfaces you want the mockObject to implement.
+ * @return The mockObject that can receive events, and JUnit test
+ * cases can call expect event on.
+ */
+ public static MockObject createMock(String id, Class[] interfaces) {
Class[] interfacesPlusMock = new Class[interfaces.length+1];
interfacesPlusMock[0] = MockObject.class;
***************
*** 64,71 ****
ClassLoader cl = MockObjectFactory.class.getClassLoader();
! MockObjectImpl impl = new MockObjectImpl(interfaces);
Object o = Proxy.newProxyInstance(cl, interfacesPlusMock, impl);
MockObject m = (MockObject)o;
! return m;
}
--- 109,116 ----
ClassLoader cl = MockObjectFactory.class.getClassLoader();
! MockObjectImpl impl = new MockObjectImpl(id, interfaces);
Object o = Proxy.newProxyInstance(cl, interfacesPlusMock, impl);
MockObject m = (MockObject)o;
! return m;
}
--- NEW FILE: Behavior.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.mock;
/**
* Use MethodBehavior interface instead of Behavior interface
*/
public interface Behavior
{
/**
* @param params
* @return
*/
public Object[] clone(Object[] params);
/**
* @param params
*/
public Object runMethod(Object[] params);
}
Index: ExpectFailedException.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/ExpectFailedException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ExpectFailedException.java 7 May 2005 18:26:17 -0000 1.1
--- ExpectFailedException.java 10 Sep 2006 18:01:12 -0000 1.2
***************
*** 18,21 ****
--- 18,25 ----
/**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+ /**
* Expect failed because an event never came and waiting finally timed out.
*/
***************
*** 25,34 ****
* all your expected methods were called.
*/
! public static final String UNEXPECTED_CALL_AFTER = "Another method that was not expected nor ignored was called after all the expected method calls";
/**
* Expect failed because a method you did not list in expected methods was called
* before or during the other methods you did expect.
*/
! public static final String UNEXPECTED_CALL_BEFORE ="Another method that was not expected nor ignored was called before all the expected method calls were called";
/**
* Expect failed because you expected no methods to be called, but a method was called.
--- 29,40 ----
* all your expected methods were called.
*/
! public static final String UNEXPECTED_CALL_AFTER =
! "Another method that was not expected nor ignored was called after all the expected method calls";
/**
* Expect failed because a method you did not list in expected methods was called
* before or during the other methods you did expect.
*/
! public static final String UNEXPECTED_CALL_BEFORE =
! "Another method that was not expected nor ignored was called before all the expected method calls were called";
/**
* Expect failed because you expected no methods to be called, but a method was called.
Index: Cloner.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/Cloner.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Cloner.java 19 Jun 2005 03:09:57 -0000 1.1
--- Cloner.java 10 Sep 2006 18:01:12 -0000 1.2
***************
*** 14,18 ****
* @return
*/
! public Object clone(Object o);
}
--- 14,18 ----
* @return
*/
! public Object[] clone(Object[] o);
}
Index: CalledMethod.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/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 7 May 2005 18:26:17 -0000 1.1
--- CalledMethod.java 10 Sep 2006 18:01:12 -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,56 ----
return howItWasCalled;
}
! public String toString() {
List paramList = null;
if(params != null)
Index: MockObject.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/MockObject.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockObject.java 24 Jun 2005 12:57:00 -0000 1.3
--- MockObject.java 10 Sep 2006 18:01:12 -0000 1.4
***************
*** 7,14 ****
package biz.xsoftware.mock;
/**
* The interface all mock objects implement. This is the interface used
* by the unit tests once the mock object is created.
! *
* @author Dean Hiller
*/
--- 7,15 ----
package biz.xsoftware.mock;
+
/**
* The interface all mock objects implement. This is the interface used
* by the unit tests once the mock object is created.
! *
* @author Dean Hiller
*/
***************
*** 24,31 ****
*/
public static String ANY = "'Any method'";
!
/**
* Waits for one and only one method to be called. If any methods are
! * called before or after this one(after can sometimes be caught by
* the MockObject when the threading is not synchronous), then
* this call will throw an ExpectFailedException.
--- 25,32 ----
*/
public static String ANY = "'Any method'";
!
/**
* Waits for one and only one method to be called. If any methods are
! * called before or after this one(after can sometimes be caught by
* the MockObject when the threading is not synchronous), then
* this call will throw an ExpectFailedException.
***************
*** 33,39 ****
* @param method The expected method.
* @return An array of params that were passed to the methods called
! */
public CalledMethod expectCall(String method);
!
/**
* Waits for all the methods to be called. If any of the methods
--- 34,66 ----
* @param method The expected method.
* @return An array of params that were passed to the methods called
! *
! * @deprecated please use expect(String method)
! */
public CalledMethod expectCall(String method);
!
! /**
! * Waits for one and only one method to be called. If any methods are
! * called before or after this one(after can sometimes be caught by
! * the MockObject when the threading is not synchronous), then
! * this call will throw an ExpectFailedException.
! *
! * @param method The expected method.
! * @return An array of params that were passed to the methods called
! */
! public CalledMethod expect(String method);
!
! /**
! * Waits for all the methods to be called. If any of the methods
! * are not called or are called out of order, this method throws
! * an exception which will fail the test case. For each method,
! * this will wait WAIT_TIME milliseconds for each method to be called.
! * If the method is not called within this period, an exception will
! * be thrown saying 'method' was not called within timeout period.
! *
! * @param methods The expected method(s) in the correct order.
! * @return An array or arrays of params that were passed to the methods called
! */
! public CalledMethod[] expect(String[] methods);
!
/**
* Waits for all the methods to be called. If any of the methods
***************
*** 42,59 ****
* this will wait WAIT_TIME milliseconds for each method to be called.
* If the method is not called within this period, an exception will
! * be thrown saying 'method' was not called within timeout period.
! *
* @param methods The expected methods in the correct order.
* @return An array or arrays of params that were passed to the methods called
! */
public CalledMethod[] expectOrderedCalls(String[] methods);
!
/**
* Expect many methods to be called irrelevant of the order in which they are
* called.
! *
* @param methods The methods to be called in no particular order
* @return An array of params that were passed to the methods called. This
* array lines up with the methods array passed in.
*/
public CalledMethod[] expectUnorderedCalls(String[] methods);
--- 69,90 ----
* this will wait WAIT_TIME milliseconds for each method to be called.
* If the method is not called within this period, an exception will
! * be thrown saying 'method' was not called within timeout period.
! *
* @param methods The expected methods in the correct order.
* @return An array or arrays of params that were passed to the methods called
! *
! * @deprecated please use expectCall(String ... methods)
! */
public CalledMethod[] expectOrderedCalls(String[] methods);
!
/**
* Expect many methods to be called irrelevant of the order in which they are
* called.
! *
* @param methods The methods to be called in no particular order
* @return An array of params that were passed to the methods called. This
* array lines up with the methods array passed in.
+ *
+ * @deprecated this will go away soon, unless you email me that you depend on it.
*/
public CalledMethod[] expectUnorderedCalls(String[] methods);
***************
*** 75,79 ****
* <li> RuntimeException for Subclasses of MockSuperclass</li>
* </ol>
! *
* @param method The method to throw the exception on when it is called.
* @param e The exception to throw on method.
--- 106,110 ----
* <li> RuntimeException for Subclasses of MockSuperclass</li>
* </ol>
! *
* @param method The method to throw the exception on when it is called.
* @param e The exception to throw on method.
***************
*** 83,87 ****
* Add a return value to return when 'method' is called.
* <br></br>
! * This can be called multiple times and each call will add
* to a queue. When 'method' is called, it will return
* the first value on the queue. If the queue is null,
--- 114,118 ----
* Add a return value to return when 'method' is called.
* <br></br>
! * This can be called multiple times and each call will add
* to a queue. When 'method' is called, it will return
* the first value on the queue. If the queue is null,
***************
*** 90,115 ****
* <br></br>
* Use Integer to return int, Long for long, etc.
! *
* @param method The method that when called returns first value on queue
* @param o The object to return that is added to the queue
*/
public void addReturnValue(String method, Object o);
!
/**
* When calling expectCall, the MockObject will ignore the methods
* in 'methods' variable so if one of the methods in this array is
* called, it will not result in an exception.
*/
public void setIgnoredMethods(String[] methods);
!
public void setCloner(Cloner c);
/**
* Set the DefaultReturnValue for a 'method'
! * @param method The method
*/
public void setDefaultReturnValue(String method, Object o);
!
public void setExpectTimeout(int timeout);
!
! public int getExpectTimeout();
}
--- 121,176 ----
* <br></br>
* Use Integer to return int, Long for long, etc.
! *
* @param method The method that when called returns first value on queue
* @param o The object to return that is added to the queue
*/
public void addReturnValue(String method, Object o);
!
/**
* When calling expectCall, the MockObject will ignore the methods
* in 'methods' variable so if one of the methods in this array is
* called, it will not result in an exception.
+ *
+ * @deprecated please use ignore(String ... methods)
*/
public void setIgnoredMethods(String[] methods);
!
!
! /**
! * When calling expect, the MockObject will ignore this method,
! * so it will not result in an exception.
! */
! public void addIgnore(String method);
!
! /**
! * When calling expect, the MockObject will ignore the methods
! * in 'methods' variable so if one of the methods in this array is
! * called, it will not result in an exception.
! */
! public void addIgnore(String[] methods);
!
!
! /**
! * Removes the method from the ignored methods set.
! */
! public void removeIgnore(String method);
!
! /**
! * Removes the methods from the ignored methods set.
! */
! public void removeIgnore(String[] methods);
!
public void setCloner(Cloner c);
/**
* Set the DefaultReturnValue for a 'method'
! * @param method The method
*/
public void setDefaultReturnValue(String method, Object o);
!
public void setExpectTimeout(int timeout);
!
! public int getExpectTimeout();
!
! public void addBehavior(String string, Behavior behavior);
!
}
Index: MockSuperclass.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/MockSuperclass.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** MockSuperclass.java 26 Jun 2005 11:57:28 -0000 1.6
--- MockSuperclass.java 10 Sep 2006 18:01:12 -0000 1.7
***************
*** 6,15 ****
--- 6,18 ----
import java.util.ArrayList;
import java.util.HashMap;
+ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
[...1034 lines suppressed...]
!
public Class[] getClasses() {
return new Class[] {this.getClass()};
}
!
public void setCloner(Cloner c) {
cloner = c;
}
+
+ public void addBehavior(String method, Behavior behavior) {
+ if(method == null)
+ throw new IllegalArgumentException("method parameter cannot be null");
+ List l = (List) methodToReturnVal.get(method);
+ if(l == null) {
+ l = new ArrayList();
+ methodToReturnVal.put(method, l);
+ }
+ l.add(behavior);
+ }
}
Index: MockObjectImpl.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib/input/javasrc/biz/xsoftware/mock/MockObjectImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockObjectImpl.java 24 Jun 2005 12:57:01 -0000 1.3
--- MockObjectImpl.java 10 Sep 2006 18:01:12 -0000 1.4
***************
*** 11,27 ****
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
!
/**
* @author Dean Hiller
- *
- * To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
*/
class MockObjectImpl extends MockSuperclass implements InvocationHandler {
! private static Map isMethodInSuper = new HashMap();
private Class[] classes;
--- 11,26 ----
import java.lang.reflect.Method;
import java.util.HashMap;
+ import java.util.HashSet;
import java.util.Map;
! import java.util.Set;
/**
* @author Dean Hiller
*/
class MockObjectImpl extends MockSuperclass implements InvocationHandler {
! private static Set isMethodInSuper = new HashSet();
! private static Map primitiveToClass = new HashMap();
private Class[] classes;
***************
*** 31,44 ****
Method[] m = c.getMethods();
for(int i = 0; i < m.length; i++) {
! isMethodInSuper.put(m[i], Boolean.TRUE);
}
c = Object.class;
m = c.getMethods();
for(int i = 0; i < m.length; i++) {
! isMethodInSuper.put(m[i], Boolean.TRUE);
! }
}
! public MockObjectImpl(Class[] interfaces) {
this.classes = interfaces;
}
--- 30,53 ----
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]);
! }
!
! primitiveToClass.put(Integer.TYPE, Integer.class);
! primitiveToClass.put(Double.TYPE, Double.class);
! primitiveToClass.put(Float.TYPE, Float.class);
! primitiveToClass.put(Boolean.TYPE, Boolean.class);
! primitiveToClass.put(Character.TYPE, Character.class);
! primitiveToClass.put(Byte.TYPE, Byte.class);
! primitiveToClass.put(Short.TYPE, Short.class);
! primitiveToClass.put(Long.TYPE, Long.class);
}
! public MockObjectImpl(String id, Class[] interfaces) {
! super(id);
this.classes = interfaces;
}
***************
*** 47,56 ****
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
! Object o = isMethodInSuper.get(method);
! if(o != null)
return callSuperMethod(proxy, method, args);
!
! return methodCalledImpl(method.getName(), args);
}
/**
*
--- 56,164 ----
*/
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);
!
! 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.\n" +
! "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.isPrimitive()) {
! //TODO: this is not working correctly no matter what I do here.....
! Class primitiveClass = (Class) primitiveToClass.get(returnType);
! if(!primitiveClass.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);
! } else if(!returnType.isInstance(o)) //if not a primitive, make sure is assignable....
! 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);
+ // }
+
/**
*
***************
*** 59,63 ****
* @param m This is the method that was invoked
* @param args These are the arguments that were passed to the method
- * @return
* @throws Throwable
*/
--- 167,170 ----
***************
*** 73,78 ****
if(e.getCause() != null)
throw e.getCause();
! else
! throw e;
}
}
--- 180,184 ----
if(e.getCause() != null)
throw e.getCause();
! throw e;
}
}
***************
*** 80,86 ****
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
! public Class[] getClasses() {
return classes;
}
}
--- 186,196 ----
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
! public Class[] getClasses() {
return classes;
}
+ public Object inst() {
+ // TODO Auto-generated method stub
+ return null;
+ }
}
|