SLING-923 - Servlet resolution examples and integration tests
git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk@765259 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ExtensionServletTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ExtensionServletTest.java
new file mode 100644
index 0000000..66ca149
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ExtensionServletTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.servlets.resolution;
+
+/** Test the ExtensionServlet */
+public class ExtensionServletTest extends ResolutionTestBase {
+
+ public void testExtensionOne() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_EXT_1", CONTENT_TYPE_PLAIN),
+ EXT_SERVLET_SUFFIX);
+ }
+
+ public void testExtensionTwo() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_EXT_2", CONTENT_TYPE_PLAIN),
+ EXT_SERVLET_SUFFIX);
+ }
+
+ public void testExtensionOther() throws Exception {
+ assertNotTestServlet(
+ getContent(testNodeNORT.nodeUrl + ".txt", CONTENT_TYPE_PLAIN));
+ }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrefixTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrefixTest.java
new file mode 100644
index 0000000..0d60e1d
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrefixTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.servlets.resolution;
+
+/** Verify that PrefixServletZero overrides PrefixServletMinusOne */
+public class PrefixTest extends ResolutionTestBase {
+
+ public void testPrefixServletZeroPresent() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_EXT_5", CONTENT_TYPE_PLAIN),
+ PREFIX_0_SERVLET_SUFFIX);
+ }
+
+ public void testPrefixServletMinusOnePresent() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_EXT_4", CONTENT_TYPE_PLAIN),
+ PREFIX_M1_SERVLET_SUFFIX);
+ }
+
+ public void testPrefixServletZeroWins() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_EXT_3", CONTENT_TYPE_PLAIN),
+ PREFIX_0_SERVLET_SUFFIX);
+ }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrioritiesTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrioritiesTest.java
new file mode 100644
index 0000000..1797a71
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PrioritiesTest.java
@@ -0,0 +1,27 @@
+/*
+ * 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.servlets.resolution;
+
+/** Test the priority of servlet selection mechanisms */
+public class PrioritiesTest extends ResolutionTestBase {
+
+ public void testExtensionWinsOverSelector() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_SEL_2.TEST_EXT_1", CONTENT_TYPE_PLAIN),
+ EXT_SERVLET_SUFFIX);
+ }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PutMethodServletTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PutMethodServletTest.java
new file mode 100644
index 0000000..9c98f40
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/PutMethodServletTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.servlets.resolution;
+
+import org.apache.commons.httpclient.methods.PutMethod;
+
+/** Test the PutMethodServlet resolution */
+public class PutMethodServletTest extends ResolutionTestBase {
+
+ public void testPutMethodServletSpecificRT() throws Exception {
+ final PutMethod put = new PutMethod(testNodeRT.nodeUrl);
+ final int status = httpClient.executeMethod(put);
+ assertEquals("PUT to testNodeRT should return 200", 200, status);
+ final String content = put.getResponseBodyAsString();
+ assertServlet(content, PUT_SERVLET_SUFFIX);
+ }
+
+ public void testPutMethodServletDefaultRT() throws Exception {
+ final PutMethod put = new PutMethod(testNodeNORT.nodeUrl);
+ final int status = httpClient.executeMethod(put);
+ assertFalse("PUT to testNodeRT should not return 200", 200 == status);
+ }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ResolutionTestBase.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ResolutionTestBase.java
new file mode 100644
index 0000000..426f28e
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ResolutionTestBase.java
@@ -0,0 +1,87 @@
+/*
+ * 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.servlets.resolution;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.sling.commons.testing.integration.HttpTestBase;
+
+/** Base class for servlet resolution tests */
+class ResolutionTestBase extends HttpTestBase {
+ public static final String CLASS_PROP = "servlet.class.name";
+ public static final String TEST_SERVLET_MARKER = "created by org.apache.sling.launchpad.testservices.servlets";
+ public static final String TEST_RESOURCE_TYPE = "LAUNCHPAD_TEST_ResourceType";
+ public static final String TEST_PATH = "/servlet-resolution-tests/" + System.currentTimeMillis();
+
+ public static final String EXT_SERVLET_SUFFIX = "testservices.servlets.ExtensionServlet";
+ public static final String SEL_SERVLET_SUFFIX = "testservices.servlets.SelectorServlet";
+ public static final String PREFIX_0_SERVLET_SUFFIX = "testservices.servlets.PrefixServletZero";
+ public static final String PREFIX_M1_SERVLET_SUFFIX = "testservices.servlets.PrefixServletMinusOne";
+ public static final String PUT_SERVLET_SUFFIX = "testservices.servlets.PutMethodServlet";
+
+ protected TestNode testNodeNORT;
+ protected TestNode testNodeRT;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ testNodeNORT = new TestNode(HTTP_BASE_URL + TEST_PATH, null);
+ final Map<String, String> properties = new HashMap<String, String>();
+ properties.put("sling:resourceType", TEST_RESOURCE_TYPE);
+ testNodeRT = new TestNode(HTTP_BASE_URL + TEST_PATH, properties);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ testNodeNORT.delete();
+ testNodeRT.delete();
+ }
+
+ /** Asserts that the given content is in Properties format and
+ * contains a property named CLASS_PROP that ends with
+ * expected suffix
+ */
+ protected void assertServlet(String content, String expectedSuffix) throws IOException {
+ final Properties props = new Properties();
+ final InputStream is = new ByteArrayInputStream(content.getBytes());
+ props.load(is);
+ assertTrue(
+ "Content represents a non-empty Properties object (" + content + ")",
+ props.size() > 0);
+ final String clazz = props.getProperty(CLASS_PROP);
+ assertNotNull(
+ "Content contains " + CLASS_PROP + " property (" + content + ")",
+ clazz);
+ assertTrue(
+ CLASS_PROP + " property value (" + clazz + ") ends with " + expectedSuffix,
+ clazz.endsWith(expectedSuffix));
+ }
+
+ /** Assert that content does not contain TEST_SERVLET_MARKER
+ */
+ protected void assertNotTestServlet(String content) {
+ if(content.contains(TEST_SERVLET_MARKER)) {
+ fail("Content should not contain " + TEST_SERVLET_MARKER + " marker (" + content + ")");
+ }
+ }
+}
diff --git a/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/SelectorServletTest.java b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/SelectorServletTest.java
new file mode 100644
index 0000000..04d392e
--- /dev/null
+++ b/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/SelectorServletTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.servlets.resolution;
+
+/** Test the SelectorServlet */
+public class SelectorServletTest extends ResolutionTestBase {
+
+ public void testSelectorOne() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_SEL_1.txt", CONTENT_TYPE_PLAIN),
+ SEL_SERVLET_SUFFIX);
+ }
+
+ public void testSelectorTwo() throws Exception {
+ assertServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_SEL_2.txt", CONTENT_TYPE_PLAIN),
+ SEL_SERVLET_SUFFIX);
+ }
+
+ public void testSelectorOther() throws Exception {
+ assertNotTestServlet(
+ getContent(testNodeNORT.nodeUrl + ".TEST_SEL_3.txt", CONTENT_TYPE_PLAIN));
+ }
+}