diff options
Diffstat (limited to 'velocity-engine-scripting/src/test/java/org/apache/velocity')
6 files changed, 403 insertions, 0 deletions
diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java new file mode 100644 index 00000000..b702df78 --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java @@ -0,0 +1,31 @@ +package org.apache.velocity.script.test; + + +import junit.framework.TestCase; +import org.apache.velocity.script.VelocityScriptEngineFactory; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; + +public abstract class AbstractScriptTest extends TestCase { + protected ScriptEngine engine; + protected ScriptEngineFactory engineFactory; + protected ScriptEngineManager manager; + + @Override + public void setUp() { + manager = new ScriptEngineManager(); + } + + public void setupEngine(ScriptEngineFactory scriptEngineFactory){ + manager.registerEngineName("velocity", scriptEngineFactory); + engine = manager.getEngineByName("velocity"); + } + + public void setupWithDefaultFactory() { + manager.registerEngineName("velocity", new VelocityScriptEngineFactory()); + engine = manager.getEngineByName("velocity"); + engineFactory = engine.getFactory(); + } +} diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java new file mode 100644 index 00000000..f7b9771f --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java @@ -0,0 +1,97 @@ +package org.apache.velocity.script.test; +/* + * 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. + */ + +import org.apache.velocity.script.VelocityScriptEngine; + +import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptContext; +import javax.script.ScriptException; +import java.io.StringWriter; + +public class ScriptEngineTest extends AbstractScriptTest { + + @Override + public void setUp() { + super.setUp(); + super.setupWithDefaultFactory(); + } + + public void testBasicOpe() { + + engine.put("name1", "value1"); + Bindings engineScope = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE); + assertEquals("Engine#put should have same effect as context.put ", engineScope.get("name1"), "value1"); + + String val = engine.get("name1").toString(); + assertEquals("Engine#get should have same effect as context.get ", engineScope.get("name1"), val); + } + + + public void testJSR223tException(){ + try { + engine.get(""); + fail("Cannot pass empty name"); + } catch (IllegalArgumentException n) { + //Success + } + + try { + engine.setContext(null); + fail("Cannot pass null to context"); + } catch (NullPointerException n) { + //Success + } + + } + + public void testEngineEvals() throws ScriptException { + String path = System.getProperty("test.resources.dir"); + engine.getContext().setWriter(new StringWriter()); + engine.getContext().setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, path + "/test-classes/velocity.properties", ScriptContext.ENGINE_SCOPE); + String script = "<html><body>#set( $foo = 'Velocity' )Hello $foo World!</body><html>"; + Object result = engine.eval(script); + assertEquals(result.toString(), "<html><body>Hello Velocity World!</body><html>"); + } + + public void testCompilable() throws ScriptException + { + String path = System.getProperty("test.resources.dir"); + engine.getContext().setWriter(new StringWriter()); + engine.getContext().setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, path + "/test-classes/velocity.properties", ScriptContext.ENGINE_SCOPE); + String script = "$foo"; + engine.put("foo", "bar"); + CompiledScript compiled = ((Compilable)engine).compile(script); + Object result = compiled.eval(); + assertEquals(result.toString(), "bar"); + } + + public void testContext() throws ScriptException + { + String path = System.getProperty("test.resources.dir"); + engine.getContext().setWriter(new StringWriter()); + engine.getContext().setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, path + "/test-classes/velocity.properties", ScriptContext.ENGINE_SCOPE); + String script = "$context.class.name $context.writer.class.name $context.reader.class.name $context.errorWriter.class.name"; + String result = engine.eval(script).toString(); + assertEquals("javax.script.SimpleScriptContext java.io.StringWriter java.io.InputStreamReader java.io.PrintWriter", result); + } + +} diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java new file mode 100644 index 00000000..8031cd0e --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java @@ -0,0 +1,159 @@ +package org.apache.velocity.script.test; + +/* +* 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. +*/ + +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.SimpleBindings; +import java.util.List; + + +public class VelocityScriptContextTest extends AbstractScriptTest { + + @Override + public void setUp() { + super.setUp(); + super.setupWithDefaultFactory(); + } + + public void testInitialScopes() { + List<Integer> defaultScopes = engine.getContext().getScopes(); + assertEquals(defaultScopes.size(), 2); + assertTrue(defaultScopes.contains(ScriptContext.ENGINE_SCOPE)); + assertTrue(defaultScopes.contains(ScriptContext.GLOBAL_SCOPE)); + } + + public void testScopes() { + + Bindings velocityBindings = new SimpleBindings(); + engine.getContext().setBindings(velocityBindings, ScriptContext.ENGINE_SCOPE); + assertNotNull(engine.getBindings(ScriptContext.ENGINE_SCOPE)); + assertNotNull("Engines Registered through manager sets the global scope", engine.getBindings(ScriptContext.GLOBAL_SCOPE)); + } + + public void testEngineScopeAttributes() { + + ScriptContext context = engine.getContext(); + context.setAttribute("engine-prop1", "engine-value1", ScriptContext.ENGINE_SCOPE); + + assertEquals(context.getAttribute("engine-prop1", ScriptContext.ENGINE_SCOPE), "engine-value1"); + assertNull(context.getAttribute("engine-prop1", ScriptContext.GLOBAL_SCOPE)); + + context.removeAttribute("engine-prop1", ScriptContext.ENGINE_SCOPE); + assertNull(context.getAttribute("engine-prop1", ScriptContext.ENGINE_SCOPE)); + + + } + + public void testGlobalScopeAttributes() { + + ScriptContext context = engine.getContext(); + context.setAttribute("global-prop1", "global-value1", ScriptContext.GLOBAL_SCOPE); + + assertEquals(context.getAttribute("global-prop1", ScriptContext.GLOBAL_SCOPE), "global-value1"); + assertNull(context.getAttribute("global-prop1", ScriptContext.ENGINE_SCOPE)); + + context.removeAttribute("global-prop1", ScriptContext.GLOBAL_SCOPE); + assertNull(context.getAttribute("global-prop1", ScriptContext.GLOBAL_SCOPE)); + + } + + public void testJSRExceptions() { + + ScriptContext context = engine.getContext(); + context.setAttribute("global-prop", "global-value", ScriptContext.GLOBAL_SCOPE); + int invalidScope = 99; + + try { + context.setBindings(null, ScriptContext.ENGINE_SCOPE); + fail("Cannot pass null binding for engine scope"); + } catch (NullPointerException n) { + //Success + } + + try { + context.setBindings(new SimpleBindings(), invalidScope); + fail("Cannot pass invalid scope"); + } catch (IllegalArgumentException n) { + //Success + } + try { + context.getBindings(invalidScope); + fail("Cannot pass invalid scope"); + } catch (IllegalArgumentException n) { + //Success + } + + + try { + context.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); + + fail("Name cannot be null"); + } catch (NullPointerException n) { + //Success + } + try { + context.setAttribute("name1", "value", invalidScope); + + fail("Cannot pass invalid scope"); + } catch (IllegalArgumentException n) { + //Success + } + + + + try { + context.getAttribute(null, ScriptContext.ENGINE_SCOPE); + + fail("Name cannot be null"); + } catch (NullPointerException n) { + //Success + } + try { + context.getAttribute("name1", invalidScope); + + fail("Cannot pass invalid scope"); + } catch (IllegalArgumentException n) { + //Success + } + + + try { + context.removeAttribute(null, ScriptContext.ENGINE_SCOPE); + + fail("Name cannot be null"); + } catch (NullPointerException n) { + //Success + } + try { + context.removeAttribute("name1", invalidScope); + + fail("Cannot pass invalid scope"); + } catch (IllegalArgumentException n) { + //Success + } + + + context.setAttribute("prop2","engine-value2",ScriptContext.ENGINE_SCOPE); + context.setAttribute("prop2","global-value2",ScriptContext.GLOBAL_SCOPE); + assertEquals("Should return lowest scope value binded value",context.getAttribute("prop2"),"engine-value2"); + + } +} diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm new file mode 100644 index 00000000..68e33fe8 --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm @@ -0,0 +1,5 @@ +$event; + +Event Created by $event.getName() +Event Created on $event.getDate() +Event ID is $event.getID() diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java new file mode 100644 index 00000000..b76970ee --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java @@ -0,0 +1,50 @@ +package org.apache.velocity.script.test.tools; +/* + * 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. + */ + +import java.util.Date; + +public class CustomEvent { + + String name = ""; + Date date; + + public CustomEvent(String name) { + this.name = name; + this.date = new Date(); + } + + public String getName() { + return name; + } + + public Date getDate() { + return date; + } + + public long getID(){ + return date.getTime(); + } + + @Override + public String toString(){ + return "This is a test event template: created by " + name + " on " + date.toString(); + } + +}
\ No newline at end of file diff --git a/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java new file mode 100644 index 00000000..eef11eb6 --- /dev/null +++ b/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java @@ -0,0 +1,61 @@ +package org.apache.velocity.script.test.tools; +/* + * 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. + */ + +import org.apache.velocity.script.VelocityScriptEngine; +import org.apache.velocity.script.test.AbstractScriptTest; + +import javax.script.ScriptContext; +import javax.script.ScriptException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Properties; + +public class EventToolTest extends AbstractScriptTest { + + @Override + public void setUp() { + super.setUp(); + super.setupWithDefaultFactory(); + } + + public void testHelloWorldTool() throws ScriptException { + ScriptContext context = engine.getContext(); + Properties properties = new Properties(); + properties.put("resource.loader", "class"); + properties.put("class.resource.loader.description", "Template Class Loader"); + properties.put("class.resource.loader.class", + "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + CustomEvent event = new CustomEvent("MyEvent"); + context.getBindings(ScriptContext.ENGINE_SCOPE).put("event", event); + context.setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, properties, ScriptContext.ENGINE_SCOPE); + context.setAttribute(VelocityScriptEngine.FILENAME, "eventtool.vm", ScriptContext.ENGINE_SCOPE); + Writer writer = new StringWriter(); + context.setWriter(writer); + engine.eval("$event;\n" + + "Event Created by $event.getName()\n" + + "Event Created on $event.getDate()\n" + + "Event ID is $event.getID()"); + // check string start + String check = "This is a test event template: created by MyEvent on "; + assertEquals(writer.toString().substring(0, check.length()), check); + } + + +} |