Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2015-11-16 10:44:18 -0500
committerBrian Vosburgh2015-11-16 10:44:18 -0500
commit0e48cf1a006be38c71c9477de0c96168affd78e6 (patch)
tree2c69507a281b17def107b15c411631c4c8eed219
parent17f618b600f7e678fbb31f9c773fe5f5ef73acec (diff)
downloadwebtools.dali-0e48cf1a006be38c71c9477de0c96168affd78e6.tar.gz
webtools.dali-0e48cf1a006be38c71c9477de0c96168affd78e6.tar.xz
webtools.dali-0e48cf1a006be38c71c9477de0c96168affd78e6.zip
improve ClassTools test coverage
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ClassTools.java29
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ClassToolsTests.java632
2 files changed, 624 insertions, 37 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ClassTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ClassTools.java
index e5c3779840..e5dfc32dd5 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ClassTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ClassTools.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2013 Oracle. All rights reserved.
+ * Copyright (c) 2009, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -175,7 +175,7 @@ public final class ClassTools {
if (javaClass == null) {
return IterableTools.emptyIterable();
}
- ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
+ ArrayList<Class<?>> classes = new ArrayList<>();
do {
classes.add(javaClass);
javaClass = javaClass.getSuperclass();
@@ -192,7 +192,7 @@ public final class ClassTools {
* @see Class#getInterfaces()
*/
public static Iterable<Class<?>> allInterfaces(Class<?> javaClass) {
- ArrayList<Class<?>> interfaces = new ArrayList<Class<?>>();
+ ArrayList<Class<?>> interfaces = new ArrayList<>();
for (Class<?> tempClass = javaClass; tempClass != null; tempClass = tempClass.getSuperclass()) {
CollectionTools.<Class<?>>addAll(interfaces, tempClass.getInterfaces());
}
@@ -200,7 +200,7 @@ public final class ClassTools {
}
- // ********** fields **********
+ // ********** static field values **********
/**
* Return the value of the specified class's static field with the specified
@@ -251,6 +251,9 @@ public final class ClassTools {
field_(javaClass, fieldName).set(null, value);
}
+
+ // ********** fields **********
+
/**
* Return the specified class's field with the specified name.
* If the class does not directly
@@ -293,7 +296,7 @@ public final class ClassTools {
* @see Class#getDeclaredFields()
*/
public static Iterable<Field> allFields(Class<?> javaClass) {
- ArrayList<Field> fields = new ArrayList<Field>();
+ ArrayList<Field> fields = new ArrayList<>();
for (Class<?> tempClass = javaClass; tempClass != null; tempClass = tempClass.getSuperclass()) {
CollectionTools.addAll(fields, declaredFields(tempClass));
}
@@ -310,7 +313,7 @@ public final class ClassTools {
}
- // ********** methods **********
+ // ********** static method execution **********
/**
* Execute the specified zero-argument static method.
@@ -374,6 +377,9 @@ public final class ClassTools {
return staticMethod_(javaClass, methodName, parameterTypes).invoke(null, arguments);
}
+
+ // ********** methods **********
+
/**
* Return the specified class's zero-argument method with the specified
* name. If the class does not directly
@@ -518,7 +524,7 @@ public final class ClassTools {
* Make any private/package/protected methods accessible.
*/
public static Iterable<Method> allMethods(Class<?> javaClass) {
- ArrayList<Method> methods = new ArrayList<Method>();
+ ArrayList<Method> methods = new ArrayList<>();
for (Class<?> tempClass = javaClass; tempClass != null; tempClass = tempClass.getSuperclass()) {
CollectionTools.addAll(methods, declaredMethods(tempClass));
}
@@ -1160,7 +1166,7 @@ public final class ClassTools {
*/
public static String buildMethodSignature(String methodName, Class<?>[] parameterTypes) {
StringBuilder sb = new StringBuilder(200);
- appendMethodSignature(sb, methodName, parameterTypes);
+ appendMethodSignature_(sb, methodName, parameterTypes);
return sb.toString();
}
@@ -1168,6 +1174,13 @@ public final class ClassTools {
// method name is null for constructors
if (methodName != null) {
sb.append('.');
+ }
+ appendMethodSignature_(sb, methodName, parameterTypes);
+ }
+
+ private static void appendMethodSignature_(StringBuilder sb, String methodName, Class<?>[] parameterTypes) {
+ // method name is null for constructors
+ if (methodName != null) {
sb.append(methodName);
}
sb.append('(');
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ClassToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ClassToolsTests.java
index 55f6f50bd7..ee10a22fd4 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ClassToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ClassToolsTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2013 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -9,24 +9,26 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;
-import junit.framework.TestCase;
+import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.common.utility.internal.ClassTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.command.CommandAdapter;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterable.TransformationIterable;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
import org.eclipse.jpt.common.utility.transformer.Transformer;
+import junit.framework.TestCase;
@SuppressWarnings("nls")
public class ClassToolsTests
extends TestCase
{
- private static String testStaticField;
-
public ClassToolsTests(String name) {
super(name);
}
@@ -55,6 +57,168 @@ public class ClassToolsTests
// return compilerDependentClassNameFor(className);
// }
+ public void testForNameString() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName(className);
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForNameString_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName(className);
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForNameCharArray() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName(className.toCharArray());
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForNameCharArray_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName(className.toCharArray());
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForName_CharArray() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName_(className.toCharArray());
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForName_CharArray_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName_(className.toCharArray());
+ fail("bogus: " + clazz);
+ } catch (ClassNotFoundException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForNameStringBooleanClassLoader() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName(className, true, this.getClass().getClassLoader());
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForNameStringBooleanClassLoader_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName(className, true, this.getClass().getClassLoader());
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForNameCharArrayBooleanClassLoader() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName(className.toCharArray(), true, this.getClass().getClassLoader());
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForNameCharArrayBooleanClassLoader_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName(className.toCharArray(), true, this.getClass().getClassLoader());
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForName_CharArrayBooleanClassLoader() throws Exception {
+ String className = "java.lang.String";
+ Class<?> clazz = ClassTools.forName_(className.toCharArray(), true, this.getClass().getClassLoader());
+ assertNotNull(clazz);
+ assertEquals(className, clazz.getName());
+ String s = (String) clazz.newInstance();
+ assertNotNull(s);
+ }
+
+ public void testForName_CharArrayBooleanClassLoader_exception() throws Exception {
+ String className = "java.lang.XXXX";
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forName_(className.toCharArray(), true, this.getClass().getClassLoader());
+ fail("bogus: " + clazz);
+ } catch (ClassNotFoundException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testToStringName() {
+ assertEquals(this.getClass().getSimpleName(), ClassTools.toStringName(this.getClass()));
+ }
+
+ public void testToStringName_anonymous() {
+ Command command = new CommandAdapter() {
+ @Override
+ public void execute() {
+ // NOP
+ }
+ };
+ assertEquals("CommandAdapter", ClassTools.toStringName(command.getClass()));
+ }
+
+ public void testToStringName_member() {
+ Command command = new LocalCommand();
+ assertEquals(this.getClass().getSimpleName() + ".LocalCommand", ClassTools.toStringName(command.getClass()));
+ }
+
+ public static class LocalCommand
+ extends CommandAdapter
+ {
+ @Override
+ public void execute() {
+ // NOP
+ }
+ }
+
public void testAllSuperclasses() {
Iterator<Class<?>> superclasses = ClassTools.allSuperclasses(java.util.Vector.class).iterator();
assertEquals(superclasses.next(), java.util.AbstractList.class);
@@ -87,6 +251,64 @@ public class ClassToolsTests
assertFalse(interfaces.hasNext());
}
+ public void testGet() {
+ assertEquals(TEST_STATIC_FINAL_FIELD, ClassTools.get(this.getClass(), "TEST_STATIC_FINAL_FIELD"));
+ }
+ public static final String TEST_STATIC_FINAL_FIELD = "XXXX value";
+
+ public void testGet_exception() {
+ boolean exCaught = false;
+ try {
+ Object value = ClassTools.get(this.getClass(), "BOGUS");
+ fail("bogus: " + value);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchFieldException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSet() {
+ ClassTools.set(this.getClass(), "testStaticField", "new value");
+ assertEquals(testStaticField, "new value");
+ }
+ private static String testStaticField;
+
+ public void testSet_exception() {
+ boolean exCaught = false;
+ try {
+ ClassTools.set(this.getClass(), "bogusStaticField", "new value");
+ fail();
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchFieldException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testField() {
+ assertNotNull(ClassTools.field(this.getClass(), "testStaticField"));
+ }
+
+ public void testField_superclass() {
+ assertNotNull(ClassTools.field(this.getClass(), "fName"));
+ }
+
+ public void testField_exception() {
+ boolean exCaught = false;
+ try {
+ Field field = ClassTools.field(this.getClass(), "BOGUS");
+ fail("bogus: " + field);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchFieldException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
public void testAllFields() {
int fieldCount = 0;
fieldCount += java.util.Vector.class.getDeclaredFields().length;
@@ -103,6 +325,172 @@ public class ClassToolsTests
assertTrue(fields.iterator().next().isAccessible());
}
+ public void testExecute() {
+ assertEquals(UNINITIALIZED, executeStaticMethod_field);
+ assertNull(ClassTools.execute(this.getClass(), "executeStaticMethod"));
+ assertEquals(TOUCHED, executeStaticMethod_field);
+ }
+
+ public static void executeStaticMethod() {
+ executeStaticMethod_field = TOUCHED;
+ }
+ private static final String UNINITIALIZED = "uninitialized";
+ private static final String TOUCHED = "touched";
+ private static String executeStaticMethod_field = UNINITIALIZED;
+
+ public void testExecute_() throws Exception {
+ assertEquals(UNINITIALIZED_, executeStaticMethod_field_);
+ assertNull(ClassTools.execute_(this.getClass(), "executeStaticMethod_"));
+ assertEquals(TOUCHED_, executeStaticMethod_field_);
+ }
+ public static void executeStaticMethod_() {
+ executeStaticMethod_field_ = TOUCHED_;
+ }
+ private static final String UNINITIALIZED_ = "uninitialized_";
+ private static final String TOUCHED_ = "touched_";
+ private static String executeStaticMethod_field_ = UNINITIALIZED_;
+
+ public void testExecute_returnValue() {
+ assertEquals(STATIC_RETURN_VALUE, ClassTools.execute(this.getClass(), "executeStaticMethod_returnValue"));
+ }
+
+ public static String executeStaticMethod_returnValue() {
+ return STATIC_RETURN_VALUE;
+ }
+ private static final String STATIC_RETURN_VALUE = "foo";
+
+ public void testExecute_noSuchMethod() {
+ boolean exCaught = false;
+ try {
+ Object value = ClassTools.execute(this.getClass(), "executeStaticMethod_BOGUS");
+ fail("bogus: " + value);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchMethodException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testExecute_exception() {
+ boolean exCaught = false;
+ try {
+ Object value = ClassTools.execute(this.getClass(), "executeStaticMethod_exception");
+ fail("bogus: " + value);
+ } catch (RuntimeException ex1) {
+ Throwable cause1 = ex1.getCause();
+ if (cause1 instanceof InvocationTargetException) {
+ Throwable cause2 = ((InvocationTargetException) cause1).getCause();
+ if (cause2 instanceof NullPointerException) {
+ if (cause2.getMessage().equals("class tools test")) {
+ exCaught = true;
+ }
+ }
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public static String executeStaticMethod_exception() {
+ throw new NullPointerException("class tools test");
+ }
+
+ public void testExecuteWithParm() {
+ assertEquals(UNINITIALIZED, executeStaticMethodWithParm_field);
+ ClassTools.execute(this.getClass(), "executeStaticMethodWithParm", String.class, TOUCHED);
+ assertEquals(TOUCHED, executeStaticMethodWithParm_field);
+ }
+
+ public static void executeStaticMethodWithParm(String value) {
+ executeStaticMethodWithParm_field = value;
+ }
+ private static String executeStaticMethodWithParm_field = UNINITIALIZED;
+
+ public void testExecuteWithParm_() throws Exception {
+ assertEquals(UNINITIALIZED_, executeStaticMethodWithParm_field_);
+ ClassTools.execute_(this.getClass(), "executeStaticMethodWithParm_", String.class, TOUCHED_);
+ assertEquals(TOUCHED_, executeStaticMethodWithParm_field_);
+ }
+
+ public static void executeStaticMethodWithParm_(String value) {
+ executeStaticMethodWithParm_field_ = value;
+ }
+ private static String executeStaticMethodWithParm_field_ = UNINITIALIZED_;
+
+ public void testMethod() {
+ assertNotNull(ClassTools.method(this.getClass(), "testMethod"));
+ }
+
+ public void testMethod_superclass() {
+ assertNotNull(ClassTools.method(this.getClass(), "run"));
+ }
+
+ public void testMethod_() throws Exception {
+ assertNotNull(ClassTools.method_(this.getClass(), "testMethod_"));
+ }
+
+ public void testMethodWithParm() {
+ assertNotNull(ClassTools.method(this.getClass(), "methodWithParm", String.class));
+ }
+
+ public void testMethodWithParm_() throws Exception {
+ assertNotNull(ClassTools.method_(this.getClass(), "methodWithParm", String.class));
+ }
+
+ public void methodWithParm(String parm) {
+ assertNotNull(parm);
+ }
+
+ public void testMethod_noSuchMethod() {
+ boolean exCaught = false;
+ try {
+ Method method = ClassTools.method(this.getClass(), "executeStaticMethod_BOGUS");
+ fail("bogus: " + method);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchMethodException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testStaticMethod() {
+ assertNotNull(ClassTools.staticMethod(this.getClass(), "staticTestMethod"));
+ }
+
+ public void testStaticMethod_() throws Exception {
+ assertNotNull(ClassTools.staticMethod_(this.getClass(), "staticTestMethod"));
+ }
+
+ public static void staticTestMethod() {
+ // NOP
+ }
+
+ public void testStaticMethodWithParm() {
+ assertNotNull(ClassTools.staticMethod(this.getClass(), "staticTestMethodWithParm", String.class));
+ }
+
+ public void testStaticMethodWithParm_() throws Exception {
+ assertNotNull(ClassTools.staticMethod_(this.getClass(), "staticTestMethodWithParm", String.class));
+ }
+
+ public static void staticTestMethodWithParm(String string) {
+ assertNotNull(string);
+ }
+
+ public void testStaticMethod_noSuchMethod() {
+ boolean exCaught = false;
+ try {
+ Method method = ClassTools.staticMethod(this.getClass(), "executeStaticMethod_BOGUS");
+ fail("bogus: " + method);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchMethodException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
public void testAllMethods() {
int methodCount = 0;
methodCount += java.util.Vector.class.getDeclaredMethods().length;
@@ -122,6 +510,12 @@ public class ClassToolsTests
assertEquals(0, v.size());
}
+ public void testNewInstanceClass_() throws Exception {
+ Vector<?> v = ClassTools.newInstance_(java.util.Vector.class);
+ assertNotNull(v);
+ assertEquals(0, v.size());
+ }
+
public void testNewInstanceClassClassObject() {
int initialCapacity = 200;
Vector<?> v = ClassTools.newInstance(java.util.Vector.class, int.class, new Integer(initialCapacity));
@@ -131,6 +525,15 @@ public class ClassToolsTests
assertEquals(initialCapacity, elementData.length);
}
+ public void testNewInstanceClassClassObject_() throws Exception {
+ int initialCapacity = 200;
+ Vector<?> v = ClassTools.newInstance_(java.util.Vector.class, int.class, new Integer(initialCapacity));
+ assertNotNull(v);
+ assertEquals(0, v.size());
+ Object[] elementData = (Object[]) ObjectTools.get(v, "elementData");
+ assertEquals(initialCapacity, elementData.length);
+ }
+
public void testNewInstanceClassClassArrayObjectArray() {
int initialCapacity = 200;
Class<?>[] parmTypes = new Class[1];
@@ -142,11 +545,17 @@ public class ClassToolsTests
assertEquals(0, v.size());
Object[] elementData = (Object[]) ObjectTools.get(v, "elementData");
assertEquals(initialCapacity, elementData.length);
+ }
+ public void testNewInstanceClassClassArrayObjectArray_exception() {
+ Class<?>[] parmTypes = new Class[1];
+ parmTypes[0] = int.class;
+ Object[] args = new Object[1];
args[0] = new Integer(-1);
boolean exCaught = false;
try {
- v = ClassTools.newInstance(java.util.Vector.class, parmTypes, args);
+ Vector<?> v = ClassTools.newInstance(java.util.Vector.class, parmTypes, args);
+ fail("bogus: " + v);
} catch (RuntimeException ex) {
exCaught = true;
}
@@ -156,7 +565,8 @@ public class ClassToolsTests
args[0] = "foo";
exCaught = false;
try {
- v = ClassTools.newInstance(java.util.Vector.class, parmTypes, args);
+ Vector<?> v = ClassTools.newInstance(java.util.Vector.class, parmTypes, args);
+ fail("bogus: " + v);
} catch (RuntimeException ex) {
if (ex.getCause() instanceof NoSuchMethodException) {
exCaught = true;
@@ -165,6 +575,43 @@ public class ClassToolsTests
assertTrue("NoSuchMethodException not thrown", exCaught);
}
+ public void testDefaultConstructor() {
+ assertNotNull(ClassTools.defaultConstructor(java.util.Vector.class));
+ }
+
+ public void testDefaultConstructor_() throws Exception {
+ assertNotNull(ClassTools.defaultConstructor_(java.util.Vector.class));
+ }
+
+ public void testConstructor() {
+ assertNotNull(ClassTools.constructor(java.util.Vector.class));
+ }
+
+ public void testConstructor_() throws Exception {
+ assertNotNull(ClassTools.constructor_(java.util.Vector.class));
+ }
+
+ public void testConstructorClass() {
+ assertNotNull(ClassTools.constructor(java.util.Vector.class, int.class));
+ }
+
+ public void testConstructorClass_() throws Exception {
+ assertNotNull(ClassTools.constructor_(java.util.Vector.class, int.class));
+ }
+
+ public void testConstructor_noSuchMethod() {
+ boolean exCaught = false;
+ try {
+ Constructor<?> ctor = ClassTools.constructor(this.getClass());
+ fail("bogus: " + ctor);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchMethodException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
public void testExecuteClassString() {
Double randomObject = (Double) ClassTools.execute(java.lang.Math.class, "random");
assertNotNull(randomObject);
@@ -211,21 +658,6 @@ public class ClassToolsTests
assertTrue("NoSuchMethodException not thrown: " + bogusStaticMethodReturnValue, exCaught);
}
- public void testSet() {
- ClassTools.set(this.getClass(), "testStaticField", "new value");
- assertEquals(testStaticField, "new value");
-
- boolean exCaught = false;
- try {
- ClassTools.set(this.getClass(), "bogusStaticField", "new value");
- } catch (RuntimeException ex) {
- if (ex.getCause() instanceof NoSuchFieldException) {
- exCaught = true;
- }
- }
- assertTrue("NoSuchFieldException not thrown", exCaught);
- }
-
public void testSimpleName() {
assertEquals("Vector", java.util.Vector.class.getSimpleName());
assertEquals("Entry", java.util.Map.Entry.class.getSimpleName());
@@ -266,7 +698,9 @@ public class ClassToolsTests
assertTrue(ClassTools.isPrimitiveWrapper(java.lang.Integer.class));
assertTrue(ClassTools.isPrimitiveWrapper(java.lang.Float.class));
- assertFalse(ClassTools.isPrimitiveWrapper(java.lang.String.class));
+ assertFalse(ClassTools.isPrimitiveWrapper((new int[0]).getClass()));
+ assertFalse(ClassTools.isPrimitiveWrapper(this.getClass()));
+ assertFalse(ClassTools.isPrimitiveWrapper((new java.lang.String[0]).getClass()));
assertFalse(ClassTools.isPrimitiveWrapper(void.class));
assertFalse(ClassTools.isPrimitiveWrapper(int.class));
}
@@ -283,6 +717,18 @@ public class ClassToolsTests
assertFalse(ClassTools.isVariablePrimitiveWrapper(int.class));
}
+ public void testIsVariablePrimitive() {
+ assertTrue(ClassTools.isVariablePrimitive(boolean.class));
+ assertTrue(ClassTools.isVariablePrimitive(int.class));
+ assertTrue(ClassTools.isVariablePrimitive(float.class));
+
+ assertFalse(ClassTools.isVariablePrimitive(void.class));
+ assertFalse(ClassTools.isVariablePrimitive(java.lang.Void.class));
+ assertFalse(ClassTools.isVariablePrimitive(java.lang.String.class));
+ assertFalse(ClassTools.isVariablePrimitive(java.lang.Boolean.class));
+ assertFalse(ClassTools.isVariablePrimitive(java.lang.Integer.class));
+ }
+
public void testPrimitiveWrapper() {
assertEquals(java.lang.Void.class, ClassTools.primitiveWrapper(void.class));
assertEquals(java.lang.Integer.class, ClassTools.primitiveWrapper(int.class));
@@ -292,6 +738,85 @@ public class ClassToolsTests
assertNull(ClassTools.primitiveWrapper(java.lang.String.class));
}
+ public void testPrimitiveForCode_char() {
+ assertEquals(byte.class, ClassTools.primitiveForCode('B'));
+ assertEquals(int.class, ClassTools.primitiveForCode('I'));
+ assertEquals(void.class, ClassTools.primitiveForCode('V'));
+ assertNull(ClassTools.primitiveForCode('X'));
+ }
+
+ public void testPrimitiveForCode_int() {
+ assertEquals(byte.class, ClassTools.primitiveForCode((int) 'B'));
+ assertEquals(int.class, ClassTools.primitiveForCode((int) 'I'));
+ assertEquals(void.class, ClassTools.primitiveForCode((int) 'V'));
+ assertNull(ClassTools.primitiveForCode((int) 'X'));
+ }
+
+ public void testPrimitiveCode() {
+ assertEquals('I', ClassTools.primitiveCode(int.class));
+ assertEquals('B', ClassTools.primitiveCode(byte.class));
+ assertEquals(0, ClassTools.primitiveCode((new byte[0]).getClass()));
+ assertEquals(0, ClassTools.primitiveCode(this.getClass()));
+ }
+
+ public void testForTypeDeclarationString() {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration("java.lang.String"));
+ }
+
+ public void testForTypeDeclarationString_exception() {
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forTypeDeclaration("java.lang.BOGUS");
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForTypeDeclarationCharArray() {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration("java.lang.String".toCharArray()));
+ }
+
+ public void testForTypeDeclarationCharArray_exception() {
+ boolean exCaught = false;
+ try {
+ Class<?> clazz = ClassTools.forTypeDeclaration("java.lang.BOGUS".toCharArray());
+ fail("bogus: " + clazz);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForTypeDeclarationString_() throws Exception {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration_("java.lang.String"));
+ }
+
+ public void testForTypeDeclarationCharArray_() throws Exception {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration_("java.lang.String".toCharArray()));
+ }
+
+ public void testForTypeDeclarationStringClassLoader() {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration("java.lang.String", this.getClass().getClassLoader()));
+ }
+
+ public void testForTypeDeclarationCharArrayClassLoader() {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration("java.lang.String".toCharArray(), this.getClass().getClassLoader()));
+ }
+
+ public void testForTypeDeclarationStringClassLoader_() throws Exception {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration_("java.lang.String", this.getClass().getClassLoader()));
+ }
+
+ public void testForTypeDeclarationCharArrayClassLoader_() throws Exception {
+ assertEquals(java.lang.String.class, ClassTools.forTypeDeclaration_("java.lang.String".toCharArray(), this.getClass().getClassLoader()));
+ }
+
public void testForTypeDeclarationStringInt() throws Exception {
assertEquals(int.class, ClassTools.forTypeDeclaration("int", 0));
assertEquals(int[].class, ClassTools.forTypeDeclaration("int", 1));
@@ -299,27 +824,61 @@ public class ClassToolsTests
assertEquals(Object.class, ClassTools.forTypeDeclaration("java.lang.Object", 0));
assertEquals(Object[][][].class, ClassTools.forTypeDeclaration("java.lang.Object", 3));
+ }
- assertEquals(void.class, ClassTools.forTypeDeclaration("void", 0));
+ public void testForTypeDeclarationStringInt_exception() throws Exception {
+ boolean exCaught = false;
try {
ClassTools.forTypeDeclaration(void.class.getName(), 1);
fail("should not get here...");
} catch (RuntimeException ex) {
- // expected
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
}
+ assertTrue(exCaught);
}
- public void testPrimitiveCode() {
- assertEquals('I', ClassTools.primitiveCode(int.class));
- assertEquals('B', ClassTools.primitiveCode(byte.class));
+ public void testForTypeDeclarationCharArrayInt() throws Exception {
+ assertEquals(int.class, ClassTools.forTypeDeclaration("int".toCharArray(), 0));
+ assertEquals(int[].class, ClassTools.forTypeDeclaration("int".toCharArray(), 1));
+ assertEquals(int[][][].class, ClassTools.forTypeDeclaration("int".toCharArray(), 3));
+
+ assertEquals(Object.class, ClassTools.forTypeDeclaration("java.lang.Object".toCharArray(), 0));
+ assertEquals(Object[][][].class, ClassTools.forTypeDeclaration("java.lang.Object".toCharArray(), 3));
+ }
+
+ public void testForTypeDeclarationCharArrayInt_exception() throws Exception {
+ boolean exCaught = false;
+ try {
+ ClassTools.forTypeDeclaration(void.class.getName().toCharArray(), 1);
+ fail("should not get here...");
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof ClassNotFoundException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testForTypeDeclarationStringInt_() throws Exception {
+ assertEquals((new java.lang.String[0][0]).getClass(), ClassTools.forTypeDeclaration_("java.lang.String", 2));
+ }
+
+ public void testForTypeDeclarationCharArrayInt_() throws Exception {
+ assertEquals((new java.lang.String[0][0]).getClass(), ClassTools.forTypeDeclaration_("java.lang.String".toCharArray(), 2));
+ }
+
+ public void testBuildMethodSignature() {
+ assertEquals("foo(java.lang.String)", ClassTools.buildMethodSignature("foo", new Class<?>[] {java.lang.String.class}));
}
private Iterable<String> fieldNames(Iterable<Field> fields) {
- return new TransformationIterable<Field, String>(fields, FIELD_NAME_TRANSFORMER);
+ return new TransformationIterable<>(fields, FIELD_NAME_TRANSFORMER);
}
private Iterable<String> methodNames(Iterable<Method> methods) {
- return new TransformationIterable<Method, String>(methods, METHOD_NAME_TRANSFORMER);
+ return new TransformationIterable<>(methods, METHOD_NAME_TRANSFORMER);
}
private static final Transformer<Field, String> FIELD_NAME_TRANSFORMER = new FieldNameTransformer();
@@ -341,4 +900,19 @@ public class ClassToolsTests
return method.getName();
}
}
+
+ public void testClassToolsConstructor() {
+ boolean exCaught = false;
+ try {
+ Object at = ClassTools.newInstance(ClassTools.class);
+ fail("bogus: " + at);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof InvocationTargetException) {
+ if (ex.getCause().getCause() instanceof UnsupportedOperationException) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue(exCaught);
+ }
}

Back to the top