SCXML-234 adding a unit test to test JavaScriptEngine itself to validate if we 
can have a common code to make it work in both Java 1.8 or earlier versions


Project: http://git-wip-us.apache.org/repos/asf/commons-scxml/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-scxml/commit/8691eb95
Tree: http://git-wip-us.apache.org/repos/asf/commons-scxml/tree/8691eb95
Diff: http://git-wip-us.apache.org/repos/asf/commons-scxml/diff/8691eb95

Branch: refs/heads/master
Commit: 8691eb95522d9421a8b2883bf65e535d581f1a06
Parents: 509cb24
Author: Woonsan Ko <[email protected]>
Authored: Sat Jul 11 16:00:52 2015 -0400
Committer: Woonsan Ko <[email protected]>
Committed: Sat Jul 11 16:00:52 2015 -0400

----------------------------------------------------------------------
 .../scxml2/env/javascript/JSBindings.java       | 119 +++++++++++++++----
 .../env/javascript/JavaScriptEngineTest.java    |  66 +++++++++-
 2 files changed, 162 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-scxml/blob/8691eb95/src/main/java/org/apache/commons/scxml2/env/javascript/JSBindings.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/scxml2/env/javascript/JSBindings.java 
b/src/main/java/org/apache/commons/scxml2/env/javascript/JSBindings.java
index f8f7ee0..ec48c12 100644
--- a/src/main/java/org/apache/commons/scxml2/env/javascript/JSBindings.java
+++ b/src/main/java/org/apache/commons/scxml2/env/javascript/JSBindings.java
@@ -35,6 +35,8 @@ import org.apache.commons.scxml2.Context;
  */
 public class JSBindings implements Bindings {
 
+    private static final String NASHORN_GLOBAL = "nashorn.global";
+
     // INSTANCE VARIABLES
 
     private Bindings bindings;
@@ -52,7 +54,7 @@ public class JSBindings implements Bindings {
      *         or <code>bindings</code> is <code>null</code>.
      *
      */
-    public JSBindings(Context context,Bindings bindings) {
+    public JSBindings(Context context, Bindings bindings) {
         // ... validate
 
         if (context == null) {
@@ -63,10 +65,10 @@ public class JSBindings implements Bindings {
            throw new IllegalArgumentException("Invalid script Bindings");
         }
 
-         // ... initialise
+        // ... initialise
 
-         this.bindings = bindings;
-         this.context  = context;
+        this.bindings = bindings;
+        this.context = context;
     }
 
     // INSTANCE METHODS
@@ -79,8 +81,13 @@ public class JSBindings implements Bindings {
      */
     @Override
     public boolean containsKey(Object key) {
-        if (bindings.containsKey(key))
-           return true;
+        if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
+            return true;
+        }
+
+        if (bindings.containsKey(key)) {
+            return true;
+        }
 
         return context.has(key.toString());
     }
@@ -99,6 +106,10 @@ public class JSBindings implements Bindings {
         keys.addAll(context.getVars().keySet());
         keys.addAll(bindings.keySet());
 
+        if (hasGlobalBindings()) {
+            keys.addAll(getGlobalBindings().keySet());
+        }
+
         return keys;
     }
 
@@ -117,6 +128,10 @@ public class JSBindings implements Bindings {
         keys.addAll(context.getVars().keySet());
         keys.addAll(bindings.keySet());
 
+        if (hasGlobalBindings()) {
+            keys.addAll(getGlobalBindings().keySet());
+        }
+
         return keys.size();
     }
 
@@ -129,8 +144,13 @@ public class JSBindings implements Bindings {
      */
     @Override
     public boolean containsValue(Object value) {
-        if (bindings.containsValue(value))
-           return true;
+        if (hasGlobalBindings() && getGlobalBindings().containsValue(value)) {
+            return true;
+        }
+
+        if (bindings.containsValue(value)) {
+            return true;
+        }
 
         return context.getVars().containsValue(value);
     }
@@ -168,8 +188,13 @@ public class JSBindings implements Bindings {
      */
     @Override
     public boolean isEmpty() {
-        if (!bindings.isEmpty())
-           return false;
+        if (hasGlobalBindings() && !getGlobalBindings().isEmpty()) {
+            return false;
+        }
+
+        if (!bindings.isEmpty()) {
+            return false;
+        }
 
         return context.getVars().isEmpty();
     }
@@ -181,8 +206,13 @@ public class JSBindings implements Bindings {
      */
     @Override
     public Object get(Object key) {
-        if (bindings.containsKey(key))
-           return bindings.get(key);
+        if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
+            return getGlobalBindings().get(key);
+        }
+
+        if (bindings.containsKey(key)) {
+            return bindings.get(key);
+        }
 
         return context.get(key.toString());
     }
@@ -201,13 +231,17 @@ public class JSBindings implements Bindings {
     @Override
     public Object put(String name, Object value) {
         Object old = context.get(name);
+
         if (context.has(name)) {
             context.set(name, value);
         } else if (bindings.containsKey(name)) {
             return bindings.put(name,value);
+        } else if (hasGlobalBindings() && 
getGlobalBindings().containsKey(name)) {
+            return getGlobalBindings().put(name, value);
         } else {
             context.setLocal(name, value);
         }
+
         return old;
     }
 
@@ -220,7 +254,7 @@ public class JSBindings implements Bindings {
      */
     @Override
     public void putAll(Map<? extends String, ? extends Object> list) {
-            bindings.putAll(list);
+        bindings.putAll(list);
     }
 
     /**
@@ -234,11 +268,17 @@ public class JSBindings implements Bindings {
      */
     @Override
     public Object remove(Object key) {
-        if (bindings.containsKey(key))
-           return bindings.remove(key);
+        if (hasGlobalBindings() && getGlobalBindings().containsKey(key)) {
+            getGlobalBindings().remove(key);
+        }
 
-        if (context.has(key.toString()))
-           return context.getVars().remove(key);
+        if (bindings.containsKey(key)) {
+            return bindings.remove(key);
+        }
+
+        if (context.has(key.toString())) {
+            return context.getVars().remove(key);
+        }
 
         return Boolean.FALSE;
     }
@@ -252,7 +292,7 @@ public class JSBindings implements Bindings {
      */
     @Override
     public void clear() {
-            bindings.clear();
+        bindings.clear();
     }
 
     /**
@@ -265,11 +305,48 @@ public class JSBindings implements Bindings {
 
         set.putAll(context.getVars());
 
-        for (String key: bindings.keySet())
-            set.put(key,bindings.get(key));
+        for (String key : bindings.keySet()) {
+            set.put(key, bindings.get(key));
+        }
+
+        if (hasGlobalBindings()) {
+            for (String key : getGlobalBindings().keySet()) {
+                set.put(key, getGlobalBindings().get(key));
+            }
+        }
 
         return set;
     }
 
-}
+    /**
+     * Return true if a global bindings (i.e. nashorn Global instance) was 
ever set by the script engine.
+     * <p>
+     * Note: because the global binding can be set by the script engine when 
evaluating a script, we should
+     *       check or retrieve the global binding whenever needed instead of 
initialization time.
+     * </p>
+     * @return true if a global bindings (i.e. nashorn Global instance) was 
ever set by the script engine
+     */
+    private boolean hasGlobalBindings() {
+        if (bindings.containsKey(NASHORN_GLOBAL)) {
+            return true;
+        }
 
+        return context.has(NASHORN_GLOBAL);
+    }
+
+    /**
+     * Return the global bindings (i.e. nashorn Global instance) set by the 
script engine if existing.
+     * @return the global bindings (i.e. nashorn Global instance) set by the 
script engine, or null if not existing.
+     */
+    private Bindings getGlobalBindings() {
+        if (bindings.containsKey(NASHORN_GLOBAL)) {
+            return (Bindings) bindings.get(NASHORN_GLOBAL);
+        }
+
+        if (context.has(NASHORN_GLOBAL)) {
+            return (Bindings) context.get(NASHORN_GLOBAL);
+        }
+
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-scxml/blob/8691eb95/src/test/java/org/apache/commons/scxml2/env/javascript/JavaScriptEngineTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/scxml2/env/javascript/JavaScriptEngineTest.java
 
b/src/test/java/org/apache/commons/scxml2/env/javascript/JavaScriptEngineTest.java
index 3ff6aef..c1bafaf 100644
--- 
a/src/test/java/org/apache/commons/scxml2/env/javascript/JavaScriptEngineTest.java
+++ 
b/src/test/java/org/apache/commons/scxml2/env/javascript/JavaScriptEngineTest.java
@@ -16,17 +16,79 @@
  */
 package org.apache.commons.scxml2.env.javascript;
 
+import static org.junit.Assert.assertEquals;
+
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.apache.commons.scxml2.Context;
 import org.junit.Before;
 import org.junit.Test;
 
 public class JavaScriptEngineTest {
 
+    private ScriptEngine engine;
+
+    private Context context;
+
     @Before
     public void before() throws Exception {
-        
+        ScriptEngineManager factory = new ScriptEngineManager();
+        engine = factory.getEngineByName("JavaScript");
+        context = new JSContext();
+    }
+
+    @Test
+    public void testSimpleEvaluation() throws Exception {
+        Object ret = engine.eval("1.0 + 2.0");
+        assertEquals(3.0, ret);
+    }
+
+    @Test
+    public void testBindingsInput() throws Exception {
+        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
+        bindings.put("x", 1.0);
+        bindings.put("y", 2.0);
+
+        Object ret = engine.eval("x + y;", bindings);
+        assertEquals(3.0, ret);
+    }
+
+    @Test
+    public void testBindingsInput_WithJSBindings() throws Exception {
+        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
+        JSBindings jsBindings = new JSBindings(context, bindings);
+        jsBindings.put("x", 1.0);
+        jsBindings.put("y", 2.0);
+
+        Object ret = engine.eval("x + y;", jsBindings);
+        assertEquals(3.0, ret);
     }
 
     @Test
-    public void testX() throws Exception {
+    public void testBindingsGlobal() throws Exception {
+        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
+        bindings.put("x", 1.0);
+        bindings.put("y", 2.0);
+        bindings.put("z", 0.0);
+
+        engine.eval("z = x + y;", bindings);
+        assertEquals("z variable is expected to set to 3.0 in global, but it 
was " + bindings.get("z") + ".",
+                     3.0, bindings.get("z"));
+    }
+
+    @Test
+    public void testBindingsGlobal_WithJSBindings() throws Exception {
+        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
+        JSBindings jsBindings = new JSBindings(context, bindings);
+        jsBindings.put("x", 1.0);
+        jsBindings.put("y", 2.0);
+        jsBindings.put("z", 0.0);
+
+        engine.eval("z = x + y;", jsBindings);
+        assertEquals("z variable is expected to set to 3.0 in global, but it 
was " + jsBindings.get("z") + ".",
+                     3.0, jsBindings.get("z"));
     }
 }

Reply via email to