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 2016/04/11 20:10:30 UTC

[03/16] incubator-tinkerpop git commit: added NPE protection to ObjectWritable and added ObjectWritableTest which ensure ObjectWritable is comparable, gettable, toStringable, etc. even when its empty() -- that is, this.t == null. CTR. Fixed TINKERPOP-125

added NPE protection to ObjectWritable and added ObjectWritableTest which ensure ObjectWritable is comparable, gettable, toStringable, etc. even when its empty() -- that is, this.t == null. CTR. Fixed TINKERPOP-1251


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

Branch: refs/heads/TINKERPOP-1020
Commit: b195b623d02dcfb5bf188ab10fba2e7a7ef399c7
Parents: 45c4303
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Wed Apr 6 15:45:50 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Wed Apr 6 15:45:50 2016 -0600

----------------------------------------------------------------------
 .../hadoop/structure/io/ObjectWritable.java     |  9 ++--
 .../hadoop/structure/io/ObjectWritableTest.java | 49 ++++++++++++++++++++
 2 files changed, 55 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/b195b623/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
index a371bc3..9e101ef 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
@@ -36,6 +36,7 @@ import java.io.Serializable;
  */
 public final class ObjectWritable<T> implements WritableComparable<ObjectWritable>, Serializable {
 
+    private static final String NULL = "null";
     private static final ObjectWritable<MapReduce.NullObject> NULL_OBJECT_WRITABLE = new ObjectWritable<>(MapReduce.NullObject.instance());
 
     T t;
@@ -57,7 +58,7 @@ public final class ObjectWritable<T> implements WritableComparable<ObjectWritabl
 
     @Override
     public String toString() {
-        return this.t.toString();
+        return null == this.t ? NULL : this.t.toString();
     }
 
     @Override
@@ -97,7 +98,9 @@ public final class ObjectWritable<T> implements WritableComparable<ObjectWritabl
 
     @Override
     public int compareTo(final ObjectWritable objectWritable) {
-        if (this.t instanceof Comparable)
+        if (null == this.t)
+            return objectWritable.isEmpty() ? 0 : -1;
+        else if (this.t instanceof Comparable && !objectWritable.isEmpty())
             return ((Comparable) this.t).compareTo(objectWritable.get());
         else if (this.t.equals(objectWritable.get()))
             return 0;
@@ -110,7 +113,7 @@ public final class ObjectWritable<T> implements WritableComparable<ObjectWritabl
     }
 
     public static ObjectWritable empty() {
-        return new ObjectWritable(null);
+        return new ObjectWritable<>(null);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/b195b623/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritableTest.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritableTest.java b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritableTest.java
new file mode 100644
index 0000000..12f4694
--- /dev/null
+++ b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritableTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tinkerpop.gremlin.hadoop.structure.io;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class ObjectWritableTest {
+
+    @Test
+    public void shouldNotHaveANullPointerException() {
+        final ObjectWritable object = new ObjectWritable<>(null);
+        assertNull(object.get());
+        assertTrue(object.isEmpty());
+        assertTrue(object.equals(ObjectWritable.empty()));
+        assertFalse(object.equals(new ObjectWritable<>("hello")));
+        assertEquals("null", object.toString());
+        assertEquals(-1, object.compareTo(new ObjectWritable<>("blah")));
+        assertEquals(-1, new ObjectWritable<>("bloop").compareTo(ObjectWritable.empty()));
+        assertEquals(-1, new ObjectWritable<>("bloop").compareTo(object));
+        assertEquals(0, ObjectWritable.empty().compareTo(object));
+        assertEquals(0, object.compareTo(ObjectWritable.empty()));
+        assertEquals(0, object.compareTo(object));
+    }
+}