Author: radu
Date: Wed Oct 19 16:54:26 2016
New Revision: 1765682
URL: http://svn.apache.org/viewvc?rev=1765682&view=rev
Log:
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
Added:
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
Modified:
sling/trunk/bundles/scripting/sightly/engine/pom.xml
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
sling/trunk/bundles/scripting/sightly/testing-content/pom.xml
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/package-info.java
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
sling/trunk/bundles/scripting/sightly/testing/pom.xml
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
Modified: sling/trunk/bundles/scripting/sightly/engine/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/pom.xml?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/pom.xml (original)
+++ sling/trunk/bundles/scripting/sightly/engine/pom.xml Wed Oct 19 16:54:26
2016
@@ -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>
Modified:
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
(original)
+++
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScript.java
Wed Oct 19 16:54:26 2016
@@ -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 @@ public class SightlyCompiledScript exten
@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;
}
Added:
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java?rev=1765682&view=auto
==============================================================================
---
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
(added)
+++
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyCompiledScriptTest.java
Wed Oct 19 16:54:26 2016
@@ -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);
+ }
+ }
+}
Modified: sling/trunk/bundles/scripting/sightly/testing-content/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/pom.xml?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/testing-content/pom.xml (original)
+++ sling/trunk/bundles/scripting/sightly/testing-content/pom.xml Wed Oct 19
16:54:26 2016
@@ -192,11 +192,18 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
- <version>1.1.0</version>
+ <version>1.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -207,8 +214,20 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
+
<artifactId>org.apache.sling.scripting.sightly.compiler.java</artifactId>
+ <version>1.0.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.scripting.sightly</artifactId>
- <version>1.0.2</version>
+ <version>1.0.24</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.4.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Modified:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
(original)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/RequestModel.java
Wed Oct 19 16:54:26 2016
@@ -22,9 +22,11 @@ import javax.inject.Inject;
import javax.inject.Named;
import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.models.annotations.Model;
-import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.Via;
+import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
@Model(adaptables = SlingHttpServletRequest.class)
public class RequestModel {
@@ -36,6 +38,10 @@ public class RequestModel {
// get it from request attributes
private String requestArgument;
+ @ScriptVariable
+ private ValueMap properties;
+ private String jcrType;
+
public String getTitle() {
return title != null ? title : "FAILED";
}
@@ -43,4 +49,9 @@ public class RequestModel {
public String getRequestArgument() {
return requestArgument != null ? requestArgument : "FAILED";
}
+
+ public String getJCRType() {
+ return PropertiesUtil.toString(properties.get("jcr:primaryType"),
"FAILED");
+ }
+
}
Modified:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/package-info.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/package-info.java?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/package-info.java
(original)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/java/org/apache/sling/scripting/sightly/testing/models/package-info.java
Wed Oct 19 16:54:26 2016
@@ -15,7 +15,7 @@
* limitations under the License.
******************************************************************************/
-@Version("1.1.0")
+@Version("1.2.0")
package org.apache.sling.scripting.sightly.testing.models;
import org.osgi.annotation.versioning.Version;
Modified:
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
(original)
+++
sling/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/use.html
Wed Oct 19 16:54:26 2016
@@ -30,6 +30,7 @@
data-sly-use.testEnum="TestEnumHolder">
<div id="reqmodel">${reqmodel.title}</div>
<div id="reqmodel-reqarg">${reqmodel.requestArgument}</div>
+ <div id="reqmodel-bindings">${reqmodel.getJCRType}</div>
<div id="resmodel">${resmodel.title}</div>
<div id="reqadapt">${reqadapt.title}</div>
<div id="resadapt">${resadapt.title}</div>
Modified: sling/trunk/bundles/scripting/sightly/testing/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing/pom.xml?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/testing/pom.xml (original)
+++ sling/trunk/bundles/scripting/sightly/testing/pom.xml Wed Oct 19 16:54:26
2016
@@ -1,24 +1,23 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<!--
- 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.
--->
+<?xml version="1.0"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
-
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
@@ -49,9 +48,6 @@
<!-- path suffix for HTTP access to Sling -->
<http.base.path/>
- <!-- path suffix for WebDAV access to the repository -->
- <webdav.workspace.path/>
-
<!-- hostname for integration tests -->
<test.host>localhost</test.host>
@@ -137,7 +133,6 @@
<systemPropertyVariables>
<io.sightly.tck.serverURL>http://${test.host}:${http.port}</io.sightly.tck.serverURL>
<launchpad.http.server.url>http://${test.host}:${http.port}/</launchpad.http.server.url>
-
<launchpad.webdav.server.url>http://${test.host}:${http.port}/${webdav.workspace.path}</launchpad.webdav.server.url>
<launchpad.servlet.context>${http.base.path}</launchpad.servlet.context>
</systemPropertyVariables>
</configuration>
Modified:
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java?rev=1765682&r1=1765681&r2=1765682&view=diff
==============================================================================
---
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
(original)
+++
sling/trunk/bundles/scripting/sightly/testing/src/test/java/org/apache/sling/scripting/sightly/it/SlingSpecificsSightlyIT.java
Wed Oct 19 16:54:26 2016
@@ -75,6 +75,7 @@ public class SlingSpecificsSightlyIT {
String pageContent = client.getStringContent(url, 200);
assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent,
"#reqmodel"));
assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent,
"#reqmodel-reqarg"));
+ assertEquals("nt:unstructured", HTMLExtractor.innerHTML(url,
pageContent, "#reqmodel-bindings"));
assertEquals("SUCCESS", HTMLExtractor.innerHTML(url, pageContent,
"#resmodel"));
}
@@ -183,9 +184,11 @@ public class SlingSpecificsSightlyIT {
String pageContent = client.getStringContent(url, 200);
assertEquals("original", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#repopojo"));
uploadFile("RepoPojo.java.updated", "RepoPojo.java",
"/apps/sightly/scripts/use");
+ Thread.sleep(1000);
pageContent = client.getStringContent(url, 200);
assertEquals("updated", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#repopojo"));
uploadFile("RepoPojo.java.original", "RepoPojo.java",
"/apps/sightly/scripts/use");
+ Thread.sleep(1000);
pageContent = client.getStringContent(url, 200);
assertEquals("original", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#repopojo"));
}
@@ -196,9 +199,11 @@ public class SlingSpecificsSightlyIT {
String pageContent = client.getStringContent(url, 200);
assertEquals("Hello world!", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#update"));
uploadFile("update.v2.html", "update.html",
"/apps/sightly/scripts/update");
+ Thread.sleep(1000);
pageContent = client.getStringContent(url, 200);
assertEquals("Hello, John Doe!", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#update"));
uploadFile("update.html", "update.html",
"/apps/sightly/scripts/update");
+ Thread.sleep(1000);
pageContent = client.getStringContent(url, 200);
assertEquals("Hello world!", HTMLExtractor.innerHTML(url +
System.currentTimeMillis(), pageContent, "#update"));
}