This is an automated email from the ASF dual-hosted git repository.

kenhuuu pushed a commit to branch TINKERPOP-3013
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 1c725ae155f3f6edabd5406d3addd1a06d753b07
Author: Ken Hu <[email protected]>
AuthorDate: Mon Nov 13 14:07:08 2023 -0800

    TINKERPOP-3013 Add handling for missing property in remote console
---
 .../gremlin/console/GremlinGroovysh.groovy         |   6 +-
 .../gremlin/console/GremlinGroovyshTest.groovy     | 161 +++++++++++++++++++++
 .../AbstractGremlinServerIntegrationTest.java      |   6 +-
 3 files changed, 171 insertions(+), 2 deletions(-)

diff --git 
a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
 
b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
index 2ba5388864..f5a65e652d 100644
--- 
a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
+++ 
b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
@@ -123,6 +123,8 @@ class GremlinGroovysh extends Groovysh {
                                     break
                                 }
                                 throw t
+                            } catch (MissingPropertyException mpe) {
+                                // Ignore any local missing properties since 
it doesn't affect remote execution.
                             }
                         } else {
                             // Evaluate Buffer wrapped with code storing 
bounded vars
@@ -135,6 +137,8 @@ class GremlinGroovysh extends Groovysh {
                                     break
                                 }
                                 throw t
+                            } catch (MissingPropertyException mpe) {
+                                // Ignore any local missing properties since 
it doesn't affect remote execution.
                             }
                         }
 
@@ -195,7 +199,7 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = 
$varname;
         } else {
             buff = [importsSpec] + ['true'] + current
         }
-        setLastResult(result = interp.evaluate(buff))
+        interp.evaluate(buff)
 
         if (variableBlocks) {
             def boundVarValues = (Map<String, Object>) 
interp.context.getVariable(COLLECTED_BOUND_VARS_MAP_VARNAME)
diff --git 
a/gremlin-console/src/test/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovyshTest.groovy
 
b/gremlin-console/src/test/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovyshTest.groovy
new file mode 100644
index 0000000000..4ecb5f562a
--- /dev/null
+++ 
b/gremlin-console/src/test/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovyshTest.groovy
@@ -0,0 +1,161 @@
+/*
+ * 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.console
+
+import org.apache.tinkerpop.gremlin.console.commands.RemoteCommand
+import 
org.apache.tinkerpop.gremlin.console.jsr223.AbstractGremlinServerIntegrationTest
+import org.apache.tinkerpop.gremlin.console.jsr223.DriverRemoteAcceptor
+import 
org.apache.tinkerpop.gremlin.console.jsr223.MockGroovyGremlinShellEnvironment
+import org.codehaus.groovy.tools.shell.IO
+import org.junit.Test
+
+import java.nio.file.Paths
+
+class GremlinGroovyshTest extends AbstractGremlinServerIntegrationTest {
+    private IO testio
+    private ByteArrayOutputStream out
+    private ByteArrayOutputStream err
+    private GremlinGroovysh shell
+
+    @Override
+    void setUp() {
+        super.setUp()
+        out = new ByteArrayOutputStream()
+        err = new ByteArrayOutputStream()
+        testio = new IO(new ByteArrayInputStream(), out, err)
+        shell = new GremlinGroovysh(new Mediator(null), testio)
+    }
+
+    @Override
+    void tearDown() {
+        super.tearDown()
+        shell.execute(":purge preferences") // for test cases where persistent 
preferences (interpreterMode) are set.
+    }
+
+    @Test
+    void shouldEnableRemoteConsole() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+
+        assert (false == shell.mediator.localEvaluation)
+        assert out.toString().startsWith("All scripts will now be sent to")
+    }
+
+    @Test
+    void shouldGetSimpleResultFromRemoteConsole() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        out.reset()
+        shell.execute("1+1")
+
+        assert ("2" == out.toString())
+    }
+
+    @Test
+    void shouldGetGremlinResultFromRemoteConsole() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        out.reset()
+        shell.execute("g.V().count()")
+
+        assert ("0" == out.toString())
+    }
+
+    @Test
+    void shouldGetMultilineResultFromRemoteConsole() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        out.reset()
+        shell.execute("if (true) {")
+        shell.execute("g.V().count() }")
+
+        assert ("0" == out.toString())
+    }
+
+    @Test
+    void shouldNotSubmitIncompleteLinesFromRemoteConsole() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        shell.execute("if (0 == g.V().count()) {")
+
+        assert (0 != shell.buffers.current().size())
+    }
+
+    @Test
+    void shouldGetGremlinResultFromRemoteConsoleInInterpreterMode() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        shell.execute(":set interpreterMode")
+        out.reset()
+        shell.execute("g.V().count()")
+
+        assert ("0" == out.toString())
+    }
+
+    @Test
+    void shouldGetMultilineResultFromRemoteConsoleInInterpreterMode() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        shell.execute(":set interpreterMode")
+        out.reset()
+        shell.execute("if (true) {")
+        shell.execute("g.V().count() }")
+
+        assert ("0" == out.toString())
+    }
+
+    @Test
+    void shouldOnlyExecuteOnceRemoteConsoleInInterpreterMode() {
+        setupRemote(shell)
+        shell.execute(":remote console")
+        shell.execute(":set interpreterMode")
+        out.reset()
+        shell.execute("a = 1")
+
+        assert "1" == out.toString()
+    }
+
+    private def setupRemote(GremlinGroovysh shell) {
+        shell.setResultHook(handleResult)
+        shell.register(new RemoteCommand(shell, shell.mediator))
+        shell.mediator.addRemote(new DriverRemoteAcceptor(new 
MockGroovyGremlinShellEnvironment(shell)))
+        
shell.mediator.currentRemote().connect([Paths.get(AbstractGremlinServerIntegrationTest.class.getResource("remote.yaml").toURI()).toString()])
+
+        
server.getServerGremlinExecutor().getGremlinExecutor().getScriptEngineManager().put(
+                "g",
+                
server.getServerGremlinExecutor().getGraphManager().getGraph("graph").traversal())
+    }
+
+    private def handleResult = { result ->
+        if (result instanceof Iterator) {
+            Iterator resultItr = (Iterator) result
+
+            while (resultItr.hasNext()) {
+                testio.out.print(resultItr.next())
+                testio.out.flush()
+            }
+        } else if (result instanceof Number) {
+            testio.out.print((Number) result)
+            testio.out.flush()
+        } else if (result instanceof String) {
+            testio.out.print((String) result)
+            testio.out.flush()
+        }
+    }
+}
diff --git 
a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/AbstractGremlinServerIntegrationTest.java
 
b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/AbstractGremlinServerIntegrationTest.java
index 1bf9193dee..1cf4a78a34 100644
--- 
a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/AbstractGremlinServerIntegrationTest.java
+++ 
b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/AbstractGremlinServerIntegrationTest.java
@@ -24,6 +24,7 @@ import org.junit.After;
 import org.junit.Before;
 
 import java.io.InputStream;
+import java.nio.file.Paths;
 
 /**
  * Starts and stops an instance for each executed test.
@@ -31,7 +32,7 @@ import java.io.InputStream;
  * @author Stephen Mallette (http://stephen.genoprime.com)
  */
 public abstract class AbstractGremlinServerIntegrationTest {
-    private GremlinServer server;
+    protected GremlinServer server;
 
     public Settings overrideSettings(final Settings settings) {
         return settings;
@@ -47,6 +48,9 @@ public abstract class AbstractGremlinServerIntegrationTest {
         final Settings settings = Settings.read(stream);
 
         final Settings overridenSettings = overrideSettings(settings);
+        String prop = 
Paths.get(AbstractGremlinServerIntegrationTest.class.getResource("tinkergraph-empty.properties").toURI()).toString();
+        overridenSettings.graphs.put("graph", prop);
+
         this.server = new GremlinServer(overridenSettings);
 
         server.start().join();

Reply via email to