Search in sources :

Example 41 with SimpleScriptContext

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

the class SandboxedGroovyScriptEngineTest method shouldUnregisterEngineAfterException.

@Test(expectedExceptions = ScriptException.class)
public void shouldUnregisterEngineAfterException() throws Exception {
    // Given
    String script = "1 + 1";
    ScriptContext context = new SimpleScriptContext();
    given(mockScriptEngine.eval(script, context)).willThrow(new ScriptException("test"));
    // When
    try {
        testEngine.eval(script, context);
    } finally {
        // Then
        verify(mockValueFilter).unregister();
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 42 with SimpleScriptContext

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

the class SandboxedGroovyScriptEngineTest method shouldApplySandboxToCompiledScripts.

@Test
public void shouldApplySandboxToCompiledScripts() throws Exception {
    // Given
    String script = "1 + 1";
    ScriptContext context = new SimpleScriptContext();
    CompiledScript mockCompiledScript = mock(CompiledScript.class);
    given(mockScriptEngine.compile(script)).willReturn(mockCompiledScript);
    // When
    CompiledScript compiledScript = testEngine.compile(script);
    compiledScript.eval(context);
    // Then
    verify(mockValueFilter).register();
    verify(mockCompiledScript).eval(context);
    verify(mockValueFilter).unregister();
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 43 with SimpleScriptContext

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

the class SandboxedGroovyScriptEngineTest method shouldRegisterSandboxWhenEvaluatingScriptAsString.

@Test
public void shouldRegisterSandboxWhenEvaluatingScriptAsString() throws Exception {
    // Given
    String script = "1 + 1";
    ScriptContext context = new SimpleScriptContext();
    // When
    testEngine.eval(script, context);
    // Then
    verify(mockValueFilter).register();
    verify(mockScriptEngine).eval(script, context);
    verify(mockValueFilter).unregister();
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 44 with SimpleScriptContext

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

the class SandboxedGroovyScriptEngineTest method shouldRegisterSandboxWhenEvaluatingScriptAsReader.

@Test
public void shouldRegisterSandboxWhenEvaluatingScriptAsReader() throws Exception {
    // Given
    String script = "1 + 1";
    Reader reader = new StringReader(script);
    ScriptContext context = new SimpleScriptContext();
    // When
    testEngine.eval(reader, context);
    // Then
    verify(mockValueFilter).register();
    verify(mockScriptEngine).eval(reader, context);
    verify(mockValueFilter).unregister();
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 45 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project opennms by OpenNMS.

the class GraphMLEdgeStatusProvider method computeEdgeStatus.

private GraphMLEdgeStatus computeEdgeStatus(final List<StatusScript> scripts, final GraphMLEdge edge) {
    return scripts.stream().flatMap(script -> {
        final SimpleBindings bindings = createBindings(edge);
        final StringWriter writer = new StringWriter();
        final ScriptContext context = new SimpleScriptContext();
        context.setWriter(writer);
        context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
        try {
            LOG.debug("Executing script: {}", script);
            final GraphMLEdgeStatus status = script.eval(context);
            if (status != null) {
                return Stream.of(status);
            } else {
                return Stream.empty();
            }
        } catch (final ScriptException e) {
            LOG.error("Failed to execute script: {}", e);
            return Stream.empty();
        } finally {
            LOG.info(writer.toString());
        }
    }).reduce(GraphMLEdgeStatus::merge).orElse(null);
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2