SLING-6173 - HTL compiled scripts need to set the SlingBindings as request attributes on eval

* the SlingBindings are now set as request attributes by compiled HTL scripts
* added unit + integration tests

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1765682 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index f1b0b87..fa7109b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -242,6 +242,12 @@
             <version>1.6.5</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+            <version>2.1.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
index 0f0397b..cf2c9ea 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
@@ -19,13 +19,17 @@
 package org.apache.sling.scripting.sightly.impl.engine;
 
 import java.io.PrintWriter;
+import javax.script.Bindings;
 import javax.script.CompiledScript;
 import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScriptConstants;
+import org.apache.sling.scripting.sightly.SightlyException;
 import org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl;
 import org.apache.sling.scripting.sightly.java.compiler.RenderUnit;
 import org.apache.sling.scripting.sightly.render.RenderContext;
@@ -42,12 +46,21 @@
 
     @Override
     public Object eval(ScriptContext context) throws ScriptException {
-        RenderContext renderContext = new RenderContextImpl(context);
+        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 {
-            renderContext.getBindings().remove(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER);
+            request.setAttribute(SlingBindings.class.getName(), oldBindings);
         }
         return null;
     }
diff --git a/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java b/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
new file mode 100644
index 0000000..00cbf51
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.impl.engine;
+
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import javax.script.SimpleBindings;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.scripting.sightly.java.compiler.RenderUnit;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.sling.MockSling;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.internal.util.reflection.Whitebox;
+import org.osgi.framework.BundleContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.*;
+
+
+public class SightlyCompiledScriptTest {
+
+    @Rule
+    public final SlingContext slingContext = new SlingContext();
+
+    /**
+     * 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);
+        }
+    }
+}