EverrestServlet extends HttpServlet { @Override public void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { EnvironmentContext env = new EnvironmentContext(); env.put(HttpServletRequest.class, httpRequest); env.put(HttpServletResponse.class, httpResponse); env.put(ServletConfig.class, getServletConfig()); env.put(ServletContext.class, getServletContext()); env.put(WebApplicationDeclaredRoles.class, webApplicationRoles); env.put(ErrorPages.class, errorPages); try { ServletContainerRequest request = ServletContainerRequest.create(httpRequest); ContainerResponse response = new ContainerResponse(new ServletContainerResponseWriter(httpResponse)); processor.process(request, response, env); } catch (IOException ioe) { if (ioe.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException")) { LOG.debug(ioe.getMessage(), ioe); } else { throw ioe; } } catch (UnhandledException e) { LOG.error(e.getMessage(), e); if (e.getResponseStatus() != 0) { httpResponse.sendError(e.getResponseStatus()); } else { throw new ServletException(e.getCause()); } } catch (Throwable e){ LOG.debug(e.getLocalizedMessage(), e); throw e; } }  @Override void init(); @Override void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse);  }
@Test public void setsUpEnvironmentContext() throws Exception { everrestServlet.service(request, response); ArgumentCaptor<EnvironmentContext> envCaptor = ArgumentCaptor.forClass(EnvironmentContext.class); verify(everrestProcessor).process(isA(ServletContainerRequest.class), isA(ContainerResponse.class), envCaptor.capture()); EnvironmentContext env = envCaptor.getValue(); assertSame(request, env.get(HttpServletRequest.class)); assertSame(response, env.get(HttpServletResponse.class)); assertSame(servletConfig, env.get(ServletConfig.class)); assertSame(servletContext, env.get(ServletContext.class)); assertNotNull(env.get(WebApplicationDeclaredRoles.class)); assertNotNull(env.get(ErrorPages.class)); }
@Test public void rethrowsIOExceptionThatThrownByEverrestProcessor() throws Exception { IOException ioException = new IOException(); doThrow(ioException).when(everrestProcessor).process(isA(ServletContainerRequest.class), isA(ContainerResponse.class), isA(EnvironmentContext.class)); thrown.expect(exceptionSameInstanceMatcher(ioException)); everrestServlet.service(request, response); }
@Test public void wrapsCauseOfUnhandledExceptionWithServletException() throws Exception { Exception exception = new Exception(); UnhandledException unhandledException = new UnhandledException(exception); doThrow(unhandledException).when(everrestProcessor).process(isA(ServletContainerRequest.class), isA(ContainerResponse.class), isA(EnvironmentContext.class)); thrown.expect(ServletException.class); thrown.expectCause(exceptionSameInstanceMatcher(exception)); everrestServlet.service(request, response); }
@Test public void sendsErrorStatusWhenStatusIsSetInUnhandledException() throws Exception { UnhandledException unhandledException = new UnhandledException(403); doThrow(unhandledException).when(everrestProcessor).process(isA(ServletContainerRequest.class), isA(ContainerResponse.class), isA(EnvironmentContext.class)); everrestServlet.service(request, response); verify(response).sendError(403); }