Search in sources :

Example 1 with HttpServiceRuntime

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

the class HttpServiceRuntimeTest method serviceWithoutRequiredPropertiesIsIgnored.

// As specified in OSGi Compendium Release 6, Chapter 140.9
@Test
public void serviceWithoutRequiredPropertiesIsIgnored() throws InterruptedException {
    // Neither pattern nor error page specified
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_NAME, "servlet");
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(), properties));
    awaitServiceRegistration();
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(0, defaultContext.servletDTOs.length);
}
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) Test(org.junit.Test)

Example 2 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime 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 3 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime 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 4 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime 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)

Example 5 with HttpServiceRuntime

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

the class HttpServiceRuntimeTest method serviceRegisteredWithHttpServiceHasNegativeServiceId.

// As specified in OSGi Compendium Release 6, Chapter 140.9
@Test
public void serviceRegisteredWithHttpServiceHasNegativeServiceId() throws Exception {
    CountDownLatch initLatch = new CountDownLatch(1);
    register("/test", new TestServlet(initLatch, null));
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(2, runtimeDTO.servletContextDTOs.length);
    assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
    assertTrue(0 > runtimeDTO.servletContextDTOs[0].servletDTOs[0].serviceId);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)34 Test (org.junit.Test)32 RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)31 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)20 CountDownLatch (java.util.concurrent.CountDownLatch)12 ServletException (javax.servlet.ServletException)5 Servlet (javax.servlet.Servlet)4 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2 ServletContextHelper (org.osgi.service.http.context.ServletContextHelper)2 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)2 RequestInfoDTO (org.osgi.service.http.runtime.dto.RequestInfoDTO)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Dictionary (java.util.Dictionary)1 Filter (javax.servlet.Filter)1 FilterConfig (javax.servlet.FilterConfig)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1