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/03/12 16:03:41 UTC

[sling-org-apache-sling-servlets-annotations-it] branch master updated: SLING-9055 - tests for the new SlingServletPathsStrict annotation

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-annotations-it.git


The following commit(s) were added to refs/heads/master by this push:
     new a55bf09  SLING-9055 - tests for the new SlingServletPathsStrict annotation
a55bf09 is described below

commit a55bf0909e333be02136e412545bf3de1aafb013
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Mar 12 17:02:47 2020 +0100

    SLING-9055 - tests for the new SlingServletPathsStrict annotation
---
 bnd.bnd                                            |   7 +-
 .../annotations/services/PathBoundService.java     |  25 +++++
 .../services/PathBoundStrictAllOptionsService.java |  30 ++++++
 .../services/PathBoundStrictJustPathService.java   |  25 +++++
 .../servlets/annotations/ServicePropertiesIT.java  | 116 +++++++++++++++++++++
 .../annotations/ServletRegistrationIT.java         |  61 ++---------
 .../sling/servlets/annotations/TestSupport.java    |  69 ++++++++++++
 7 files changed, 277 insertions(+), 56 deletions(-)

diff --git a/bnd.bnd b/bnd.bnd
index bedec37..539b4ad 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -1 +1,6 @@
-Sling-Initial-Content: SLING-CONTENT/content/servlettest;overwrite:=true;path:=/content/servlettest
\ No newline at end of file
+Sling-Initial-Content: SLING-CONTENT/content/servlettest;overwrite:=true;path:=/content/servlettest
+
+# This simulates using the right version of the servlets resolver bundle,
+# for the SlingServletPathsStrict annotation
+Provide-Capability:\
+  osgi.extender;osgi.extender="org.apache.sling.servlets.resolver";version:Version="1.1"
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundService.java b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundService.java
new file mode 100644
index 0000000..3e37c06
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundService.java
@@ -0,0 +1,25 @@
+/*
+ * 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.annotations.services;
+
+import org.apache.sling.servlets.annotations.SlingServletPaths;
+import org.osgi.service.component.annotations.Component;
+
+@Component(service=Object.class)
+@SlingServletPaths("PathBoundServicePath")
+public class PathBoundService {
+}
diff --git a/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictAllOptionsService.java b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictAllOptionsService.java
new file mode 100644
index 0000000..93aa9c6
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictAllOptionsService.java
@@ -0,0 +1,30 @@
+/*
+ * 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.annotations.services;
+
+import org.apache.sling.servlets.annotations.SlingServletPathsStrict;
+import org.osgi.service.component.annotations.Component;
+
+@Component(service=Object.class)
+@SlingServletPathsStrict(
+    paths = {"P1","P2"},
+    selectors = { "S1", "S2"},
+    extensions = { "E1", "E2"},
+    methods = { "M1", "M2"}
+)
+public class PathBoundStrictAllOptionsService {
+}
diff --git a/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictJustPathService.java b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictJustPathService.java
new file mode 100644
index 0000000..c32f5ca
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/annotations/services/PathBoundStrictJustPathService.java
@@ -0,0 +1,25 @@
+/*
+ * 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.annotations.services;
+
+import org.apache.sling.servlets.annotations.SlingServletPathsStrict;
+import org.osgi.service.component.annotations.Component;
+
+@Component(service=Object.class)
+@SlingServletPathsStrict(paths = "JustThePath")
+public class PathBoundStrictJustPathService {
+}
diff --git a/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java b/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java
new file mode 100644
index 0000000..4ddbb31
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java
@@ -0,0 +1,116 @@
+/*
+ * 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.annotations;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.servlets.annotations.services.PathBoundService;
+import org.apache.sling.servlets.annotations.services.PathBoundStrictAllOptionsService;
+import org.apache.sling.servlets.annotations.services.PathBoundStrictJustPathService;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.util.JsonUtils;
+import org.codehaus.jackson.JsonNode;
+import org.junit.Test;
+
+/** Assuming servlet selection is tested in the servlets resolver module,
+ *  it's good enough to verify that the expected OSGi service properties
+ *  are set by our annotations - this is what this class does for some of
+ *  our test services.
+ */
+public class ServicePropertiesIT extends TestSupport {
+    
+    private static String componentPath(Object nameOrId) {
+        return "/system/console/components/" + nameOrId + ".json";
+    }
+
+    /** Getting service properties is not directly supported by the OsgiConsoleClient */
+    private List<String> getServiceProperties(String serviceName) throws ClientException {
+        // Need to get the component ID first, then its details
+        final List<String> result = new ArrayList<>();
+        final JsonNode idInfo = JsonUtils.getJsonNodeFromString(CLIENT.doGet(componentPath(serviceName)).getContent());
+        final String id = idInfo.get("data").get(0).get("id").getTextValue();
+        assertTrue("Expecting non-null component ID", !id.equals("null"));
+        final JsonNode details = JsonUtils.getJsonNodeFromString(CLIENT.doGet(componentPath(id)).getContent());
+        final JsonNode data = details.get("data").get(0);
+        final JsonNode props = data.get("props");
+        for(JsonNode prop : props) {
+            if("Properties".equals(prop.get("key").getTextValue())) {
+                for(JsonNode value : prop.get("value")) {
+                    result.add(value.getTextValue().replaceAll(" ", "").trim());
+                }
+            }
+        }
+        return result;
+    }
+
+    private void assertProperty(List<String> props, String key, String value) {
+        final String pattern = key + "=" + value;
+        assertTrue(
+            "Expecting " + pattern + " in " + props,
+            props.stream().anyMatch(line -> line.equals(pattern))
+        );
+    }
+
+    private void assertAbsentProperties(List<String> props, String ... keys) {
+        for(String key : keys) {
+            assertFalse(
+                "Expecting " + key + " to be absent in " + props,
+                props.stream().anyMatch(line -> line.startsWith(key))
+            );
+        }
+    }
+
+    @Test
+    public void testPathBoundService() throws ClientException {
+        final List<String> props = getServiceProperties(PathBoundService.class.getName());
+        assertProperty(props, "sling.servlet.paths", "[PathBoundServicePath]");
+        assertAbsentProperties(
+            props,
+            "sling.servlet.extensions",
+            "sling.servlet.selectors",
+            "sling.servlet.methods",
+            "sling.servlet.paths.strict"
+        );
+    }
+
+    @Test
+    public void testPathBoundStrictAllOptionsService() throws ClientException {
+        final List<String> props = getServiceProperties(PathBoundStrictAllOptionsService.class.getName());
+        assertProperty(props, "sling.servlet.paths.strict", "true");
+        assertProperty(props, "sling.servlet.paths", "[P1,P2]");
+        assertProperty(props, "sling.servlet.extensions", "[E1,E2]");
+        assertProperty(props, "sling.servlet.selectors", "[S1,S2]");
+        assertProperty(props, "sling.servlet.methods", "[M1,M2]");
+    }
+
+    @Test
+    public void testPathBoundStrictJustPathService() throws ClientException {
+        final List<String> props = getServiceProperties(PathBoundStrictJustPathService.class.getName());
+        assertProperty(props, "sling.servlet.paths.strict", "true");
+        assertProperty(props, "sling.servlet.paths", "[JustThePath]");
+        assertAbsentProperties(
+            props,
+            "sling.servlet.extensions",
+            "sling.servlet.selectors",
+            "sling.servlet.methods"
+        );
+    }
+}
diff --git a/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java b/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
index 754d7ed..297f599 100644
--- a/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
+++ b/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
@@ -16,68 +16,19 @@
  */
 package org.apache.sling.servlets.annotations;
 
-import java.io.File;
 import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.Collections;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
 import org.apache.http.entity.StringEntity;
 import org.apache.sling.testing.clients.ClientException;
-import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
-import org.apache.sling.testing.junit.rules.SlingRule;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Rule;
 import org.junit.Test;
 
-public class ServletRegistrationIT {
-    
-    @Rule
-    public SlingRule methodRule = new SlingRule();
-    
-    public static OsgiConsoleClient CLIENT;
-    
-    private static final String BUNDLE_SYMBOLICNAME = "org.apache.sling.servlets.annotations.it";
-    private static final long BUNDLE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
-    private static final long SERVICE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(60);
-    
-    @BeforeClass
-    public static void setupOnce() throws ClientException, InterruptedException, TimeoutException, URISyntaxException {
-        String baseUrl = System.getProperty("baseUrl");
-        if (baseUrl == null) {
-            throw new IllegalArgumentException("IT must be started with environment variable 'baseUrl' set");
-        }
-        URI url = new URI(baseUrl);
-        CLIENT = new OsgiConsoleClient(url, "admin", "admin");
-        
-        String bundleFile = System.getProperty("bundleFile");
-        if (bundleFile == null) {
-            throw new IllegalArgumentException("IT must be started with environment variable 'bundleFile' set");
-        }
-        // deploy bundle to server
-        File file = new File(bundleFile);
-        if (!file.exists()) {
-            throw new IllegalArgumentException("Test bundle file in " + file + " does not exist!");
-        }
-        // wait until the server is fully started
-        CLIENT.waitExists("/starter/index.html", SERVICE_START_TIMEOUT, 500);
-
-        CLIENT.waitInstallBundle(file, true, -1, BUNDLE_START_TIMEOUT, 500);
-        
-        // the following method somehow fails sometimes
-        CLIENT.waitServiceRegistered("javax.servlet.Servlet", BUNDLE_SYMBOLICNAME, SERVICE_START_TIMEOUT, 500);
-        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservlets.PathBoundServlet", SERVICE_START_TIMEOUT, 500);
-        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservletfilters.SimpleServletFilter", SERVICE_START_TIMEOUT, 500);
-    }
+/** Verify servlet selection based on our annotations. This duplicates
+ *  some of the servlet resolver tests, see also {@link ServicePropertiesIT}
+ *  for another testing method.
+ */
+public class ServletRegistrationIT extends TestSupport {
     
-    @AfterClass
-    public static void tearDownOnce() throws ClientException {
-        //CLIENT.uninstallBundle(BUNDLE_SYMBOLICNAME);
-    }
-
     @Test
     public void testPathBoundServlet() throws ClientException, UnsupportedEncodingException {
         CLIENT.doGet("/bin/PathBoundServlet", 555);
@@ -161,4 +112,4 @@ public class ServletRegistrationIT {
         CLIENT.doGet("/content/servlettest/inheritingResourceTypeBoundServlet.sel1.someext", 610); // delegated to Base
         CLIENT.doGet("/content/servlettest/inheritingResourceTypeBoundServlet.sel2.someext", 610); // delegated to Base
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java b/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java
new file mode 100644
index 0000000..6973804
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java
@@ -0,0 +1,69 @@
+/*
+ * 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.annotations;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
+import org.junit.BeforeClass;
+
+public class TestSupport {
+    
+    static OsgiConsoleClient CLIENT;
+    
+    private static final String BUNDLE_SYMBOLICNAME = "org.apache.sling.servlets.annotations.it";
+    private static final long BUNDLE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
+    private static final long SERVICE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(60);
+    
+    @BeforeClass
+    public static synchronized void setupOnce() throws ClientException, InterruptedException, TimeoutException, URISyntaxException {
+        if(CLIENT != null) {
+            return;
+        }
+
+        String baseUrl = System.getProperty("baseUrl");
+        if (baseUrl == null) {
+            throw new IllegalArgumentException("IT must be started with environment variable 'baseUrl' set");
+        }
+        URI url = new URI(baseUrl);
+        CLIENT = new OsgiConsoleClient(url, "admin", "admin");
+        
+        String bundleFile = System.getProperty("bundleFile");
+        if (bundleFile == null) {
+            throw new IllegalArgumentException("IT must be started with environment variable 'bundleFile' set");
+        }
+        // deploy bundle to server
+        File file = new File(bundleFile);
+        if (!file.exists()) {
+            throw new IllegalArgumentException("Test bundle file in " + file + " does not exist!");
+        }
+        // wait until the server is fully started
+        CLIENT.waitExists("/starter/index.html", SERVICE_START_TIMEOUT, 500);
+
+        CLIENT.waitInstallBundle(file, true, -1, BUNDLE_START_TIMEOUT, 500);
+        
+        // the following method somehow fails sometimes
+        CLIENT.waitServiceRegistered("javax.servlet.Servlet", BUNDLE_SYMBOLICNAME, SERVICE_START_TIMEOUT, 500);
+        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservlets.PathBoundServlet", SERVICE_START_TIMEOUT, 500);
+        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservletfilters.SimpleServletFilter", SERVICE_START_TIMEOUT, 500);
+    }
+}