Skip to content

Commit

Permalink
Merge pull request #1361 from hcoles/feature/cross_module
Browse files Browse the repository at this point in the history
Maven cross module tests
  • Loading branch information
hcoles authored Nov 4, 2024
2 parents d93b3c2 + a94bd6a commit 4b7a7f5
Show file tree
Hide file tree
Showing 18 changed files with 450 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.pitest.functional.Streams;

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -36,7 +37,7 @@ public Stream<ClassTree> codeTrees() {
}

public Set<ClassName> getCodeUnderTestNames() {
return this.classPath.code().stream().collect(Collectors.toSet());
return new HashSet<>(this.classPath.code());
}

public Set<ClassName> getTestClassNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import org.pitest.util.Verbosity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -502,8 +502,7 @@ public Optional<Reader> createHistoryReader() {
try {
if (this.historyInputLocation.exists()
&& (this.historyInputLocation.length() > 0)) {
return Optional.ofNullable(new InputStreamReader(new FileInputStream(
this.historyInputLocation), StandardCharsets.UTF_8));
return Optional.of(new InputStreamReader(Files.newInputStream(this.historyInputLocation.toPath()), StandardCharsets.UTF_8));
}
return Optional.empty();
} catch (final IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public AnalysisResult execute(File baseDir, ReportOptions data,
final LaunchOptions launchOptions = new LaunchOptions(ja,
settings.getJavaExecutable(), createJvmArgs(data), environmentVariables)
.usingClassPathJar(data.useClasspathJar());

final ProjectClassPaths cps = data.getMutationClassPaths();

final CodeSource code = settings.createCodeSource(cps);
Expand Down
40 changes: 25 additions & 15 deletions pitest-maven-verification/src/test/java/org/pitest/PitMojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,6 @@ public void shouldWorkWithGWTMockito() throws Exception {
assertThat(actual).doesNotContain("status='RUN_ERROR'");
}

@Test
@Ignore("yatspec is not available on maven central. Repo currently down")
public void shouldWorkWithYatspec() throws Exception {
File testDir = prepare("/pit-263-yatspec");
verifier.executeGoal("test");
verifier.executeGoal("org.pitest:pitest-maven:mutationCoverage");

String actual = readResults(testDir);
assertThat(actual)
.contains(
"<mutation detected='true' status='KILLED' numberOfTestsRun='1'><sourceFile>SomeClass.java</sourceFile>");
assertThat(actual).doesNotContain("status='NO_COVERAGE'");
assertThat(actual).doesNotContain("status='RUN_ERROR'");
}

@Test
// note this test depends on the junit5 plugin
public void shouldWorkWithQuarkus() throws Exception {
Expand Down Expand Up @@ -465,6 +450,31 @@ public void resolvesCorrectFilesForKotlinMultiModules() throws Exception {

}

@Test
public void handlesTestsInSeparateModulesWhenConfigured()
throws Exception {
File testDir = prepare("/pit-cross-module-tests");

verifier.executeGoal("install");
verifier.executeGoal("org.pitest:pitest-maven:mutationCoverage");

verifier.executeGoal("org.pitest:pitest-maven:report-aggregate-module");

File siteParentDir = buildFilePath(testDir, "target", "pit-reports");
assertThat(buildFilePath(siteParentDir, "index.html")).exists();
String projectReportsHtmlContents = FileUtils
.readFileToString(buildFilePath(testDir, "target", "pit-reports",
"index.html"));

assertTrue("miss data of subModule 1",
projectReportsHtmlContents
.contains("<a href=\"./org.example1/index.html\">org.example1</a>"));

assertTrue("coverage included",
projectReportsHtmlContents
.contains("85%"));
}

private void runForJava8Only() {
String javaVersion = System.getProperty("java.version");
assumeTrue(javaVersion.startsWith("1.8"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>pit-parent-module</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>cross-tests-code</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>cross-tests-code</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>
<properties>
<junit.version>4.13.1</junit.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example1;

public class SystemUnderTest {

private int aNumber = 0;

public SystemUnderTest() {
super();

int a = 25;
int b = 10;
if (a < b) {
aNumber = 10;
} else {
aNumber = -25;
}
}

public String toString() {
return "SystemUnderTest";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.example2;

public class SystemUnderTest {

private int aNumber = 0;

public SystemUnderTest() {
super();

int a = 25;
int b = 10;
if (a < b) {
aNumber = 10;
} else {
aNumber = -25;
}
}

public int getNumber() {
return aNumber;
}

public String toString() {
return "SystemUnderTest";
}

public boolean isActive() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>pit-parent-module</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>cross-tests-tests</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>cross-tests-tests</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cross-tests-code</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>
<properties>
<junit.version>4.13.1</junit.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example1;

import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNotNull;

import java.io.File;
import org.junit.Test;

public class FileOpeningTest {

@Test
public void testOpenFileRelativeToWorkingDirectory() {
SystemUnderTest sut = new SystemUnderTest();

File file = new File("src/test/resources/fixture.file");
assertTrue(file.exists());
assertNotNull(sut.toString());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example2;

import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNotNull;

import java.io.File;
import org.junit.Test;

public class FileOpeningTest {

@Test
public void testOpenFileRelativeToWorkingDirectory() {
SystemUnderTest sut = new SystemUnderTest();

File file = new File("src/test/resources/fixture.file");
assertTrue(file.exists());
assertNotNull(sut.toString());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.example2;

import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNotNull;

import org.junit.Test;

public class SystemUnderTestTest {

@Test
public void testGetNumber() {
SystemUnderTest sut = new SystemUnderTest();

int result = sut.getNumber();
assertTrue(result == -25);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test123
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>pit-parent-module</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>pit-parent-module</name>

<properties>
<junit.version>4.13.1</junit.version>
<pit.version>dev-SNAPSHOT</pit.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pit.version}</version>
<configuration>
<crossModule>true</crossModule>
<timestampedReports>false</timestampedReports>
<outputFormats>
<outputFormat>HTML</outputFormat>
<!-- xml is used by the report aggregate -->
<outputFormat>XML</outputFormat>
</outputFormats>
<!-- exportLineCoverage is used by the report aggregate -->
<exportLineCoverage>true</exportLineCoverage>

</configuration>
</plugin>
</plugins>
</pluginManagement>

</build>

<profiles>
<profile>
<id>pitest</id>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<executions>
<execution>
<id>pitest</id>
<phase>test-compile</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<modules>
<module>cross-tests-code</module>
<module>cross-tests-tests</module>
</modules>
</project>
Loading

0 comments on commit 4b7a7f5

Please sign in to comment.