TINKERPOP-1562 Added more tests for plugins/customizers

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

Branch: refs/heads/TINKERPOP-1562
Commit: bea343e7d5e1fc0a865b45f29230c7e831735524
Parents: 469787a
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Nov 23 14:37:26 2016 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Dec 1 06:41:43 2016 -0500

----------------------------------------------------------------------
 .../CachedGremlinScriptEngineManager.java       |  1 +
 .../gremlin/jsr223/DefaultImportCustomizer.java |  2 -
 .../gremlin/jsr223/LazyBindingsCustomizer.java  |  2 -
 .../SingleGremlinScriptEngineManager.java       |  2 +-
 .../jsr223/DefaultImportCustomizerTest.java     | 76 ++++++++++++++++++++
 .../jsr223/DefaultScriptCustomizerTest.java     | 56 +++++++++++++++
 .../jsr223/ScriptFileGremlinPluginTest.java     | 61 ++++++++++++++++
 .../jsr223/SingleScriptEngineManagerTest.java   | 45 ++++++++++++
 .../gremlin/jsr223/script-customizer-1.groovy   |  3 +
 .../gremlin/jsr223/script-customizer-2.groovy   |  2 +
 pom.xml                                         |  3 +-
 11 files changed, 247 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManager.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManager.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManager.java
index 9839b1b..5798e1c 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManager.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManager.java
@@ -92,6 +92,7 @@ public class CachedGremlinScriptEngineManager extends 
DefaultGremlinScriptEngine
     }
 
     private void registerLookUpInfo(final GremlinScriptEngine engine, final 
String shortName) {
+        if (null == engine) throw new 
IllegalArgumentException(String.format("%s is not an available 
GremlinScriptEngine", shortName));
         cache.putIfAbsent(shortName, engine);
         engine.getFactory().getExtensions().forEach(ext -> 
extensionToName.putIfAbsent(ext, shortName));
         engine.getFactory().getMimeTypes().forEach(mime -> 
mimeToName.putIfAbsent(mime, shortName));

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizer.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizer.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizer.java
index fa0965d..3642f97 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizer.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizer.java
@@ -18,8 +18,6 @@
  */
 package org.apache.tinkerpop.gremlin.jsr223;
 
-import org.apache.tinkerpop.gremlin.util.CoreImports;
-
 import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.Collection;

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/LazyBindingsCustomizer.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/LazyBindingsCustomizer.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/LazyBindingsCustomizer.java
index 4117ae5..01ae662 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/LazyBindingsCustomizer.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/LazyBindingsCustomizer.java
@@ -18,8 +18,6 @@
  */
 package org.apache.tinkerpop.gremlin.jsr223;
 
-import org.apache.tinkerpop.gremlin.jsr223.BindingsCustomizer;
-
 import javax.script.Bindings;
 import java.util.function.Supplier;
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/SingleGremlinScriptEngineManager.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/SingleGremlinScriptEngineManager.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/SingleGremlinScriptEngineManager.java
index 9474368..f9022dc 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/SingleGremlinScriptEngineManager.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/SingleGremlinScriptEngineManager.java
@@ -23,7 +23,7 @@ package org.apache.tinkerpop.gremlin.jsr223;
  *
  * @author Stephen Mallette (http://stephen.genoprime.com)
  */
-public class SingleGremlinScriptEngineManager {
+public final class SingleGremlinScriptEngineManager {
     private static final GremlinScriptEngineManager cached = new 
CachedGremlinScriptEngineManager();
 
     private SingleGremlinScriptEngineManager() {}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
new file mode 100644
index 0000000..c010aa5
--- /dev/null
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.jsr223;
+
+import org.apache.tinkerpop.gremlin.structure.T;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.time.DayOfWeek;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsCollectionContaining.hasItems;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class DefaultImportCustomizerTest {
+    @Test
+    public void shouldReturnAssignedImports() throws Exception {
+        final Method abs = Math.class.getMethod("abs", double.class);
+        final Enum dayOfWeekEnum = DayOfWeek.SATURDAY;
+        final Enum tEnum = T.id;
+        final ImportCustomizer imports = DefaultImportCustomizer.build()
+                .addClassImports(java.awt.Color.class, 
java.awt.AlphaComposite.class)
+                .addMethodImports(abs)
+                .addEnumImports(dayOfWeekEnum, tEnum).create();
+
+        assertEquals(2, imports.getClassImports().size());
+        assertThat(imports.getClassImports(), hasItems(java.awt.Color.class, 
java.awt.AlphaComposite.class));
+
+        assertEquals(1, imports.getMethodImports().size());
+        assertThat(imports.getMethodImports(), hasItems(abs));
+
+        assertEquals(2, imports.getEnumImports().size());
+        assertThat(imports.getEnumImports(), hasItems(dayOfWeekEnum, tEnum));
+    }
+
+    @Test
+    public void shouldReturnAssignedImportsWhenBuiltViaCollections() throws 
Exception {
+        final Method abs = Math.class.getMethod("abs", double.class);
+        final Enum dayOfWeekEnum = DayOfWeek.SATURDAY;
+        final Enum tEnum = T.id;
+        final ImportCustomizer imports = DefaultImportCustomizer.build()
+                .addClassImports(Arrays.asList(java.awt.Color.class, 
java.awt.AlphaComposite.class))
+                .addMethodImports(Collections.singletonList(abs))
+                .addEnumImports(Arrays.asList(dayOfWeekEnum, tEnum)).create();
+
+        assertEquals(2, imports.getClassImports().size());
+        assertThat(imports.getClassImports(), hasItems(java.awt.Color.class, 
java.awt.AlphaComposite.class));
+
+        assertEquals(1, imports.getMethodImports().size());
+        assertThat(imports.getMethodImports(), hasItems(abs));
+
+        assertEquals(2, imports.getEnumImports().size());
+        assertThat(imports.getEnumImports(), hasItems(dayOfWeekEnum, tEnum));
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultScriptCustomizerTest.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultScriptCustomizerTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultScriptCustomizerTest.java
new file mode 100644
index 0000000..3e4da13
--- /dev/null
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultScriptCustomizerTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.jsr223;
+
+import org.apache.tinkerpop.gremlin.TestHelper;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class DefaultScriptCustomizerTest {
+
+    @Test
+    public void shouldOpenViaPropertiesFileConfig() throws IOException {
+        final File scriptFile1 = 
TestHelper.generateTempFileFromResource(DefaultScriptCustomizerTest.class, 
"script-customizer-1.groovy", ".groovy");
+        final File scriptFile2 = 
TestHelper.generateTempFileFromResource(DefaultScriptCustomizerTest.class, 
"script-customizer-2.groovy", ".groovy");
+        final Set<File> files = new HashSet<>();
+        files.add(scriptFile1);
+        files.add(scriptFile2);
+        final ScriptCustomizer scripts = new DefaultScriptCustomizer(files);
+
+        final Collection<List<String>> linesInFiles = scripts.getScripts();
+        final String scriptCombined = 
linesInFiles.stream().flatMap(Collection::stream).map(s -> s + 
System.lineSeparator()).reduce("", String::concat);
+        assertEquals("x = 1 + 1" +  System.lineSeparator() +
+                     "y = 10 * x" +   System.lineSeparator() +
+                     "z = 1 + x + y" +  System.lineSeparator() +
+                     "l = g.V(z).out()" +  System.lineSeparator() +
+                     "        .group().by('name')" + System.lineSeparator(), 
scriptCombined);
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/ScriptFileGremlinPluginTest.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/ScriptFileGremlinPluginTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/ScriptFileGremlinPluginTest.java
new file mode 100644
index 0000000..81cf9e6
--- /dev/null
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/ScriptFileGremlinPluginTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.jsr223;
+
+import org.apache.tinkerpop.gremlin.TestHelper;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class ScriptFileGremlinPluginTest {
+    @Test
+    public void shouldOpenViaPropertiesFileConfig() throws IOException {
+        final File scriptFile1 = 
TestHelper.generateTempFileFromResource(DefaultScriptCustomizerTest.class, 
"script-customizer-1.groovy", ".groovy");
+        final File scriptFile2 = 
TestHelper.generateTempFileFromResource(DefaultScriptCustomizerTest.class, 
"script-customizer-2.groovy", ".groovy");
+        final Set<String> files = new HashSet<>();
+        files.add(scriptFile1.getAbsolutePath());
+        files.add(scriptFile2.getAbsolutePath());
+        final GremlinPlugin plugin = 
ScriptFileGremlinPlugin.build().files(files).create();
+
+        assertThat(plugin.getCustomizers().isPresent(), is(true));
+        assertThat(plugin.getCustomizers().get()[0], 
instanceOf(ScriptCustomizer.class));
+        final ScriptCustomizer customizer = (ScriptCustomizer) 
plugin.getCustomizers().get()[0];
+        final Collection<List<String>> linesInFiles = customizer.getScripts();
+        final String scriptCombined = 
linesInFiles.stream().flatMap(Collection::stream).map(s -> s + 
System.lineSeparator()).reduce("", String::concat);
+        assertEquals("x = 1 + 1" +  System.lineSeparator() +
+                "y = 10 * x" +   System.lineSeparator() +
+                "z = 1 + x + y" +  System.lineSeparator() +
+                "l = g.V(z).out()" +  System.lineSeparator() +
+                "        .group().by('name')" + System.lineSeparator(), 
scriptCombined);
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/SingleScriptEngineManagerTest.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/SingleScriptEngineManagerTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/SingleScriptEngineManagerTest.java
new file mode 100644
index 0000000..6765c94
--- /dev/null
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/SingleScriptEngineManagerTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.jsr223;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertSame;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class SingleScriptEngineManagerTest {
+    private static final GremlinScriptEngineManager mgr = 
SingleGremlinScriptEngineManager.instance();
+
+    @Test
+    public void shouldGetSameInstance() {
+        assertSame(mgr, SingleGremlinScriptEngineManager.instance());
+        assertSame(mgr, SingleGremlinScriptEngineManager.instance());
+        assertSame(mgr, SingleGremlinScriptEngineManager.instance());
+        assertSame(mgr, SingleGremlinScriptEngineManager.instance());
+        assertSame(mgr, SingleGremlinScriptEngineManager.instance());
+        assertSame(mgr, SingleGremlinScriptEngineManager.getInstance());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void shouldNotGetGremlinScriptEngineAsItIsNotRegistered() {
+        mgr.getEngineByName("gremlin-groovy");
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-1.groovy
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-1.groovy
 
b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-1.groovy
new file mode 100644
index 0000000..c2cc784
--- /dev/null
+++ 
b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-1.groovy
@@ -0,0 +1,3 @@
+x = 1 + 1
+y = 10 * x
+z = 1 + x + y
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-2.groovy
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-2.groovy
 
b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-2.groovy
new file mode 100644
index 0000000..1a4f9a9
--- /dev/null
+++ 
b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-2.groovy
@@ -0,0 +1,2 @@
+l = g.V(z).out()
+        .group().by('name')
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bea343e7/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index e58bf07..8db0fb0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -285,9 +285,10 @@ limitations under the License.
                         <exclude>**/*.xml</exclude>
                         <exclude>**/*.ldjson</exclude>
                         <exclude>**/goal.txt</exclude>
-                        
<exclude>**/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/*.txt</exclude>
                         
<exclude>**/src/main/resources/META-INF/services/**</exclude>
                         
<exclude>**/src/test/resources/META-INF/services/**</exclude>
+                        
<exclude>**/src/test/resources/org/apache/tinkerpop/gremlin/jsr223/script-customizer-*.groovy</exclude>
+                        
<exclude>**/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/*.txt</exclude>
                         <exclude>**/src/main/ext/**</exclude>
                         <exclude>**/src/main/static/**</exclude>
                         <exclude>**/_bsp/**</exclude>

Reply via email to