PieAdminFilter implements Filter { public PieAdminFilter(PieInitializer pieInitializer) { this.pieInitializer = pieInitializer; } PieAdminFilter(PieInitializer pieInitializer); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain); @Override void destroy(); static final String ADMIN_FILTER_URI; }
@Test public void testPieAdminFilter() throws IOException, ServletException, InterruptedException { TestPolicyEnforcer testPolicyEnforcer = new TestPolicyEnforcer(); try { testPolicyEnforcer.init(new PieConfig()); testPolicyEnforcer.getPolicy().doLogViolation("foo", "bar"); final long startTime = System.currentTimeMillis() + 1; Thread.sleep(10L); testPolicyEnforcer.getPolicy().doLogViolation("fizz", "buzz"); PieInitializer pieInitializer = createMock(PieInitializer.class); expect(pieInitializer.getPolicyEnforcer("simple")).andReturn(testPolicyEnforcer).anyTimes(); replay(pieInitializer); PieAdminFilter pieAdminFilter = new PieAdminFilter(pieInitializer); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getRequestURI()).andReturn("/my-app/foo/bar").anyTimes(); expect(request.getContextPath()).andReturn("/my-app").anyTimes(); HttpServletResponse response = createMock(HttpServletResponse.class); FilterChain chain = createMock(FilterChain.class); chain.doFilter(request, response); expectLastCall(); replay(request, response, chain); pieAdminFilter.doFilter(request, response, chain); verify(request, response, chain); request = createMock(HttpServletRequest.class); expect(request.getRequestURI()).andReturn("/my-app" + PieAdminFilter.ADMIN_FILTER_URI).anyTimes(); expect(request.getContextPath()).andReturn("/my-app").anyTimes(); expect(request.getParameter("policyEnforcer")).andReturn("simple").anyTimes(); expect(request.getParameter("startTime")).andReturn(null).anyTimes(); StringWriter output = new StringWriter(); response = createMock(HttpServletResponse.class); expect(response.getWriter()).andReturn(new PrintWriter(output)).anyTimes(); chain = createMock(FilterChain.class); replay(request, response, chain); pieAdminFilter.doFilter(request, response, chain); verify(request, response, chain); assertEquals(output.toString(), "=== PIE REPORT ===\nfoo\tbar\nfizz\tbuzz\n"); request = createMock(HttpServletRequest.class); expect(request.getRequestURI()).andReturn("/my-app" + PieAdminFilter.ADMIN_FILTER_URI).anyTimes(); expect(request.getContextPath()).andReturn("/my-app").anyTimes(); expect(request.getParameter("policyEnforcer")).andReturn("simple").anyTimes(); expect(request.getParameter("startTime")).andReturn(Long.toString(startTime)).anyTimes(); output = new StringWriter(); response = createMock(HttpServletResponse.class); expect(response.getWriter()).andReturn(new PrintWriter(output)).anyTimes(); chain = createMock(FilterChain.class); replay(request, response, chain); pieAdminFilter.doFilter(request, response, chain); verify(request, response, chain); assertEquals(output.toString(), "=== PIE REPORT ===\nfizz\tbuzz\n"); verify(pieInitializer); } finally { testPolicyEnforcer.shutdown(); } }