@Test public void testRegisterServlet() throws Exception { HttpService httpService = getHttpService(bundleContext); initServletListener(null); TestServlet servlet = new TestServlet(); httpService.registerServlet("/test", servlet, null, null); waitForServletListener(); testClient.testWebPath("http://127.0.0.1:8181/test", "TEST OK"); Assert.assertTrue("Servlet.init(ServletConfig) was not called", servlet.isInitCalled()); }
/** * This test registers a servlet using HttpService.registerServlet(). It listens do the * servlet-deployed event and then registers a second servlet on the same context. It checks that * Servlet.init() was called after every invocation of registerServlet() and that both servlets * live in the same servlet context. */ @Test public void testRegisterMultipleServletsSameContext() throws Exception { final HttpService httpService = getHttpService(bundleContext); final AtomicReference<HttpContext> httpContext1 = new AtomicReference<HttpContext>(); final AtomicReference<HttpContext> httpContext2 = new AtomicReference<HttpContext>(); bundleContext.registerService( ServletListener.class, new ServletListener() { @Override public void servletEvent(ServletEvent servletEvent) { if (servletEvent.getType() == ServletEvent.DEPLOYED && "/test1".equals(servletEvent.getAlias())) { httpContext1.set(servletEvent.getHttpContext()); } if (servletEvent.getType() == ServletEvent.DEPLOYED && "/test2".equals(servletEvent.getAlias())) { httpContext2.set(servletEvent.getHttpContext()); } } }, null); TestServlet servlet1 = new TestServlet(); httpService.registerServlet("/test1", servlet1, null, null); for (int count = 0; count < 100; count++) { if (httpContext1.get() == null) { Thread.sleep(100); } } if (httpContext1.get() == null) { Assert.fail("Timout waiting for servlet event"); } testClient.testWebPath("http://127.0.0.1:8181/test1", "TEST OK"); Assert.assertTrue("Servlet.init(ServletConfig) was not called", servlet1.isInitCalled()); TestServlet servlet2 = new TestServlet(); httpService.registerServlet("/test2", servlet2, null, httpContext1.get()); for (int count = 0; count < 100; count++) { if (httpContext2.get() == null) { Thread.sleep(100); } } if (httpContext2.get() == null) { Assert.fail("Timout waiting for servlet event"); } testClient.testWebPath("http://127.0.0.1:8181/test1", "TEST OK"); testClient.testWebPath("http://127.0.0.1:8181/test2", "TEST OK"); Assert.assertTrue("Servlet.init(ServletConfig) was not called", servlet2.isInitCalled()); Assert.assertSame(httpContext1.get(), httpContext2.get()); Assert.assertSame(servlet1.getServletContext(), servlet2.getServletContext()); }
/** * This test registers a servlet to a already configured web context created by the war extender. * It checks that Servlet.init() was called after the invocation of registerServlet() and that the * servlet uses the same http context that the webapp uses. */ @Test public void testRegisterServletToWarContext() throws Exception { final AtomicReference<HttpContext> httpContext1 = new AtomicReference<HttpContext>(); bundleContext.registerService( WebListener.class, new WebListener() { @Override public void webEvent(WebEvent webEvent) { if (webEvent.getType() == WebEvent.DEPLOYED) { httpContext1.set(webEvent.getHttpContext()); } } }, null); LOG.debug("installing war-simple war"); String bundlePath = WEB_BUNDLE + "mvn:org.ops4j.pax.web.samples/war-simple/" + VersionUtil.getProjectVersion() + "/war?" + WEB_CONTEXT_PATH + "=/war"; Bundle installWarBundle = installAndStartBundle(bundlePath); for (int count = 0; count < 100; count++) { if (httpContext1.get() == null) { Thread.sleep(100); } } if (httpContext1.get() == null) { Assert.fail("Timout waiting for web event"); } LOG.debug("context registered, calling web request ..."); testClient.testWebPath("http://127.0.0.1:8181/war", "Hello, World, from JSP"); // --- final HttpService httpService = getHttpService(installWarBundle.getBundleContext()); LOG.debug("... adding additional content to war"); final AtomicReference<HttpContext> httpContext2 = new AtomicReference<HttpContext>(); bundleContext.registerService( ServletListener.class, new ServletListener() { @Override public void servletEvent(ServletEvent servletEvent) { if (servletEvent.getType() == ServletEvent.DEPLOYED && "/test2".equals(servletEvent.getAlias())) { httpContext2.set(servletEvent.getHttpContext()); } } }, null); TestServlet servlet2 = new TestServlet(); httpService.registerServlet("/test2", servlet2, null, httpContext1.get()); for (int count = 0; count < 100; count++) { if (httpContext2.get() == null) { Thread.sleep(100); } } if (httpContext2.get() == null) { Assert.fail("Timout waiting for servlet event"); } Assert.assertSame(httpContext1.get(), httpContext2.get()); testClient.testWebPath("http://127.0.0.1:8181/war", "Hello, World, from JSP"); testClient.testWebPath("http://127.0.0.1:8181/war/test2", "TEST OK"); Assert.assertTrue("Servlet.init(ServletConfig) was not called", servlet2.isInitCalled()); }