You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/01/29 12:13:26 UTC
[sling-org-apache-sling-servlets-resolver] 02/04: SLING-8110 -
PathBasedServletAcceptorTest added + renamed activation property
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit 7d342f0749a7b777cedc92c6706f27780273b0e9
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jan 29 12:57:29 2020 +0100
SLING-8110 - PathBasedServletAcceptorTest added + renamed activation property
---
pom.xml | 2 +-
.../internal/PathBasedServletAcceptor.java | 4 +-
.../internal/PathBasedServletAcceptorTest.java | 210 +++++++++++++++++++++
.../resolver/it/ServletResolverTestSupport.java | 2 +-
.../servlets/resolver/it/ServletSelectionIT.java | 2 +-
5 files changed, 215 insertions(+), 5 deletions(-)
diff --git a/pom.xml b/pom.xml
index ee1d2d1..1d33cb9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,7 +46,7 @@
<site.jira.version.id>12314292</site.jira.version.id>
<pax.exam.version>4.13.1</pax.exam.version>
<!-- To debug the pax process, override this with -D -->
- <pax.vm.options>-Xmx256M -XX:MaxPermSize=256m</pax.vm.options>
+ <pax.vm.options>-Xmx512M</pax.vm.options>
</properties>
<build>
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java b/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
index cb5d1c2..adcc150 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptor.java
@@ -37,7 +37,7 @@ class PathBasedServletAcceptor {
public static final Logger LOGGER = LoggerFactory.getLogger(PathBasedServletAcceptor.class);
// TODO should be in ServletResolverConstants
- public static final String EXTPATHS_SERVICE_PROPERTY = "sling.servlet.extpaths";
+ private static final String STRICT_PATHS_SERVICE_PROPERTY = "sling.servlet.paths.strict";
boolean accept(SlingHttpServletRequest request, Servlet servlet) {
// Get OSGi service properties from the SlingServletConfig
@@ -51,7 +51,7 @@ class PathBasedServletAcceptor {
// If the servlet properties have the "extpaths" option, check extension, selector etc.
boolean accepted = true;
- final Object extpaths = config.getServiceProperty(EXTPATHS_SERVICE_PROPERTY);
+ final Object extpaths = config.getServiceProperty(STRICT_PATHS_SERVICE_PROPERTY);
if(extpaths != null && Boolean.valueOf(extpaths.toString())) {
accepted =
accept(servletName, config, ServletResolverConstants.SLING_SERVLET_EXTENSIONS, request.getRequestPathInfo().getExtension())
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java
new file mode 100644
index 0000000..5bcc91f
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/PathBasedServletAcceptorTest.java
@@ -0,0 +1,210 @@
+/*
+ * 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.servlets.resolver.internal;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.servlets.resolver.internal.resource.SlingServletConfig;
+import org.apache.sling.servlets.resolver.it.ServletResolverTestSupport;
+import org.junit.Test;
+import org.osgi.framework.ServiceReference;
+
+public class PathBasedServletAcceptorTest {
+
+ public static final String [] STRING_ARRAY = new String[0];
+
+ private final PathBasedServletAcceptor acceptor = new PathBasedServletAcceptor();
+
+ private class TestCase {
+ private Dictionary<String, Object> serviceProperties = new Hashtable<>();
+ private String extension;
+ private List<String> selectors = new ArrayList<>();
+ private String method;
+
+ TestCase() {
+ serviceProperties.put(ServletResolverTestSupport.P_STRICT_PATHS, true);
+ }
+
+ TestCase withServiceProperty(String key, Object value) {
+ serviceProperties.put(key, value);
+ return this;
+ }
+
+ TestCase withServiceProperty(String key, Object ... values) {
+ serviceProperties.put(key, values);
+ return this;
+ }
+
+ TestCase withExtension(String ext) {
+ extension = ext;
+ return this;
+ }
+
+ TestCase withSelector(String sel) {
+ selectors.add(sel);
+ return this;
+ }
+
+ TestCase withSelectors(String ... sels) {
+ selectors.addAll(Arrays.asList(sels));
+ return this;
+ }
+
+ TestCase withMethod(String m) {
+ method = m;
+ return this;
+ }
+
+ void assertAccept(boolean expected) {
+
+ // Stub the ServiceReference with our service properties
+ final ServiceReference<Servlet> reference = mock(ServiceReference.class);
+ final String [] keys = Collections.list(serviceProperties.keys()).toArray(STRING_ARRAY);
+ when(reference.getPropertyKeys()).thenReturn(keys);
+ for(String key: keys) {
+ when(reference.getProperty(key)).thenReturn(serviceProperties.get(key));
+ }
+
+ // Wire the Servlet to our ServiceReference
+ final ServletContext sc = mock(ServletContext.class);
+ final SlingServletConfig ssc = new SlingServletConfig(sc, reference, "42");
+ final Servlet servlet = mock(Servlet.class);
+ when(servlet.getServletConfig()).thenReturn(ssc);
+
+ // Setup the request values
+ final RequestPathInfo rpi = mock(RequestPathInfo.class);
+ when(rpi.getExtension()).thenReturn(extension);
+ when(rpi.getSelectors()).thenReturn(selectors.toArray(STRING_ARRAY));
+
+ final SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
+ when(request.getRequestPathInfo()).thenReturn(rpi);
+ when(request.getMethod()).thenReturn(method);
+
+ // And call the acceptor
+ final boolean actual = acceptor.accept(request, servlet);
+ assertEquals(expected, actual);
+ }
+
+ }
+
+ @Test
+ public void extensionNoMatch() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_EXTENSIONS, "sp")
+ .withExtension("somethingElse")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void extensionPropertyNotSet() {
+ new TestCase()
+ .withExtension("somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void selectorNoMatch() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "sel")
+ .withSelector("somethingElse")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void selectorOneMatchesOne() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "sel")
+ .withSelector("sel")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void selectorOneAmongSeveral() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "one")
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "two")
+ .withServiceProperty(ServletResolverTestSupport.P_SELECTORS, "three")
+ .withSelector("three")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void selectorPropertyNotSet() {
+ new TestCase()
+ .withSelector("somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void methodNoMatch() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_METHODS, "meth")
+ .withMethod("somethingElse")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void methodPropertyNotSet() {
+ new TestCase()
+ .withMethod("somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void testStringStrict() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_STRICT_PATHS, "true")
+ .withServiceProperty(ServletResolverTestSupport.P_METHODS, "meth")
+ .withMethod("somethingElse")
+ .assertAccept(false);
+ }
+
+ @Test
+ public void testStringFalseStrict() {
+ new TestCase()
+ .withServiceProperty(ServletResolverTestSupport.P_STRICT_PATHS, "false")
+ .withServiceProperty(ServletResolverTestSupport.P_METHODS, "meth")
+ .withMethod("somethingElse")
+ .assertAccept(true);
+ }
+
+ @Test
+ public void testNoSlingServletConfig() {
+ final Servlet s = mock(Servlet.class);
+ when(s.getServletConfig()).thenReturn(mock(ServletConfig.class));
+ assertTrue(acceptor.accept(null, s));
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java b/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java
index 3ce1b27..6c30337 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java
@@ -71,7 +71,7 @@ public class ServletResolverTestSupport extends TestSupport {
public static final String P_METHODS = "sling.servlet.methods";
public static final String P_EXTENSIONS = "sling.servlet.extensions";
public static final String P_SELECTORS = "sling.servlet.selectors";
- public static final String P_EXTPATHS = "sling.servlet.extpaths";
+ public static final String P_STRICT_PATHS = "sling.servlet.paths.strict";
public static final String RT_DEFAULT = "sling/servlet/default";
public static final String M_GET = "GET";
public static final String M_POST = "POST";
diff --git a/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
index fdcacfa..0f4cf5f 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
@@ -70,7 +70,7 @@ public class ServletSelectionIT extends ServletResolverTestSupport {
new TestServlet("ExtPaths")
.with(P_PATHS, "/extpaths")
- .with(P_EXTPATHS, "true")
+ .with(P_STRICT_PATHS, "true")
.with(P_METHODS, "POST")
.with(P_EXTENSIONS, "extPathsExt")
.with(P_SELECTORS, "extPathsSel")