Search in sources :

Example 11 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 12 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 13 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 14 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 15 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)67 Bindings (javax.script.Bindings)47 Test (org.junit.Test)20 ScriptContext (javax.script.ScriptContext)14 SimpleScriptContext (javax.script.SimpleScriptContext)12 Test (org.testng.annotations.Test)11 ScriptException (javax.script.ScriptException)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 StringWriter (java.io.StringWriter)5 HashMap (java.util.HashMap)5 ScriptEngine (javax.script.ScriptEngine)5 PrintWriter (java.io.PrintWriter)4 CompiledScript (javax.script.CompiledScript)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 StringReader (java.io.StringReader)3 Map (java.util.Map)3 ScriptEngineManager (javax.script.ScriptEngineManager)3 Resource (org.apache.sling.api.resource.Resource)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ProtectedBindings (org.apache.sling.scripting.core.impl.helper.ProtectedBindings)3