Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1278 9d8363e62 -> dec5caf05
Add basic test framework for GremlinScriptEngine. The test framework is built based on the same patterns as the Graph test framework. This will let us validate that each Gremlin-enabled ScriptEngine properly observes the semantics prescribed for it. Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/dec5caf0 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/dec5caf0 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/dec5caf0 Branch: refs/heads/TINKERPOP-1278 Commit: dec5caf05738c938bce489a5fee80c5efd95861e Parents: 9d8363e Author: Stephen Mallette <sp...@genoprime.com> Authored: Mon Jul 11 19:29:24 2016 -0400 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Mon Jul 11 19:29:24 2016 -0400 ---------------------------------------------------------------------- .../gremlin/util/ScriptEngineCache.java | 3 + .../jsr223/GroovyGremlinScriptEngineTest.java | 29 +++++ .../util/function/ScriptEngineLambdaTest.java | 6 - .../jsr223/PythonGremlinScriptEngineTest.java | 29 +++++ .../CachedGremlinScriptEngineManagerTest.java | 40 ++++++ .../tinkerpop/gremlin/jsr223/EngineToTest.java | 35 ++++++ .../jsr223/GremlinScriptEngineSuite.java | 46 +++++++ .../gremlin/jsr223/ScriptEngineLambdaTest.java | 125 +++++++++++++++++++ 8 files changed, 307 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/ScriptEngineCache.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/ScriptEngineCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/ScriptEngineCache.java index 70be8af..9e942d9 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/ScriptEngineCache.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/ScriptEngineCache.java @@ -20,6 +20,7 @@ package org.apache.tinkerpop.gremlin.util; import org.apache.tinkerpop.gremlin.jsr223.DefaultGremlinScriptEngineManager; import org.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngineManager; +import org.apache.tinkerpop.gremlin.jsr223.SingleGremlinScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -28,7 +29,9 @@ import java.util.concurrent.ConcurrentHashMap; /** * @author Daniel Kuppitz (http://gremlin.guru) + * @deprecated As of release 3.3.0, replaced by {@link SingleGremlinScriptEngineManager}. */ +@Deprecated public final class ScriptEngineCache { private ScriptEngineCache() {} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/jsr223/GroovyGremlinScriptEngineTest.java ---------------------------------------------------------------------- diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/jsr223/GroovyGremlinScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/jsr223/GroovyGremlinScriptEngineTest.java new file mode 100644 index 0000000..deae99c --- /dev/null +++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/jsr223/GroovyGremlinScriptEngineTest.java @@ -0,0 +1,29 @@ +/* + * 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.runner.RunWith; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +@RunWith(GremlinScriptEngineSuite.class) +@EngineToTest(engineName = "gremlin-groovy") +public class GroovyGremlinScriptEngineTest { +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/util/function/ScriptEngineLambdaTest.java ---------------------------------------------------------------------- diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/util/function/ScriptEngineLambdaTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/util/function/ScriptEngineLambdaTest.java index 9701f90..7973d0a 100644 --- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/util/function/ScriptEngineLambdaTest.java +++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/util/function/ScriptEngineLambdaTest.java @@ -127,12 +127,6 @@ public class ScriptEngineLambdaTest { assertEquals(lambda.apply("foo"), "foo2"); } - @AfterClass - public static void clearScriptEngineCache() { - ScriptEngineCache.get(GROOVY_SCRIPT_ENGINE_NAME).getBindings(ScriptContext.GLOBAL_SCOPE).clear(); - } - - // Utilities /** http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/jsr223/PythonGremlinScriptEngineTest.java ---------------------------------------------------------------------- diff --git a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/jsr223/PythonGremlinScriptEngineTest.java b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/jsr223/PythonGremlinScriptEngineTest.java new file mode 100644 index 0000000..e1de47d --- /dev/null +++ b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/jsr223/PythonGremlinScriptEngineTest.java @@ -0,0 +1,29 @@ +/* + * 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.runner.RunWith; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +@RunWith(GremlinScriptEngineSuite.class) +@EngineToTest(engineName = "gremlin-jython") +public class PythonGremlinScriptEngineTest { +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManagerTest.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManagerTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManagerTest.java new file mode 100644 index 0000000..e32cf85 --- /dev/null +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/CachedGremlinScriptEngineManagerTest.java @@ -0,0 +1,40 @@ +/* + * 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.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngineSuite.ENGINE_TO_TEST; +import static org.junit.Assert.assertSame; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class CachedGremlinScriptEngineManagerTest { + private static final GremlinScriptEngineManager manager = new CachedGremlinScriptEngineManager(); + + @Test + public void shouldBeSameInstance() { + final GremlinScriptEngine engineFirst = manager.getEngineByName(ENGINE_TO_TEST); + final GremlinScriptEngine engineSecond = manager.getEngineByName(ENGINE_TO_TEST); + + assertSame(engineFirst, engineSecond); + assertSame(engineSecond, engineFirst); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/EngineToTest.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/EngineToTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/EngineToTest.java new file mode 100644 index 0000000..9688529 --- /dev/null +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/EngineToTest.java @@ -0,0 +1,35 @@ +/* + * 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 java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface EngineToTest { + public String engineName(); +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/GremlinScriptEngineSuite.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/GremlinScriptEngineSuite.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/GremlinScriptEngineSuite.java new file mode 100644 index 0000000..e1fe340 --- /dev/null +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/GremlinScriptEngineSuite.java @@ -0,0 +1,46 @@ +/* + * 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.runners.Suite; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.RunnerBuilder; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class GremlinScriptEngineSuite extends Suite { + static String ENGINE_TO_TEST; + + private static final Class<?>[] allTests = new Class<?>[]{ + CachedGremlinScriptEngineManagerTest.class, + ScriptEngineLambdaTest.class }; + + public GremlinScriptEngineSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError { + super(builder, klass, allTests); + ENGINE_TO_TEST = getScriptEngineName(klass); + } + + public static String getScriptEngineName(final Class<?> klass) throws InitializationError { + final EngineToTest annotation = klass.getAnnotation(EngineToTest.class); + if (null == annotation) + throw new InitializationError(String.format("class '%s' must have a EngineToTest annotation", klass.getName())); + return annotation.engineName(); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/dec5caf0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptEngineLambdaTest.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptEngineLambdaTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptEngineLambdaTest.java new file mode 100644 index 0000000..9145ced --- /dev/null +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptEngineLambdaTest.java @@ -0,0 +1,125 @@ +/* + * 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.util.function.ScriptEngineLambda; +import org.junit.Test; + +import static org.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngineSuite.ENGINE_TO_TEST; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +/** + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class ScriptEngineLambdaTest { + + @Test + public void shouldCallAsFunction() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "1+a"); + assertEquals(11, Integer.parseInt(lambda.apply(10).toString())); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsFunction() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").apply("a"); + } + + @Test + public void shouldCallAsSupplier() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "11"); + assertEquals(11, lambda.get()); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsSupplier() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").get(); + } + + @Test + public void shouldCallAsPredicate() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "a > 10"); + assertThat(lambda.test(100), is(true)); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsPredicate() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").test(1); + } + + @Test + public void shouldCallAsConsumer() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "a.setData('test')"); + final Junk junk = new Junk(); + lambda.accept(junk); + assertEquals("test", junk.getData()); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsConsumer() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").accept("1"); + } + + @Test + public void shouldCallAsBiConsumer() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "a.setData('testa');b.setData('testb')"); + final Junk junkA = new Junk(); + final Junk junkB = new Junk(); + lambda.accept(junkA, junkB); + + assertEquals("testa", junkA.getData()); + assertEquals("testb", junkB.getData()); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsBiConsumer() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").accept("1", "2"); + } + + @Test + public void shouldCallAsTriConsumer() { + final ScriptEngineLambda lambda = new ScriptEngineLambda(ENGINE_TO_TEST, "a.setData('testa');b.setData('testb');c.setData('testc')"); + final Junk junkA = new Junk(); + final Junk junkB = new Junk(); + final Junk junkC = new Junk(); + lambda.accept(junkA, junkB, junkC); + + assertEquals("testa", junkA.getData()); + assertEquals("testb", junkB.getData()); + assertEquals("testc", junkC.getData()); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowOnBadScriptAsTriConsumer() { + new ScriptEngineLambda(ENGINE_TO_TEST, "1432423)a").accept("1", "2", "3"); + } + + public static class Junk { + private String data = ""; + + public String getData() { + return data; + } + + public void setData(final String x) { + data = x; + } + } +} \ No newline at end of file