use of org.apache.felix.http.base.internal.handler.ServletHandler 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);
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ServletRegistryTest method testMatcherOrdering.
@Test
public void testMatcherOrdering() throws InvalidSyntaxException {
final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
final ServletHandler h2 = createServletHandler(2L, 0, "/foo/*");
final List<PathResolver> resolvers = new ArrayList<PathResolver>();
resolvers.add(PathResolverFactory.createPatternMatcher(h1, "/foo"));
resolvers.add(PathResolverFactory.createPatternMatcher(h2, "/foo/*"));
Collections.sort(resolvers);
assertEquals("/foo", resolvers.get(0).getPattern());
assertEquals("/foo/*", resolvers.get(1).getPattern());
}
use of org.apache.felix.http.base.internal.handler.ServletHandler 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);
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ErrorPageRegistryTest method testRangeRegistration.
@Test
public void testRangeRegistration() throws InvalidSyntaxException {
final ErrorPageRegistry reg = new ErrorPageRegistry();
final FailedDTOHolder holder = new FailedDTOHolder();
final ServletContextDTO dto = new ServletContextDTO();
final ServletHandler handler4 = createServletHandler(1L, 0, "4xx");
final ServletHandler handler5 = createServletHandler(2L, 0, "5xx");
reg.addServlet(handler4);
reg.addServlet(handler5);
// check DTO
reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
assertTrue(holder.failedErrorPageDTOs.isEmpty());
assertEquals(2, dto.errorPageDTOs.length);
assertEquals(100, dto.errorPageDTOs[0].errorCodes.length);
final Set<Long> codes4 = new HashSet<Long>();
for (final long c : dto.errorPageDTOs[0].errorCodes) {
assertTrue(c >= 400 && c < 500);
codes4.add(c);
}
assertEquals(100, codes4.size());
assertEquals(100, dto.errorPageDTOs[1].errorCodes.length);
final Set<Long> codes5 = new HashSet<Long>();
for (final long c : dto.errorPageDTOs[1].errorCodes) {
assertTrue(c >= 500 && c < 600);
codes5.add(c);
}
assertEquals(100, codes5.size());
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ServletRegistryTest method testServletOrdering.
@Test
public void testServletOrdering() throws InvalidSyntaxException {
final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
reg.addServlet(h1);
final ServletHandler h2 = createServletHandler(2L, 0, "/foo/*");
reg.addServlet(h2);
final ServletHandler h3 = createServletHandler(3L, 0, "/foo/rsrc");
reg.addServlet(h3);
final ServletHandler h4 = createServletHandler(4L, 0, "/foo/rsrc/*");
reg.addServlet(h4);
final ServletHandler h5 = createServletHandler(5L, 0, "/other");
reg.addServlet(h5);
PathResolution pr = null;
pr = reg.resolve("/foo");
assertNotNull(pr);
assertEquals("/foo", pr.patterns[0]);
assertEquals(h1, pr.handler);
pr = reg.resolve("/fool");
assertNull(pr);
pr = reg.resolve("/foo/bar");
assertEquals("/foo", pr.patterns[0]);
assertEquals(h1, pr.handler);
pr = reg.resolve("/foo/rsrc");
assertEquals("/foo/rsrc", pr.patterns[0]);
assertEquals(h3, pr.handler);
pr = reg.resolve("/foo/rsrc/some");
assertEquals("/foo/rsrc", pr.patterns[0]);
assertEquals(h3, pr.handler);
pr = reg.resolve("/other");
assertEquals("/other", pr.patterns[0]);
assertEquals(h5, pr.handler);
pr = reg.resolve("/other/bla");
assertEquals("/other", pr.patterns[0]);
assertEquals(h5, pr.handler);
// cleanup
reg.removeServlet(h1.getServletInfo(), true);
reg.removeServlet(h2.getServletInfo(), true);
reg.removeServlet(h3.getServletInfo(), true);
reg.removeServlet(h4.getServletInfo(), true);
reg.removeServlet(h5.getServletInfo(), true);
}
Aggregations