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