diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java b/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java index 9a79e7b..54b5914 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java @@ -49,7 +49,6 @@ public ClassFileCollector(Project project, SourceSetContainer sourceSetContainer this.sourceSetContainer = sourceSetContainer; } - /** * Collects all {@link ClassFile}s in the project. * diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java new file mode 100644 index 0000000..495650d --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.skippy.gradle.collector; + +import io.skippy.gradle.model.ClassFile; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import static io.skippy.gradle.collector.CollectorTestUtils.mockSourceSetContainer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link ClassFileCollector}. + * + * @author Florian McKee + */ +public class ClassFileCollectorTest { + + @Test + void testCollect() throws URISyntaxException { + + var currentDir = Paths.get(getClass().getResource("sourceset1").toURI()).toFile(); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(currentDir.getParentFile().getParentFile()); + + var classFileCollector = new ClassFileCollector(project, mockSourceSetContainer("sourceset1", "sourceset2")); + + var classFiles = classFileCollector.collect(); + assertEquals(4, classFiles.size()); + + var classFile0 = classFiles.get(0); + assertEquals("com.example.NormalClass1", classFile0.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/NormalClass1.class"), classFile0.getRelativePath()); + + var classFile1 = classFiles.get(1); + assertEquals("com.example.NormalClass2", classFile1.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/NormalClass2.class"), classFile1.getRelativePath()); + + var classFile2 = classFiles.get(2); + assertEquals("com.example.SkippifiedTest1", classFile2.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/SkippifiedTest1.class"), classFile2.getRelativePath()); + + var classFile3 = classFiles.get(3); + assertEquals("com.example.SkippifiedTest2", classFile3.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/SkippifiedTest2.class"), classFile3.getRelativePath()); + + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java new file mode 100644 index 0000000..86820ba --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java @@ -0,0 +1,65 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.skippy.gradle.collector; + +import org.gradle.api.file.FileCollection; +import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.api.tasks.SourceSetOutput; + +import java.io.File; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.util.Set; + +import static java.util.Arrays.asList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Utility method to mock a {@link SourceSet}. + * + * @author Florian McKee + */ +class CollectorTestUtils { + + static SourceSetContainer mockSourceSetContainer(String... sourceSetDirectories) { + var sourceSetContainer = mock(SourceSetContainer.class); + var sourceSets = asList(sourceSetDirectories).stream().map(CollectorTestUtils::mockSourceSet).toList(); + for (int i = 0; i < sourceSets.size(); i++) { + when(sourceSetContainer.getByName(sourceSetDirectories[i])).thenReturn(sourceSets.get(i)); + } + when(sourceSetContainer.iterator()).thenReturn(sourceSets.iterator()); + return sourceSetContainer; + } + + private static SourceSet mockSourceSet(String directory) { + try { + File outputDirectory = Paths.get(CollectorTestUtils.class.getResource(directory).toURI()).toFile(); + var sourceSet = mock(SourceSet.class); + var sourceSetOutput = mock(SourceSetOutput.class); + when(sourceSet.getOutput()).thenReturn(sourceSetOutput); + var classesDir = mock(FileCollection.class); + when(sourceSetOutput.getClassesDirs()).thenReturn(classesDir); + when(classesDir.getFiles()).thenReturn(Set.of(outputDirectory)); + return sourceSet; + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java new file mode 100644 index 0000000..fae1358 --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.skippy.gradle.collector; + +import io.skippy.gradle.SkippyPluginExtension; +import io.skippy.gradle.model.SourceSetWithTestTask; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static io.skippy.gradle.collector.CollectorTestUtils.mockSourceSetContainer; +import static java.util.Arrays.asList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link SkippifiedTestCollector}. + * + * @author Florian McKee + */ +public class SkippifiedTestCollectorTest { + + @Test + void testCollect() throws URISyntaxException { + var currentDir = Paths.get(getClass().getResource("sourceset1").toURI()).toFile(); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(currentDir.getParentFile().getParentFile()); + + var sourceSetContainer = mockSourceSetContainer("sourceset1", "sourceset2", "sourceset3"); + var classFileCollector = new ClassFileCollector(project, sourceSetContainer); + var skippyPluginExtension = mock(SkippyPluginExtension.class); + + when(skippyPluginExtension.getSourceSetsWithTestTasks()).thenReturn(asList( + new SourceSetWithTestTask("sourceset1", "test"), + new SourceSetWithTestTask("sourceset2", "integrationTest"), + new SourceSetWithTestTask("sourceset3", "functionalTest") + )); + var skippifiedTestCollector = new SkippifiedTestCollector(classFileCollector, sourceSetContainer, skippyPluginExtension); + + var skippifiedTests = skippifiedTestCollector.collect(); + + assertEquals(3, skippifiedTests.size()); + + var skippifiedTests0 = skippifiedTests.get(0); + assertEquals("com.example.SkippifiedTest1", skippifiedTests0.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/SkippifiedTest1.class"), skippifiedTests0.classFile().getRelativePath()); + assertEquals("test", skippifiedTests0.testTask()); + + var skippifiedTests1 = skippifiedTests.get(1); + assertEquals("com.example.SkippifiedTest2", skippifiedTests1.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/SkippifiedTest2.class"), skippifiedTests1.classFile().getRelativePath()); + assertEquals("integrationTest", skippifiedTests1.testTask()); + + var skippifiedTests2 = skippifiedTests.get(2); + assertEquals("com.example.SkippifiedTest3", skippifiedTests2.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset3/SkippifiedTest3.class"), skippifiedTests2.classFile().getRelativePath()); + assertEquals("functionalTest", skippifiedTests2.testTask()); + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java new file mode 100644 index 0000000..94e74b8 --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.skippy.gradle.coveragebuild; + +import io.skippy.gradle.model.ClassFile; +import io.skippy.gradle.model.SkippifiedTest; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link CoverageBuildTasksAndArguments}. + * + * @author Florian McKee + */ +public class CoverageBuildTasksAndArgumentsTest { + + @Test + void testForSkippifiedTest() throws URISyntaxException { + var classFile = Paths.get(getClass().getResource("LeftPadderTest.class").toURI()); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(classFile.toFile().getParentFile().getParentFile()); + var skippifiedTest = new SkippifiedTest(new ClassFile(project, classFile), "test"); + var tasksAndArgs = CoverageBuildTasksAndArguments.forSkippifiedTest(skippifiedTest); + assertThat(tasksAndArgs.tasks()).contains( + "test", + "jacocoTestReport" + ); + assertThat(tasksAndArgs.arguments()).contains( + "-PskippyCoverageBuild=true", + "-PskippyClassFile=coveragebuild/LeftPadderTest.class", + "-PskippyTestTask=test" + ); + } +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java index 8f9b161..4fb5524 100644 --- a/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java +++ b/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java @@ -16,16 +16,17 @@ package io.skippy.gradle.model; -import io.skippy.gradle.model.ClassFile; import org.gradle.api.Project; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import java.net.URISyntaxException; +import java.nio.file.Path; import java.nio.file.Paths; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Tests for {@link ClassFile}. @@ -44,6 +45,18 @@ void testGetFullyQualifiedClassName(String fileName, String expectedValue) throw assertEquals(expectedValue, new ClassFile(mock(Project.class), classFile).getFullyQualifiedClassName()); } + @ParameterizedTest + @CsvSource(value = { + "SourceFileTest1.class:model/SourceFileTest1.class", + "SourceFileTest2.class:model/SourceFileTest2.class" + }, delimiter = ':') + void testGetRelativePath(String fileName, String expectedValue) throws URISyntaxException { + var classFile = Paths.get(getClass().getResource(fileName).toURI()); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(classFile.toFile().getParentFile().getParentFile()); + assertEquals(Path.of(expectedValue), new ClassFile(project, classFile).getRelativePath()); + } + @ParameterizedTest @CsvSource(value = { "SourceFileTest1.class:w7P+0X1Grw3y8nPkIceITQ==", diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class new file mode 100644 index 0000000..558ca66 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/SkippifiedTest1.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/SkippifiedTest1.class new file mode 100644 index 0000000..0f94ea1 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/SkippifiedTest1.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class new file mode 100644 index 0000000..8a977c3 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class new file mode 100644 index 0000000..f102057 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/NormalClass3.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/NormalClass3.class new file mode 100644 index 0000000..135cd75 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/NormalClass3.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class new file mode 100644 index 0000000..a182b5a Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class differ diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class b/skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class new file mode 100644 index 0000000..a254282 Binary files /dev/null and b/skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class differ