You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by di...@apache.org on 2012/06/13 19:55:39 UTC

svn commit: r1349973 - in /velocity/sandbox/jsr223/velocity-engine-scripting/src: main/java/org/apache/velocity/script/ test/java/org/ test/java/org/apache/ test/java/org/apache/velocity/ test/java/org/apache/velocity/script/ test/java/org/apache/veloc...

Author: dishara
Date: Wed Jun 13 17:55:39 2012
New Revision: 1349973

URL: http://svn.apache.org/viewvc?rev=1349973&view=rev
Log:
Implemented Velocity Bindings and Added a dummy basic test class 

Added:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/BasicTest.java
Modified:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityBindings.java

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityBindings.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityBindings.java?rev=1349973&r1=1349972&r2=1349973&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityBindings.java (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityBindings.java Wed Jun 13 17:55:39 2012
@@ -23,14 +23,29 @@ import org.apache.velocity.context.Conte
 
 import javax.script.Bindings;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
 public class VelocityBindings implements Bindings,Context {
 
+    private Map<String,Object> map;
+
+    public VelocityBindings(Map<String,Object> map) {
+        if(map == null) {
+          throw new NullPointerException("Cannot pass a null map to initialize VelocityBindings");
+        }
+        this.map = map;
+    }
+
+    public VelocityBindings(){
+        this.map = new HashMap<String, Object>();
+    }
+
 
     public Object put(String s, Object o) {
-        return null;
+        validateKey(s);
+        return map.put(s,o);
     }
 
     /**
@@ -39,57 +54,82 @@ public class VelocityBindings implements
      * @return
      */
     public Object get(String key) {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
+        validateKey(key);
+        return map.get(key);
     }
 
     /**
      * inherits from Context
      */
     public Object[] getKeys() {
-        return new Object[0];  //To change body of implemented methods use File | Settings | File Templates.
+       return map.keySet().toArray();
     }
 
     public void putAll(Map<? extends String, ? extends Object> map) {
-
+         for (Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) {
+             String key = entry.getKey();
+             validateKey(key);
+             put(key, entry.getValue());
+         }
     }
 
     public void clear() {
-
+     map.clear();
     }
 
     public Set<String> keySet() {
-        return null;
+        return map.keySet();
     }
 
     public Collection<Object> values() {
-        return null;
+        return map.values();
     }
 
     public Set<Entry<String, Object>> entrySet() {
-        return null;
+        return map.entrySet();
     }
 
     public int size() {
-        return 0;
+        return map.size();
     }
 
     public boolean isEmpty() {
-        return false;
+        return map.isEmpty();
     }
 
     public boolean containsKey(Object o) {
-        return false;
+        validateKey(o);
+        return map.containsKey(o);
     }
 
     public boolean containsValue(Object o) {
-        return false;
+        return map.containsValue(o);
     }
 
     public Object get(Object o) {
-        return null;
+        validateKey(o);
+        return map.get(o);
     }
 
     public Object remove(Object o) {
-        return null;
+        validateKey(o);
+        return map.remove(o);
     }
+
+ /**
+     *    Validates key and throw corresponding exceptions for JSR 223 compliance
+     * @param key
+     */
+    private void validateKey(Object key) {
+        if (key == null) {
+            throw new NullPointerException("The key cannot be null..!!");
+        }
+        if (!(key instanceof String)) {
+            throw new ClassCastException("The key must be of the type String..!!");
+        }
+        if (key.equals("")) {
+            throw new IllegalArgumentException("The key cannot be empty..!!");
+        }
+    }
+
 }

Added: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/BasicTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/BasicTest.java?rev=1349973&view=auto
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/BasicTest.java (added)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/BasicTest.java Wed Jun 13 17:55:39 2012
@@ -0,0 +1,49 @@
+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 org.apache.velocity.script.VelocityScriptEngineFactory;
+import org.junit.Test;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class BasicTest {
+
+    @Test
+    public void simpleTestCase() throws ScriptException {
+
+        ScriptEngineManager manager = new ScriptEngineManager();
+        manager.registerEngineName("velocity",new VelocityScriptEngineFactory());
+        ScriptEngine engine = manager.getEngineByName("velocity");
+        ScriptEngineFactory fac = engine.getFactory();
+        engine.put("first", "HELLO");
+        engine.put("second", "world");
+        //TODO this is a dummt test infact. Need to complete the rest as engine impl finishes.
+
+
+    }
+
+}