You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2015/05/22 17:47:43 UTC

incubator-tinkerpop git commit: Added TestableConsolePluginAcceptor so people that have console-based plugins can test them easier. HadoopGremlinPluginTest now tests HadoopGremlinPlugin and no more manual testing to make sure all the HDFS, sugar, remote,

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 8d10b81b8 -> f069cdf6a


Added TestableConsolePluginAcceptor so people that have console-based plugins can test them easier. HadoopGremlinPluginTest now tests HadoopGremlinPlugin and no more manual testing to make sure all the HDFS, sugar, remote, etc. functionality works. What a time save.


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

Branch: refs/heads/master
Commit: f069cdf6a8595a0272cfdb669c414ce7cbeac4f8
Parents: 8d10b81
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Fri May 22 09:47:05 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Fri May 22 09:47:18 2015 -0600

----------------------------------------------------------------------
 .../util/TestableConsolePluginAcceptor.java     |  82 +++++++++++
 .../groovy/plugin/HadoopGremlinPlugin.java      |   6 +-
 .../groovy/plugin/HadoopRemoteAcceptor.java     |   2 +-
 .../groovy/plugin/HadoopGremlinPluginTest.java  | 145 +++++++++++++++++++
 .../hadoop/groovy/plugin/HadoopPluginSuite.java |   2 +-
 .../groovy/plugin/HadoopRemoteAcceptorTest.java |  96 ------------
 6 files changed, 234 insertions(+), 99 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/util/TestableConsolePluginAcceptor.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/util/TestableConsolePluginAcceptor.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/util/TestableConsolePluginAcceptor.java
new file mode 100644
index 0000000..d535866
--- /dev/null
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/util/TestableConsolePluginAcceptor.java
@@ -0,0 +1,82 @@
+/*
+ *
+ *  * 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.groovy.util;
+
+import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin;
+import org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor;
+import org.codehaus.groovy.tools.shell.Groovysh;
+import org.codehaus.groovy.tools.shell.IO;
+
+import javax.script.ScriptException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class TestableConsolePluginAcceptor implements PluginAcceptor {
+
+    public static final String ENVIRONMENT_NAME = "console";
+    public static final String ENVIRONMENT_SHELL = "ConsolePluginAcceptor.shell";
+    public static final String ENVIRONMENT_IO = "ConsolePluginAcceptor.io";
+
+    private Groovysh shell = new Groovysh(new IO(System.in, new OutputStream() {
+        @Override
+        public void write(int b) throws IOException {
+
+        }
+    }, System.err));
+
+    @Override
+    public void addImports(final Set<String> importStatements) {
+        importStatements.forEach(this.shell::execute);
+    }
+
+    @Override
+    public void addBinding(final String key, final Object val) {
+        this.shell.getInterp().getContext().setVariable(key, val);
+    }
+
+    @Override
+    public Map<String, Object> getBindings() {
+        return Collections.unmodifiableMap(this.shell.getInterp().getContext().getVariables());
+    }
+
+    @Override
+    public Object eval(final String script) throws ScriptException {
+        return this.shell.execute(script);
+    }
+
+    @Override
+    public Map<String, Object> environment() {
+        final Map<String, Object> env = new HashMap<>();
+        env.put(GremlinPlugin.ENVIRONMENT, ENVIRONMENT_NAME);
+        env.put(ENVIRONMENT_IO, this.shell.getIo());
+        env.put(ENVIRONMENT_SHELL, this.shell);
+        return env;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPlugin.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPlugin.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPlugin.java
index dd62194..8ce5b62 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPlugin.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPlugin.java
@@ -49,7 +49,7 @@ import java.util.Set;
  */
 public final class HadoopGremlinPlugin extends AbstractGremlinPlugin {
 
-    private static final Set<String> IMPORTS = new HashSet<String>() {{
+    protected static final Set<String> IMPORTS = new HashSet<String>() {{
         add("import org.apache.hadoop.hdfs.*");
         add("import org.apache.hadoop.conf.*");
         add("import org.apache.hadoop.fs.*");
@@ -73,6 +73,10 @@ public final class HadoopGremlinPlugin extends AbstractGremlinPlugin {
         add(IMPORT_SPACE + MapReduceGraphComputer.class.getPackage().getName() + DOT_STAR);
     }};
 
+    public HadoopGremlinPlugin() {
+        super(true);
+    }
+
     @Override
     public String getName() {
         return "tinkerpop.hadoop";

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptor.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptor.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptor.java
index 15b4a8e..4499cc2 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptor.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptor.java
@@ -41,7 +41,7 @@ public final class HadoopRemoteAcceptor implements RemoteAcceptor {
     private static final String USE_SUGAR = "useSugar";
     private static final String SPACE = " ";
 
-    protected HadoopGraph hadoopGraph;
+    private HadoopGraph hadoopGraph;
     private Groovysh shell;
     private boolean useSugarPlugin = false;
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPluginTest.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPluginTest.java b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPluginTest.java
new file mode 100644
index 0000000..768585b
--- /dev/null
+++ b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopGremlinPluginTest.java
@@ -0,0 +1,145 @@
+/*
+ *
+ *  * 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.groovy.plugin;
+
+import org.apache.tinkerpop.gremlin.GraphManager;
+import org.apache.tinkerpop.gremlin.LoadGraphWith;
+import org.apache.tinkerpop.gremlin.groovy.plugin.RemoteAcceptor;
+import org.apache.tinkerpop.gremlin.groovy.util.TestableConsolePluginAcceptor;
+import org.apache.tinkerpop.gremlin.hadoop.HadoopGraphProvider;
+import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+
+public class HadoopGremlinPluginTest extends AbstractGremlinProcessTest {
+
+    private boolean ignore = false;
+
+    @Before
+    public void setup() throws Exception {
+        if (GraphManager.getGraphProvider() != null) {
+            super.setup();
+        } else {
+            this.ignore = true;
+        }
+    }
+
+    @Before
+    public void setupTest() {
+        if (GraphManager.getGraphProvider() != null) {
+            try {
+                super.setupTest();
+                this.console = new TestableConsolePluginAcceptor();
+                final HadoopGremlinPlugin plugin = new HadoopGremlinPlugin();
+                plugin.pluginTo(this.console);
+                this.remote = (HadoopRemoteAcceptor) plugin.remoteAcceptor().get();
+            } catch (final Exception e) {
+                throw new IllegalStateException(e.getMessage(), e);
+            }
+
+        } else {
+            this.ignore = true;
+        }
+    }
+
+    ///////////////////
+
+    private HadoopRemoteAcceptor remote;
+    private TestableConsolePluginAcceptor console;
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldReturnResultIterator() throws Exception {
+        if (!ignore) {
+            this.console.addBinding("graph", this.graph);
+            this.remote.connect(Arrays.asList("graph"));
+            //
+            Traversal<?, ?> traversal = (Traversal<?, ?>) this.remote.submit(Arrays.asList("g.V().count()"));
+            assertEquals(6L, traversal.next());
+            assertFalse(traversal.hasNext());
+            assertNotNull(this.console.getBindings().get(RemoteAcceptor.RESULT));
+        }
+    }
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldSupportSugar() throws Exception {
+        if (!ignore) {
+            this.console.addBinding("graph", this.graph);
+            this.remote.connect(Arrays.asList("graph"));
+            //
+            this.remote.configure(Arrays.asList("useSugar", "true"));
+            Traversal<?, ?> traversal = (Traversal<?, ?>) this.remote.submit(Arrays.asList("g.V.name.map{it.length()}.sum"));
+            assertEquals(28.0d, traversal.next());
+            assertFalse(traversal.hasNext());
+            assertNotNull(this.console.getBindings().get(RemoteAcceptor.RESULT));
+        }
+    }
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldSupportHDFSMethods() throws Exception {
+        if (!ignore) {
+            List<String> ls = (List<String>) this.console.eval("hdfs.ls()");
+            for (final String line : ls) {
+                assertTrue(line.startsWith("-") || line.startsWith("r") || line.startsWith("w") || line.startsWith("x"));
+                assertEquals(" ", line.substring(9, 10));
+            }
+            ls = (List<String>) this.console.eval("local.ls()");
+            for (final String line : ls) {
+                assertTrue(line.startsWith("-") || line.startsWith("r") || line.startsWith("w") || line.startsWith("x"));
+                assertEquals(" ", line.substring(9, 10));
+            }
+            ////
+            this.console.eval("hdfs.copyFromLocal('" + HadoopGraphProvider.PATHS.get("tinkerpop-classic.txt") + "', 'target/tinkerpop-classic.txt')");
+            assertTrue((Boolean) this.console.eval("hdfs.exists('target/tinkerpop-classic.txt')"));
+            ////
+            List<String> head = IteratorUtils.asList(this.console.eval("hdfs.head('target/tinkerpop-classic.txt')"));
+            assertEquals(6, head.size());
+            for (final String line : head) {
+                assertEquals(":", line.substring(1, 2));
+                assertTrue(Integer.valueOf(line.substring(0, 1)) <= 6);
+            }
+            head = IteratorUtils.asList(this.console.eval("hdfs.head('target/tinkerpop-classic.txt',3)"));
+            assertEquals(3, head.size());
+            for (final String line : head) {
+                assertEquals(":", line.substring(1, 2));
+                assertTrue(Integer.valueOf(line.substring(0, 1)) <= 3);
+            }
+            ////
+            this.console.eval("hdfs.rm('target/tinkerpop-classic.txt')");
+            assertFalse((Boolean) this.console.eval("hdfs.exists('target/tinkerpop-classic.txt')"));
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopPluginSuite.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopPluginSuite.java b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopPluginSuite.java
index 81fd021..2756ad2 100644
--- a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopPluginSuite.java
+++ b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopPluginSuite.java
@@ -31,7 +31,7 @@ import org.junit.runners.model.RunnerBuilder;
 public class HadoopPluginSuite extends ProcessStandardSuite {
 
     public HadoopPluginSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
-        super(klass, builder, new Class<?>[]{HadoopRemoteAcceptorTest.class});
+        super(klass, builder, new Class<?>[]{HadoopGremlinPluginTest.class});
     }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f069cdf6/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptorTest.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptorTest.java b/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptorTest.java
deleted file mode 100644
index 4adbbd8..0000000
--- a/hadoop-gremlin/src/test/java/org/apache/tinkerpop/gremlin/hadoop/groovy/plugin/HadoopRemoteAcceptorTest.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- *
- *  * 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.groovy.plugin;
-
-import org.apache.tinkerpop.gremlin.GraphManager;
-import org.apache.tinkerpop.gremlin.LoadGraphWith;
-import org.apache.tinkerpop.gremlin.groovy.plugin.RemoteAcceptor;
-import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
-import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
-import org.codehaus.groovy.tools.shell.Groovysh;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Arrays;
-
-import static org.junit.Assert.*;
-
-/**
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- */
-
-public class HadoopRemoteAcceptorTest extends AbstractGremlinProcessTest {
-
-    private boolean ignore = false;
-
-    @Before
-    public void setup() throws Exception {
-        if (GraphManager.getGraphProvider() != null) {
-            super.setup();
-        } else {
-            this.ignore = true;
-        }
-    }
-
-    @Before
-    public void setupTest() {
-        if (GraphManager.getGraphProvider() != null) {
-            super.setupTest();
-        } else {
-            this.ignore = true;
-        }
-    }
-
-    ///////////////////
-
-    private Groovysh shell = new Groovysh();
-
-    @Test
-    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
-    public void shouldReturnResultIterator() throws Exception {
-        if (!ignore) {
-            final HadoopRemoteAcceptor remoteAcceptor = new HadoopRemoteAcceptor(this.shell);
-            this.shell.getInterp().getContext().setVariable("graph", graph);
-            remoteAcceptor.connect(Arrays.asList("graph"));
-            Traversal<?, ?> traversal = (Traversal<?, ?>) remoteAcceptor.submit(Arrays.asList("g.V().count()"));
-            assertEquals(6L, traversal.next());
-            assertFalse(traversal.hasNext());
-            assertNotNull(this.shell.getInterp().getContext().getVariable(RemoteAcceptor.RESULT));
-        }
-    }
-
-    @Test
-    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
-    public void shouldSupportSugar() throws Exception {
-        if (!ignore) {
-            final HadoopRemoteAcceptor remoteAcceptor = new HadoopRemoteAcceptor(this.shell);
-            this.shell.getInterp().getContext().setVariable("graph", graph);
-            remoteAcceptor.connect(Arrays.asList("graph"));
-            remoteAcceptor.configure(Arrays.asList("useSugar", "true"));
-            Traversal<?, ?> traversal = (Traversal<?, ?>) remoteAcceptor.submit(Arrays.asList("g.V.name.map{it.length()}.sum"));
-            assertEquals(28.0d, traversal.next());
-            assertFalse(traversal.hasNext());
-            assertNotNull(this.shell.getInterp().getContext().getVariable(RemoteAcceptor.RESULT));
-        }
-    }
-
-}