use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.
the class ErrorPageRegistryTest method testSingleErrorPage.
@Test
public void testSingleErrorPage() throws InvalidSyntaxException, ServletException {
final ErrorPageRegistry reg = new ErrorPageRegistry();
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
// empty reg
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertEmpty(dto, holder);
// register error page
final ServletHandler h1 = createServletHandler(1L, 0, "404", "java.io.IOException");
reg.addServlet(h1);
verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
// one entry in reg
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertNull(dto.resourceDTOs);
assertNull(dto.servletDTOs);
assertNotNull(dto.errorPageDTOs);
assertEquals(1, dto.errorPageDTOs.length);
assertEquals(1L, dto.errorPageDTOs[0].serviceId);
assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
assertEquals("java.io.IOException", dto.errorPageDTOs[0].exceptions[0]);
assertTrue(holder.failedErrorPageDTOs.isEmpty());
// test error handling
assertNotNull(reg.get(new IOException(), 404));
assertNotNull(reg.get(new RuntimeException(), 404));
assertNotNull(reg.get(new IOException(), 500));
assertNotNull(reg.get(new FileNotFoundException(), 500));
assertNull(reg.get(new RuntimeException(), 500));
// remove servlet
final Servlet s = h1.getServlet();
reg.removeServlet(h1.getServletInfo(), true);
verify(s).destroy();
// empty again
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertEmpty(dto, holder);
}
use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.
the class ErrorPageRegistryTest method testSimpleHiding.
@Test
public void testSimpleHiding() throws InvalidSyntaxException, ServletException {
final ErrorPageRegistry reg = new ErrorPageRegistry();
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
// check DTO
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertEmpty(dto, holder);
// register error pages
final ServletHandler h1 = createServletHandler(1L, 0, "404", "java.io.IOException");
reg.addServlet(h1);
final ServletHandler h2 = createServletHandler(2L, 10, "404", "some.other.Exception");
reg.addServlet(h2);
verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
// two entries in DTO
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertNull(dto.resourceDTOs);
assertNull(dto.servletDTOs);
assertNotNull(dto.errorPageDTOs);
assertEquals(2, dto.errorPageDTOs.length);
assertEquals(0, dto.errorPageDTOs[1].errorCodes.length);
assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
assertEquals(1, dto.errorPageDTOs[1].exceptions.length);
assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
assertEquals("java.io.IOException", dto.errorPageDTOs[1].exceptions[0]);
assertEquals("some.other.Exception", dto.errorPageDTOs[0].exceptions[0]);
assertEquals(1, holder.failedErrorPageDTOs.size());
assertEquals(1L, holder.failedErrorPageDTOs.get(0).serviceId);
assertEquals(1, holder.failedErrorPageDTOs.get(0).errorCodes.length);
assertEquals(404, holder.failedErrorPageDTOs.get(0).errorCodes[0]);
assertEquals(0, holder.failedErrorPageDTOs.get(0).exceptions.length);
assertEquals(DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, holder.failedErrorPageDTOs.get(0).failureReason);
// remove second page
final Servlet s2 = h2.getServlet();
reg.removeServlet(h2.getServletInfo(), true);
verify(s2).destroy();
// one entry in DTO
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertNull(dto.resourceDTOs);
assertNull(dto.servletDTOs);
assertNotNull(dto.errorPageDTOs);
assertEquals(1, dto.errorPageDTOs.length);
assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
assertEquals("java.io.IOException", dto.errorPageDTOs[0].exceptions[0]);
assertTrue(holder.failedErrorPageDTOs.isEmpty());
// test error handling
assertNotNull(reg.get(new IOException(), 404));
assertNotNull(reg.get(new RuntimeException(), 404));
assertNotNull(reg.get(new IOException(), 500));
assertNotNull(reg.get(new FileNotFoundException(), 500));
assertNull(reg.get(new RuntimeException(), 500));
// remove first page
final Servlet s1 = h1.getServlet();
reg.removeServlet(h1.getServletInfo(), true);
verify(s1).destroy();
// empty again
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertEmpty(dto, holder);
}
use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.
the class EventListenerRegistryTest method testSingleFilter.
@Test
public void testSingleFilter() throws InvalidSyntaxException, ServletException {
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
// check DTO
reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
assertEmpty(dto, holder);
// register filter
final FilterHandler h1 = createFilterHandler(1L, 0, "/foo");
reg.addFilter(h1);
verify(h1.getFilter()).init(Matchers.any(FilterConfig.class));
// one entry in DTO
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
assertTrue(holder.failedFilterDTOs.isEmpty());
assertNotNull(dto.filterDTOs);
assertEquals(1, dto.filterDTOs.length);
assertEquals(1, dto.filterDTOs[0].patterns.length);
assertEquals("/foo", dto.filterDTOs[0].patterns[0]);
// remove filter
final Filter f = h1.getFilter();
reg.removeFilter(h1.getFilterInfo(), true);
verify(f).destroy();
// empty again
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
assertEmpty(dto, holder);
}
use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.
the class FilterRegistryTest method testSingleListener.
@Test
public void testSingleListener() throws InvalidSyntaxException, ServletException {
final EventListenerRegistry reg = new EventListenerRegistry();
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
// check DTO
reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
assertEmpty(dto, holder);
// register listener
final ListenerHandler h1 = createListenerHandler(1L, 0, ServletContextListener.class);
reg.addListeners(h1);
// one entry in DTO
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
assertTrue(holder.failedListenerDTOs.isEmpty());
assertNotNull(dto.listenerDTOs);
assertEquals(1, dto.listenerDTOs.length);
assertEquals(1, dto.listenerDTOs[0].types.length);
assertEquals(ServletContextListener.class.getName(), dto.listenerDTOs[0].types[0]);
// remove listener
reg.removeListeners(h1.getListenerInfo());
// empty again
clear(dto, holder);
reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
assertEmpty(dto, holder);
}
use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.
the class HandlerRegistryTest method testAddRemoveServlet.
@Test
public void testAddRemoveServlet() throws Exception {
registry.init();
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
dto.serviceId = HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID;
dto.servletDTOs = new ServletDTO[0];
Servlet servlet = Mockito.mock(Servlet.class);
final ServletInfo info = new ServletInfo("foo", "/foo", Collections.<String, String>emptyMap());
ServletHandler handler = new HttpServiceServletHandler(null, info, servlet);
assertTrue(registry.getRuntimeInfo(dto, holder));
assertEquals("Precondition", 0, dto.servletDTOs.length);
registry.getRegistry(handler.getContextServiceId()).registerServlet(handler);
Mockito.verify(servlet, Mockito.times(1)).init(Mockito.any(ServletConfig.class));
assertTrue(registry.getRuntimeInfo(dto, holder));
assertEquals(1, dto.servletDTOs.length);
assertEquals(info.getServiceId(), dto.servletDTOs[0].serviceId);
final ServletInfo info2 = new ServletInfo("bar", "/bar", Collections.<String, String>emptyMap());
ServletHandler handler2 = new HttpServiceServletHandler(null, info2, Mockito.mock(Servlet.class));
registry.getRegistry(handler.getContextServiceId()).registerServlet(handler2);
assertTrue(registry.getRuntimeInfo(dto, holder));
assertEquals(2, dto.servletDTOs.length);
final ServletInfo info3 = new ServletInfo("zar", "/foo", Collections.<String, String>emptyMap());
ServletHandler handler3 = new HttpServiceServletHandler(null, info3, Mockito.mock(Servlet.class));
registry.getRegistry(handler.getContextServiceId()).registerServlet(handler3);
assertTrue(registry.getRuntimeInfo(dto, holder));
assertEquals(2, dto.servletDTOs.length);
assertEquals(1, holder.failedServletDTOs.size());
registry.shutdown();
}
Aggregations