use of org.osgi.service.http.runtime.dto.RuntimeDTO 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);
}
use of org.osgi.service.http.runtime.dto.RuntimeDTO 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);
}
use of org.osgi.service.http.runtime.dto.RuntimeDTO 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);
}
use of org.osgi.service.http.runtime.dto.RuntimeDTO in project felix by apache.
the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredContexts.
@Test
public void dtosForSuccesfullyRegisteredContexts() throws Exception {
// register first additional context
registerContext("contextA", "/contextA");
HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
RuntimeDTO runtimeDTOWithAdditionalContext = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithAdditionalContext.failedServletContextDTOs.length);
assertEquals(3, runtimeDTOWithAdditionalContext.servletContextDTOs.length);
// default context is last, as it has the lowest service ranking
assertEquals(HTTP_CONTEXT_NAME, runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
assertEquals("contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].name);
assertEquals("/contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].contextPath);
assertEquals("default", runtimeDTOWithAdditionalContext.servletContextDTOs[2].name);
// TODO should this be "/" ?
assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[2].contextPath);
// register second additional context
registerContext("contextB", "/contextB");
RuntimeDTO runtimeDTOWithAllContexts = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithAllContexts.failedServletContextDTOs.length);
assertEquals(4, runtimeDTOWithAllContexts.servletContextDTOs.length);
// default context is last, as it has the lowest service ranking
assertEquals(HTTP_CONTEXT_NAME, runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
assertEquals("contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].name);
assertEquals("/contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].contextPath);
assertEquals("contextB", runtimeDTOWithAllContexts.servletContextDTOs[2].name);
assertEquals("/contextB", runtimeDTOWithAllContexts.servletContextDTOs[2].contextPath);
assertEquals("default", runtimeDTOWithAllContexts.servletContextDTOs[3].name);
assertEquals("", runtimeDTOWithAllContexts.servletContextDTOs[3].contextPath);
}
use of org.osgi.service.http.runtime.dto.RuntimeDTO in project felix by apache.
the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredResources.
@Test
public void dtosForSuccesfullyRegisteredResources() throws Exception {
// register first resource service
registerResource("/resources", "/resource_1/*");
HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
RuntimeDTO runtimeDTOWithFirstResource = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithFirstResource.failedResourceDTOs.length);
ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstResource);
assertEquals(1, contextDTO.resourceDTOs.length);
assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
assertArrayEquals(new String[] { "/resource_1/*" }, contextDTO.resourceDTOs[0].patterns);
// register second resource service
registerResource("/resources", "/resource_2/*");
RuntimeDTO runtimeDTOWithBothResources = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithBothResources.failedResourceDTOs.length);
contextDTO = assertDefaultContext(runtimeDTOWithBothResources);
assertEquals(2, contextDTO.resourceDTOs.length);
assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
assertEquals(1, contextDTO.resourceDTOs[0].patterns.length);
assertEquals(1, contextDTO.resourceDTOs[1].patterns.length);
final Set<String> patterns = new HashSet<>();
patterns.add(contextDTO.resourceDTOs[0].patterns[0]);
patterns.add(contextDTO.resourceDTOs[1].patterns[0]);
assertTrue(patterns.contains("/resource_1/*"));
assertTrue(patterns.contains("/resource_2/*"));
}
Aggregations