aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-04-02 15:41:12 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-04-07 17:20:46 +0200
commita9de737d0e2b6342c55a946665af99a35a7f2cb8 (patch)
treebded717c8a4cf485f80c65ab20f1429d27d6d2ed /sonar-batch
parentca8ad6b8b61305bd336325d05789da57b76efd7c (diff)
downloadsonarqube-a9de737d0e2b6342c55a946665af99a35a7f2cb8.tar.gz
sonarqube-a9de737d0e2b6342c55a946665af99a35a7f2cb8.zip
SONAR-6339 Fix coverage in report
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java121
2 files changed, 122 insertions, 2 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java
index b2b4eaf5442..6bc33a24c6e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java
@@ -58,7 +58,6 @@ public class CoveragePublisher implements ReportPublisherStep {
@Override
public void apply(String value, Coverage.Builder builder) {
builder.setUtHits(Integer.parseInt(value) > 0);
- builder.setConditions(1);
}
});
applyLineMeasure(resource.key(), ((InputFile) resource.inputPath()).lines(), CoreMetrics.CONDITIONS_BY_LINE_KEY, coveragePerLine, new MeasureOperation() {
@@ -88,7 +87,7 @@ public class CoveragePublisher implements ReportPublisherStep {
applyLineMeasure(resource.key(), ((InputFile) resource.inputPath()).lines(), CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, new MeasureOperation() {
@Override
public void apply(String value, Coverage.Builder builder) {
- builder.setItCoveredConditions(Integer.parseInt(value));
+ builder.setOverallCoveredConditions(Integer.parseInt(value));
}
});
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java
new file mode 100644
index 00000000000..2e1614bbdf1
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java
@@ -0,0 +1,121 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.report;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.protocol.output.BatchReport.Coverage;
+import org.sonar.batch.protocol.output.BatchReportReader;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+import org.sonar.batch.scan.measure.MeasureCache;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class CoveragePublisherTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private MeasureCache measureCache;
+ private CoveragePublisher publisher;
+ private org.sonar.api.resources.File aFile = org.sonar.api.resources.File.create("org/foo/Bar.java");
+
+ private org.sonar.api.resources.Resource sampleFile;
+
+ @Before
+ public void prepare() {
+ Project p = new Project("foo").setAnalysisDate(new Date(1234567L));
+ ResourceCache resourceCache = new ResourceCache();
+ sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php");
+ resourceCache.add(p, null).setSnapshot(new Snapshot().setId(2));
+ resourceCache.add(sampleFile, null).setInputPath(new DefaultInputFile("foo", "src/Foo.php").setLines(5));
+ measureCache = mock(MeasureCache.class);
+ when(measureCache.byMetric(anyString(), anyString())).thenReturn(Collections.<Measure>emptyList());
+ publisher = new CoveragePublisher(resourceCache, measureCache);
+ }
+
+ @Test
+ public void publishCoverage() 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(Arrays.asList(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(Arrays.asList(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(Arrays.asList(coveredConditionsByUts));
+
+ 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(Arrays.asList(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(Arrays.asList(coveredConditionsByIts));
+
+ Measure overallCoveredConditions = new Measure<>(CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE).setData("3=2");
+ when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(Arrays.asList(overallCoveredConditions));
+
+ File outputDir = temp.newFolder();
+ BatchReportWriter writer = new BatchReportWriter(outputDir);
+
+ publisher.publish(writer);
+
+ BatchReportReader reader = new BatchReportReader(outputDir);
+
+ assertThat(reader.readFileCoverage(2).iterator()).containsOnly(
+ Coverage.newBuilder()
+ .setLine(2)
+ .setUtHits(true)
+ .setItHits(false)
+ .build(),
+ Coverage.newBuilder()
+ .setLine(3)
+ .setUtHits(true)
+ .setItHits(false)
+ .setConditions(4)
+ .setUtCoveredConditions(2)
+ .setItCoveredConditions(1)
+ .setOverallCoveredConditions(2)
+ .build(),
+ Coverage.newBuilder()
+ .setLine(5)
+ .setUtHits(false)
+ .setItHits(true)
+ .build());
+
+ }
+
+}