diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-29 18:13:12 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-30 11:32:39 +0200 |
commit | 946a448c08ccc10ad1c0af778ae5d9f179655ef8 (patch) | |
tree | 14acab59210015b9a2e3aeba04a7b54bcfa5fc19 /sonar-scanner-engine | |
parent | 2cf5d0f82c96846af16007233b674e0e5c638eea (diff) | |
download | sonarqube-946a448c08ccc10ad1c0af778ae5d9f179655ef8.tar.gz sonarqube-946a448c08ccc10ad1c0af778ae5d9f179655ef8.zip |
SONAR-7497 Restore behavior for line coverage after move to proto3
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/batch/report/CoveragePublisher.java | 20 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java | 75 |
2 files changed, 90 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/batch/report/CoveragePublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/batch/report/CoveragePublisher.java index 1d8d5fee448..1ed96d1ceb7 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/batch/report/CoveragePublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/batch/report/CoveragePublisher.java @@ -62,25 +62,35 @@ public class CoveragePublisher implements ReportPublisherStep { builder.setUtHits(Integer.parseInt(value) > 0); } }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.CONDITIONS_BY_LINE_KEY, coveragePerLine, + applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY, coveragePerLine, + new MeasureOperation() { + @Override + public void apply(String value, LineCoverage.Builder builder) { + builder.setItHits(Integer.parseInt(value) > 0); + } + }); + // Just in case we have only IT and no UT, we first take conditions from ITs, then from UTs (UTs will override ITs). + // Note that it would be very strange (and unsupported in scanner report) to have ITs and UTs don't report the same number of + // conditions. Not even talking about overall conditions... + applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_CONDITIONS_BY_LINE_KEY, coveragePerLine, new MeasureOperation() { @Override public void apply(String value, LineCoverage.Builder builder) { builder.setConditions(Integer.parseInt(value)); } }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, + applyLineMeasure(resource.key(), lineCount, CoreMetrics.CONDITIONS_BY_LINE_KEY, coveragePerLine, new MeasureOperation() { @Override public void apply(String value, LineCoverage.Builder builder) { - builder.setUtCoveredConditions(Integer.parseInt(value)); + builder.setConditions(Integer.parseInt(value)); } }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY, coveragePerLine, + applyLineMeasure(resource.key(), lineCount, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, new MeasureOperation() { @Override public void apply(String value, LineCoverage.Builder builder) { - builder.setItHits(Integer.parseInt(value) > 0); + builder.setUtCoveredConditions(Integer.parseInt(value)); } }); applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, diff --git a/sonar-scanner-engine/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java index 1175661f8b9..b040483a57b 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java @@ -70,6 +70,9 @@ public class CoveragePublisherTest { Measure utLineHits = new Measure<>(CoreMetrics.COVERAGE_LINE_HITS_DATA).setData("2=1;3=1;5=0;6=3"); when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(utLineHits); + Measure itsConditionsByLine = new Measure<>(CoreMetrics.IT_CONDITIONS_BY_LINE).setData("3=4"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_CONDITIONS_BY_LINE_KEY)).thenReturn(itsConditionsByLine); + Measure conditionsByLine = new Measure<>(CoreMetrics.CONDITIONS_BY_LINE).setData("3=4"); when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn(conditionsByLine); @@ -113,4 +116,76 @@ public class CoveragePublisherTest { } } + + @Test + public void publishCoverageOnlyUts() throws Exception { + + Measure utLineHits = new Measure<>(CoreMetrics.COVERAGE_LINE_HITS_DATA).setData("2=1;3=1;5=0;6=3"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(utLineHits); + + Measure conditionsByLine = new Measure<>(CoreMetrics.CONDITIONS_BY_LINE).setData("3=4"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn(conditionsByLine); + + Measure coveredConditionsByUts = new Measure<>(CoreMetrics.COVERED_CONDITIONS_BY_LINE).setData("3=2"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(coveredConditionsByUts); + + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + + publisher.publish(writer); + + try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(2)) { + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(2) + .setUtHits(true) + .build()); + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(3) + .setUtHits(true) + .setConditions(4) + .setUtCoveredConditions(2) + .build()); + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(5) + .setUtHits(false) + .build()); + } + + } + + @Test + public void publishCoverageOnlyIts() throws Exception { + + Measure itsConditionsByLine = new Measure<>(CoreMetrics.IT_CONDITIONS_BY_LINE).setData("3=4"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_CONDITIONS_BY_LINE_KEY)).thenReturn(itsConditionsByLine); + + Measure itLineHits = new Measure<>(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA).setData("2=0;3=0;5=1"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(itLineHits); + + Measure coveredConditionsByIts = new Measure<>(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE).setData("3=1"); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(coveredConditionsByIts); + + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + + publisher.publish(writer); + + try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(2)) { + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(2) + .setItHits(false) + .build()); + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(3) + .setItHits(false) + .setConditions(4) + .setItCoveredConditions(1) + .build()); + assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() + .setLine(5) + .setItHits(true) + .build()); + } + + } } |