SLING-428 - integration tests for ESP load() and print() functions

git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk@690209 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/EspLoadTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/EspLoadTest.java
new file mode 100644
index 0000000..fc276c2
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/EspLoadTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.launchpad.webapp.integrationtest;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.commons.testing.integration.HttpTestBase;
+
+/** Test the SLING-428 esp load function */
+public class EspLoadTest extends HttpTestBase {
+    
+    private String basePath;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        basePath = "/" + getClass().getSimpleName() + "_" + System.currentTimeMillis();
+    }
+    
+    public void testNestedInclude() throws Exception {
+        final Map<String,String> props = new HashMap<String,String>();
+        props.put("scriptToInclude", "included-a.esp");
+        props.put(SLING_RESOURCE_TYPE, getClass().getSimpleName());
+        final TestNode tn = new TestNode(HTTP_BASE_URL + basePath, props);
+        final String subfolder = tn.scriptPath + "/subfolder";
+        testClient.mkdirs(WEBDAV_BASE_URL, subfolder);
+        final String [] toDelete = {
+                uploadTestScript(tn.scriptPath, "esp-load/main.esp", "html.esp"),
+                uploadTestScript(tn.scriptPath, "esp-load/included-a.esp", "included-a.esp"),
+                uploadTestScript(subfolder, 
+                        "esp-load/subfolder/included-b.esp", "included-b.esp")
+        };
+
+        try {
+            final String content = getContent(tn.nodeUrl + ".html", CONTENT_TYPE_HTML);
+            
+            final String [] expectedStringsInOrder = {
+                    "main.esp before load",
+                    "included-a.esp before load",
+                    "included-b.esp",
+                    "included-a.esp after load",
+                    "main.esp after load"
+            };
+            
+            int pos = 0;
+            for(String expected : expectedStringsInOrder) {
+                final int newPos = content.indexOf(expected);
+                assertTrue("Content (" + content + ") must contain '" + expected + "'", newPos >= 0);
+                assertTrue("String '" + expected + "' must come after previous expected string", newPos > pos);
+                pos = newPos;
+            }
+        } finally {
+            for(String s : toDelete) {
+                testClient.delete(s);
+            }
+        }
+    }
+    
+    public void testNonExistentInclude() throws Exception {
+        final Map<String,String> props = new HashMap<String,String>();
+        final String badScript = "nonexistent.esp";
+        props.put("scriptToInclude", badScript);
+        props.put(SLING_RESOURCE_TYPE, getClass().getSimpleName());
+        final TestNode tn = new TestNode(HTTP_BASE_URL + basePath, props);
+        final String toDelete = uploadTestScript(tn.scriptPath, "esp-load/main.esp", "html.esp");
+        try {
+            assertHttpStatus(tn.nodeUrl + ".html", HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                    "Including " + badScript + " must fail");
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/NodetypeRenderingTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/NodetypeRenderingTest.java
index 28cfa5e..cbf750c 100644
--- a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/NodetypeRenderingTest.java
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/NodetypeRenderingTest.java
@@ -68,7 +68,19 @@
             testClient.delete(toDelete);
         }
     }
-
+    
+    public void testPrint() throws IOException {
+        final String toDelete = uploadTestScript("print.esp","html.esp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            final String expected = "print.esp ends";
+            assertTrue("Content (" + content + ") must contain '" + expected + "'",
+                    content.contains(expected));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+    
     public void testEspHtml() throws IOException {
         final String toDelete = uploadTestScript("rendering-test.esp","html.esp");
         try {
diff --git a/src/test/resources/integration-test/esp-load/included-a.esp b/src/test/resources/integration-test/esp-load/included-a.esp
new file mode 100644
index 0000000..64f48af
--- /dev/null
+++ b/src/test/resources/integration-test/esp-load/included-a.esp
@@ -0,0 +1,6 @@
+// Test SLING-428
+included-a.esp before load.
+
+<% load("subfolder/included-b.esp"); %>
+
+included-a.esp after load
\ No newline at end of file
diff --git a/src/test/resources/integration-test/esp-load/main.esp b/src/test/resources/integration-test/esp-load/main.esp
new file mode 100644
index 0000000..4bf1fe8
--- /dev/null
+++ b/src/test/resources/integration-test/esp-load/main.esp
@@ -0,0 +1,6 @@
+// Test SLING-428
+main.esp before load.
+
+<% load(currentNode.scriptToInclude); %>
+
+main.esp after load
\ No newline at end of file
diff --git a/src/test/resources/integration-test/esp-load/subfolder/included-b.esp b/src/test/resources/integration-test/esp-load/subfolder/included-b.esp
new file mode 100644
index 0000000..bd6493d
--- /dev/null
+++ b/src/test/resources/integration-test/esp-load/subfolder/included-b.esp
@@ -0,0 +1,2 @@
+// Test SLING-428
+included-b.esp
\ No newline at end of file
diff --git a/src/test/resources/integration-test/print.esp b/src/test/resources/integration-test/print.esp
new file mode 100644
index 0000000..829f7b3
--- /dev/null
+++ b/src/test/resources/integration-test/print.esp
@@ -0,0 +1,3 @@
+// Verify that the print() method compiles
+<% print("something from print.esp"); %>
+print.esp ends
\ No newline at end of file