You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2015/06/03 02:12:20 UTC

[15/50] [abbrv] incubator-tinkerpop git commit: Add tests for ElementHelper.

Add tests for ElementHelper.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/5a41ae8c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/5a41ae8c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/5a41ae8c

Branch: refs/heads/preprocessor
Commit: 5a41ae8c67141ad1cd7a48128ff378bfad8edfb3
Parents: c1d15ca
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri May 29 13:38:58 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri May 29 13:38:58 2015 -0400

----------------------------------------------------------------------
 .../gremlin/structure/util/ElementHelper.java   | 26 +++++--
 .../structure/util/ElementHelperTest.java       | 74 +++++++++++++++-----
 2 files changed, 79 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5a41ae8c/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
index 024e07f..0245086 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
@@ -117,19 +117,31 @@ public final class ElementHelper {
     }
 
     /**
-     * Remove a key from the set of key value pairs. Assumes that validations have already taken place to
+     * Remove a key from the set of key/value pairs. Assumes that validations have already taken place to
      * assure that key positions contain strings and that there are an even number of elements. If after removal
      * there are no values left, the key value list is returned as empty.
+     *
+     * @param keyToRemove the key to remove
+     * @param keyValues the list to remove the accessor from
+     * @return the key/values without the specified accessor or an empty array if no values remain after removal
      */
     public static Optional<Object[]> remove(final String keyToRemove, final Object... keyValues) {
         return ElementHelper.remove((Object) keyToRemove, keyValues);
     }
 
+    /**
+     * Removes an accessor from the set of key/value pairs. Assumes that validations have already taken place to
+     * assure that key positions contain strings and that there are an even number of elements. If after removal
+     * there are no values left, the key value list is returned as empty.
+     *
+     * @param accessor to remove
+     * @param keyValues the list to remove the accessor from
+     * @return the key/values without the specified accessor or an empty array if no values remain after removal
+     */
     public static Optional<Object[]> remove(final T accessor, final Object... keyValues) {
         return ElementHelper.remove((Object) accessor, keyValues);
     }
 
-
     private static Optional<Object[]> remove(final Object keyToRemove, final Object... keyValues) {
         final List list = Arrays.asList(keyValues);
         final List revised = IntStream.range(0, list.size())
@@ -162,6 +174,13 @@ public final class ElementHelper {
         }
     }
 
+    /**
+     * Replaces one key with a different key.
+     *
+     * @param keyValues the list of key/values to alter
+     * @param oldKey the key to replace
+     * @param newKey the new key
+     */
     public static Object[] replaceKey(final Object[] keyValues, final Object oldKey, final Object newKey) {
         final Object[] kvs = new Object[keyValues.length];
         for (int i = 0; i < keyValues.length; i = i + 2) {
@@ -246,7 +265,7 @@ public final class ElementHelper {
     }
 
     /**
-     * Assign key/value pairs as properties to a {@link org.apache.tinkerpop.gremlin.structure.Vertex}.  If the value of {@link T#id} or
+     * Assign key/value pairs as properties to a {@link Vertex}.  If the value of {@link T#id} or
      * {@link T#label} is in the set of pairs, then they are ignored.
      *
      * @param vertex            the vertex to attach the properties to
@@ -331,7 +350,6 @@ public final class ElementHelper {
      * @param a The first {@link org.apache.tinkerpop.gremlin.structure.Element}
      * @param b The second {@link org.apache.tinkerpop.gremlin.structure.Element} (as an {@link Object})
      * @return true if elements and equal and false otherwise
-     * @throws IllegalArgumentException if either argument is null
      */
     public static boolean areEqual(final Element a, final Object b) {
         if (null == b || null == a)

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5a41ae8c/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelperTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelperTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelperTest.java
index 2187298..0c8dd4a 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelperTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelperTest.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.structure.Element;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Property;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.javatuples.Pair;
 import org.junit.Test;
 
@@ -77,6 +78,17 @@ public class ElementHelperTest {
     }
 
     @Test
+    public void shouldValidatePropertyAndNotAllowHiddenKey() {
+        final String key = Graph.Hidden.hide("key");
+        try {
+            ElementHelper.validateProperty(key, "test");
+            fail("Should fail as property key cannot be hidden");
+        } catch (IllegalArgumentException iae) {
+            assertEquals(Property.Exceptions.propertyKeyCanNotBeAHiddenKey(key).getMessage(), iae.getMessage());
+        }
+    }
+
+    @Test
     public void shouldHaveValidProperty() {
         ElementHelper.validateProperty("aKey", "value");
     }
@@ -162,7 +174,7 @@ public class ElementHelperTest {
     }
 
     @Test
-    public void shouldAttachKeyValuesButNotLabelsOrId() {
+    public void shouldAttachPropertiesButNotLabelsOrId() {
         final Element mockElement = mock(Element.class);
         ElementHelper.attachProperties(mockElement, "test", 123, T.id, 321, T.label, "friends");
         verify(mockElement, times(1)).property("test", 123);
@@ -171,13 +183,13 @@ public class ElementHelperTest {
     }
 
     @Test(expected = ClassCastException.class)
-    public void shouldFailTryingToAttachNonStringKey() {
+    public void shouldFailTryingToAttachPropertiesNonStringKey() {
         final Element mockElement = mock(Element.class);
         ElementHelper.attachProperties(mockElement, "test", 123, 321, "test");
     }
 
     @Test
-    public void shouldFailTryingToAttachKeysToNullElement() {
+    public void shouldFailTryingToAttachPropertiesToNullElement() {
         try {
             ElementHelper.attachProperties(null, "test", 123, 321, "test");
             fail("Should throw exception since the element argument is null");
@@ -186,26 +198,30 @@ public class ElementHelperTest {
         }
     }
 
-    /*@Test
-    public void shouldFailElementAreEqualTestBecauseFirstArgumentIsNull() {
-        try {
-            ElementHelper.areEqual((Element) null, "some object");
-            fail("Should throw exception since the first argument is null");
-        } catch (IllegalArgumentException iae) {
-            assertEquals(Graph.Exceptions.argumentCanNotBeNull("a").getMessage(), iae.getMessage());
-        }
+    @Test
+    public void shouldAttachPropertiesWithCardinalityButNotLabelsOrId() {
+        final Vertex mockElement = mock(Vertex.class);
+        ElementHelper.attachProperties(mockElement, VertexProperty.Cardinality.single, "test", 123, T.id, 321, T.label, "friends");
+        verify(mockElement, times(1)).property(VertexProperty.Cardinality.single, "test", 123);
+        verify(mockElement, times(0)).property(VertexProperty.Cardinality.single, T.id.getAccessor(), 321);
+        verify(mockElement, times(0)).property(VertexProperty.Cardinality.single, T.label.getAccessor(), "friends");
+    }
+
+    @Test(expected = ClassCastException.class)
+    public void shouldFailTryingToAttachPropertiesWithCardinalityNonStringKey() {
+        final Element mockElement = mock(Vertex.class);
+        ElementHelper.attachProperties(mockElement, VertexProperty.Cardinality.single, "test", 123, 321, "test");
     }
 
     @Test
-    public void shouldFailElementAreEqualTestBecauseSecondArgumentIsNull() {
-        final Element mockElement = mock(Element.class);
+    public void shouldFailTryingToAttachPropertiesWithCardinalityToNullElement() {
         try {
-            ElementHelper.areEqual(mockElement, null);
-            fail("Should throw exception since the second argument is null");
+            ElementHelper.attachProperties((Vertex) null, VertexProperty.Cardinality.single, "test", 123, 321, "test");
+            fail("Should throw exception since the element argument is null");
         } catch (IllegalArgumentException iae) {
-            assertEquals(Graph.Exceptions.argumentCanNotBeNull("b").getMessage(), iae.getMessage());
+            assertEquals(Graph.Exceptions.argumentCanNotBeNull("vertex").getMessage(), iae.getMessage());
         }
-    }*/
+    }
 
     @Test
     public void shouldDetermineElementsAreEqualAsTheyAreSameObject() {
@@ -437,6 +453,19 @@ public class ElementHelperTest {
     }
 
     @Test
+    public void shouldRemoveAccessor() {
+        final Optional<Object[]> kvs = ElementHelper.remove(T.id, "1", "this", T.id, 6l, "3", "other", "4", 1);
+        assertEquals(6, kvs.get().length);
+        assertTrue(Stream.of(kvs.get()).noneMatch(kv -> kv.equals("2") || kv.equals(6l)));
+    }
+
+    @Test
+    public void shouldRemoveAccessorAndReturnEmpty() {
+        final Optional<Object[]> kvs = ElementHelper.remove(T.id, T.id, "this");
+        assertEquals(Optional.empty(), kvs);
+    }
+
+    @Test
     public void shouldUpsertKeyValueByAddingIt() {
         final Object[] oldKvs = new Object[]{"k", "v"};
         final Object[] newKvs = ElementHelper.upsert(oldKvs, "k1", "v1");
@@ -452,4 +481,15 @@ public class ElementHelperTest {
         assertEquals(4, newKvs.length);
         assertEquals("v1", newKvs[3]);
     }
+
+    @Test
+    public void shouldReplaceKey() {
+        final Object[] oldKvs = new Object[]{"k", "v", "k1", "v0"};
+        final Object[] newKvs = ElementHelper.replaceKey(oldKvs, "k", "k2");
+        assertEquals(4, newKvs.length);
+        assertEquals("k2", newKvs[0]);
+        assertEquals("v", newKvs[1]);
+        assertEquals("k1", newKvs[2]);
+        assertEquals("v0", newKvs[3]);
+    }
 }