From: Julien HENRY Date: Fri, 21 Oct 2016 09:09:42 +0000 (+0200) Subject: SONAR-8281 coverage_*_data measure should be sorted X-Git-Tag: 6.2-RC1~306 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8ed37b59b5b1e954811ef3ca8610cab7eba8b744;p=sonarqube.git SONAR-8281 coverage_*_data measure should be sorted --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index 7efd4e8ce5e..1a14f1882e8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -29,6 +29,7 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -264,7 +265,7 @@ public class DefaultSensorStorage implements SensorStorage { if (previousMeasure != null) { measureCache.put(file.key(), metric.key(), new DefaultMeasure() .forMetric((Metric) metric) - .withValue(KeyValueFormat.format(mergeCoverageLineMetric(metric, (String) previousMeasure.value(), (String) measure.value())))); + .withValue(mergeCoverageLineMetric(metric, (String) previousMeasure.value(), (String) measure.value()))); } else { measureCache.put(file.key(), metric.key(), measure); } @@ -278,27 +279,29 @@ public class DefaultSensorStorage implements SensorStorage { * Merge the two line coverage data measures. For lines hits use the sum, and for conditions * keep max value in case they both contains a value for the same line. */ - private static Map mergeCoverageLineMetric(Metric metric, String value1, String value2) { + static String mergeCoverageLineMetric(Metric metric, String value1, String value2) { Map data1 = KeyValueFormat.parseIntInt(value1); Map data2 = KeyValueFormat.parseIntInt(value2); if (metric.key().equals(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)) { - return Stream.of(data1, data2) + return KeyValueFormat.format(Stream.of(data1, data2) .map(Map::entrySet) .flatMap(Collection::stream) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, - Integer::sum)); + Integer::sum, + TreeMap::new))); } else { - return Stream.of(data1, data2) + return KeyValueFormat.format(Stream.of(data1, data2) .map(Map::entrySet) .flatMap(Collection::stream) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, - Integer::max)); + Integer::max, + TreeMap::new))); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index c78affbaa9b..715c779c7e1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java @@ -192,4 +192,15 @@ public class DefaultSensorStorageTest { underTest.validateCoverageMeasure(data, file); } + @Test + public void mergeCoverageLineMetrics_should_be_sorted() { + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=1", "1=1")).isEqualTo("1=2"); + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=1", "2=1")).isEqualTo("1=1;2=1"); + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "2=1", "1=1")).isEqualTo("1=1;2=1"); + + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "1=1", "1=1")).isEqualTo("1=1"); + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "1=1", "2=1")).isEqualTo("1=1;2=1"); + assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "2=1", "1=1")).isEqualTo("1=1;2=1"); + } + }