Search in sources :

Example 26 with SimpleBindings

use of javax.script.SimpleBindings in project sling by apache.

the class SightlyCompiledScriptTest method testEvalSlingBindings.

/**
     * Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
     * attributes.
     * @throws ScriptException
     */
@Test
public void testEvalSlingBindings() throws ScriptException {
    ScriptEngine scriptEngine = mock(ScriptEngine.class);
    final RenderUnit renderUnit = mock(RenderUnit.class);
    Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
    final BundleContext bundleContext = MockOsgi.newBundleContext();
    bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
    ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
    final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
    SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
    ScriptContext scriptContext = mock(ScriptContext.class);
    StringWriter writer = new StringWriter();
    when(scriptContext.getWriter()).thenReturn(writer);
    Bindings scriptContextBindings = new SimpleBindings() {

        {
            put("test", "testValue");
            put(SlingBindings.REQUEST, request);
            put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
        }
    };
    SlingBindings oldBindings = new SlingBindings();
    oldBindings.put("old", "oldValue");
    request.setAttribute(SlingBindings.class.getName(), oldBindings);
    when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
    compiledScript.eval(scriptContext);
    ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
    ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
    // request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
    verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
    List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
    int invocation = 1;
    for (SlingBindings bindings : slingBindingsValues) {
        switch(invocation) {
            case 1:
                assertEquals(oldBindings, bindings);
                break;
            case 2:
                assertEquals(3, bindings.size());
                for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
                    assertEquals(entry.getValue(), bindings.get(entry.getKey()));
                }
                break;
            case 3:
                assertEquals(oldBindings, bindings);
        }
        invocation++;
    }
    for (String key : attributeNameArgumentCaptor.getAllValues()) {
        assertEquals(SlingBindings.class.getName(), key);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 27 with SimpleBindings

use of javax.script.SimpleBindings in project sling by apache.

the class SightlyCompiledScript method eval.

@Override
public Object eval(ScriptContext context) throws ScriptException {
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    Object oldBindings = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        RenderContext renderContext = new RenderContextImpl(context);
        PrintWriter out = new PrintWriter(context.getWriter());
        renderUnit.render(out, renderContext, new SimpleBindings());
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldBindings);
    }
    return null;
}
Also used : RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 28 with SimpleBindings

use of javax.script.SimpleBindings in project sling by apache.

the class RenderUnit method buildGlobalScope.

private Bindings buildGlobalScope(Bindings bindings) {
    SimpleBindings simpleBindings = new SimpleBindings(bindings);
    simpleBindings.putAll(bindings);
    if (siblings != null) {
        simpleBindings.putAll(siblings);
    }
    simpleBindings.putAll(subTemplates);
    return new CaseInsensitiveBindings(simpleBindings);
}
Also used : SimpleBindings(javax.script.SimpleBindings)

Example 29 with SimpleBindings

use of javax.script.SimpleBindings in project JMRI by JMRI.

the class Jdk9Application method getContext.

private ScriptContext getContext(Object handler) {
    Bindings bindings = new SimpleBindings();
    // NOI18N
    bindings.put("handler", handler);
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    return context;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 30 with SimpleBindings

use of javax.script.SimpleBindings in project es6draft by anba.

the class CompilableTest method compileStringWithSimpleBindings.

@Test
public void compileStringWithSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 6);
    assertThat(script.eval(bindings), instanceOfWith(Number.class, is(numberCloseTo(12))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.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