<commit_before>// Copyright © 2011-2013, <NAME> <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package fi.jumi.core.junit;

import fi.jumi.api.drivers.TestId;
import fi.jumi.core.api.TestFile;
import fi.jumi.core.results.*;
import fi.jumi.core.runs.RunId;
import fi.jumi.core.testbench.TestBench;
import fi.jumi.core.util.SpyListener;
import org.junit.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class JUnitCompatibilityDriverTest {

    private final TestBench testBench = new TestBench();
    private final SpyListener<RunVisitor> spy = new SpyListener<>(RunVisitor.class);
    private final RunVisitor expect = spy.getListener();

    private Class<?> testClass;
    private TestFile testFile;
    private SuiteEventDemuxer results;

    @Before
    public void setup() throws Exception {
        testBench.setDriverFinder(new JUnitCompatibilityDriverFinder());
//        testBench.setActorsMessageListener(new PrintStreamMessageLogger(System.out));
    }

    private void runTestClass(Class<?> testClass) {
        this.testClass = testClass;
        testFile = TestFile.fromClass(testClass);
        results = testBench.run(testClass);
    }

    private void checkExpectations() {
        spy.replay();
        results.visitAllRuns(expect);
        spy.verify();
    }


    @Test
    public void single_passing_test() {
        runTestClass(OnePassing.class);

        assertThat(results.getTestName(testFile, TestId.ROOT), is(testClass.getSimpleName()));
        assertThat(results.getTestName(testFile, TestId.of(0)), is("testPassing"));

        expect.onRunStarted(new RunId(1), testFile);
        expect.onTestStarted(new RunId(1), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(1), testFile);

        checkExpectations();
    }

    @Test
    public void multiple_passing_tests() {
        runTestClass(TwoPassing.class);

        assertThat(results.getTestName(testFile, TestId.ROOT), is(testClass.getSimpleName()));
        assertThat(results.getTestName(testFile, TestId.of(0)), is("testOne"));
        assertThat(results.getTestName(testFile, TestId.of(1)), is("testTwo"));

        expect.onRunStarted(new RunId(1), testFile);
        expect.onTestStarted(new RunId(1), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(1), testFile);

        expect.onRunStarted(new RunId(2), testFile);
        expect.onTestStarted(new RunId(2), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(2), testFile, TestId.of(1));
        expect.onTestFinished(new RunId(2), testFile, TestId.of(1));
        expect.onTestFinished(new RunId(2), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(2), testFile);

        checkExpectations();
    }


    // TODO: deep test hierarchies
    // TODO: failures
    // TODO: ignored tests
    // TODO: assumptions

    public static class OnePassing {
        @Test
        public void testPassing() {
        }
    }
<commit_msg>Fixed a flaky test: cannot rely on JUnit's order of test methods<commit_after>// Copyright © 2011-2013, <NAME> <www.orfjack<EMAIL>>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package fi.jumi.core.junit;

import fi.jumi.api.drivers.TestId;
import fi.jumi.core.api.TestFile;
import fi.jumi.core.results.*;
import fi.jumi.core.runs.RunId;
import fi.jumi.core.testbench.TestBench;
import fi.jumi.core.util.SpyListener;
import org.junit.*;

import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class JUnitCompatibilityDriverTest {

    private final TestBench testBench = new TestBench();
    private final SpyListener<RunVisitor> spy = new SpyListener<>(RunVisitor.class);
    private final RunVisitor expect = spy.getListener();

    private Class<?> testClass;
    private TestFile testFile;
    private SuiteEventDemuxer results;

    @Before
    public void setup() throws Exception {
        testBench.setDriverFinder(new JUnitCompatibilityDriverFinder());
//        testBench.setActorsMessageListener(new PrintStreamMessageLogger(System.out));
    }

    private void runTestClass(Class<?> testClass) {
        this.testClass = testClass;
        testFile = TestFile.fromClass(testClass);
        results = testBench.run(testClass);
    }

    private void checkExpectations() {
        spy.replay();
        results.visitAllRuns(expect);
        spy.verify();
    }


    @Test
    public void single_passing_test() {
        runTestClass(OnePassing.class);

        assertThat(results.getTestName(testFile, TestId.ROOT), is(testClass.getSimpleName()));
        assertThat(results.getTestName(testFile, TestId.of(0)), is("testPassing"));

        expect.onRunStarted(new RunId(1), testFile);
        expect.onTestStarted(new RunId(1), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(1), testFile);

        checkExpectations();
    }

    @Test
    public void multiple_passing_tests() {
        runTestClass(TwoPassing.class);

        assertThat(results.getTestName(testFile, TestId.ROOT), is("TwoPassing"));
        String name0 = results.getTestName(testFile, TestId.of(0));
        String name1 = results.getTestName(testFile, TestId.of(1));
        assertThat(Arrays.asList(name0, name1), containsInAnyOrder("testOne", "testTwo")); // JUnit's order of test methods is undefined

        expect.onRunStarted(new RunId(1), testFile);
        expect.onTestStarted(new RunId(1), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.of(0));
        expect.onTestFinished(new RunId(1), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(1), testFile);

        expect.onRunStarted(new RunId(2), testFile);
        expect.onTestStarted(new RunId(2), testFile, TestId.ROOT);
        expect.onTestStarted(new RunId(2), testFile, TestId.of(1));
        expect.onTestFinished(new RunId(2), testFile, TestId.of(1));
        expect.onTestFinished(new RunId(2), testFile, TestId.ROOT);
        expect.onRunFinished(new RunId(2), testFile);

        checkExpectations();
    }


    // TODO: deep test hierarchies
    // TODO: failures
    // TODO: ignored tests
    // TODO: assumptions

    public static class OnePassing {
        @Test
        public void testPassing() {
        }
    }

{'fraction_non_alphanumeric': 0.12289257349867633, 'fraction_numerical': 0.011425386651804375, 'mean_word_length': 3.4694894146948942, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3112249', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2253, 'n_words': 447}