001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env.javascript;
018
019import static org.junit.Assert.assertEquals;
020
021import javax.script.Bindings;
022import javax.script.ScriptContext;
023import javax.script.ScriptEngine;
024import javax.script.ScriptEngineManager;
025
026import org.apache.commons.scxml2.Context;
027import org.junit.Before;
028import org.junit.Test;
029
030public class JavaScriptEngineTest {
031
032    private ScriptEngine engine;
033
034    private Context context;
035
036    @Before
037    public void before() throws Exception {
038        ScriptEngineManager factory = new ScriptEngineManager();
039        engine = factory.getEngineByName("JavaScript");
040        context = new JSContext();
041    }
042
043    @Test
044    public void testSimpleEvaluation() throws Exception {
045        Object ret = engine.eval("1.0 + 2.0");
046        assertEquals(3.0, ret);
047    }
048
049    @Test
050    public void testBindingsInput() throws Exception {
051        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
052        bindings.put("x", 1.0);
053        bindings.put("y", 2.0);
054
055        Object ret = engine.eval("x + y;", bindings);
056        assertEquals(3.0, ret);
057    }
058
059    @Test
060    public void testBindingsInput_WithJSBindings() throws Exception {
061        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
062        JSBindings jsBindings = new JSBindings(context, bindings);
063        jsBindings.put("x", 1.0);
064        jsBindings.put("y", 2.0);
065
066        Object ret = engine.eval("x + y;", jsBindings);
067        assertEquals(3.0, ret);
068    }
069
070    @Test
071    public void testBindingsGlobal() throws Exception {
072        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
073        bindings.put("x", 1.0);
074        bindings.put("y", 2.0);
075        bindings.put("z", 0.0);
076
077        engine.eval("z = x + y;", bindings);
078        assertEquals("z variable is expected to set to 3.0 in global, but it was " + bindings.get("z") + ".",
079                     3.0, bindings.get("z"));
080    }
081
082    @Test
083    public void testBindingsGlobal_WithJSBindings() throws Exception {
084        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
085        JSBindings jsBindings = new JSBindings(context, bindings);
086        jsBindings.put("x", 1.0);
087        jsBindings.put("y", 2.0);
088        jsBindings.put("z", 0.0);
089
090        engine.eval("z = x + y;", jsBindings);
091        assertEquals("z variable is expected to set to 3.0 in global, but it was " + jsBindings.get("z") + ".",
092                     3.0, jsBindings.get("z"));
093    }
094}