Author: bdelacretaz
Date: Fri Aug 14 11:31:39 2015
New Revision: 1695866
URL: http://svn.apache.org/r1695866
Log:
SLING-4862 - add test for HC servlet activation
Added:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
Modified:
sling/trunk/bundles/extensions/healthcheck/it/pom.xml
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/U.java
Modified: sling/trunk/bundles/extensions/healthcheck/it/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/it/pom.xml?rev=1695866&r1=1695865&r2=1695866&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/it/pom.xml (original)
+++ sling/trunk/bundles/extensions/healthcheck/it/pom.xml Fri Aug 14 11:31:39
2015
@@ -121,5 +121,16 @@
<version>4.2.1</version>
<scope>test</scope>
</dependency>
- </dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.http.servlet-api</artifactId>
+ <version>1.1.0</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
Added:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java?rev=1695866&view=auto
==============================================================================
---
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
(added)
+++
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
Fri Aug 14 11:31:39 2015
@@ -0,0 +1,121 @@
+/*
+ * 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 SF 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.hc.it.core;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.BundleContext;
+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;
+
+/** Verify that the HealthCheckExecutorServlet becomes available
+ * after creating the corresponding config
+ */
+@RunWith(PaxExam.class)
+public class HealthCheckServletTest {
+
+ @Inject
+ private ConfigurationAdmin configAdmin;
+
+ @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 ServiceReference<?> [] refs =
bundleContext.getServiceReferences("javax.servlet.Servlet", null);
+ int count = 0;
+ if(refs != null) {
+ for(ServiceReference ref : refs) {
+ final Object o = bundleContext.getService(ref);
+ if(o.getClass().getName().startsWith(packageNamePrefix)) {
+ count++;
+ }
+ bundleContext.ungetService(ref);
+ }
+ }
+ return count;
+ }
+
+ @Before
+ public void setup() {
+ httpService = new MockHttpService();
+ reg = bundleContext.registerService(HttpService.class, httpService,
null);
+ }
+
+ @After
+ public void cleanup() {
+ reg.unregister();
+ reg = null;
+ httpService = null;
+ }
+
+ @Test
+ public void testServletBecomesActive() throws InvalidSyntaxException,
IOException, InterruptedException {
+ final String property = "servletPath";
+ final String path = "/test/" + UUID.randomUUID();
+ final String packagePrefix = "org.apache.sling.hc";
+ assertEquals("Initially expecting no servlet from " + packagePrefix,
0, countServletServices(packagePrefix));
+ final int pathsBefore = httpService.getPaths().size();
+
+ // Activate servlet and wait for it to show up
+ final String pid =
"org.apache.sling.hc.core.impl.servlet.HealthCheckExecutorServlet";
+ final org.osgi.service.cm.Configuration cfg =
configAdmin.getConfiguration(pid, null);
+ final Dictionary<String, Object> props = new Hashtable<String,
Object>();
+ props.put(property, path);
+ cfg.update(props);
+
+ final long timeoutMsec = 5000L;
+ final long endTime = System.currentTimeMillis() + timeoutMsec;
+ while(System.currentTimeMillis() < endTime) {
+ if(countServletServices(packagePrefix) > 0) {
+ break;
+ }
+ Thread.sleep(50L);
+ }
+
+ assertEquals("After adding configuration, expecting one servlet from "
+ packagePrefix, 1, countServletServices(packagePrefix));
+ final List<String> paths = httpService.getPaths();
+ assertEquals("Expecting one new servlet registration", pathsBefore +
1, paths.size());
+ assertEquals("Expecting the HC servlet to be registered at " + path,
path, paths.get(paths.size() - 1));
+ }
+}
Added:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java?rev=1695866&view=auto
==============================================================================
---
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
(added)
+++
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
Fri Aug 14 11:31:39 2015
@@ -0,0 +1,55 @@
+/*
+ * 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 SF 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.hc.it.core;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.List;
+
+import javax.servlet.Servlet;
+
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+
+class MockHttpService implements HttpService {
+
+ private List<String> paths = new ArrayList<String>();
+
+ @Override
+ public void registerResources(String alias, String name, HttpContext
context) {
+ }
+
+ @Override
+ public void registerServlet(String alias, Servlet servlet, Dictionary
initparams, HttpContext context) {
+ paths.add(alias);
+ }
+
+ public void unregister(String alias) {
+ paths.remove(alias);
+ }
+
+ @Override
+ public HttpContext createDefaultHttpContext() {
+ return null;
+ }
+
+ List<String> getPaths() {
+ return Collections.unmodifiableList(paths);
+ }
+}
Modified:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/U.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/U.java?rev=1695866&r1=1695865&r2=1695866&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/U.java
(original)
+++
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/U.java
Fri Aug 14 11:31:39 2015
@@ -75,11 +75,13 @@ public class U {
provision(
mavenBundle("org.apache.felix",
"org.apache.felix.gogo.shell", "0.10.0"),
mavenBundle("org.apache.felix",
"org.apache.felix.gogo.runtime", "0.10.0"),
- mavenBundle("org.apache.felix",
"org.apache.felix.gogo.command", "0.12.0")
+ mavenBundle("org.apache.felix",
"org.apache.felix.gogo.command", "0.12.0"),
+ mavenBundle("org.apache.felix",
"org.apache.felix.shell.remote", "1.1.2")
)
),
provision(
mavenBundle("org.apache.felix", "org.apache.felix.scr",
"1.6.2"),
+ mavenBundle("org.apache.felix",
"org.apache.felix.configadmin", "1.8.8"),
mavenBundle("org.apache.felix",
"org.apache.felix.http.servlet-api", "1.1.0"),
mavenBundle("org.apache.sling",
"org.apache.sling.hc.core", coreVersion),
mavenBundle("org.apache.sling",
"org.apache.sling.hc.samples", samplesVersion),