Skip to content

Commit

Permalink
Merge pull request #1095 from Pfoerd/add_aggregated_results_to_report…
Browse files Browse the repository at this point in the history
…_aggregator

add aggregated results to report aggregator and configurable thresholds to maven aggregator mojo
  • Loading branch information
hcoles authored Oct 14, 2022
2 parents 584c6a0 + 4926c6e commit 0006900
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pitest.aggregate;

public class AggregationResult {
private final long mutations;
private final long mutationsSurvived;
private final int mutationCoverage;

private final int testStrength;

public AggregationResult(long mutations, long mutationsSurvived, int mutationCoverage, int testStrength) {
this.mutations = mutations;
this.mutationsSurvived = mutationsSurvived;
this.mutationCoverage = mutationCoverage;
this.testStrength = testStrength;
}

public long getMutations() {
return mutations;
}

public long getMutationsSurvived() {
return mutationsSurvived;
}

public int getMutationCoverage() {
return mutationCoverage;
}

public int getTestStrength() {
return testStrength;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,23 @@ private ReportAggregator(final ResultOutputStrategy resultOutputStrategy, final
this.outputCharset = outputCharset;
}

public void aggregateReport() throws ReportAggregationException {
public AggregationResult aggregateReport() throws ReportAggregationException {
final MutationMetaData mutationMetaData = new MutationMetaData(new ArrayList<>(this.mutationLoader.loadData()));

final MutationResultListener mutationResultListener = createResultListener(mutationMetaData);
final ReportAggregatorResultListener reportAggregatorResultListener = new ReportAggregatorResultListener();

reportAggregatorResultListener.runStart();
mutationResultListener.runStart();

for (final ClassMutationResults mutationResults : mutationMetaData.toClassResults()) {
reportAggregatorResultListener.handleMutationResult(mutationResults);
mutationResultListener.handleMutationResult(mutationResults);
}
reportAggregatorResultListener.runEnd();
mutationResultListener.runEnd();

return reportAggregatorResultListener.result();
}

private MutationResultListener createResultListener(final MutationMetaData mutationMetaData) throws ReportAggregationException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pitest.aggregate;

import org.pitest.mutationtest.ClassMutationResults;
import org.pitest.mutationtest.MutationResultListener;
import org.pitest.mutationtest.report.html.MutationTotals;

public class ReportAggregatorResultListener implements MutationResultListener {
MutationTotals totals = new MutationTotals();

@Override
public void runStart() {
// nothing to do here
}

@Override
public void handleMutationResult(ClassMutationResults results) {
totals.addFiles(1);
totals.addMutations(results.getMutations().size());
totals.addMutationsDetetcted(results.getMutations().stream().filter(mutation -> mutation.getStatus().isDetected()).count());
totals.addMutationsWithCoverage(results.getMutations().stream().filter(it -> it.getStatus().hasCoverage()).count());
}

@Override
public void runEnd() {
// nothing to do here
}

public AggregationResult result() {
return new AggregationResult(totals.getNumberOfMutations(), totals.getNumberOfMutations() - totals.getNumberOfMutationsDetected(),
totals.getMutationCoverage(), totals.getTestStrength());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void shouldComputeReportOfTheSubModule()

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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
</outputFormats>
<!-- exportLineCoverage is used by the report aggregate -->
<exportLineCoverage>true</exportLineCoverage>
<aggregatedTestStrengthThreshold>26</aggregatedTestStrengthThreshold>
<aggregatedMutationThreshold>20</aggregatedMutationThreshold>
<aggregatedMaxSurviving>6</aggregatedMaxSurviving>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ public SystemUnderTest() {
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,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
@@ -1,10 +1,12 @@
package org.pitest.maven.report;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.util.FileUtils;
import org.pitest.aggregate.AggregationResult;
import org.pitest.aggregate.ReportAggregator;
import org.pitest.functional.FCollection;
import org.pitest.maven.DependencyFilter;
Expand Down Expand Up @@ -36,6 +38,24 @@ abstract class AbstractPitAggregationReportMojo extends PitReportMojo {
@Parameter(property = "reactorProjects", readonly = true)
List<MavenProject> reactorProjects;

/**
* Mutation score threshold at which to fail build
*/
@Parameter(defaultValue = "0", property = "aggregatedMutationThreshold")
private int aggregatedMutationThreshold;

/**
* Test strength score threshold at which to fail build
*/
@Parameter(defaultValue = "0", property = "aggregatedTestStrengthThreshold")
private int aggregatedTestStrengthThreshold;

/**
* Maximum surviving mutants to allow
*/
@Parameter(defaultValue = "-1", property = "aggregatedMaxSurviving")
private int aggregatedMaxSurviving = -1;

private final ReportSourceLocator reportSourceLocator = new ReportSourceLocator();

/**
Expand Down Expand Up @@ -69,7 +89,12 @@ protected void executeReport(final Locale locale)
new UndatedReportDirCreationStrategy()))
.build();

reportAggregator.aggregateReport();
AggregationResult result = reportAggregator.aggregateReport();

throwErrorIfTestStrengthBelowThreshold(result.getTestStrength());
throwErrorIfScoreBelowThreshold(result.getMutationCoverage());
throwErrorIfMoreThanMaximumSurvivors(result.getMutationsSurvived());

} catch (final Exception e) {
throw new MavenReportException(e.getMessage(), e);
}
Expand Down Expand Up @@ -138,4 +163,34 @@ private List<File> getCompiledDirs(final MavenProject project)
project.getBuild().getTestOutputDirectory()),
sourceRoots);
}

private void throwErrorIfScoreBelowThreshold(final int mutationCoverage)
throws MojoFailureException {
if ((this.aggregatedMutationThreshold != 0)
&& (mutationCoverage < this.aggregatedMutationThreshold)) {
throw new MojoFailureException("Mutation score of "
+ mutationCoverage + " is below threshold of "
+ this.aggregatedMutationThreshold);
}
}

private void throwErrorIfTestStrengthBelowThreshold(final int testStrength)
throws MojoFailureException {
if ((this.aggregatedTestStrengthThreshold != 0)
&& (testStrength < this.aggregatedTestStrengthThreshold)) {
throw new MojoFailureException("Test strength score of "
+ testStrength + " is below threshold of "
+ this.aggregatedTestStrengthThreshold);
}
}

private void throwErrorIfMoreThanMaximumSurvivors(final long mutationsSurvived)
throws MojoFailureException {
if ((this.aggregatedMaxSurviving >= 0)
&& (mutationsSurvived > this.aggregatedMaxSurviving)) {
throw new MojoFailureException("Had "
+ mutationsSurvived + " surviving mutants, but only "
+ this.aggregatedMaxSurviving + " survivors allowed");
}
}
}

0 comments on commit 0006900

Please sign in to comment.