-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issues/40: Increase unit test coverage for skippy-gradle (#41)
- Loading branch information
Showing
13 changed files
with
282 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
|
||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...adle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+285 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class
Binary file not shown.
Binary file added
BIN
+662 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/SkippifiedTest1.class
Binary file not shown.
Binary file added
BIN
+285 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class
Binary file not shown.
Binary file added
BIN
+662 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class
Binary file not shown.
Binary file added
BIN
+285 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/NormalClass3.class
Binary file not shown.
Binary file added
BIN
+662 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class
Binary file not shown.
Binary file added
BIN
+846 Bytes
skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class
Binary file not shown.