You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/07/10 09:41:10 UTC
svn commit: r420454 - in
/incubator/harmony/enhanced/classlib/trunk/modules/beans/src:
main/java/java/beans/Statement.java
test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java
Author: mloenko
Date: Mon Jul 10 00:41:09 2006
New Revision: 420454
URL: http://svn.apache.org/viewvc?rev=420454&view=rev
Log:
fixes for HARMONY-804
[classlib][beans] enabling of tests from StatementTest class (2)
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java?rev=420454&r1=420453&r2=420454&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java Mon Jul 10 00:41:09 2006
@@ -21,14 +21,12 @@
package java.beans;
import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
-
import java.security.AccessController;
import java.security.PrivilegedAction;
-import java.util.StringTokenizer;
/**
* @author Maxim V. Berkultsev
@@ -36,9 +34,11 @@
*/
public class Statement {
-
+
private Object target;
+
private String methodName;
+
private Object[] arguments;
/**
@@ -59,8 +59,8 @@
*/
public String toString() {
StringBuffer sb = new StringBuffer();
- String targetVar =
- target != null ? convertClassName(target.getClass()) : "null";
+ String targetVar = target != null ? convertClassName(target.getClass())
+ : "null";
sb.append(targetVar);
sb.append('.');
@@ -75,15 +75,12 @@
if (arguments[i] == null) {
sb.append("null");
- }
- else if (arguments[i] instanceof String) {
+ } else if (arguments[i] instanceof String) {
sb.append('"');
sb.append(arguments[i].toString());
sb.append('"');
- }
- else {
- sb.append(convertClassName(
- arguments[i].getClass()));
+ } else {
+ sb.append(convertClassName(arguments[i].getClass()));
}
}
}
@@ -119,30 +116,29 @@
public void execute() throws Exception {
invokeMethod();
}
-
+
Object invokeMethod() throws Exception {
Object result = null;
try {
- if(target.getClass().isArray()) {
+ if (target.getClass().isArray()) {
Method method = findArrayMethod();
Object[] ama = getArrayMethodArguments();
result = method.invoke(null, ama);
- } else if (methodName.equals("newInstance") &&
- target instanceof Class &&
- ((Class) target).getName().equals("java.lang.reflect.Array"))
- {
+ } else if (methodName.equals("newInstance")
+ && target instanceof Class
+ && ((Class) target).getName().equals(
+ "java.lang.reflect.Array")) {
Class<?> componentType = (Class) arguments[0];
int length = ((Integer) arguments[1]).intValue();
result = Array.newInstance(componentType, length);
- } else if(methodName.equals("new") ||
- methodName.equals("newInstance"))
- {
- if(target instanceof Class) {
+ } else if (methodName.equals("new")
+ || methodName.equals("newInstance")) {
+ if (target instanceof Class) {
Constructor<?> constructor = findConstructor();
-
+
result = constructor.newInstance(arguments);
} else {
// XXX should be investigated, dead code?
@@ -150,14 +146,27 @@
result = constructor.newInstance(arguments);
}
- } else if(target instanceof Class) {
- Method method = findStaticMethod();
+ } else if (target instanceof Class) {
+ Method method = null;
- result = method.invoke(null, arguments);
+ try {
+ // try to look for static method at first
+ method = findMethod((Class) target, methodName, arguments,
+ true);
+ result = method.invoke(null, arguments);
+ } catch (NoSuchMethodException e) {
+ // static method was not found
+ // try to invoke non-static method of Class object
+ method = findMethod(target.getClass(), methodName,
+ arguments, false);
+ result = method.invoke(target, arguments);
+ }
} else {
- final Method method = findMethod();
-
+ final Method method = findMethod(target.getClass(), methodName,
+ arguments, false);
+
AccessController.doPrivileged(new PrivilegedAction<Object>() {
+
public Object run() {
method.setAccessible(true);
return null;
@@ -173,201 +182,166 @@
}
return result;
}
-
+
private Method findArrayMethod() throws NoSuchMethodException {
- if(!methodName.equals("set") && !methodName.equals("get")) {
+ if (!methodName.equals("set") && !methodName.equals("get")) {
throw new NoSuchMethodException("Unknown method name for array");
- } else if(methodName.equals("get") && (arguments.length != 1) ) {
+ } else if (methodName.equals("get") && (arguments.length != 1)) {
throw new NoSuchMethodException(
"Illegal number of arguments in array getter");
- } else if(methodName.equals("set") && (arguments.length != 2) ) {
+ } else if (methodName.equals("set") && (arguments.length != 2)) {
throw new NoSuchMethodException(
"Illegal number of arguments in array setter");
- } else if(arguments[0].getClass() != Integer.class) {
+ } else if (arguments[0].getClass() != Integer.class) {
throw new NoSuchMethodException(
"First parameter in array getter(setter) is not of "
- + "Integer type");
+ + "Integer type");
}
-
- Class[] argClasses = methodName.equals("get") ?
- new Class[] { Object.class, int.class } :
- new Class[] { Object.class, int.class, Object.class };
- return Array.class.getMethod(methodName, argClasses );
+ Class[] argClasses = methodName.equals("get") ? new Class[] {
+ Object.class, int.class } : new Class[] { Object.class,
+ int.class, Object.class };
+
+ return Array.class.getMethod(methodName, argClasses);
}
-
+
private Object[] getArrayMethodArguments() {
Object[] args = new Object[arguments.length + 1];
args[0] = target;
- for(int i = 0; i < arguments.length; ++i) {
+ for (int i = 0; i < arguments.length; ++i) {
args[i + 1] = arguments[i];
}
return args;
}
-
+
private Constructor<?> findConstructor() throws NoSuchMethodException {
- Class[] argClasses = getClasses();
+ Class[] argClasses = getClasses(arguments);
Class<?> targetClass = (Class) target;
Constructor<?> result = null;
Constructor[] constructors = targetClass.getConstructors();
- for(int i = 0; i < constructors.length; ++i) {
+ for (int i = 0; i < constructors.length; ++i) {
Constructor<?> constructor = constructors[i];
Class<?>[] parameterTypes = constructor.getParameterTypes();
- if(parameterTypes.length == argClasses.length) {
+ if (parameterTypes.length == argClasses.length) {
boolean found = true;
- for(int j = 0; j < parameterTypes.length; ++j) {
+ for (int j = 0; j < parameterTypes.length; ++j) {
boolean argIsNull = argClasses[j] == null;
boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
argClasses[j], parameterTypes[j]);
- boolean paramIsPrimitive =
- parameterTypes[j].isPrimitive();
- boolean paramIsAssignable = argIsNull ? false
- : parameterTypes[j].isAssignableFrom(
- argClasses[j]);
-
- if(!argIsNull && !paramIsAssignable
- && !argIsPrimitiveWrapper
- || argIsNull && paramIsPrimitive) {
- found = false;
- break;
- }
- }
-
- if(found) {
- result = constructor;
- break;
- }
- }
- }
-
- if(result == null) {
- throw new NoSuchMethodException(
- "No constructor for class " + targetClass.getName()
- + " found");
- }
-
- return result;
- }
-
- private Method findStaticMethod() throws NoSuchMethodException {
- Class[] argClasses = getClasses();
- Class<?> targetClass = (Class) target;
-
- Method result = null;
-
- Method[] methods = targetClass.getMethods();
- for(int i = 0; i < methods.length; ++i) {
- Method method = methods[i];
- if(!method.getName().equals(methodName) || !Modifier.isStatic(
- method.getModifiers())) {
- continue;
- }
- Class<?>[] parameterTypes = method.getParameterTypes();
- if(parameterTypes.length == argClasses.length) {
- boolean found = true;
-
- for(int j = 0; j < parameterTypes.length; ++j) {
- boolean argIsNull = (argClasses[j] == null);
- boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
- argClasses[j], parameterTypes[j]);
boolean paramIsPrimitive = parameterTypes[j].isPrimitive();
boolean paramIsAssignable = argIsNull ? false
: parameterTypes[j].isAssignableFrom(argClasses[j]);
-
- if(!argIsNull && !paramIsAssignable
- && !argIsPrimitiveWrapper
- || argIsNull && paramIsPrimitive) {
+
+ if (!argIsNull && !paramIsAssignable
+ && !argIsPrimitiveWrapper || argIsNull
+ && paramIsPrimitive) {
found = false;
break;
}
}
-
- if(found) {
- result = method;
+
+ if (found) {
+ result = constructor;
break;
}
}
}
-
- if(result == null) {
- throw new NoSuchMethodException("No method with name " + methodName
- + " is found");
+
+ if (result == null) {
+ throw new NoSuchMethodException("No constructor for class "
+ + targetClass.getName() + " found");
}
-
+
return result;
}
-
- private Method findMethod() throws NoSuchMethodException {
- Class[] argClasses = getClasses();
- Class<?> targetClass = target.getClass();
-
+
+ /**
+ * Method lookup is performed initially in the current class
+ * then in superclass, then in super class of super class and so on.
+ */
+ private static Method findMethod(Class<?> targetClass, String methodName,
+ Object[] arguments, boolean methodIsStatic)
+ throws NoSuchMethodException {
+ Class[] argClasses = getClasses(arguments);
+
Method result = null;
-
- Method[] methods = targetClass.getMethods();
- for(int i = 0; i < methods.length; ++i) {
+ Method[] methods = targetClass.getDeclaredMethods();
+
+ for (int i = 0; i < methods.length; ++i) {
Method method = methods[i];
-
- if(method.getName().equals(methodName)) {
+ int mods = method.getModifiers();
+
+ if (method.getName().equals(methodName) && Modifier.isPublic(mods)
+ && (methodIsStatic ? Modifier.isStatic(mods) : true)) {
+
Class<?>[] parameterTypes = method.getParameterTypes();
-
- if(parameterTypes.length == argClasses.length) {
+
+ if (parameterTypes.length == argClasses.length) {
boolean found = true;
-
- for(int j = 0; j < parameterTypes.length; ++j) {
- boolean argIsNull = argClasses[j] == null;
- boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
+
+ for (int j = 0; j < parameterTypes.length; ++j) {
+ boolean argIsNull = (argClasses[j] == null);
+ boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
argClasses[j], parameterTypes[j]);
- boolean paramIsPrimitive =
- parameterTypes[j].isPrimitive();
+ boolean paramIsPrimitive = parameterTypes[j]
+ .isPrimitive();
boolean paramIsAssignable = argIsNull ? false
- : parameterTypes[j].isAssignableFrom(
- argClasses[j]);
-
- if(!argIsNull && !paramIsAssignable
- && !argIsPrimitiveWrapper
- || argIsNull && paramIsPrimitive) {
+ : parameterTypes[j]
+ .isAssignableFrom(argClasses[j]);
+
+ if (!argIsNull && !paramIsAssignable
+ && !argIsPrimitiveWrapper || argIsNull
+ && paramIsPrimitive) {
found = false;
break;
}
}
-
- if(found) {
+
+ if (found) {
result = method;
break;
}
}
}
}
-
- if(result == null) {
- throw new NoSuchMethodException("No method with name " + methodName
- + " is found");
+
+ if (result == null) {
+ // let's look for this method in the super class
+ Class<?> parent = targetClass.getSuperclass();
+
+ if (parent != null) {
+ result = findMethod(parent, methodName, arguments,
+ methodIsStatic);
+ } else {
+ throw new NoSuchMethodException("No method with name "
+ + methodName + " is found");
+ }
}
-
+
return result;
}
-
+
private static boolean isPrimitiveWrapper(Class<?> wrapper, Class<?> base) {
- return
- (base == boolean.class) && (wrapper == Boolean.class) ||
- (base == byte.class) && (wrapper == Byte.class) ||
- (base == char.class) && (wrapper == Character.class) ||
- (base == short.class) && (wrapper == Short.class) ||
- (base == int.class) && (wrapper == Integer.class) ||
- (base == long.class) && (wrapper == Long.class) ||
- (base == float.class) && (wrapper == Float.class) ||
- (base == double.class) && (wrapper == Double.class);
+ return (base == boolean.class) && (wrapper == Boolean.class)
+ || (base == byte.class) && (wrapper == Byte.class)
+ || (base == char.class) && (wrapper == Character.class)
+ || (base == short.class) && (wrapper == Short.class)
+ || (base == int.class) && (wrapper == Integer.class)
+ || (base == long.class) && (wrapper == Long.class)
+ || (base == float.class) && (wrapper == Float.class)
+ || (base == double.class) && (wrapper == Double.class);
}
-
+
static String convertClassName(Class<?> type) {
Class<?> componentType = type.getComponentType();
Class<?> resultType = (componentType == null) ? type : componentType;
String result = resultType.getName();
- int k = result.lastIndexOf('.');;
+ int k = result.lastIndexOf('.');
+ ;
if (k != -1 && k < result.length()) {
result = result.substring(k + 1);
@@ -379,37 +353,38 @@
return result;
}
-
- private Class[] getClasses() {
+
+ private static Class[] getClasses(Object[] arguments) {
Class[] result = new Class[arguments.length];
- for(int i = 0; i < arguments.length; ++i) {
+
+ for (int i = 0; i < arguments.length; ++i) {
result[i] = (arguments[i] == null) ? null : arguments[i].getClass();
}
return result;
}
-
+
public boolean equals(Object o) {
- if(o instanceof Statement) {
+ if (o instanceof Statement) {
Statement s = (Statement) o;
-
+
Object[] otherArguments = s.getArguments();
boolean argsEqual = (otherArguments.length == arguments.length);
- if(argsEqual) {
- for(int i = 0; i < arguments.length; ++i) {
- if(otherArguments[i] != arguments[i]) {
+ if (argsEqual) {
+ for (int i = 0; i < arguments.length; ++i) {
+ if (otherArguments[i] != arguments[i]) {
argsEqual = false;
break;
}
}
}
-
- if(!argsEqual) {
+
+ if (!argsEqual) {
return false;
} else {
- return (s.getTarget() == this.getTarget() &&
- s.getMethodName().equals(this.getMethodName()));
+ return (s.getTarget() == this.getTarget() && s.getMethodName()
+ .equals(this.getMethodName()));
}
-
+
} else {
return false;
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java?rev=420454&r1=420453&r2=420454&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java Mon Jul 10 00:41:09 2006
@@ -15,12 +15,10 @@
package org.apache.harmony.beans.tests.java.beans;
-import java.beans.Statement;
import java.beans.DefaultPersistenceDelegate;
+import java.beans.Statement;
import java.util.Arrays;
import java.util.Vector;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -42,14 +40,14 @@
public StatementTest() {
super();
}
-
+
/**
- *
+ *
*/
public StatementTest(String name) {
super(name);
}
-
+
/**
* The test checks the method execute() for setter
*/
@@ -59,47 +57,48 @@
s.execute();
assertEquals("hello", bean.getText());
}
-
+
/**
* The test checks the method execute() for indexed setter
*/
public void testIndexedSetter() throws Exception {
Bean bean = new Bean("hello");
- Statement s = new Statement(bean, "setChar",
- new Object[] { new Integer(1), new Character('a') });
+ Statement s = new Statement(bean, "setChar", new Object[] {
+ new Integer(1), new Character('a') });
s.execute();
assertEquals("hallo", bean.getText());
}
-
+
/**
* The test checks the method execute() for array setter
*/
public void testArraySetter() throws Exception {
- int[] a = {1, 2, 3};
- Statement s = new Statement(a, "set",
- new Object[] { new Integer(1), new Integer(7) });
+ int[] a = { 1, 2, 3 };
+ Statement s = new Statement(a, "set", new Object[] { new Integer(1),
+ new Integer(7) });
s.execute();
assertEquals(7, a[1]);
}
-
+
/**
* The test checks the method execute() for static method
*/
public void testStatic() throws Exception {
int currentId = getTestId();
Statement s = new Statement(StatementTest.class, "nextTestId",
- new Object[] { });
+ new Object[] {});
s.execute();
assertEquals(++currentId, getTestId());
}
-
+
/**
- * The test checks the method execute() if exception is thrown on method call
+ * The test checks the method execute() if exception is thrown on method
+ * call
*/
public void testExceptionThrownOnMethodCall() {
Bean bean = new Bean("hello");
- Statement s = new Statement(bean, "setChar",
- new Object[] { new Integer(5), new Character('a') });
+ Statement s = new Statement(bean, "setChar", new Object[] {
+ new Integer(5), new Character('a') });
try {
s.execute();
@@ -109,14 +108,14 @@
// correct situation
}
}
-
+
/**
- * The test checks the method execute() if exception is thrown on
- * static method call
+ * The test checks the method execute() if exception is thrown on static
+ * method call
*/
public void testExceptionThrownOnStaticMethodCall() throws Exception {
Statement s = new Statement(StatementTest.class, "methodWithException",
- new Object[] {} );
+ new Object[] {});
try {
s.execute();
@@ -125,42 +124,41 @@
// SampleException is thrown as expected
}
}
-
+
/**
- * The test checks the method execute() with array as parameter
+ * The test checks the method execute() with array as parameter
*/
public void testMethodWithArrayParam() throws Exception {
Statement s = new Statement(StatementTest.class, "methodWithIntArray",
- new Object[] { new int[] { 3 } } );
+ new Object[] { new int[] { 3 } });
s.execute();
}
-
+
/**
*
*/
public static int getTestId() {
return testId;
}
-
+
/**
*
*/
public static void nextTestId() {
++testId;
}
-
+
/**
*
*/
public static void methodWithException() throws Exception {
throw new SampleException("sample");
}
-
+
/**
*
*/
- public static void methodWithIntArray(int[] array) {
- }
+ public static void methodWithIntArray(int[] array) {}
/**
*
@@ -168,34 +166,34 @@
public static Test suite() {
return new TestSuite(StatementTest.class);
}
-
+
/**
*
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
-
+
public class Bean {
-
+
private String text;
-
+
public Bean() {
text = null;
}
-
+
public Bean(String text) {
this.text = text;
}
-
+
public String getText() {
return text;
}
-
+
public void setText(String text) {
this.text = text;
}
-
+
public char getChar(int idx) throws IllegalAccessException {
if (text == null) {
throw new IllegalAccessException("Text property is null.");
@@ -203,7 +201,7 @@
return text.charAt(idx);
}
}
-
+
public void setChar(int idx, char value) throws IllegalAccessException {
if (text == null) {
throw new IllegalAccessException("Text property is null.");
@@ -215,832 +213,827 @@
if (idx < 0 || idx >= text.length()) {
throw new IndexOutOfBoundsException();
}
-
+
if (idx > 0) {
sb.append(text.substring(0, idx));
}
sb.append(value);
-
- if(idx < (text.length() - 1)) {
- sb.append(text.substring(idx + 1));
+
+ if (idx < (text.length() - 1)) {
+ sb.append(text.substring(idx + 1));
}
text = sb.toString();
}
}
}
-
-
-
- /*
- * Test the constructor under normal conditions.
- */
- public void testConstructor_Normal() {
- Object arg1 = new Object();
- Object arg2 = "string";
- Object[] oa = new Object[] { arg1, arg2 };
- Statement t = new Statement(arg1, "method", oa);
- assertSame(arg1, t.getTarget());
- assertSame("method", t.getMethodName());
- assertSame(oa, t.getArguments());
- assertSame(arg1, t.getArguments()[0]);
- assertSame(arg2, t.getArguments()[1]);
+
+ /*
+ * Test the constructor under normal conditions.
+ */
+ public void testConstructor_Normal() {
+ Object arg1 = new Object();
+ Object arg2 = "string";
+ Object[] oa = new Object[] { arg1, arg2 };
+ Statement t = new Statement(arg1, "method", oa);
+ assertSame(arg1, t.getTarget());
+ assertSame("method", t.getMethodName());
+ assertSame(oa, t.getArguments());
+ assertSame(arg1, t.getArguments()[0]);
+ assertSame(arg2, t.getArguments()[1]);
System.out.println(t.toString());
- assertEquals(
- "Object.method(Object, \"string\");",
- t.toString());
- }
-
- /*
- * Test the constructor with null target.
- */
- public void testConstructor_NullTarget() {
- Object arg = new Object();
- Object[] oa = new Object[] { arg };
- Statement t = new Statement(null, "method", oa);
- assertSame(null, t.getTarget());
- assertSame("method", t.getMethodName());
- assertSame(oa, t.getArguments());
- assertSame(arg, t.getArguments()[0]);
+ assertEquals("Object.method(Object, \"string\");", t.toString());
+ }
+
+ /*
+ * Test the constructor with null target.
+ */
+ public void testConstructor_NullTarget() {
+ Object arg = new Object();
+ Object[] oa = new Object[] { arg };
+ Statement t = new Statement(null, "method", oa);
+ assertSame(null, t.getTarget());
+ assertSame("method", t.getMethodName());
+ assertSame(oa, t.getArguments());
+ assertSame(arg, t.getArguments()[0]);
assertEquals("null.method(Object);", t.toString());
- }
+ }
- /*
- * Test the constructor with an array target.
- */
- public void testConstructor_ArrayTarget() {
- Object arg = new Object();
- Object[] oa = new Object[] { arg };
+ /*
+ * Test the constructor with an array target.
+ */
+ public void testConstructor_ArrayTarget() {
+ Object arg = new Object();
+ Object[] oa = new Object[] { arg };
Statement t = new Statement(oa, "method", oa);
- assertSame(oa, t.getTarget());
- assertSame("method", t.getMethodName());
- assertSame(oa, t.getArguments());
- assertSame(arg, t.getArguments()[0]);
+ assertSame(oa, t.getTarget());
+ assertSame("method", t.getMethodName());
+ assertSame(oa, t.getArguments());
+ assertSame(arg, t.getArguments()[0]);
assertEquals("ObjectArray.method(Object);", t.toString());
- }
+ }
- /*
- * Test the constructor with null method name.
- */
- public void testConstructor_NullMethodName() {
- Object target = new Object();
- Object[] oa = new Object[] { new Object() };
- Statement t = new Statement(target, null, oa);
- assertSame(target, t.getTarget());
- assertSame(null, t.getMethodName());
- assertSame(oa, t.getArguments());
+ /*
+ * Test the constructor with null method name.
+ */
+ public void testConstructor_NullMethodName() {
+ Object target = new Object();
+ Object[] oa = new Object[] { new Object() };
+ Statement t = new Statement(target, null, oa);
+ assertSame(target, t.getTarget());
+ assertSame(null, t.getMethodName());
+ assertSame(oa, t.getArguments());
assertEquals("Object.null(Object);", t.toString());
- }
+ }
- /*
- * Test the constructor with the method name "new".
- */
- public void testConstructor_NewMethodName() {
- Object target = new Object();
- Object[] oa = new Object[] { new Object() };
- Statement t = new Statement(target, "new", oa);
- assertSame(target, t.getTarget());
- assertSame("new", t.getMethodName());
- assertSame(oa, t.getArguments());
+ /*
+ * Test the constructor with the method name "new".
+ */
+ public void testConstructor_NewMethodName() {
+ Object target = new Object();
+ Object[] oa = new Object[] { new Object() };
+ Statement t = new Statement(target, "new", oa);
+ assertSame(target, t.getTarget());
+ assertSame("new", t.getMethodName());
+ assertSame(oa, t.getArguments());
assertEquals("Object.new(Object);", t.toString());
- }
+ }
- /*
- * Test the constructor with empty method name.
- */
- public void testConstructor_EmptyMethodName() {
- Object target = new Object();
- Object[] oa = new Object[] { new Object() };
- Statement t = new Statement(target, "", oa);
- assertSame(target, t.getTarget());
- assertSame("", t.getMethodName());
- assertSame(oa, t.getArguments());
+ /*
+ * Test the constructor with empty method name.
+ */
+ public void testConstructor_EmptyMethodName() {
+ Object target = new Object();
+ Object[] oa = new Object[] { new Object() };
+ Statement t = new Statement(target, "", oa);
+ assertSame(target, t.getTarget());
+ assertSame("", t.getMethodName());
+ assertSame(oa, t.getArguments());
assertEquals("Object.(Object);", t.toString());
- }
+ }
- /*
- * Test the constructor with null arguments.
- */
- public void testConstructor_NullArguments() {
- Object target = new Object();
- Statement t = new Statement(target, "method", null);
- assertSame(target, t.getTarget());
- assertSame("method", t.getMethodName());
- assertEquals(0, t.getArguments().length);
+ /*
+ * Test the constructor with null arguments.
+ */
+ public void testConstructor_NullArguments() {
+ Object target = new Object();
+ Statement t = new Statement(target, "method", null);
+ assertSame(target, t.getTarget());
+ assertSame("method", t.getMethodName());
+ assertEquals(0, t.getArguments().length);
assertEquals("Object.method();", t.toString());
- }
+ }
- /*
- * Test the constructor with a null argument.
- */
- public void testConstructor_NullArgument() {
- Object target = new Object();
- Object[] oa = new Object[] { null };
- Statement t = new Statement(target, "method", oa);
- assertSame(target, t.getTarget());
- assertSame("method", t.getMethodName());
- assertSame(oa, t.getArguments());
- assertNull(t.getArguments()[0]);
+ /*
+ * Test the constructor with a null argument.
+ */
+ public void testConstructor_NullArgument() {
+ Object target = new Object();
+ Object[] oa = new Object[] { null };
+ Statement t = new Statement(target, "method", oa);
+ assertSame(target, t.getTarget());
+ assertSame("method", t.getMethodName());
+ assertSame(oa, t.getArguments());
+ assertNull(t.getArguments()[0]);
assertEquals("Object.method(null);", t.toString());
- }
+ }
+
+ // public void testGetArguments() {
+ // // Covered in the testcases for the constructor
+ // }
+ //
+ // public void testGetMethodName() {
+ // // Covered in the testcases for the constructor
+ // }
+ //
+ // public void testGetTarget() {
+ // // Covered in the testcases for the constructor
+ // }
+ //
+ // public void testToString() {
+ // // Covered in the testcases for the constructor
+ // }
+
+ /*
+ * Test the method execute() with a normal object, a valid method name and
+ * valid arguments.
+ */
+ public void testExecute_NormalInstanceMethod() throws Exception {
+ MockObject mo = new MockObject(false);
+ Statement t = new Statement(mo, "method", new Object[0]);
+ t.execute();
+ MockObject.assertCalled("method1", new Object[0]);
+ t = new Statement(mo, "method", null);
+ t.execute();
+ MockObject.assertCalled("method1", new Object[0]);
+ }
+
+ /*
+ * Test the method execute() with a normal object, a valid method that
+ * throws an exception and valid arguments.
+ */
+ public void testExecute_ExceptionalMethod() throws Exception {
+ MockObject mo = new MockObject(false);
+ Statement t = new Statement(mo, "method", new Object[] { null, null });
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ MockObject.assertCalled("method4", new Object[] { null, null });
+ }
+
+ /*
+ * Test the method execute() with a normal object and a non-existing method
+ * name.
+ */
+ public void testExecute_NonExistingMethod() throws Exception {
+ MockObject mo = new MockObject(false);
+ Statement t = new Statement(mo, "method_not_existing", new Object[] {
+ null, null });
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a null object.
+ */
+ public void testExecute_NullTarget() throws Exception {
+ Statement t = new Statement(null, "method_not_existing", new Object[] {
+ null, null });
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a null method name.
+ */
+ // public void testExecute_NullMethodName() throws Exception {
+ // MockObject mo = new MockObject(false);
+ // Statement t = new Statement(mo, null, new Object[] { null, null });
+ // try {
+ // t.execute();
+ // fail("Should throw NoSuchMethodException!");
+ // } catch (NoSuchMethodException ex) {
+ // // expected
+ // }
+ // }
+ /*
+ * Test the method execute() with a normal object, a valid method and
+ * invalid arguments (in terms of type, numbers, etc.).
+ */
+ public void testExecute_InvalidArguments() throws Exception {
+ MockObject mo = new MockObject(false);
+ Statement t = new Statement(mo, "method", new Object[] { new Object(),
+ new Object(), new Object() });
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal object, an overloaded method and
+ * valid arguments.
+ */
+ public void testExecute_OverloadedMethods() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new Object() };
+ Statement t = new Statement(mo, "method", arguments);
+ t.execute();
+ MockObject.assertCalled("method2", arguments);
+
+ arguments = new Object[] { "test" };
+ t = new Statement(mo, "method", arguments);
+ t.execute();
+ MockObject.assertCalled("method3", arguments);
+ }
+
+ /*
+ * Test the method execute() with a normal object, an overloaded method and
+ * null arguments. See Java Language Specification (15.11) for reference.
+ */
+ public void testExecute_OverloadedMethodsNull() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { null };
+ Statement t = new Statement(mo, "method", arguments);
+ t.execute();
+ MockObject.assertCalled("method1-2", arguments);
+ }
+
+ /*
+ * Test the method execute() with a normal object, the method name "new" and
+ * valid arguments.
+ */
+ public void testExecute_NormalConstructor() throws Exception {
+ Statement t = new Statement(MockObject.class, "new", new Object[0]);
+ t.execute();
+ MockObject.assertCalled("new0", new Object[0]);
+ t = new Statement(MockObject.class, "new", null);
+ t.execute();
+ MockObject.assertCalled("new0", new Object[0]);
+ }
+
+ /*
+ * Test the method execute() with a normal object, the method name "new"
+ * that throws an exception and valid arguments.
+ */
+ public void testExecute_ExceptionalConstructor() throws Exception {
+ Statement t = new Statement(MockObject.class, "new", new Object[] {
+ null, null });
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ MockObject.assertCalled("new4", new Object[] { null, null });
+ }
+
+ /*
+ * Test the method execute() with a normal object, the method name "new" and
+ * invalid arguments (in terms of type, numbers, etc.).
+ */
+ public void testExecute_NonExistingConstructor() throws Exception {
+ Statement t = new Statement(MockObject.class, "new", new Object[] {
+ null, null, null });
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal object with overloaded
+ * constructors, the method name "new" and valid arguments.
+ */
+ public void testExecute_OverloadedConstructors() throws Exception {
+ Object[] arguments = new Object[] { new Object() };
+ Statement t = new Statement(MockObject.class, "new", arguments);
+ t.execute();
+ MockObject.assertCalled("new2", arguments);
+
+ arguments = new Object[] { "test" };
+ t = new Statement(MockObject.class, "new", arguments);
+ t.execute();
+ // MockObject.assertCalled("new2", arguments);
+
+ arguments = new Object[] { new Integer(1) };
+ t = new Statement(MockObject.class, "new", arguments);
+ t.execute();
+ MockObject.assertCalled("new1-2", arguments);
+ }
+
+ /*
+ * Test the method execute() with a normal object with overloaded
+ * constructors, the method name "new" and null arguments.
+ */
+ public void testExecute_OverloadedConstructorsNull() throws Exception {
+ Object[] arguments = new Object[] { null };
+ Statement t = new Statement(MockObject.class, "new", arguments);
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ // MockObject.assertCalled("new2", arguments);
+ }
+
+ /*
+ * Test the method execute() with the Class object, a static method name and
+ * valid arguments.
+ */
+ public void testExecute_NormalStaticMethodViaClass() throws Exception {
+ Object[] arguments = new Object[] { new Object() };
+ Statement t = new Statement(MockObject.class, "staticMethod", arguments);
+ t.execute();
+ MockObject.assertCalled("staticMethod", arguments);
+ }
+
+ /*
+ * Test the method execute() with an object, a static method name and valid
+ * arguments.
+ */
+ public void testExecute_NormalStaticMethodViaObject() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new Object() };
+ Statement t = new Statement(mo, "staticMethod", arguments);
+ t.execute();
+ MockObject.assertCalled("staticMethod", arguments);
+ }
+
+ /*
+ * Test the method execute() with a Class object of a normal class that has
+ * a method of the same signature as Class.forName(String), a static method
+ * name "forName" and valid argument "string".
+ */
+ public void testExecute_AmbiguousStaticMethod() throws Exception {
+ Object[] arguments = new String[] { "test" };
+ Statement t = new Statement(MockObject.class, "forName", arguments);
+ t.execute();
+ MockObject.assertCalled("forName", arguments);
+
+ t = new Statement(String.class, "forName",
+ new Object[] { "java.lang.String" });
+ t.execute();
+ }
+
+ /*
+ * Test the method execute() with the special method Class.forName().
+ */
+ public void testExecute_ClassForName() throws Exception {
+ Object[] arguments = new String[] { this.getClass().getName() };
+ Statement t = new Statement(Class.class, "forName", arguments);
+ t.execute();
+
+ t = new Statement(String.class, "forName",
+ new Object[] { "java.lang.String" });
+ t.execute();
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "get" and valid and invalid arguments.
+ */
+ public void testExecute_ArrayGet() throws Exception {
+ Object[] array = new Object[] { "test" };
+ Statement t = new Statement(array, "get", new Object[] {
+ new Integer(0), new Object() });
+ t.execute();
+
+ array = new Object[] { "test" };
+ t = new Statement(array, "get", new Object[0]);
+ try {
+ t.execute();
+ fail("Should throw ArrayIndexOutOfBoundsException!");
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "set" and valid arguments.
+ */
+ public void testExecute_ArraySet() throws Exception {
+ Object[] array = new Object[] { "test" };
+ Statement t = new Statement(array, "set", new Object[] {
+ new Integer(0), "test2" });
+ t.execute();
+ assertEquals("test2", array[0]);
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "set" and null index argument.
+ */
+ public void testExecute_ArrayNullIndex() throws Exception {
+ Object[] array = new Object[] { "test" };
+ Statement t = new Statement(array, "set",
+ new Object[] { null, "test2" });
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "set" and invalid arguments.
+ */
+ public void testExecute_ArrayInvalidSet() throws Exception {
+ Object[] array = new Object[] { "test" };
+ Statement t = new Statement(array, "set", new Object[] {
+ new Integer(0), "test2", new Object() });
+ t.execute();
+ assertEquals("test2", array[0]);
+
+ try {
+ t = new Statement(array, "set", new Object[] { "testtest", "test2",
+ new Object() });
+ t.execute();
+ fail("Should throw ClassCastException!");
+ } catch (ClassCastException ex) {
+ // expected
+ }
-// public void testGetArguments() {
-// // Covered in the testcases for the constructor
-// }
-//
-// public void testGetMethodName() {
-// // Covered in the testcases for the constructor
-// }
-//
-// public void testGetTarget() {
-// // Covered in the testcases for the constructor
-// }
-//
-// public void testToString() {
-// // Covered in the testcases for the constructor
-// }
-
- /*
- * Test the method execute() with a normal object, a valid method name and
- * valid arguments.
- */
- public void testExecute_NormalInstanceMethod() throws Exception {
- MockObject mo = new MockObject(false);
- Statement t = new Statement(mo, "method", new Object[0]);
- t.execute();
- MockObject.assertCalled("method1", new Object[0]);
- t = new Statement(mo, "method", null);
- t.execute();
- MockObject.assertCalled("method1", new Object[0]);
- }
-
- /*
- * Test the method execute() with a normal object, a valid method that
- * throws an exception and valid arguments.
- */
- public void testExecute_ExceptionalMethod() throws Exception {
- MockObject mo = new MockObject(false);
- Statement t = new Statement(mo, "method", new Object[] { null, null });
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- MockObject.assertCalled("method4", new Object[] { null, null });
- }
-
- /*
- * Test the method execute() with a normal object and a non-existing method
- * name.
- */
- public void testExecute_NonExistingMethod() throws Exception {
- MockObject mo = new MockObject(false);
- Statement t = new Statement(mo, "method_not_existing", new Object[] {
- null, null });
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a null object.
- */
- public void testExecute_NullTarget() throws Exception {
- Statement t = new Statement(null, "method_not_existing", new Object[] {
- null, null });
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a null method name.
- */
-// public void testExecute_NullMethodName() throws Exception {
-// MockObject mo = new MockObject(false);
-// Statement t = new Statement(mo, null, new Object[] { null, null });
-// try {
-// t.execute();
-// fail("Should throw NoSuchMethodException!");
-// } catch (NoSuchMethodException ex) {
-// // expected
-// }
-// }
-
- /*
- * Test the method execute() with a normal object, a valid method and
- * invalid arguments (in terms of type, numbers, etc.).
- */
- public void testExecute_InvalidArguments() throws Exception {
- MockObject mo = new MockObject(false);
- Statement t = new Statement(mo, "method", new Object[] { new Object(),
- new Object(), new Object() });
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal object, an overloaded method and
- * valid arguments.
- */
- public void testExecute_OverloadedMethods() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new Object() };
- Statement t = new Statement(mo, "method", arguments);
- t.execute();
- MockObject.assertCalled("method2", arguments);
-
- arguments = new Object[] { "test" };
- t = new Statement(mo, "method", arguments);
- t.execute();
- MockObject.assertCalled("method3", arguments);
- }
-
- /*
- * Test the method execute() with a normal object, an overloaded method and
- * null arguments. See Java Language Specification (15.11) for reference.
- */
- public void testExecute_OverloadedMethodsNull() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { null };
- Statement t = new Statement(mo, "method", arguments);
- t.execute();
- MockObject.assertCalled("method1-2", arguments);
- }
-
- /*
- * Test the method execute() with a normal object, the method name "new" and
- * valid arguments.
- */
- public void testExecute_NormalConstructor() throws Exception {
- Statement t = new Statement(MockObject.class, "new", new Object[0]);
- t.execute();
- MockObject.assertCalled("new0", new Object[0]);
- t = new Statement(MockObject.class, "new", null);
- t.execute();
- MockObject.assertCalled("new0", new Object[0]);
- }
-
- /*
- * Test the method execute() with a normal object, the method name "new"
- * that throws an exception and valid arguments.
- */
- public void testExecute_ExceptionalConstructor() throws Exception {
- Statement t = new Statement(MockObject.class, "new", new Object[] {
- null, null });
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- MockObject.assertCalled("new4", new Object[] { null, null });
- }
-
- /*
- * Test the method execute() with a normal object, the method name "new" and
- * invalid arguments (in terms of type, numbers, etc.).
- */
- public void testExecute_NonExistingConstructor() throws Exception {
- Statement t = new Statement(MockObject.class, "new", new Object[] {
- null, null, null });
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal object with overloaded
- * constructors, the method name "new" and valid arguments.
- */
- public void testExecute_OverloadedConstructors() throws Exception {
- Object[] arguments = new Object[] { new Object() };
- Statement t = new Statement(MockObject.class, "new", arguments);
- t.execute();
- MockObject.assertCalled("new2", arguments);
-
- arguments = new Object[] { "test" };
- t = new Statement(MockObject.class, "new", arguments);
- t.execute();
- // MockObject.assertCalled("new2", arguments);
-
- arguments = new Object[] { new Integer(1) };
- t = new Statement(MockObject.class, "new", arguments);
- t.execute();
- MockObject.assertCalled("new1-2", arguments);
- }
-
- /*
- * Test the method execute() with a normal object with overloaded
- * constructors, the method name "new" and null arguments.
- */
- public void testExecute_OverloadedConstructorsNull() throws Exception {
- Object[] arguments = new Object[] { null };
- Statement t = new Statement(MockObject.class, "new", arguments);
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- // MockObject.assertCalled("new2", arguments);
- }
-
- /*
- * Test the method execute() with the Class object, a static method name and
- * valid arguments.
- */
- public void testExecute_NormalStaticMethodViaClass() throws Exception {
- Object[] arguments = new Object[] { new Object() };
- Statement t = new Statement(MockObject.class, "staticMethod", arguments);
- t.execute();
- MockObject.assertCalled("staticMethod", arguments);
- }
-
- /*
- * Test the method execute() with an object, a static method name and valid
- * arguments.
- */
- public void testExecute_NormalStaticMethodViaObject() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new Object() };
- Statement t = new Statement(mo, "staticMethod", arguments);
- t.execute();
- MockObject.assertCalled("staticMethod", arguments);
- }
-
- /*
- * Test the method execute() with a Class object of a normal class that has
- * a method of the same signature as Class.forName(String), a static method
- * name "forName" and valid argument "string".
- */
- public void testExecute_AmbitiousStaticMethod() throws Exception {
- Object[] arguments = new String[] { "test" };
- Statement t = new Statement(MockObject.class, "forName", arguments);
- t.execute();
- MockObject.assertCalled("forName", arguments);
-
- t = new Statement(String.class, "forName",
- new Object[] { "java.lang.String" });
- t.execute();
- }
-
- /*
- * Test the method execute() with the special method Class.forName().
- */
- public void testExecute_ClassForName() throws Exception {
- Object[] arguments = new String[] { this.getClass().getName() };
- Statement t = new Statement(Class.class, "forName", arguments);
- t.execute();
-
- t = new Statement(String.class, "forName",
- new Object[] { "java.lang.String" });
- t.execute();
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "get" and valid and invalid arguments.
- */
- public void testExecute_ArrayGet() throws Exception {
- Object[] array = new Object[] { "test" };
- Statement t = new Statement(array, "get", new Object[] {
- new Integer(0), new Object() });
- t.execute();
-
- array = new Object[] { "test" };
- t = new Statement(array, "get", new Object[0]);
- try {
- t.execute();
- fail("Should throw ArrayIndexOutOfBoundsException!");
- } catch (ArrayIndexOutOfBoundsException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "set" and valid arguments.
- */
- public void testExecute_ArraySet() throws Exception {
- Object[] array = new Object[] { "test" };
- Statement t = new Statement(array, "set", new Object[] {
- new Integer(0), "test2" });
- t.execute();
- assertEquals("test2", array[0]);
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "set" and null index argument.
- */
- public void testExecute_ArrayNullIndex() throws Exception {
- Object[] array = new Object[] { "test" };
- Statement t = new Statement(array, "set",
- new Object[] { null, "test2" });
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "set" and invalid arguments.
- */
- public void testExecute_ArrayInvalidSet() throws Exception {
- Object[] array = new Object[] { "test" };
- Statement t = new Statement(array, "set", new Object[] {
- new Integer(0), "test2", new Object() });
- t.execute();
- assertEquals("test2", array[0]);
-
- try {
- t = new Statement(array, "set", new Object[] { "testtest", "test2",
- new Object() });
- t.execute();
- fail("Should throw ClassCastException!");
- } catch (ClassCastException ex) {
- // expected
- }
-
- t = new Statement(array, "set", new Object[] { new Integer(0) });
- try {
- t.execute();
- fail("Should throw ArrayIndexOutOfBoundsException!");
- } catch (ArrayIndexOutOfBoundsException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "getInt" and invalid arguments.
- */
- public void testExecute_ArrayInvalidSetInt() throws Exception {
- int[] array = new int[] { 1 };
- Statement t = new Statement(array, "getInt",
- new Object[] { new Integer(0) });
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal array object, the method name
- * "gets".
- */
- public void testExecute_ArrayInvalidName() throws Exception {
- Object[] array = new Object[] { "test" };
- Statement t = new Statement(array, "gets", new Object[] {
- new Integer(0), new Object() });
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a normal object with overloaded methods
- * (primitive type VS wrapper class), a valid method name and valid
- * arguments.
- *
- * Note: decided by definition position!
- */
- public void testExecute_PrimitiveVSWrapper() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new Integer(1) };
- Statement t = new Statement(mo, "methodB", arguments);
- t.execute();
- MockObject.assertCalled("methodB1", arguments);
-
- arguments = new Object[] { Boolean.FALSE };
- t = new Statement(mo, "methodB", arguments);
- t.execute();
- MockObject.assertCalled("methodB4", arguments);
- }
-
- /*
- * Test the method execute() with a protected method but within java.beans
- * package.
- */
- public void testExecute_ProtectedMethodWithPackage() throws Exception {
- DefaultPersistenceDelegate dpd = new DefaultPersistenceDelegate();
- Object[] arguments = new Object[] { "test", "test" };
- Statement t = new Statement(dpd, "mutatesTo", arguments);
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException e) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with a method that is applicable via type
- * conversion.
- */
- public void testExecute_ApplicableViaTypeConversion() throws Exception {
- MockObject mo = new MockObject(false);
- // mo.methodB('c');
- Object[] arguments = new Object[] { new Character((char) 1) };
- Statement t = new Statement(mo, "methodB", arguments);
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException e) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with two equal specific methods.
- *
- * Note: decided by definition position!
- */
- public void testExecute_EqualSpecificMethods() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new MockObject(false),
- new MockObject(false) };
- Statement t = new Statement(mo, "equalSpecificMethod", arguments);
- t.execute();
- MockObject.assertCalled("equalSpecificMethod1", arguments);
- }
-
- /*
- * Test the method execute() with two equal specific methods but one
- * declaring thrown exception.
- *
- * Note: decided by definition position!
- */
- public void testExecute_EqualSpecificMethodsException() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new MockObject(false),
- new MockObject(false), new Object() };
- Statement t = new Statement(mo, "equalSpecificMethod", arguments);
- t.execute();
- MockObject.assertCalled("equalSpecificMethod4", arguments);
- }
-
- /*
- * Test the method execute() with int method while providing a null
- * parameter.
- */
- public void testExecute_IntMethodNullParameter() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { null };
- Statement t = new Statement(mo, "intMethod", arguments);
- try {
- t.execute();
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with int array method while providing an
- * Integer array parameter.
- */
- public void testExecute_IntArrayMethod() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new Integer[] { new Integer(1) } };
- Statement t = new Statement(mo, "intArrayMethod", arguments);
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Test the method execute() with Integer array method while providing an
- * int array parameter.
- */
- public void testExecute_IntegerArrayMethod() throws Exception {
- MockObject mo = new MockObject(false);
- Object[] arguments = new Object[] { new int[] { 1 } };
- Statement t = new Statement(mo, "integerArrayMethod", arguments);
- try {
- t.execute();
- fail("Should throw NoSuchMethodException!");
- } catch (NoSuchMethodException ex) {
- // expected
- }
- }
-
- /*
- * Super class of MockObject.
- */
- public static class MockParent {
-
- protected static String calledMethod = null;
-
- protected static Vector receivedArguments = new Vector();
-
- public void method() {
- reset();
- calledMethod = "method1";
- }
-
- protected void method(Boolean o) {
- reset();
- calledMethod = "method1-1";
- receivedArguments.add(o);
- }
-
- public void method(Integer o) {
- reset();
- calledMethod = "method1-2";
- receivedArguments.add(o);
- }
-
- public void method(Object o) {
- reset();
- calledMethod = "method2";
- receivedArguments.add(o);
- }
-
- public void method(String o) {
- reset();
- calledMethod = "method3";
- receivedArguments.add(o);
- }
-
- public void method(Object o, Object o2) {
- reset();
- calledMethod = "method4";
- receivedArguments.add(o);
- receivedArguments.add(o2);
- throw new NullPointerException();
- }
-
- public static void reset() {
- receivedArguments.clear();
- calledMethod = null;
- }
- }
-
- /*
- * Mock object.
- */
- public static class MockObject extends MockParent {
-
- public MockObject() {
- reset();
- calledMethod = "new0";
- }
-
- public MockObject(boolean testingConstructor) {
- reset();
- if (testingConstructor) {
- calledMethod = "new1";
- }
- }
-
- public MockObject(Integer o) {
- reset();
- calledMethod = "new1-2";
- receivedArguments.add(o);
- }
-
- public MockObject(Object o) {
- reset();
- calledMethod = "new2";
- receivedArguments.add(o);
- }
-
- public MockObject(String o) {
- reset();
- calledMethod = "new3";
- receivedArguments.add(o);
- }
-
- public MockObject(Object o, Object o2) {
- reset();
- calledMethod = "new4";
- receivedArguments.add(o);
- receivedArguments.add(o2);
- throw new NullPointerException();
- }
-
- public void intMethod(int i) {
- reset();
- calledMethod = "intMethod";
- receivedArguments.add(new Integer(i));
- }
-
- public void intArrayMethod(int[] ia) {
- reset();
- calledMethod = "intArrayMethod";
- receivedArguments.add(ia);
- }
-
- public void integerArrayMethod(Integer[] ia) {
- reset();
- calledMethod = "integerArrayMethod";
- receivedArguments.add(ia);
- }
-
- public void methodB(Integer i) {
- reset();
- calledMethod = "methodB1";
- receivedArguments.add(i);
- }
-
- public void methodB(int i) {
- reset();
- calledMethod = "methodB2";
- receivedArguments.add(new Integer(i));
- }
-
- public void methodB(boolean b) {
- reset();
- calledMethod = "methodB3";
- receivedArguments.add(new Boolean(b));
- }
-
- public void methodB(Boolean b) {
- reset();
- calledMethod = "methodB4";
- receivedArguments.add(b);
- }
-
- public static void staticMethod(Object o) {
- reset();
- calledMethod = "staticMethod";
- receivedArguments.add(o);
- }
-
- public void equalSpecificMethod(MockObject o, MockParent p) {
- reset();
- calledMethod = "equalSpecificMethod1";
- receivedArguments.add(o);
- receivedArguments.add(p);
- }
-
- public void equalSpecificMethod(MockParent p, MockObject o) {
- reset();
- calledMethod = "equalSpecificMethod2";
- receivedArguments.add(p);
- receivedArguments.add(o);
- }
-
- public void equalSpecificMethod(MockParent p, MockObject o, Object o2)
- throws Exception {
- reset();
- calledMethod = "equalSpecificMethod4";
- receivedArguments.add(p);
- receivedArguments.add(o);
- receivedArguments.add(o2);
- }
-
- public void equalSpecificMethod(MockObject o, MockParent p, Object o2) {
- reset();
- calledMethod = "equalSpecificMethod3";
- receivedArguments.add(o);
- receivedArguments.add(p);
- receivedArguments.add(o2);
- }
-
- public static Class forName(String o) {
- reset();
- calledMethod = "forName";
- receivedArguments.add(o);
- return null;
- }
-
- public static void assertCalled(String methodName, Object[] arguments) {
- assertEquals(methodName, calledMethod);
- assertTrue(Arrays.equals(arguments, receivedArguments.toArray()));
- reset();
- }
-
- public static void assertNotCalled() {
- assertEquals(null, calledMethod);
- assertTrue(receivedArguments.isEmpty());
- }
- }
+ t = new Statement(array, "set", new Object[] { new Integer(0) });
+ try {
+ t.execute();
+ fail("Should throw ArrayIndexOutOfBoundsException!");
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "getInt" and invalid arguments.
+ */
+ public void testExecute_ArrayInvalidSetInt() throws Exception {
+ int[] array = new int[] { 1 };
+ Statement t = new Statement(array, "getInt",
+ new Object[] { new Integer(0) });
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal array object, the method name
+ * "gets".
+ */
+ public void testExecute_ArrayInvalidName() throws Exception {
+ Object[] array = new Object[] { "test" };
+ Statement t = new Statement(array, "gets", new Object[] {
+ new Integer(0), new Object() });
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a normal object with overloaded methods
+ * (primitive type VS wrapper class), a valid method name and valid
+ * arguments.
+ *
+ * Note: decided by definition position!
+ */
+ public void testExecute_PrimitiveVSWrapper() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new Integer(1) };
+ Statement t = new Statement(mo, "methodB", arguments);
+ t.execute();
+ MockObject.assertCalled("methodB1", arguments);
+
+ arguments = new Object[] { Boolean.FALSE };
+ t = new Statement(mo, "methodB", arguments);
+ t.execute();
+ MockObject.assertCalled("methodB4", arguments);
+ }
+
+ /*
+ * Test the method execute() with a protected method but within java.beans
+ * package.
+ */
+ public void testExecute_ProtectedMethodWithPackage() throws Exception {
+ DefaultPersistenceDelegate dpd = new DefaultPersistenceDelegate();
+ Object[] arguments = new Object[] { "test", "test" };
+ Statement t = new Statement(dpd, "mutatesTo", arguments);
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with a method that is applicable via type
+ * conversion.
+ */
+ public void testExecute_ApplicableViaTypeConversion() throws Exception {
+ MockObject mo = new MockObject(false);
+ // mo.methodB('c');
+ Object[] arguments = new Object[] { new Character((char) 1) };
+ Statement t = new Statement(mo, "methodB", arguments);
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with two equal specific methods.
+ *
+ * Note: decided by definition position!
+ */
+ public void testExecute_EqualSpecificMethods() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new MockObject(false),
+ new MockObject(false) };
+ Statement t = new Statement(mo, "equalSpecificMethod", arguments);
+ t.execute();
+ MockObject.assertCalled("equalSpecificMethod1", arguments);
+ }
+
+ /*
+ * Test the method execute() with two equal specific methods but one
+ * declaring thrown exception.
+ *
+ * Note: decided by definition position!
+ */
+ public void testExecute_EqualSpecificMethodsException() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new MockObject(false),
+ new MockObject(false), new Object() };
+ Statement t = new Statement(mo, "equalSpecificMethod", arguments);
+ t.execute();
+ MockObject.assertCalled("equalSpecificMethod4", arguments);
+ }
+
+ /*
+ * Test the method execute() with int method while providing a null
+ * parameter.
+ */
+ public void testExecute_IntMethodNullParameter() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { null };
+ Statement t = new Statement(mo, "intMethod", arguments);
+ try {
+ t.execute();
+ fail("Should throw NullPointerException!");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with int array method while providing an
+ * Integer array parameter.
+ */
+ public void testExecute_IntArrayMethod() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new Integer[] { new Integer(1) } };
+ Statement t = new Statement(mo, "intArrayMethod", arguments);
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Test the method execute() with Integer array method while providing an
+ * int array parameter.
+ */
+ public void testExecute_IntegerArrayMethod() throws Exception {
+ MockObject mo = new MockObject(false);
+ Object[] arguments = new Object[] { new int[] { 1 } };
+ Statement t = new Statement(mo, "integerArrayMethod", arguments);
+ try {
+ t.execute();
+ fail("Should throw NoSuchMethodException!");
+ } catch (NoSuchMethodException ex) {
+ // expected
+ }
+ }
+
+ /*
+ * Super class of MockObject.
+ */
+ public static class MockParent {
+
+ protected static String calledMethod = null;
+
+ protected static Vector receivedArguments = new Vector();
+
+ public void method() {
+ reset();
+ calledMethod = "method1";
+ }
+
+ protected void method(Boolean o) {
+ reset();
+ calledMethod = "method1-1";
+ receivedArguments.add(o);
+ }
+
+ public void method(Integer o) {
+ reset();
+ calledMethod = "method1-2";
+ receivedArguments.add(o);
+ }
+
+ public void method(Object o) {
+ reset();
+ calledMethod = "method2";
+ receivedArguments.add(o);
+ }
+
+ public void method(String o) {
+ reset();
+ calledMethod = "method3";
+ receivedArguments.add(o);
+ }
+
+ public void method(Object o, Object o2) {
+ reset();
+ calledMethod = "method4";
+ receivedArguments.add(o);
+ receivedArguments.add(o2);
+ throw new NullPointerException();
+ }
+
+ public static void reset() {
+ receivedArguments.clear();
+ calledMethod = null;
+ }
+ }
+
+ /*
+ * Mock object.
+ */
+ public static class MockObject extends MockParent {
+
+ public MockObject() {
+ reset();
+ calledMethod = "new0";
+ }
+
+ public MockObject(boolean testingConstructor) {
+ reset();
+ if (testingConstructor) {
+ calledMethod = "new1";
+ }
+ }
+
+ public MockObject(Integer o) {
+ reset();
+ calledMethod = "new1-2";
+ receivedArguments.add(o);
+ }
+
+ public MockObject(Object o) {
+ reset();
+ calledMethod = "new2";
+ receivedArguments.add(o);
+ }
+
+ public MockObject(String o) {
+ reset();
+ calledMethod = "new3";
+ receivedArguments.add(o);
+ }
+
+ public MockObject(Object o, Object o2) {
+ reset();
+ calledMethod = "new4";
+ receivedArguments.add(o);
+ receivedArguments.add(o2);
+ throw new NullPointerException();
+ }
+
+ public void intMethod(int i) {
+ reset();
+ calledMethod = "intMethod";
+ receivedArguments.add(new Integer(i));
+ }
+
+ public void intArrayMethod(int[] ia) {
+ reset();
+ calledMethod = "intArrayMethod";
+ receivedArguments.add(ia);
+ }
+
+ public void integerArrayMethod(Integer[] ia) {
+ reset();
+ calledMethod = "integerArrayMethod";
+ receivedArguments.add(ia);
+ }
+
+ public void methodB(Integer i) {
+ reset();
+ calledMethod = "methodB1";
+ receivedArguments.add(i);
+ }
+
+ public void methodB(int i) {
+ reset();
+ calledMethod = "methodB2";
+ receivedArguments.add(new Integer(i));
+ }
+
+ public void methodB(boolean b) {
+ reset();
+ calledMethod = "methodB3";
+ receivedArguments.add(new Boolean(b));
+ }
+
+ public void methodB(Boolean b) {
+ reset();
+ calledMethod = "methodB4";
+ receivedArguments.add(b);
+ }
+
+ public static void staticMethod(Object o) {
+ reset();
+ calledMethod = "staticMethod";
+ receivedArguments.add(o);
+ }
+
+ public void equalSpecificMethod(MockObject o, MockParent p) {
+ reset();
+ calledMethod = "equalSpecificMethod1";
+ receivedArguments.add(o);
+ receivedArguments.add(p);
+ }
+
+ public void equalSpecificMethod(MockParent p, MockObject o) {
+ reset();
+ calledMethod = "equalSpecificMethod2";
+ receivedArguments.add(p);
+ receivedArguments.add(o);
+ }
+
+ public void equalSpecificMethod(MockParent p, MockObject o, Object o2)
+ throws Exception {
+ reset();
+ calledMethod = "equalSpecificMethod4";
+ receivedArguments.add(p);
+ receivedArguments.add(o);
+ receivedArguments.add(o2);
+ }
+
+ public void equalSpecificMethod(MockObject o, MockParent p, Object o2) {
+ reset();
+ calledMethod = "equalSpecificMethod3";
+ receivedArguments.add(o);
+ receivedArguments.add(p);
+ receivedArguments.add(o2);
+ }
+
+ public static Class forName(String o) {
+ reset();
+ calledMethod = "forName";
+ receivedArguments.add(o);
+ return null;
+ }
+
+ public static void assertCalled(String methodName, Object[] arguments) {
+ assertEquals(methodName, calledMethod);
+ assertTrue(Arrays.equals(arguments, receivedArguments.toArray()));
+ reset();
+ }
+
+ public static void assertNotCalled() {
+ assertEquals(null, calledMethod);
+ assertTrue(receivedArguments.isEmpty());
+ }
+ }
}