velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dish...@apache.org
Subject svn commit: r1367860 - in /velocity/sandbox/jsr223/velocity-engine-scripting/src: main/java/org/apache/velocity/script/ test/java/org/apache/velocity/script/test/ test/java/org/apache/velocity/script/test/resources/ test/java/org/apache/velocity/script...
Date Wed, 01 Aug 2012 05:32:37 GMT
Author: dishara
Date: Wed Aug  1 05:32:37 2012
New Revision: 1367860

URL: http://svn.apache.org/viewvc?rev=1367860&view=rev
Log:
Adding velocity tools evaluation capability to JSR 223 scripting API

Added:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
Modified:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineFactoryTest.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java?rev=1367860&r1=1367859&r2=1367860&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
(original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
Wed Aug  1 05:32:37 2012
@@ -19,10 +19,9 @@ package org.apache.velocity.script;
  * under the License.
  */
 
-import com.sun.org.apache.bcel.internal.generic.RET;
+import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
-
 import javax.script.*;
 import java.io.*;
 import java.util.Properties;
@@ -228,7 +227,14 @@ public class VelocityScriptEngine implem
         boolean result;
 
         try {
-         result = velocityEngine.evaluate(velocityContext,outPut,fileName,reader);
+            BufferedReader bf = new BufferedReader(reader);
+            String vm = bf.readLine();
+            //Check for velocity tools vm file
+            if(vm != null && vm.endsWith(".vm")) {
+                Template template = velocityEngine.getTemplate(vm);
+                template.merge(velocityContext,outPut);
+            }
+           result = velocityEngine.evaluate(velocityContext,outPut,fileName,reader);
         } catch(Exception exp) {
             return new ScriptException(exp);
         }

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java?rev=1367860&r1=1367859&r2=1367860&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java
(original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/AbstractScriptTest.java
Wed Aug  1 05:32:37 2012
@@ -9,9 +9,9 @@ import javax.script.ScriptEngineFactory;
 import javax.script.ScriptEngineManager;
 
 public abstract class AbstractScriptTest  extends TestCase {
-    ScriptEngine engine;
-    ScriptEngineFactory engineFactory;
-    ScriptEngineManager manager;
+   protected ScriptEngine engine;
+   protected  ScriptEngineFactory engineFactory;
+   protected  ScriptEngineManager manager;
 
     @Override
     public void setUp() {

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineFactoryTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineFactoryTest.java?rev=1367860&r1=1367859&r2=1367860&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineFactoryTest.java
(original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineFactoryTest.java
Wed Aug  1 05:32:37 2012
@@ -30,7 +30,6 @@ public class ScriptEngineFactoryTest ext
 
     @Override
     public void setUp() {
-        System.out.println("------- Velocity scripting test framework started : ScriptEngineFactoryTest
--------");
         super.setUp();
         super.setupEngine(constructFactory());
     }

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java?rev=1367860&r1=1367859&r2=1367860&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
(original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
Wed Aug  1 05:32:37 2012
@@ -28,7 +28,6 @@ public class ScriptEngineTest extends Ab
 
     @Override
     public void setUp() {
-        System.out.println("------- Velocity scripting test framework started : VelocityScriptContextTest
--------");
         super.setUp();
         super.setupWithDefaultFactory();
     }
@@ -69,7 +68,14 @@ public class ScriptEngineTest extends Ab
                 "src/test/java/org/apache/velocity/script/test/resources/velocity.properties";
         System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES,  propertyFile) ;
         //Comment test case
-        Object result = engine.eval("## This is a comment ");
+        String script = "<html>\n" +
+                "<body>\n" +
+                "#set( $foo = \"Velocity\" )\n" +
+                "Hello $foo World!\n" +
+                "</body>\n" +
+                "<html>";
+//        String script = "## This is a comment ";
+        Object result = engine.eval(script);
         assertTrue(Boolean.valueOf(result.toString()));
 
         //TODO add more engine script evaluation test cases

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java?rev=1367860&r1=1367859&r2=1367860&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java
(original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/VelocityScriptContextTest.java
Wed Aug  1 05:32:37 2012
@@ -28,7 +28,6 @@ public class VelocityScriptContextTest e
 
     @Override
     public void setUp() {
-        System.out.println("------- Velocity scripting test framework started : VelocityScriptContextTest
--------");
         super.setUp();
         super.setupWithDefaultFactory();
     }

Added: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm?rev=1367860&view=auto
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
(added)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
Wed Aug  1 05:32:37 2012
@@ -0,0 +1,5 @@
+$event;
+
+Event Created by $event.getName()
+Event Created on $event.getDate()
+

Added: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java?rev=1367860&view=auto
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
(added)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
Wed Aug  1 05:32:37 2012
@@ -0,0 +1,46 @@
+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;
+    }
+
+    @Override
+    public String toString(){
+        return "This is a test event template: created by " + name + " on " + date.toString();
+    }
+
+}
\ No newline at end of file

Added: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java?rev=1367860&view=auto
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
(added)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
Wed Aug  1 05:32:37 2012
@@ -0,0 +1,54 @@
+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, properties, ScriptContext.ENGINE_SCOPE);
+        Writer writer = new StringWriter();
+        context.setWriter(writer);
+        engine.eval("eventtool.vm");
+        System.out.println("####### Tools output #########\n"+writer);
+    }
+
+
+}



Mime
View raw message