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();
}
}
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();
}
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();
}
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();
}
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);
}
Aggregations