Search in sources :

Example 1 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldOverrideScriptVariablesWithParameterVariables.

@Test
public void shouldOverrideScriptVariablesWithParameterVariables() throws Exception {
    // Given
    String varName = "testVar";
    String paramValue = "param value";
    String scriptValue = "script value";
    Bindings scriptBindings = new SimpleBindings();
    scriptBindings.put(varName, scriptValue);
    ScriptObject script = getJavascript(scriptBindings, varName);
    Bindings paramBindings = new SimpleBindings();
    paramBindings.put(varName, paramValue);
    // When
    String result = testEvaluator.evaluateScript(script, paramBindings);
    // Then
    assertThat(result).isEqualTo(paramValue);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 2 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldExposeScriptVariables.

@Test
public void shouldExposeScriptVariables() throws Exception {
    // Given
    String varName = "scriptVar";
    String value = "a script value";
    Bindings bindings = new SimpleBindings();
    bindings.put(varName, value);
    ScriptObject script = getJavascript(bindings, varName);
    // When
    String result = testEvaluator.evaluateScript(script, null);
    // Then
    assertThat(result).isEqualTo(value);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 3 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldSupportJSONParsing.

@Test
public void shouldSupportJSONParsing() throws Exception {
    ScriptObject script = getJavascript("var json = JSON.parse(x)", "json['a']");
    Bindings scope = new SimpleBindings();
    scope.put("x", "{\"a\" : 12}");
    Object result = testEvaluator.evaluateScript(script, scope);
    assertThat(result).isEqualTo(12);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 4 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldPassBindingsByReference.

/**
     * Ensure that binding scopes are passed by reference to the script engine so that any changes made by the script
     * are reflected in the final state of the bindings passed in.
     */
@Test
public void shouldPassBindingsByReference() throws Exception {
    // Given
    String varName = "state";
    Bindings scope = new SimpleBindings();
    scope.put(varName, "initial");
    String expected = "expected";
    ScriptObject script = getJavascript(varName + " = '" + expected + "'");
    // When
    testEvaluator.evaluateScript(script, scope);
    // Then
    assertThat(scope.get(varName)).isEqualTo(expected);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 5 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldSupportGroovyScripts.

@Test
public void shouldSupportGroovyScripts() throws Exception {
    // Given
    String varName = "state";
    String expected = "expected";
    ScriptObject groovyScript = getGroovyScript(varName + " = \"" + expected + "\"");
    Bindings scope = new SimpleBindings();
    scope.put(varName, "initial");
    // When
    testEvaluator.evaluateScript(groovyScript, scope);
    // Then
    assertThat(scope.get(varName)).isEqualTo(expected);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Aggregations

SimpleBindings (javax.script.SimpleBindings)103 Bindings (javax.script.Bindings)66 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4