CandlepinScopeFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (response.isCommitted()) { log.warn("Response was already committed!"); return; } requestScope.enter(); try { chain.doFilter(request, response); } finally { requestScope.exit(); } } @Inject  CandlepinScopeFilter(CandlepinRequestScope requestScope); @Override void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain); @Override void destroy(); @Override void init(FilterConfig config);  }
@Test public void ensureFilterEntersAndExitsScope() throws Exception { filter.doFilter(request, response, chain); verify(scope).enter(); verify(scope).exit(); }
@Test public void ensureFilterExitsScopeOnError() throws Exception { doThrow(new ServletException()).when(chain).doFilter(request, response); try { filter.doFilter(request, response, chain); fail("Expected an exception to be thrown."); } catch (ServletException e) { } verify(scope).enter(); verify(scope).exit(); }
@Test public void ensureAlreadyCommitedResponsesSafelyExit() throws Exception { doReturn(true).when(response).isCommitted(); filter.doFilter(request, response, chain); verify(scope, VerificationModeFactory.noMoreInteractions()).enter(); }