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-capabilities.git

commit 276f885b60840dadc96e4883c22104410cf4788c
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Thu Oct 11 14:22:59 2018 +0200

    Test the SlingServletsSource
---
 .../defaultsources/SlingServletsSource.java        |  10 +-
 .../defaultsources/SlingServletsSourceTest.java    | 106 +++++++++++++++++++++
 2 files changed, 113 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
 
b/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
index ce63bc6..b196f66 100644
--- 
a/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
+++ 
b/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
@@ -84,7 +84,7 @@ public class SlingServletsSource implements 
CapabilitiesSource {
         final ServiceReference [] refs = 
bundleContext.getServiceReferences(Servlet.class.getName(), ldapFilter);
         if(refs != null) {
             for(ServiceReference ref : refs) {
-                result.put(classOf(ref), getCapabilities(ref));
+                result.put(uniqueKey(ref), getCapabilities(ref));
             }
         }
         return result;
@@ -115,12 +115,16 @@ public class SlingServletsSource implements 
CapabilitiesSource {
         return result;
     }
     
-    private String classOf(ServiceReference ref) {
+    /** Compute a somewhat representative unique key for ref */
+    private String uniqueKey(ServiceReference ref) {
+        final StringBuilder result = new StringBuilder();
         final Object service = bundleContext.getService(ref);
         try {
-            return service.getClass().getName();
+            result.append(service.getClass().getSimpleName());
+            result.append("_").append(service.hashCode());
         } finally {
             bundleContext.ungetService(ref);
         }
+        return result.toString();
     }
 }
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
 
b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
new file mode 100644
index 0000000..390e1d4
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
@@ -0,0 +1,106 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.capabilities.defaultsources;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.regex.Pattern;
+import javax.servlet.Servlet;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.capabilities.CapabilitiesSource;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+public class SlingServletsSourceTest {
+
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    static class MockServlet extends SlingSafeMethodsServlet {
+        private final Map<String, Object> props = new HashMap<>();
+        private static final String PREFIX = "sling.servlet.";
+
+        MockServlet(String extension, int id) {
+            props.put(PREFIX + "extensions", extension);
+            props.put(PREFIX + "id", id);
+            props.put("some.other.property", id + 12);
+        }
+
+        final Map<String, Object> getProps() {
+            return props;
+        }
+    }
+
+    @Before
+    public void setup() throws IOException {
+        // Configure allowed path patterns
+        final ConfigurationAdmin ca = 
context.getService(ConfigurationAdmin.class);
+        assertNotNull("Expecting a ConfigurationAdmin service", ca);
+        final Configuration cfg = 
ca.getConfiguration(SlingServletsSource.class.getName());
+        final Dictionary<String, Object> props = new Hashtable<>();
+        props.put("capabilitiesNamespace", "TEST_NS");
+        props.put("servletsLdapFilter", "(sling.servlet.extensions=json)");
+        cfg.update(props);
+
+        context.registerInjectActivateService(new SlingServletsSource());
+
+        // Need a few (fake) Sling servlets to test
+        final String [] ext = { "json", "txt", "json" };
+        final int [] id = { 12, 24, 41 };
+        for(int i=0 ; i < ext.length; i++) {
+            final MockServlet s = new MockServlet(ext[i], id[i]);
+            context.registerService(Servlet.class, s, s.getProps());
+        }
+    }
+
+    @Test
+    public void testServletsSource() throws Exception {
+        final CapabilitiesSource src = 
context.getService(CapabilitiesSource.class);
+        assertNotNull("Expecting a CapabilitiesSource", src);
+
+        final Map<String, Object> caps = src.getCapabilities();
+        assertNotNull("Expecting to get Capabilities", caps);
+        System.err.println(caps);
+
+        assertEquals("Expecting capabilities for 2 json servlets", 2, 
caps.size());
+
+        final Pattern keyPattern = Pattern.compile("MockServlet_[0-9]+");
+        for(String key : caps.keySet()) {
+            assertTrue("Expecting key " + key + " to match", 
keyPattern.matcher(key).matches());
+        }
+
+        for(Object o: caps.values()) {
+            assertTrue("Expecting Maps in values", o instanceof Map);
+            Map<?, ?> map = (Map<?,?>)o;
+            assertEquals("Expecting 2 properties per map", 2, map.size());
+            final String id = map.get("sling.servlet.id").toString();
+            assertTrue("Expecting id=12 or 41", id.equals("12") || 
id.equals("41"));
+        }
+    }
+}
\ No newline at end of file

Reply via email to