diff options
author | Anjum Fatima | 2019-05-22 19:28:39 +0000 |
---|---|---|
committer | Anjum Fatima | 2019-05-23 14:54:28 +0000 |
commit | db6425d1ed4ea19912f7624071d647c684c67cc4 (patch) | |
tree | a9f78aa0ac21d01fbff246943c4df7176fa06b0f | |
parent | 7d3d176e363878f43cd2189e690cc52b40360ce6 (diff) | |
download | rt.equinox.bundles-db6425d1ed4ea19912f7624071d647c684c67cc4.tar.gz rt.equinox.bundles-db6425d1ed4ea19912f7624071d647c684c67cc4.tar.xz rt.equinox.bundles-db6425d1ed4ea19912f7624071d647c684c67cc4.zip |
Bug 547389 - [http] need tests to cover ContextPathCustomizer andI20190528-0915I20190528-0850I20190528-0600I20190527-1805I20190527-0600I20190526-1800I20190526-0600I20190525-1800I20190525-0600I20190524-1800
httpcontext usage in isolation scenarios
Change-Id: I8c3a98d03ce41c41eb7f5800daa30383f89651c4
Signed-off-by: Anjum Fatima <anjum.eclipse@gmail.com>
3 files changed, 328 insertions, 4 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/AllTests.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/AllTests.java index 44aa63545..389eee8f0 100644 --- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/AllTests.java +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/AllTests.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.equinox.http.servlet.testbase; +import org.eclipse.equinox.http.servlet.tests.ContextHelperCustomizerTests; import org.eclipse.equinox.http.servlet.tests.AuthenticationTest; import org.eclipse.equinox.http.servlet.tests.DispatchingTest; import org.eclipse.equinox.http.servlet.tests.PreprocessorTestCase; @@ -93,6 +94,7 @@ import org.junit.runners.Suite.SuiteClasses; TestHttpServiceAndErrorPage.class, TestHttpServiceAndNamedServlet.class, TestUpload.class, + ContextHelperCustomizerTests.class }) public class AllTests { // see @SuiteClasses diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/BaseTest.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/BaseTest.java index bb9800dbb..6b9bc00ab 100644 --- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/BaseTest.java +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/testbase/BaseTest.java @@ -694,10 +694,10 @@ public class BaseTest { } - protected static class TestContextPathAdaptor extends ContextPathCustomizer { - private final String defaultFilter; - private final String contextPrefix; - private final String testName; + protected class TestContextPathAdaptor extends ContextPathCustomizer { + protected final String defaultFilter; + protected final String contextPrefix; + protected final String testName; /** * @param defaultFilter diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ContextHelperCustomizerTests.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ContextHelperCustomizerTests.java new file mode 100644 index 000000000..4413e6e2c --- /dev/null +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ContextHelperCustomizerTests.java @@ -0,0 +1,322 @@ +/******************************************************************************* + * Copyright (c) 2019 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + *******************************************************************************/ +package org.eclipse.equinox.http.servlet.tests; + +import static org.junit.Assert.assertNotNull; + +import java.util.Collection; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.concurrent.atomic.AtomicReference; + +import javax.servlet.Servlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextListener; + +import org.eclipse.equinox.http.servlet.context.ContextPathCustomizer; +import org.eclipse.equinox.http.servlet.testbase.BaseTest; +import org.eclipse.equinox.http.servlet.tests.util.BaseServlet; +import org.eclipse.equinox.http.servlet.tests.util.MockSCL; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.hooks.service.FindHook; +import org.osgi.service.http.HttpContext; +import org.osgi.service.http.HttpService; +import org.osgi.service.http.context.ServletContextHelper; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; + +public class ContextHelperCustomizerTests extends BaseTest { + @Rule + public TestName testName = new TestName(); + private ServiceReference<HttpService> httpServiceReference; + private HttpService httpService; + private BundleContext context; + + @Before + public void begin() throws Exception { + httpServiceReference = getBundleContext().getServiceReference(HttpService.class); + context = httpServiceReference.getBundle().getBundleContext(); + httpService = context.getService(httpServiceReference); + } + + @After + public void end() throws Exception { + context.ungetService(httpServiceReference); + } + + @Test + public void testCreateDefaultHttpContextCreatesNewServletContextHelper() throws Exception { + HttpContext context1 = httpService.createDefaultHttpContext(); + HttpContext context2 = httpService.createDefaultHttpContext(); + Assert.assertNotEquals(context1, context2); + } + + @Test + public void testServletContextHelpersNotHiddenWhenRegisteredUsingConsumingContext() throws Exception { + ServiceRegistration<ServletContextHelper> helperReg = null; + ServiceRegistration<FindHook> findHookReg = null; + + try { + Dictionary<String, Object> properties = new Hashtable<String, Object>(); + properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "context1"); + properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/context1"); + //register a ServletContextHelper using the consuming bundle context "org.eclipse.equinox.http.servlet" + helperReg = context.registerService(ServletContextHelper.class, new ServletContextHelper() {}, properties); + + FindHook findHook = new FindHook() { + + @Override + public void find( + BundleContext bundleContext, String name, String filter, + boolean allServices, Collection<ServiceReference<?>> references) { + + if (bundleContext != context) { + return; + } + + // don't show ServletContextHelper + for (Iterator<ServiceReference<?>> iterator = references.iterator(); iterator.hasNext();) { + ServiceReference<?> sr = iterator.next(); + + if ("context1".equals(sr.getProperty("osgi.http.whiteboard.context.name"))) { + iterator.remove(); + } + } + } + + }; + + findHookReg = context.registerService(FindHook.class, findHook, null); + AtomicReference<ServletContext> sc1 = new AtomicReference<ServletContext>(); + + properties = new Hashtable<String, Object>(); + properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); + properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(osgi.http.whiteboard.context.name=context1)"); + context.registerService(ServletContextListener.class, new MockSCL(sc1), properties); + + //ServletContextHelpers registered using the consuming context should not be hidden + assertNotNull(sc1.get()); + } finally { + if (helperReg != null) { + helperReg.unregister(); + } + if (findHookReg != null) { + findHookReg.unregister(); + } + } + + } + + @Test + public void testWBServletContextPathCustomizerContextPrefix() throws Exception { + ServiceRegistration<ContextPathCustomizer> pathAdaptorReg = null; + ServiceRegistration<ServletContextHelper> helperReg = null; + ServiceRegistration<Servlet> servlet = null; + ServiceRegistration<FindHook> findHookReg = null; + + try { + Dictionary<String, String> helperProps = new Hashtable<String, String>(); + helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "testContext" + testName.getMethodName()); + helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/helperContext"); + helperProps.put(TEST_PATH_CUSTOMIZER_NAME, testName.getMethodName()); + helperReg = context.registerService(ServletContextHelper.class, new TestServletContextHelperFactory(), helperProps); + + //Pass the context path prefix paramater + ContextPathCustomizer pathAdaptor = new TestContextPathAdaptor(null, "testPrefix", testName.getMethodName()); + pathAdaptorReg = context.registerService(ContextPathCustomizer.class, pathAdaptor, null); + + FindHook findHook = new FindHook() { + + @Override + public void find( + BundleContext bundleContext, String name, String filter, + boolean allServices, Collection<ServiceReference<?>> references) { + + if (bundleContext != context) { + return; + } + + // don't show ServletContextHelper + for (Iterator<ServiceReference<?>> iterator = references.iterator(); iterator.hasNext();) { + ServiceReference<?> sr = iterator.next(); + if (("testContext" + testName.getMethodName()).equals(sr.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME))) { + iterator.remove(); + } + } + } + + }; + + findHookReg = context.registerService(FindHook.class, findHook, null); + + //Register a servlet service with a matching context helper + BaseServlet baseServlet = new BaseServlet("content"); + Dictionary<String, Object> serviceProps = new Hashtable<String, Object>(); + serviceProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet"); + serviceProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + "testContext" + testName.getMethodName() + ")"); + servlet = context.registerService(Servlet.class, baseServlet, serviceProps); + + String actual = requestAdvisor.request("testPrefix/helperContext/servlet"); + Assert.assertEquals("content", actual); + } finally { + if (helperReg != null) { + helperReg.unregister(); + } + if (pathAdaptorReg != null) { + pathAdaptorReg.unregister(); + } + if (servlet != null) { + servlet.unregister(); + } + if (findHookReg != null) { + findHookReg.unregister(); + } + } + } + + @Test + public void testWBServletContextPathCustomizerDefaultFilter() throws Exception { + ServiceRegistration<ContextPathCustomizer> pathAdaptorReg = null; + ServiceRegistration<ServletContextHelper> helperReg = null; + ServiceRegistration<Servlet> servlet = null; + ServiceRegistration<FindHook> findHookReg = null; + + try { + Dictionary<String, String> helperProps = new Hashtable<String, String>(); + helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "testContext" + testName.getMethodName()); + helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/helperContext"); + helperProps.put(TEST_PATH_CUSTOMIZER_NAME, testName.getMethodName()); + helperReg = context.registerService(ServletContextHelper.class, new TestServletContextHelperFactory(), helperProps); + + //Pass the filter parameter + ContextPathCustomizer pathAdaptor = new TestContextPathAdaptor("(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + "testContext" + testName.getMethodName() + ")", null, testName.getMethodName()); + pathAdaptorReg = context.registerService(ContextPathCustomizer.class, pathAdaptor, null); + + FindHook findHook = new FindHook() { + + @Override + public void find( + BundleContext bundleContext, String name, String filter, + boolean allServices, Collection<ServiceReference<?>> references) { + + if (bundleContext != context) { + return; + } + + // don't show ServletContextHelper + for (Iterator<ServiceReference<?>> iterator = references.iterator(); iterator.hasNext();) { + ServiceReference<?> sr = iterator.next(); + if (("testContext" + testName.getMethodName()).equals(sr.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME))) { + iterator.remove(); + } + } + } + + }; + + findHookReg = context.registerService(FindHook.class, findHook, null); + + //Register a servlet service with a matching context helper + BaseServlet baseServlet = new BaseServlet("content"); + Dictionary<String, Object> serviceProps = new Hashtable<String, Object>(); + serviceProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet"); + //Filter property + serviceProps.put("servlet.init." + TEST_PATH_CUSTOMIZER_NAME, testName.getMethodName()); + servlet = context.registerService(Servlet.class, baseServlet, serviceProps); + + String actual = requestAdvisor.request("helperContext/servlet"); + Assert.assertEquals("content", actual); + } finally { + if (helperReg != null) { + helperReg.unregister(); + } + if (pathAdaptorReg != null) { + pathAdaptorReg.unregister(); + } + if (servlet != null) { + servlet.unregister(); + } + if (findHookReg != null) { + findHookReg.unregister(); + } + } + } + + @Test + public void testLegacyServletContextPathCustomizerContextPrefix() throws Exception { + ServiceRegistration<ContextPathCustomizer> pathAdaptorReg = null; + ServiceRegistration<FindHook> findHookReg = null; + + try { + //Pass the context path prefix paramater + ContextPathCustomizer pathAdaptor = new TestContextPathAdaptor(null, "testPrefix", testName.getMethodName()) { + + @Override + public String getContextPathPrefix(ServiceReference<ServletContextHelper> helper) { + if (Boolean.TRUE.equals(helper.getProperty("equinox.legacy.context.helper"))) { + return contextPrefix; + } + return null; + } + }; + pathAdaptorReg = context.registerService(ContextPathCustomizer.class, pathAdaptor, null); + + FindHook findHook = new FindHook() { + + @Override + public void find( + BundleContext bundleContext, String name, String filter, + boolean allServices, Collection<ServiceReference<?>> references) { + + if (bundleContext != context) { + return; + } + + // don't show ServletContextHelper + for (Iterator<ServiceReference<?>> iterator = references.iterator(); iterator.hasNext();) { + ServiceReference<?> sr = iterator.next(); + if (Boolean.TRUE.equals(sr.getProperty("equinox.legacy.context.helper"))) { + iterator.remove(); + } + iterator.remove(); + } + } + }; + + findHookReg = context.registerService(FindHook.class, findHook, null); + //Register a servlet service using HttpService + BaseServlet baseServlet = new BaseServlet("content"); + httpService.registerServlet("/servlet", baseServlet, null, null); + + String actual = requestAdvisor.request("testPrefix/servlet"); + Assert.assertEquals("content", actual); + } finally { + if (pathAdaptorReg != null) { + pathAdaptorReg.unregister(); + } + if (findHookReg != null) { + findHookReg.unregister(); + } + httpService.unregister("/servlet"); + } + } +} + + |