Search in sources :

Example 51 with SimpleBindings

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

the class AbstractSandboxTests method shouldNotBeAllowedToAccessBlackListedMember.

@Test
public void shouldNotBeAllowedToAccessBlackListedMember() throws Exception {
    // Given
    Allowed allowed = new Allowed();
    Bindings bindings = new SimpleBindings();
    bindings.put("allowed", allowed);
    // When
    try {
        eval(bindings, "allowed.forbiddenFruit.setDirty()");
        fail("Sandbox failed to protect access to black-listed member");
    } catch (ScriptException ex) {
        // Then
        assertThat(allowed.fruit.dirty).isFalse();
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 52 with SimpleBindings

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

the class StandardScriptEvaluatorTest method shouldSupportJSONParsingInGroovy.

@Test
public void shouldSupportJSONParsingInGroovy() throws Exception {
    ScriptObject script = getGroovyScript("import groovy.json.JsonSlurper", "def slurper = new JsonSlurper()", "def json = slurper.parseText(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 53 with SimpleBindings

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

the class ThreadPoolScriptEvaluatorTest method shouldDelegateToConfiguredScriptEvaluator.

@Test
public void shouldDelegateToConfiguredScriptEvaluator() throws Exception {
    testEvaluator = new ThreadPoolScriptEvaluator(scriptEngineManager, Executors.newSingleThreadExecutor(), mockEvaluator);
    ScriptObject testScript = getGroovyScript("x + 1");
    Bindings bindings = new SimpleBindings();
    bindings.put("x", 3);
    int expectedResult = 42;
    given(mockEvaluator.evaluateScript(testScript, bindings)).willReturn(expectedResult);
    // When
    Number result = testEvaluator.evaluateScript(testScript, bindings);
    // Then
    verify(mockEvaluator).evaluateScript(testScript, bindings);
    assertThat(result.intValue()).isEqualTo(expectedResult);
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Example 54 with SimpleBindings

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

the class StandardScriptEvaluatorTest method shouldOverrideGlobalVariablesWithScriptVariables.

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

Example 55 with SimpleBindings

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

the class StandardScriptEvaluatorTest method shouldExposeParameterVariables.

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

Aggregations

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 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