Search in sources :

Example 11 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class ServletRegistryTest method testSimpleHiding.

@Test
public void testSimpleHiding() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // empty reg
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
    // register servlets
    final ServletHandler h1 = createServletHandler(1L, 10, "/foo");
    reg.addServlet(h1);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    final ServletHandler h2 = createServletHandler(2L, 0, "/foo");
    reg.addServlet(h2);
    verify(h2.getServlet(), never()).init(Matchers.any(ServletConfig.class));
    verify(h1.getServlet(), never()).destroy();
    // two entries in reg
    // h1 is active
    // h2 is hidden
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertFalse(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    assertEquals(1, dto.servletDTOs[0].serviceId);
    assertEquals(1, holder.failedServletDTOs.size());
    final FailedServletDTO failedDTO = holder.failedServletDTOs.iterator().next();
    assertEquals(1, failedDTO.patterns.length);
    assertEquals("/foo", failedDTO.patterns[0]);
    assertEquals(2, failedDTO.serviceId);
    // remove servlet 1
    final Servlet s1 = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s1).destroy();
    verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
    // h2 is active
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertTrue(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    assertEquals(2, dto.servletDTOs[0].serviceId);
    // remove servlet 2
    final Servlet s2 = h2.getServlet();
    reg.removeServlet(h2.getServletInfo(), true);
    verify(s2).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 12 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class ServletRegistryTest method testSingleServlet.

@Test
public void testSingleServlet() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // empty reg
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
    // register servlet
    final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
    reg.addServlet(h1);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    // one entry in reg
    // check DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertTrue(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    // remove servlet
    final Servlet s = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 13 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredServlets.

@Test
public void dtosForSuccesfullyRegisteredServlets() throws Exception {
    // register first servlet
    registerServlet("testServlet 1", "/servlet_1");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithFirstSerlvet = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithFirstSerlvet.failedServletDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstSerlvet);
    assertEquals(1, contextDTO.servletDTOs.length);
    assertEquals("testServlet 1", contextDTO.servletDTOs[0].name);
    // register second servlet
    registerServlet("testServlet 2", "/servlet_2");
    RuntimeDTO runtimeDTOWithBothSerlvets = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithBothSerlvets.failedServletDTOs.length);
    contextDTO = assertDefaultContext(runtimeDTOWithBothSerlvets);
    assertEquals(2, contextDTO.servletDTOs.length);
    final Set<String> names = new HashSet<>();
    names.add(contextDTO.servletDTOs[0].name);
    names.add(contextDTO.servletDTOs[1].name);
    assertTrue(names.contains("testServlet 1"));
    assertTrue(names.contains("testServlet 2"));
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method contextReplacedWithHigherRankingContext.

// As specified in OSGi Compendium Release 6, Chapter 140.8
@Test
public void contextReplacedWithHigherRankingContext() throws Exception {
    ServiceRegistration<?> firstContext = registerContext("test-context", "/first");
    Long firstContextId = (Long) firstContext.getReference().getProperty(Constants.SERVICE_ID);
    CountDownLatch initLatch = new CountDownLatch(1);
    registerServlet("servlet", "/servlet", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
    assertEquals(3, runtimeDTO.servletContextDTOs.length);
    assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[1].serviceId);
    assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
    assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
    assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
    assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
    assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
    assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_CONTEXT_NAME, "test-context", HTTP_WHITEBOARD_CONTEXT_PATH, "/second", SERVICE_RANKING, Integer.MAX_VALUE);
    ServiceRegistration<?> secondContext = m_context.registerService(ServletContextHelper.class.getName(), mock(ServletContextHelper.class), properties);
    registrations.add(secondContext);
    Long secondContextId = (Long) secondContext.getReference().getProperty(Constants.SERVICE_ID);
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
    assertEquals(firstContextId.longValue(), runtimeDTO.failedServletContextDTOs[0].serviceId);
    assertEquals("test-context", runtimeDTO.failedServletContextDTOs[0].name);
    assertEquals("/first", runtimeDTO.failedServletContextDTOs[0].contextPath);
    assertEquals(3, runtimeDTO.servletContextDTOs.length);
    final List<String> names = new ArrayList<>();
    for (final ServletContextDTO dto : runtimeDTO.servletContextDTOs) {
        names.add(dto.name);
    }
    final int httpContextIndex = names.indexOf(HTTP_CONTEXT_NAME);
    final int secondContextIndex = names.indexOf("test-context");
    final int defaultContextIndex = names.indexOf("default");
    assertEquals(secondContextId.longValue(), runtimeDTO.servletContextDTOs[secondContextIndex].serviceId);
    assertEquals("test-context", runtimeDTO.servletContextDTOs[secondContextIndex].name);
    assertEquals("/second", runtimeDTO.servletContextDTOs[secondContextIndex].contextPath);
    assertEquals("default", runtimeDTO.servletContextDTOs[defaultContextIndex].name);
    assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[httpContextIndex].name);
    assertEquals(1, runtimeDTO.servletContextDTOs[secondContextIndex].servletDTOs.length);
    assertEquals("servlet", runtimeDTO.servletContextDTOs[secondContextIndex].servletDTOs[0].name);
    secondContext.unregister();
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
    assertEquals(3, runtimeDTO.servletContextDTOs.length);
    assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[1].serviceId);
    assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
    assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
    assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
    assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
    assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
    assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
}
Also used : ServletContextHelper(org.osgi.service.http.context.ServletContextHelper) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ArrayList(java.util.ArrayList) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method multipleServletsForSamePatternChoosenByServiceRankingRules.

// As specified in OSGi Compendium Release 6, Chapter 140.4.1
@Test
public void multipleServletsForSamePatternChoosenByServiceRankingRules() throws InterruptedException {
    registerServlet("servlet 1", "/pathcollision");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(1, defaultContext.servletDTOs.length);
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/pathcollision", HTTP_WHITEBOARD_SERVLET_NAME, "servlet 2", SERVICE_RANKING, Integer.MAX_VALUE);
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    TestServlet testServlet = new TestServlet(initLatch, destroyLatch);
    ServiceRegistration<?> higherRankingServlet = m_context.registerService(Servlet.class.getName(), testServlet, properties);
    registrations.add(higherRankingServlet);
    RuntimeDTO runtimeWithShadowedServlet = serviceRuntime.getRuntimeDTO();
    awaitServiceRegistration(initLatch);
    defaultContext = assertDefaultContext(runtimeWithShadowedServlet);
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals(1, runtimeWithShadowedServlet.failedServletDTOs.length);
    FailedServletDTO failedServletDTO = runtimeWithShadowedServlet.failedServletDTOs[0];
    assertEquals("servlet 1", failedServletDTO.name);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, failedServletDTO.failureReason);
    higherRankingServlet.unregister();
    awaitServiceRegistration(destroyLatch);
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals("servlet 1", defaultContext.servletDTOs[0].name);
}
Also used : FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Servlet(javax.servlet.Servlet) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)34 Test (org.junit.Test)29 RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)21 HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)20 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 Servlet (javax.servlet.Servlet)7 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)7 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)7 ServletConfig (javax.servlet.ServletConfig)5 HashSet (java.util.HashSet)4 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)4 ArrayList (java.util.ArrayList)3 FailedServletContextDTO (org.osgi.service.http.runtime.dto.FailedServletContextDTO)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletContextListener (javax.servlet.ServletContextListener)2 ServletException (javax.servlet.ServletException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2