This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 94cd6a32e9 FELIX-6565 : Fix test case, improve context selection
94cd6a32e9 is described below

commit 94cd6a32e95b4a03f334c7d7a989d0aae38bcc90
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Jul 28 15:04:12 2023 +0200

    FELIX-6565 : Fix test case, improve context selection
---
 .../impl/servlet/HealthCheckExecutorServlet.java   |  5 +-
 .../HealthCheckExecutorServletConfiguration.java   | 10 +--
 .../felix/hc/core/it/HealthCheckServletIT.java     | 71 +++++++++-------------
 3 files changed, 38 insertions(+), 48 deletions(-)

diff --git 
a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServlet.java
 
b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServlet.java
index cd4fa8bd1e..319c99c799 100644
--- 
a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServlet.java
+++ 
b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServlet.java
@@ -18,6 +18,7 @@
 package org.apache.felix.hc.core.impl.servlet;
 
 import static 
org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
+import static 
org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
 import static 
org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
 
 import java.io.IOException;
@@ -252,7 +253,9 @@ public class HealthCheckExecutorServlet extends HttpServlet 
{
     private void registerServlet(final ServletInfoDTO servletInfo) {
        final Dictionary<String, Object> properties = new Hashtable<>();
 
-       properties.put(HTTP_WHITEBOARD_CONTEXT_SELECT, servletInfo.contextName);
+        if (servletInfo.contextName != null && 
!servletInfo.contextName.isEmpty()) {
+           properties.put(HTTP_WHITEBOARD_CONTEXT_SELECT, 
"(".concat(HTTP_WHITEBOARD_CONTEXT_NAME).concat("=").concat(servletInfo.contextName).concat(")"));
+        }
        properties.put(HTTP_WHITEBOARD_SERVLET_PATTERN, 
servletInfo.servletPath);
        
        final ServiceRegistration<Servlet> registration = 
bundleContext.registerService(Servlet.class, servletInfo.servlet, properties);
diff --git 
a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletConfiguration.java
 
b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletConfiguration.java
index adb48fab1a..46676598b1 100644
--- 
a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletConfiguration.java
+++ 
b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletConfiguration.java
@@ -24,11 +24,13 @@ import org.osgi.service.metatype.annotations.Option;
 @ObjectClassDefinition(name = "Apache Felix Health Check Executor Servlet", 
description = "Serializes health check results into html, json or txt format")
 @interface HealthCheckExecutorServletConfiguration {
 
-       @AttributeDefinition(name = "Servlet Context Name", description = 
"Servlet Context Name")
-    String servletContextName() default "*";
+    String SERVLET_PATH_DEFAULT = "/system/health";
 
-    @AttributeDefinition(name = "Servlet Path", description = "Servlet Path)")
-    String servletPath() default "/system/health";
+       @AttributeDefinition(name = "Servlet Context Name", description = 
"Servlet Context Name to register the servlet with. If not specified, the 
default context is used.")
+    String servletContextName();
+
+    @AttributeDefinition(name = "Servlet Path", description = "Servlet path 
(defaults to " + SERVLET_PATH_DEFAULT + ")")
+    String servletPath() default SERVLET_PATH_DEFAULT;
 
     @AttributeDefinition(name = "Http Status Mapping", description = "Maps HC 
result status values to http response codes. Can be overwritten via request 
parameter 'httpStatus'")
     String httpStatusMapping() default 
"OK:200,WARN:200,CRITICAL:503,TEMPORARILY_UNAVAILABLE:503,HEALTH_CHECK_ERROR:500";
diff --git 
a/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckServletIT.java
 
b/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckServletIT.java
index bcfc707e7b..6ceac09597 100644
--- 
a/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckServletIT.java
+++ 
b/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckServletIT.java
@@ -18,14 +18,19 @@
 package org.apache.felix.hc.core.it;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
+import java.security.Provider.Service;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.UUID;
 
 import javax.inject.Inject;
+import javax.servlet.Servlet;
 
 import org.junit.After;
 import org.junit.Before;
@@ -37,9 +42,10 @@ import org.ops4j.pax.exam.junit.PaxExam;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.http.HttpService;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -54,47 +60,30 @@ public class HealthCheckServletIT {
     @Inject
     private BundleContext bundleContext;
 
-    private MockHttpService httpService;
-    private ServiceRegistration reg;
-
     @Configuration
     public Option[] config() {
         return U.config();
     }
 
-    private int countServletServices(String packageNamePrefix) throws 
InvalidSyntaxException {
-        final List<String> classNames = httpService.getServletClassNames();
-        int count = 0;
-        for (final String className : classNames) {
-            if (className.startsWith(packageNamePrefix)) {
-                count++;
+    private List<String> getRegisteredServletPaths() throws 
InvalidSyntaxException {
+        final Collection<ServiceReference<Servlet>> refs = 
bundleContext.getServiceReferences(Servlet.class, null);
+        final List<String> result = new ArrayList<>();
+        for(final ServiceReference<Servlet> ref : refs) {
+            final String path = (String) 
ref.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN);
+            if (path != null) {
+                result.add(path);
             }
         }
-        return count;
-    }
-
-    @Before
-    public void setup() {
-        httpService = new MockHttpService();
-        Dictionary<String,Object> httpServiceProps = new 
Hashtable<String,Object> ();
-        httpServiceProps.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
-        reg = bundleContext.registerService(HttpService.class.getName(), 
httpService, httpServiceProps);
-    }
-
-    @After
-    public void cleanup() {
-        reg.unregister();
-        reg = null;
-        httpService = null;
+        return result;
     }
 
     @Test
     public void testServletBecomesActive() throws InvalidSyntaxException, 
IOException, InterruptedException {
         final String servletPathPropertyName = "servletPath";
         final String servletPathPropertyVal = "/test/" + UUID.randomUUID();
-        final String packagePrefix = "org.apache.felix.hc";
-        assertEquals("Initially expecting no servlet from " + packagePrefix, 
0, countServletServices(packagePrefix));
-        final int pathsBefore = httpService.getPaths().size();
+
+        final List<String> initialPaths = this.getRegisteredServletPaths();
+        assertEquals("Initially expecting no servlets", 0, 
initialPaths.size());
 
         // Activate servlet and wait for it to show up
         final String factoryPid = 
"org.apache.felix.hc.core.impl.servlet.HealthCheckExecutorServlet";
@@ -107,25 +96,21 @@ public class HealthCheckServletIT {
 
         final long timeoutMsec = 5000L;
         final long endTime = System.currentTimeMillis() + timeoutMsec;
+        int expectedServletCount = 6;
         while (System.currentTimeMillis() < endTime) {
-            if (countServletServices(packagePrefix) > 0) {
+            if (getRegisteredServletPaths().size() >= expectedServletCount) {
                 break;
             }
             Thread.sleep(50L);
         }
 
-        int expectedServletCount = 6;
-        assertEquals("After adding configuration, expecting six servlets from 
" + packagePrefix, expectedServletCount,
-                countServletServices(packagePrefix));
-        final List<String> paths = httpService.getPaths();
-        assertEquals("Expecting six new servlet registration", pathsBefore + 
expectedServletCount, paths.size());
-        assertEquals("Expecting the HC servlet to be registered at " + 
servletPathPropertyVal, servletPathPropertyVal, paths.get(paths.size() - 6)); 
// paths list is longer,
-                                                                               
                                  // use last entries in list
-        assertEquals("Expecting the HTML HC servlet to be registered at " + 
servletPathPropertyVal + ".html", servletPathPropertyVal + ".html", 
paths.get(paths.size() - 5));
-        assertEquals("Expecting the JSON HC servlet to be registered at " + 
servletPathPropertyVal + ".json", servletPathPropertyVal + ".json", 
paths.get(paths.size() - 4));
-        assertEquals("Expecting the JSONP HC servlet to be registered at " + 
servletPathPropertyVal + ".jsonp", servletPathPropertyVal + ".jsonp", 
paths.get(paths.size() - 3));
-        assertEquals("Expecting the TXT HC servlet to be registered at " + 
servletPathPropertyVal + ".txt", servletPathPropertyVal + ".txt", 
paths.get(paths.size() - 2));
-        assertEquals("Expecting the verbose TXT HC servlet to be registered at 
" + servletPathPropertyVal + ".verbose.txt", servletPathPropertyVal + 
".verbose.txt",
-                paths.get(paths.size() - 1));
+        final List<String> paths = this.getRegisteredServletPaths();
+        assertEquals("After adding configuration, expecting six servlets", 
expectedServletCount, paths.size());
+        assertTrue("Expecting the HC servlet to be registered at " + 
servletPathPropertyVal, paths.contains(servletPathPropertyVal));
+        assertTrue("Expecting the HTML HC servlet to be registered at " + 
servletPathPropertyVal + ".html", paths.contains(servletPathPropertyVal + 
".html"));
+        assertTrue("Expecting the JSON HC servlet to be registered at " + 
servletPathPropertyVal + ".json", paths.contains(servletPathPropertyVal + 
".json"));
+        assertTrue("Expecting the JSONP HC servlet to be registered at " + 
servletPathPropertyVal + ".jsonp", paths.contains(servletPathPropertyVal + 
".jsonp"));
+        assertTrue("Expecting the TXT HC servlet to be registered at " + 
servletPathPropertyVal + ".txt", paths.contains(servletPathPropertyVal + 
".txt"));
+        assertTrue("Expecting the verbose TXT HC servlet to be registered at " 
+ servletPathPropertyVal + ".verbose.txt", 
paths.contains(servletPathPropertyVal + ".verbose.txt"));
     }
 }

Reply via email to