<commit_before>package com.coverity.security.pie.web;

import com.coverity.security.pie.core.PieConfig;
import com.coverity.security.pie.core.test.TestPolicyEnforcer;
import org.testng.annotations.Test;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static org.easymock.EasyMock.*;
import static org.testng.Assert.assertEquals;

public class PieAdminFilterTest {
    @Test
    public void testPieAdminFilter() throws IOException, ServletException, InterruptedException {

        TestPolicyEnforcer testPolicyEnforcer = new TestPolicyEnforcer();
        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 that nothing happened with the response and that chain filter is called
        verify(request, response, chain);


        // Now test a call to the filter
        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 that chain filter is NOT called
        verify(request, response, chain);
        // Verify output
        assertEquals(output.toString(), "=== PIE REPORT ===\nfoo\tbar\nfizz\tbuzz\n");


        // Now test with a startTime parameter
        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 that chain filter is NOT called
        verify(request, response, chain);
        // Verify output
        assertEquals(output.toString(), "=== PIE REPORT ===\nfizz\tbuzz\n");

        verify(pieInitializer);
    }
}
<commit_msg>Fixed test failure when running full maven test.<commit_after>package com.coverity.security.pie.web;

import com.coverity.security.pie.core.PieConfig;
import com.coverity.security.pie.core.test.TestPolicyEnforcer;
import org.testng.annotations.Test;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static org.easymock.EasyMock.*;
import static org.testng.Assert.assertEquals;

public class PieAdminFilterTest {
    @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 that nothing happened with the response and that chain filter is called
            verify(request, response, chain);


            // Now test a call to the filter
            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 that chain filter is NOT called
            verify(request, response, chain);
            // Verify output
            assertEquals(output.toString(), "=== PIE REPORT ===\nfoo\tbar\nfizz\tbuzz\n");


            // Now test with a startTime parameter
            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 that chain filter is NOT called
            verify(request, response, chain);
            // Verify output
            assertEquals(output.toString(), "=== PIE REPORT ===\nfizz\tbuzz\n");

            verify(pieInitializer);
        } finally {
            testPolicyEnforcer.shutdown();
        }
    }
}

{'fraction_non_alphanumeric': 0.1184241847179243, 'fraction_numerical': 0.0007141156867412521, 'mean_word_length': 3.383411580594679, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7490140', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2171, 'n_words': 391}