View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2.env.javascript;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import javax.script.Bindings;
22  import javax.script.ScriptContext;
23  import javax.script.ScriptEngine;
24  import javax.script.ScriptEngineManager;
25  
26  import org.apache.commons.scxml2.Context;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  public class JavaScriptEngineTest {
31  
32      private ScriptEngine engine;
33  
34      private Context context;
35  
36      @Before
37      public void before() throws Exception {
38          ScriptEngineManager factory = new ScriptEngineManager();
39          engine = factory.getEngineByName("JavaScript");
40          context = new JSContext();
41      }
42  
43      @Test
44      public void testSimpleEvaluation() throws Exception {
45          Object ret = engine.eval("1.0 + 2.0");
46          assertEquals(3.0, ret);
47      }
48  
49      @Test
50      public void testBindingsInput() throws Exception {
51          Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
52          bindings.put("x", 1.0);
53          bindings.put("y", 2.0);
54  
55          Object ret = engine.eval("x + y;", bindings);
56          assertEquals(3.0, ret);
57      }
58  
59      @Test
60      public void testBindingsInput_WithJSBindings() throws Exception {
61          Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
62          JSBindings jsBindings = new JSBindings(context, bindings);
63          jsBindings.put("x", 1.0);
64          jsBindings.put("y", 2.0);
65  
66          Object ret = engine.eval("x + y;", jsBindings);
67          assertEquals(3.0, ret);
68      }
69  
70      @Test
71      public void testBindingsGlobal() throws Exception {
72          Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
73          bindings.put("x", 1.0);
74          bindings.put("y", 2.0);
75          bindings.put("z", 0.0);
76  
77          engine.eval("z = x + y;", bindings);
78          assertEquals("z variable is expected to set to 3.0 in global, but it was " + bindings.get("z") + ".",
79                       3.0, bindings.get("z"));
80      }
81  
82      @Test
83      public void testBindingsGlobal_WithJSBindings() throws Exception {
84          Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
85          JSBindings jsBindings = new JSBindings(context, bindings);
86          jsBindings.put("x", 1.0);
87          jsBindings.put("y", 2.0);
88          jsBindings.put("z", 0.0);
89  
90          engine.eval("z = x + y;", jsBindings);
91          assertEquals("z variable is expected to set to 3.0 in global, but it was " + jsBindings.get("z") + ".",
92                       3.0, jsBindings.get("z"));
93      }
94  }