Search in sources :

Example 41 with ScriptContext

use of javax.script.ScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test4.

private static void test4() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("function foo() { print('bar') };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context = new SimpleScriptContext();
    context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("foo();", context);
    System.out.println(context.getAttribute("foo"));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 42 with ScriptContext

use of javax.script.ScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test5.

private static void test5() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var obj = { foo: 'foo' };");
    engine.eval("function printFoo() { print(obj.foo) };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("obj.foo = 'bar';", context1);
    engine.eval("printFoo();", context1);
    engine.eval("printFoo();", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 43 with ScriptContext

use of javax.script.ScriptContext in project zaproxy by zaproxy.

the class ScriptVarsUnitTest method shouldNotSetScriptCustomVariableWithNullKeyUsingScriptContext.

@Test
void shouldNotSetScriptCustomVariableWithNullKeyUsingScriptContext() {
    // Given
    ScriptContext scriptContext = createScriptContextWithName("ScriptName");
    // When / Then
    assertThrows(IllegalArgumentException.class, () -> ScriptVars.setScriptCustomVar(scriptContext, null, createCustomValue()));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.jupiter.api.Test)

Example 44 with ScriptContext

use of javax.script.ScriptContext in project zaproxy by zaproxy.

the class ScriptVarsUnitTest method shouldNotSetScriptVariableWithInvalidKeyLengthUsingScriptContext.

@Test
void shouldNotSetScriptVariableWithInvalidKeyLengthUsingScriptContext() {
    // Given
    ScriptContext scriptContext = createScriptContextWithName("ScriptName");
    // When / Then
    assertThrows(IllegalArgumentException.class, () -> ScriptVars.setScriptVar(scriptContext, createKeyWithInvalidLength(), createValue()));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.jupiter.api.Test)

Example 45 with ScriptContext

use of javax.script.ScriptContext in project zaproxy by zaproxy.

the class ScriptVarsUnitTest method shouldNotSetScriptCustomVariableUsingNonStringScriptNameInScriptContext.

@Test
void shouldNotSetScriptCustomVariableUsingNonStringScriptNameInScriptContext() {
    // Given
    ScriptContext scriptContext = createScriptContextWithName(10);
    // When / Then
    assertThrows(IllegalArgumentException.class, () -> ScriptVars.setScriptCustomVar(scriptContext, createKey(), createCustomValue()));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.jupiter.api.Test)

Aggregations

ScriptContext (javax.script.ScriptContext)124 SimpleScriptContext (javax.script.SimpleScriptContext)81 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptException (javax.script.ScriptException)17 ScriptEngine (javax.script.ScriptEngine)16 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)8 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5