use of org.osgi.service.http.runtime.HttpServiceRuntime 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.HttpServiceRuntime 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/*"));
}
use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.
the class HttpServiceRuntimeTest method exceptionInServletInitAppearsAsFailure.
@Test
public void exceptionInServletInitAppearsAsFailure() throws ServletException, InterruptedException {
Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet", HTTP_WHITEBOARD_SERVLET_NAME, "servlet");
CountDownLatch initLatch = new CountDownLatch(1);
@SuppressWarnings("serial") Servlet failingServlet = new TestServlet(initLatch, null) {
@Override
public void init() throws ServletException {
super.init();
throw new ServletException();
}
};
registrations.add(m_context.registerService(Servlet.class.getName(), failingServlet, properties));
awaitServiceRegistration(initLatch);
HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
assertEquals(1, runtimeDTO.failedServletDTOs.length);
assertEquals("servlet", runtimeDTO.failedServletDTOs[0].name);
assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[0].failureReason);
}
use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.
the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredErrorPages.
@Test
public void dtosForSuccesfullyRegisteredErrorPages() throws Exception {
// register first error page
registerErrorPage("error page 1", asList("404", NoSuchElementException.class.getName()));
HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
RuntimeDTO runtimeDTOWithFirstErrorPage = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithFirstErrorPage.failedServletDTOs.length);
assertEquals(0, runtimeDTOWithFirstErrorPage.failedErrorPageDTOs.length);
ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstErrorPage);
assertEquals(1, contextDTO.errorPageDTOs.length);
assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
assertArrayEquals(new String[] { NoSuchElementException.class.getName() }, contextDTO.errorPageDTOs[0].exceptions);
assertArrayEquals(new long[] { 404 }, contextDTO.errorPageDTOs[0].errorCodes);
// register second error page
registerErrorPage("error page 2", asList("500", ServletException.class.getName()));
RuntimeDTO runtimeDTOWithBothErrorPages = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTOWithBothErrorPages.failedServletDTOs.length);
assertEquals(0, runtimeDTOWithBothErrorPages.failedErrorPageDTOs.length);
contextDTO = assertDefaultContext(runtimeDTOWithBothErrorPages);
assertEquals(2, contextDTO.errorPageDTOs.length);
assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
assertEquals("error page 2", contextDTO.errorPageDTOs[1].name);
assertArrayEquals(new String[] { ServletException.class.getName() }, contextDTO.errorPageDTOs[1].exceptions);
assertArrayEquals(new long[] { 500 }, contextDTO.errorPageDTOs[1].errorCodes);
}
use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.
the class HttpServiceRuntimeTest method contextHelperWithDuplicateNameAppearsAsFailure.
// As specified in OSGi Compendium Release 6, Chapter 140.1
@Test
public void contextHelperWithDuplicateNameAppearsAsFailure() throws InterruptedException {
ServiceRegistration<?> firstContextReg = registerContext("contextA", "/first");
registerContext("contextA", "/second");
HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
assertEquals("contextA", runtimeDTO.failedServletContextDTOs[0].name);
assertEquals("/second", runtimeDTO.failedServletContextDTOs[0].contextPath);
assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletContextDTOs[0].failureReason);
assertEquals(3, runtimeDTO.servletContextDTOs.length);
assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
firstContextReg.unregister();
runtimeDTO = serviceRuntime.getRuntimeDTO();
assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
assertEquals(3, runtimeDTO.servletContextDTOs.length);
assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
assertEquals("/second", runtimeDTO.servletContextDTOs[1].contextPath);
}
Aggregations