Skip to content

Commit

Permalink
issues/40: Increase unit test coverage for skippy-gradle (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmck3516 authored Dec 10, 2023
1 parent ef4948e commit efb06e1
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public ClassFileCollector(Project project, SourceSetContainer sourceSetContainer
this.sourceSetContainer = sourceSetContainer;
}


/**
* Collects all {@link ClassFile}s in the project.
*
Expand Down
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());

}

}
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);
}
}

}
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());
}

}
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"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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==",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit efb06e1

Please sign in to comment.