Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2015-11-16 15:39:49 +0000
committerBrian Vosburgh2015-11-16 15:39:49 +0000
commit9593579176fc5188682deceea39b899496efe5c3 (patch)
tree7b6e5ebe79aed238918abec2eb8ebe259723836d
parent554e62225689a82a3e5850d778d55fbe71fd93cd (diff)
downloadwebtools.dali-9593579176fc5188682deceea39b899496efe5c3.tar.gz
webtools.dali-9593579176fc5188682deceea39b899496efe5c3.tar.xz
webtools.dali-9593579176fc5188682deceea39b899496efe5c3.zip
improve ObjectTools test coverage
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java14
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ObjectToolsTests.java253
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/GraphIterableTests.java11
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/TreeIterableTests.java4
4 files changed, 246 insertions, 36 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java
index 846d92b566..1e456b2e16 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.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.
@@ -113,7 +113,7 @@ public final class ObjectTools {
* @see RepeatingElementIterator
*/
public static <E> RepeatingElementList<E> repeat(E object, int size) {
- return new RepeatingElementList<E>(object, size);
+ return new RepeatingElementList<>(object, size);
}
/**
@@ -291,7 +291,7 @@ public final class ObjectTools {
}
- // ********** fields **********
+ // ********** field values **********
/**
* Return the value of the specified object's field with the specified
@@ -343,6 +343,9 @@ public final class ObjectTools {
field_(object, fieldName).set(object, value);
}
+
+ // ********** fields **********
+
/**
* @see ClassTools#field(Class, String)
*/
@@ -371,7 +374,7 @@ public final class ObjectTools {
}
- // ********** methods **********
+ // ********** method execution **********
/**
* Execute the specified zero-argument method.
@@ -442,6 +445,9 @@ public final class ObjectTools {
return method_(object, methodName, parameterTypes).invoke(object, arguments);
}
+
+ // ********** methods **********
+
/**
* Return the zero-argument method for the specified object
* and method name. If the object's class does not directly
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ObjectToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ObjectToolsTests.java
index c460eb1f60..d424af280e 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ObjectToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/ObjectToolsTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2013 Oracle. All rights reserved.
+ * Copyright (c) 2010, 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,10 +9,16 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.List;
import java.util.Vector;
import org.eclipse.jpt.common.utility.internal.ClassTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
-
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
+import org.eclipse.jpt.common.utility.transformer.Transformer;
import junit.framework.TestCase;
@SuppressWarnings("nls")
@@ -63,6 +69,116 @@ public class ObjectToolsTests
assertTrue(ObjectTools.notEquals("foo", "bar"));
}
+ public void testHashCode() {
+ Object o = null;
+ assertEquals(0, ObjectTools.hashCode(o));
+ o = new Object();
+ assertEquals(o.hashCode(), ObjectTools.hashCode(o));
+ }
+
+ public void testHashCodeNull() {
+ Object o = null;
+ assertEquals(-1, ObjectTools.hashCode(o, -1));
+ }
+
+ public void testChain() {
+ Iterable<Class<?>> classes = ObjectTools.chain(Vector.class, SUPERCLASS_TRANSFORMER);
+ assertEquals(4, IterableTools.size(classes));
+ }
+ public static final Transformer<Class<?>, Class<?>> SUPERCLASS_TRANSFORMER = new SuperClassTransformer();
+ static class SuperClassTransformer
+ extends TransformerAdapter<Class<?>, Class<?>>
+ {
+ @Override
+ public Class<?> transform(Class<?> input) {
+ return input.getSuperclass();
+ }
+ }
+
+ public void testRepeat() {
+ String s = "foo";
+ List<String> strings = ObjectTools.repeat(s, 3);
+ assertEquals(3, strings.size());
+ assertEquals(s, strings.get(0));
+ assertEquals(s, strings.get(1));
+ assertEquals(s, strings.get(2));
+ }
+
+ public void testToStringObjectObject() {
+ String s = ObjectTools.toString(this, Arrays.asList(new Object[] {"foo", "bar"}));
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("([foo, bar])"));
+ }
+
+ public void testToStringObjectObjectArray() {
+ String s = ObjectTools.toString(this, new Object[] {"foo", "bar"});
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("([foo, bar])"));
+ }
+
+ public void testToStringObjectBoolean() {
+ String s = ObjectTools.toString(this, false);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(false)"));
+ }
+
+ public void testToStringObjectChar() {
+ String s = ObjectTools.toString(this, 'x');
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(x)"));
+ }
+
+ public void testToStringObjectCharArray() {
+ String s = ObjectTools.toString(this, "foo".toCharArray());
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(foo)"));
+ }
+
+ public void testToStringObjectCharSequence() {
+ String s = ObjectTools.toString(this, "foo");
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(foo)"));
+ }
+
+ public void testToStringObjectDouble() {
+ String s = ObjectTools.toString(this, 77.77);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(77.77)"));
+ }
+
+ public void testToStringObjectFloat() {
+ String s = ObjectTools.toString(this, 77.77f);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(77.77)"));
+ }
+
+ public void testToStringObjectInt() {
+ String s = ObjectTools.toString(this, 77);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(77)"));
+ }
+
+ public void testToStringObjectLong() {
+ String s = ObjectTools.toString(this, 77l);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("(77)"));
+ }
+
+ public void testToStringObject() {
+ String s = ObjectTools.toString(this);
+ assertTrue(s.startsWith(this.getClass().getSimpleName()));
+ assertTrue(s.endsWith("]"));
+ }
+
+ public void testIdentityToStringObject() {
+ Object o = new Object();
+ assertEquals(o.toString(), ObjectTools.identityToString(o));
+ }
+
+ public void testSingletonToStringObject() {
+ assertEquals(this.getClass().getSimpleName(), ObjectTools.singletonToString(this));
+ }
+
public void testToStringName_anonymous() {
Object o = new Object(){/*anonymous subclass of Object*/};
assertEquals("Object", ClassTools.toStringName(o.getClass()));
@@ -91,7 +207,7 @@ public class ObjectToolsTests
public void testGet() {
int initialCapacity = 200;
- Vector<?> v = new Vector<Object>(initialCapacity);
+ Vector<?> v = new Vector<>(initialCapacity);
Object[] elementData = (Object[]) ObjectTools.get(v, "elementData");
assertEquals(initialCapacity, elementData.length);
@@ -99,21 +215,69 @@ public class ObjectToolsTests
Integer modCountInteger = (Integer) ObjectTools.get(v, "modCount");
int modCount = modCountInteger.intValue();
assertEquals(0, modCount);
+ }
+
+ public void testGet_exception() {
+ boolean exCaught = false;
+ try {
+ Object value = ObjectTools.get(new Vector<String>(), "bogusField");
+ fail("bogus: " + value);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchFieldException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSet() {
+ Vector<String> v = new Vector<>();
+ Object[] newElementData = new Object[5];
+ newElementData[0] = "foo";
+ ObjectTools.set(v, "elementData", newElementData);
+ ObjectTools.set(v, "elementCount", new Integer(1));
+ // test inherited field
+ ObjectTools.set(v, "modCount", new Integer(1));
+ assertTrue(v.contains("foo"));
+ }
+
+ public void testSet_exception() {
+ Vector<String> v = new Vector<>();
+ Object[] newElementData = new Object[5];
+ newElementData[0] = "foo";
+
+ boolean exCaught = false;
+ try {
+ ObjectTools.set(v, "bogusField", "foo");
+ fail();
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof NoSuchFieldException) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testField() {
+ assertNotNull(ObjectTools.field(this, "baz"));
+ }
+ public String baz;
+ public void testField_exception() {
boolean exCaught = false;
- Object bogusFieldValue = null;
try {
- bogusFieldValue = ObjectTools.get(v, "bogusField");
+ Field field = ObjectTools.field(this, "BOGUS");
+ fail("bogus: " + field);
} catch (RuntimeException ex) {
if (ex.getCause() instanceof NoSuchFieldException) {
exCaught = true;
}
}
- assertTrue("NoSuchFieldException not thrown: " + bogusFieldValue, exCaught);
+ assertTrue(exCaught);
}
public void testExecuteObjectString() {
- Vector<String> v = new Vector<String>();
+ Vector<String> v = new Vector<>();
int size = ((Integer) ObjectTools.execute(v, "size")).intValue();
assertEquals(0, size);
@@ -122,8 +286,18 @@ public class ObjectToolsTests
assertEquals(1, size);
}
+ public void testExecuteObjectString_() throws Exception {
+ Vector<String> v = new Vector<>();
+ int size = ((Integer) ObjectTools.execute_(v, "size")).intValue();
+ assertEquals(0, size);
+
+ v.addElement("foo");
+ size = ((Integer) ObjectTools.execute(v, "size")).intValue();
+ assertEquals(1, size);
+ }
+
public void testExecuteObjectStringClassObject() {
- Vector<String> v = new Vector<String>();
+ Vector<String> v = new Vector<>();
boolean booleanResult = ((Boolean) ObjectTools.execute(v, "add", Object.class, "foo")).booleanValue();
assertTrue(booleanResult);
assertTrue(v.contains("foo"));
@@ -131,8 +305,17 @@ public class ObjectToolsTests
assertNull(voidResult);
}
+ public void testExecuteObjectStringClassObject_() throws Exception {
+ Vector<String> v = new Vector<>();
+ boolean booleanResult = ((Boolean) ObjectTools.execute_(v, "add", Object.class, "foo")).booleanValue();
+ assertTrue(booleanResult);
+ assertTrue(v.contains("foo"));
+ Object voidResult = ObjectTools.execute(v, "addElement", Object.class, "bar");
+ assertNull(voidResult);
+ }
+
public void testExecuteObjectStringClassArrayObjectArray() {
- Vector<String> v = new Vector<String>();
+ Vector<String> v = new Vector<>();
Class<?>[] parmTypes = new Class[1];
parmTypes[0] = java.lang.Object.class;
Object[] args = new Object[1];
@@ -140,37 +323,57 @@ public class ObjectToolsTests
boolean booleanResult = ((Boolean) ObjectTools.execute(v, "add", parmTypes, args)).booleanValue();
assertTrue(booleanResult);
assertTrue(v.contains("foo"));
+ }
+ public void testExecuteObjectStringClassArrayObjectArray_exception() {
+ Vector<String> v = new Vector<>();
+ Class<?>[] parmTypes = new Class[1];
+ parmTypes[0] = java.lang.Object.class;
+ Object[] args = new Object[1];
+ args[0] = "foo";
boolean exCaught = false;
- Object bogusMethodReturnValue = null;
try {
- bogusMethodReturnValue = ObjectTools.execute(v, "bogusMethod", parmTypes, args);
+ Object value = ObjectTools.execute(v, "bogusMethod", parmTypes, args);
+ fail("bogus: " + value);
} catch (RuntimeException ex) {
if (ex.getCause() instanceof NoSuchMethodException) {
exCaught = true;
}
}
- assertTrue("NoSuchMethodException not thrown: " + bogusMethodReturnValue, exCaught);
+ assertTrue(exCaught);
}
- public void testSet() {
- Vector<String> v = new Vector<String>();
- Object[] newElementData = new Object[5];
- newElementData[0] = "foo";
- ObjectTools.set(v, "elementData", newElementData);
- ObjectTools.set(v, "elementCount", new Integer(1));
- // test inherited field
- ObjectTools.set(v, "modCount", new Integer(1));
- assertTrue(v.contains("foo"));
+ public void testMethod() {
+ assertNotNull(ObjectTools.method(this, "testMethod"));
+ }
+
+ public void testMethod_() throws Exception {
+ assertNotNull(ObjectTools.method_(this, "testMethod"));
+ }
+ public void testMethodWithParm() {
+ assertNotNull(ObjectTools.method(this, "methodWithParm", String.class));
+ }
+
+ public void testMethodWithParm_() throws Exception {
+ assertNotNull(ObjectTools.method_(this, "methodWithParm", String.class));
+ }
+ public void methodWithParm(String string) {
+ assertNotNull(string);
+ }
+
+ public void testConstructor() {
boolean exCaught = false;
try {
- ObjectTools.set(v, "bogusField", "foo");
+ Object at = ClassTools.newInstance(ObjectTools.class);
+ fail("bogus: " + at);
} catch (RuntimeException ex) {
- if (ex.getCause() instanceof NoSuchFieldException) {
- exCaught = true;
+ if (ex.getCause() instanceof InvocationTargetException) {
+ if (ex.getCause().getCause() instanceof UnsupportedOperationException) {
+ exCaught = true;
+ }
}
}
- assertTrue("NoSuchFieldException not thrown", exCaught);
+ assertTrue(exCaught);
}
}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/GraphIterableTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/GraphIterableTests.java
index 95715eef43..89c0f7c987 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/GraphIterableTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/GraphIterableTests.java
@@ -11,20 +11,21 @@ package org.eclipse.jpt.common.utility.tests.internal.iterable;
import java.util.ArrayList;
import java.util.Collection;
-import junit.framework.TestCase;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.ListTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.transformer.DisabledTransformer;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;
import org.eclipse.jpt.common.utility.transformer.Transformer;
+import junit.framework.TestCase;
@SuppressWarnings("nls")
public class GraphIterableTests
extends TestCase
{
/** this will be populated with all the nodes created for the test */
- Collection<GraphNode> nodes = new ArrayList<GraphNode>();
+ Collection<GraphNode> nodes = new ArrayList<>();
public GraphIterableTests(String name) {
super(name);
@@ -43,7 +44,7 @@ public class GraphIterableTests
}
private Iterable<GraphNode> buildGraphIterable() {
- return IterableTools.graphIterable(this.buildGraphRoot(), CHILDREN_TRANSFORMER);
+ return ObjectTools.graph(this.buildGraphRoot(), CHILDREN_TRANSFORMER);
}
public void testNeighbors_roots() {
@@ -73,7 +74,7 @@ public class GraphIterableTests
assertTrue(exCaught);
}
- private GraphNode buildGraphRoot() {
+ public GraphNode buildGraphRoot() {
GraphNode ncNode = new GraphNode("North Carolina");
GraphNode vaNode = new GraphNode("Virginia");
GraphNode scNode = new GraphNode("South Carolina");
@@ -108,7 +109,7 @@ public class GraphIterableTests
public class GraphNode {
private String name;
- private Collection<GraphNode> neighbors = new ArrayList<GraphNode>();
+ private Collection<GraphNode> neighbors = new ArrayList<>();
public GraphNode(String name) {
super();
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/TreeIterableTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/TreeIterableTests.java
index 1b449b402f..a66a9a5029 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/TreeIterableTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterable/TreeIterableTests.java
@@ -21,7 +21,7 @@ import junit.framework.TestCase;
@SuppressWarnings("nls")
public class TreeIterableTests extends TestCase {
/** this will be populated with all the nodes created for the test */
- Collection<TreeNode> nodes = new ArrayList<TreeNode>();
+ Collection<TreeNode> nodes = new ArrayList<>();
public TreeIterableTests(String name) {
super(name);
@@ -46,7 +46,7 @@ public class TreeIterableTests extends TestCase {
}
public void testToString() {
- assertNotNull(IterableTools.treeIterable(this.buildTree(), CHILDREN_TRANSFORMER).toString());
+ assertNotNull(ObjectTools.tree(this.buildTree(), CHILDREN_TRANSFORMER).toString());
}
private TreeNode buildTree() {

Back to the top