|
| 1 | +package de.donnerbart.split; |
| 2 | + |
| 3 | +import de.donnerbart.split.model.TestCase; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.io.TempDir; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.EnumSource; |
| 10 | + |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.attribute.PosixFilePermission; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.concurrent.atomic.AtomicReference; |
| 16 | + |
| 17 | +import static de.donnerbart.split.TestUtil.copyResourceToTarget; |
| 18 | +import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; |
| 19 | +import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.assertj.core.api.Assertions.byLessThan; |
| 22 | + |
| 23 | +class TestLoaderTest { |
| 24 | + |
| 25 | + private static final @NotNull Set<PosixFilePermission> PERMISSIONS = Set.of(OWNER_READ, OWNER_WRITE); |
| 26 | + |
| 27 | + @TempDir |
| 28 | + private @NotNull Path tmp; |
| 29 | + |
| 30 | + private final @NotNull AtomicReference<Integer> exitCode = new AtomicReference<>(); |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() throws Exception { |
| 34 | + final var projectFolder = tmp.resolve("example-project") |
| 35 | + .resolve("src") |
| 36 | + .resolve("main") |
| 37 | + .resolve("java") |
| 38 | + .resolve("de") |
| 39 | + .resolve("donnerbart") |
| 40 | + .resolve("example"); |
| 41 | + Files.createDirectories(projectFolder); |
| 42 | + // ignored tests |
| 43 | + copyResourceToTarget(projectFolder, "tests/AbstractTest.java", "AbstractTest.java", PERMISSIONS); |
| 44 | + copyResourceToTarget(projectFolder, "tests/BaseTest.java", "BaseTest.java", PERMISSIONS); |
| 45 | + copyResourceToTarget(projectFolder, "tests/DisabledTest.java", "DisabledTest.java", PERMISSIONS); |
| 46 | + copyResourceToTarget(projectFolder, "tests/IgnoreTest.java", "IgnoreTest.java", PERMISSIONS); |
| 47 | + copyResourceToTarget(projectFolder, "tests/InterfaceTest.java", "InterfaceTest.java", PERMISSIONS); |
| 48 | + // valid tests |
| 49 | + copyResourceToTarget(projectFolder, "tests/FastTest.java", "FastTest.java", PERMISSIONS); |
| 50 | + copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS); |
| 51 | + copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS); |
| 52 | + copyResourceToTarget(projectFolder, "tests/SlowTest.java", "SlowTest.java", PERMISSIONS); |
| 53 | + copyResourceToTarget(projectFolder, "tests/SlowestTest.java", "SlowestTest.java", PERMISSIONS); |
| 54 | + |
| 55 | + final var reportFolder = tmp.resolve("junit-reports"); |
| 56 | + copyResourceToTarget(reportFolder, |
| 57 | + "reports/TEST-de.donnerbart.example.DeletedTest.xml", |
| 58 | + "TEST-de.donnerbart.example.DeletedTest.xml", |
| 59 | + PERMISSIONS); |
| 60 | + copyResourceToTarget(reportFolder, |
| 61 | + "reports/TEST-de.donnerbart.example.FastTest.xml", |
| 62 | + "TEST-de.donnerbart.example.FastTest.xml", |
| 63 | + PERMISSIONS); |
| 64 | + copyResourceToTarget(reportFolder, |
| 65 | + "reports/TEST-de.donnerbart.example.SlowTest.xml", |
| 66 | + "TEST-de.donnerbart.example.SlowTest.xml", |
| 67 | + PERMISSIONS); |
| 68 | + copyResourceToTarget(reportFolder, |
| 69 | + "reports/TEST-de.donnerbart.example.SlowestTest.xml", |
| 70 | + "TEST-de.donnerbart.example.SlowestTest.xml", |
| 71 | + PERMISSIONS); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void load_withoutJUnit() throws Exception { |
| 76 | + final var testCases = loadTests(false, NewTestTimeOption.ZERO); |
| 77 | + assertThat(testCases).satisfiesExactlyInAnyOrder( // |
| 78 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.FastTest", 0d)), |
| 79 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowTest", 0d)), |
| 80 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowestTest", 0d)), |
| 81 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingOneTest", 0d)), |
| 82 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingTwoTest", 0d))); |
| 83 | + assertThat(exitCode).hasNullValue(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void load_withJUnit() throws Exception { |
| 88 | + final var testCases = loadTests(true, NewTestTimeOption.ZERO); |
| 89 | + assertThat(testCases).satisfiesExactlyInAnyOrder( // |
| 90 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.FastTest", 2.374d)), |
| 91 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowTest", 12.386d)), |
| 92 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowestTest", 153.457d)), |
| 93 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingOneTest", 0d)), |
| 94 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingTwoTest", 0d))); |
| 95 | + assertThat(exitCode).hasNullValue(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void load_withJUnit_withAverageTestTime() throws Exception { |
| 100 | + final var testCases = loadTests(true, NewTestTimeOption.AVERAGE); |
| 101 | + assertThat(testCases).satisfiesExactlyInAnyOrder( // |
| 102 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.FastTest", 2.374d)), |
| 103 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowTest", 12.386d)), |
| 104 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowestTest", 153.457d)), |
| 105 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingOneTest", 56.0723d)), |
| 106 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingTwoTest", 56.0723d))); |
| 107 | + assertThat(exitCode).hasNullValue(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void load_withJUnit_withMinTestTime() throws Exception { |
| 112 | + final var testCases = loadTests(true, NewTestTimeOption.MIN); |
| 113 | + assertThat(testCases).satisfiesExactlyInAnyOrder( // |
| 114 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.FastTest", 2.374d)), |
| 115 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowTest", 12.386d)), |
| 116 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowestTest", 153.457d)), |
| 117 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingOneTest", 2.374d)), |
| 118 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingTwoTest", 2.374d))); |
| 119 | + assertThat(exitCode).hasNullValue(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void load_withJUnit_withMaxTestTime() throws Exception { |
| 124 | + final var testCases = loadTests(true, NewTestTimeOption.MAX); |
| 125 | + assertThat(testCases).satisfiesExactlyInAnyOrder( // |
| 126 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.FastTest", 2.374d)), |
| 127 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowTest", 12.386d)), |
| 128 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.SlowestTest", 153.457d)), |
| 129 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingOneTest", 153.457d)), |
| 130 | + testCase -> assertTestCase(testCase, new TestCase("de.donnerbart.example.NoTimingTwoTest", 153.457d))); |
| 131 | + assertThat(exitCode).hasNullValue(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void load_whitespaceClassDefinition() throws Exception { |
| 136 | + final var projectFolder = |
| 137 | + tmp.resolve("multiline-class-definition-project").resolve("src").resolve("main").resolve("java"); |
| 138 | + copyResourceToTarget(projectFolder, |
| 139 | + "tests/WhitespaceClassDefinitionTest.java", |
| 140 | + "WhitespaceClassDefinitionTest.java", |
| 141 | + PERMISSIONS); |
| 142 | + |
| 143 | + final var testLoader = new TestLoader("**/multiline-class-definition-project/**/*Test.java", |
| 144 | + null, |
| 145 | + null, |
| 146 | + NewTestTimeOption.ZERO, |
| 147 | + tmp, |
| 148 | + exitCode::set); |
| 149 | + final var testCases = testLoader.load(); |
| 150 | + assertThat(testCases).singleElement().satisfies(testCase -> assertTestCase(testCase, // |
| 151 | + new TestCase("de.donnerbart.example.WhitespaceClassDefinitionTest", 0d))); |
| 152 | + assertThat(exitCode).hasNullValue(); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void load_thirdPartyLibrary() throws Exception { |
| 157 | + final var projectFolder = |
| 158 | + tmp.resolve("third-party-library-project").resolve("src").resolve("main").resolve("java"); |
| 159 | + copyResourceToTarget(projectFolder, |
| 160 | + "tests/ThirdPartyLibraryTest.java", |
| 161 | + "ThirdPartyLibraryTest.java", |
| 162 | + PERMISSIONS); |
| 163 | + |
| 164 | + final var testCases = |
| 165 | + loadTests(false, NewTestTimeOption.ZERO, "**/third-party-library-project/**/*Test.java", projectFolder); |
| 166 | + assertThat(testCases).singleElement().satisfies(testCase -> assertTestCase(testCase, // |
| 167 | + new TestCase("de.donnerbart.example.ThirdPartyLibraryTest", 0d))); |
| 168 | + assertThat(exitCode).hasNullValue(); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void load_noPackage() throws Exception { |
| 173 | + final var projectFolder = tmp.resolve("no-package-project").resolve("src").resolve("main").resolve("java"); |
| 174 | + copyResourceToTarget(projectFolder, "tests/NoPackageTest.java", "NoPackageTest.java", PERMISSIONS); |
| 175 | + |
| 176 | + final var testCases = |
| 177 | + loadTests(false, NewTestTimeOption.ZERO, "**/no-package-project/**/*Test.java", projectFolder); |
| 178 | + assertThat(testCases).singleElement().satisfies(testCase -> assertTestCase(testCase, // |
| 179 | + new TestCase("NoPackageTest", 0d))); |
| 180 | + assertThat(exitCode).hasNullValue(); |
| 181 | + } |
| 182 | + |
| 183 | + @ParameterizedTest |
| 184 | + @EnumSource(NewTestTimeOption.class) |
| 185 | + void load_noTests(final @NotNull NewTestTimeOption newTestTimeOption) throws Exception { |
| 186 | + final var projectFolder = tmp.resolve("no-tests-project").resolve("src").resolve("main").resolve("java"); |
| 187 | + Files.createDirectories(projectFolder); |
| 188 | + |
| 189 | + final var testCases = loadTests(false, newTestTimeOption, "**/no-tests-project/**/*Test.java", projectFolder); |
| 190 | + assertThat(testCases).isEmpty(); |
| 191 | + assertThat(exitCode).hasValue(1); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + void load_noClassName() throws Exception { |
| 196 | + final var projectFolder = tmp.resolve("no-classname-project").resolve("src").resolve("main").resolve("java"); |
| 197 | + copyResourceToTarget(projectFolder, "tests/NoClassNameTest.java", "NoClassNameTest.java", PERMISSIONS); |
| 198 | + |
| 199 | + final var testCases = |
| 200 | + loadTests(false, NewTestTimeOption.ZERO, "**/no-classname-project/**/*Test.java", projectFolder); |
| 201 | + assertThat(testCases).isEmpty(); |
| 202 | + assertThat(exitCode).hasValue(1); |
| 203 | + } |
| 204 | + |
| 205 | + private @NotNull Set<TestCase> loadTests( |
| 206 | + final boolean withJUnit, |
| 207 | + final @NotNull NewTestTimeOption newTestTimeOption) throws Exception { |
| 208 | + return loadTests(withJUnit, newTestTimeOption, "**/example-project/**/*Test.java", tmp); |
| 209 | + } |
| 210 | + |
| 211 | + private @NotNull Set<TestCase> loadTests( |
| 212 | + final boolean withJUnit, |
| 213 | + final @NotNull NewTestTimeOption newTestTimeOption, |
| 214 | + final @NotNull String glob, |
| 215 | + final @NotNull Path workingDir) throws Exception { |
| 216 | + final var testLoader = new TestLoader(glob, |
| 217 | + "**/example-project/**/*Abstract*.java", |
| 218 | + withJUnit ? "**/junit-reports/*.xml" : null, |
| 219 | + newTestTimeOption, |
| 220 | + workingDir, |
| 221 | + exitCode::set); |
| 222 | + return testLoader.load(); |
| 223 | + } |
| 224 | + |
| 225 | + private static void assertTestCase(final @NotNull TestCase actual, final @NotNull TestCase expected) { |
| 226 | + assertThat(actual.name()).isEqualTo(expected.name()); |
| 227 | + assertThat(actual.time()).isEqualTo(expected.time(), byLessThan(0.0001d)); |
| 228 | + } |
| 229 | +} |
0 commit comments