diff --git a/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/MutationCoverageReport.java b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/MutationCoverageReport.java index 1b91d45c1..23639b4e7 100644 --- a/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/MutationCoverageReport.java +++ b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/MutationCoverageReport.java @@ -14,17 +14,15 @@ */ package org.pitest.mutationtest.commandline; -import java.util.HashMap; - -import org.pitest.coverage.CoverageSummary; import org.pitest.mutationtest.config.PluginServices; import org.pitest.mutationtest.config.ReportOptions; -import org.pitest.mutationtest.statistics.MutationStatistics; import org.pitest.mutationtest.tooling.AnalysisResult; import org.pitest.mutationtest.tooling.CombinedStatistics; import org.pitest.mutationtest.tooling.EntryPoint; import org.pitest.util.Unchecked; +import java.util.HashMap; + /** * Entry point for command line interface */ @@ -35,6 +33,7 @@ public static void main(final String[] args) { final PluginServices plugins = PluginServices.makeForContextLoader(); final OptionsParser parser = new OptionsParser(new PluginFilter(plugins)); final ParseResult pr = parser.parse(args); + final ThresholdValidator thresholdValidator = new ThresholdValidator(); if (!pr.isOk()) { parser.printHelp(); @@ -44,42 +43,16 @@ public static void main(final String[] args) { final CombinedStatistics stats = runReport(data, plugins); - throwErrorIfScoreBelowCoverageThreshold(stats.getCoverageSummary(), + thresholdValidator.throwErrorIfScoreBelowCoverageThreshold(stats.getCoverageSummary(), data.getCoverageThreshold()); - throwErrorIfScoreBelowMutationThreshold(stats.getMutationStatistics(), + thresholdValidator.throwErrorIfScoreBelowMutationThreshold(stats.getMutationStatistics(), data.getMutationThreshold()); - throwErrorIfMoreThanMaxSuvivingMutants(stats.getMutationStatistics(), data.getMaximumAllowedSurvivors()); + thresholdValidator.throwErrorIfMoreThanMaxSurvivingMutants(stats.getMutationStatistics(), + data.getMaximumAllowedSurvivors()); } } - private static void throwErrorIfScoreBelowCoverageThreshold( - CoverageSummary stats, int threshold) { - if ((threshold != 0) && (stats.getCoverage() < threshold)) { - throw new RuntimeException("Line coverage of " + stats.getCoverage() - + " is below threshold of " + threshold); - } - } - - private static void throwErrorIfScoreBelowMutationThreshold( - final MutationStatistics stats, final int threshold) { - if ((threshold != 0) && (stats.getPercentageDetected() < threshold)) { - throw new RuntimeException("Mutation score of " - + stats.getPercentageDetected() + " is below threshold of " - + threshold); - } - } - - private static void throwErrorIfMoreThanMaxSuvivingMutants( - final MutationStatistics stats, final long threshold) { - if ((threshold > 0) - && (stats.getTotalSurvivingMutations() > threshold)) { - throw new RuntimeException("Had " - + stats.getTotalSurvivingMutations() + " surviving mutants, but only " - + threshold + " survivors allowed"); - } - } - private static CombinedStatistics runReport(ReportOptions data, PluginServices plugins) { diff --git a/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/ThresholdValidator.java b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/ThresholdValidator.java new file mode 100644 index 000000000..2670b7c33 --- /dev/null +++ b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/ThresholdValidator.java @@ -0,0 +1,34 @@ +package org.pitest.mutationtest.commandline; + +import org.pitest.coverage.CoverageSummary; +import org.pitest.mutationtest.statistics.MutationStatistics; + +public class ThresholdValidator { + + public void throwErrorIfScoreBelowCoverageThreshold(CoverageSummary stats, int threshold) { + if ((threshold != 0) && (stats.getCoverage() < threshold)) { + throw new RuntimeException("Line coverage of " + stats.getCoverage() + + " is below threshold of " + threshold); + } + } + + public void throwErrorIfScoreBelowMutationThreshold( + final MutationStatistics stats, final int threshold) { + if ((threshold != 0) && (stats.getPercentageDetected() < threshold)) { + throw new RuntimeException("Mutation score of " + + stats.getPercentageDetected() + " is below threshold of " + + threshold); + } + } + + public void throwErrorIfMoreThanMaxSurvivingMutants( + final MutationStatistics stats, final long threshold) { + if ((threshold >= 0) + && (stats.getTotalSurvivingMutations() > threshold)) { + throw new RuntimeException("Had " + + stats.getTotalSurvivingMutations() + " surviving mutants, but only " + + threshold + " survivors allowed"); + } + } + +} diff --git a/pitest-command-line/src/test/java/org/pitest/mutationtest/commandline/ThresholdValidatorTest.java b/pitest-command-line/src/test/java/org/pitest/mutationtest/commandline/ThresholdValidatorTest.java new file mode 100644 index 000000000..65e36aab0 --- /dev/null +++ b/pitest-command-line/src/test/java/org/pitest/mutationtest/commandline/ThresholdValidatorTest.java @@ -0,0 +1,168 @@ +package org.pitest.mutationtest.commandline; + +import org.junit.Test; +import org.pitest.coverage.CoverageSummary; +import org.pitest.mutationtest.statistics.MutationStatistics; +import org.pitest.mutationtest.statistics.Score; + +import java.util.Collections; + +import static org.junit.Assert.fail; + +public class ThresholdValidatorTest { + + private final ThresholdValidator testee = new ThresholdValidator(); + + @Test + public void shouldThrowErrorIfScoreBelowCoverageThreshold() throws Exception { + final CoverageSummary coverageSummary = new CoverageSummary(1, 0); + + try { + this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 1); + fail(); + } catch (RuntimeException e) { + } + } + + @Test + public void shouldNotThrowErrorIfScoreBelowCoverageThresholdZero() throws Exception { + final CoverageSummary coverageSummary = new CoverageSummary(1, 0); + + try { + this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 0); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfScoreAboveCoverage() throws Exception { + final CoverageSummary coverageSummary = new CoverageSummary(2, 1); + + try { + this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 49); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfScoreEqualToCoverage() throws Exception { + final CoverageSummary coverageSummary = new CoverageSummary(2, 1); + + try { + this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 50); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldThrowErrorIfScoreBelowMutationThreshold() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0); + + try { + this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 1); + fail(); + } catch (RuntimeException e) { + } + } + + @Test + public void shouldNotThrowErrorIfScoreBelowMutationThresholdZero() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0); + + try { + this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 0); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfScoreAboveMutationThreshold() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 4, 1, 0); + + try { + this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 24); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfScoreEqualToMutationThreshold() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 4, 1, 0); + + try { + this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 25); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldThrowErrorIfMoreThanMaxSurvivingMutantsThresholdZero() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0); + + try { + this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 0); + fail(); + } catch (RuntimeException e) { + } + } + + @Test + public void shouldNotThrowErrorIfEqualToMaxSurvivingMutantsThresholdZero() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0); + + try { + this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfLessThanMaxSurvivingMutantsThresholdOne() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 1, 0); + + try { + this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldNotThrowErrorIfEqualToMaxSurvivingMutantsThresholdOne() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 2, 1, 0); + + try { + this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1); + } catch (RuntimeException e) { + fail(); + } + } + + @Test + public void shouldThrowErrorIfMoreThanMaxSurvivingMutantsThresholdOne() throws Exception { + final Iterable scores = Collections.emptyList(); + final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0); + + try { + this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1); + } catch (RuntimeException e) { + fail(); + } + } + +}