]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8281 coverage_*_data measure should be sorted
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 21 Oct 2016 09:09:42 +0000 (11:09 +0200)
committerJulien HENRY <henryju@yahoo.fr>
Fri, 21 Oct 2016 11:17:30 +0000 (13:17 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java

index 7efd4e8ce5e49b10a0bdf601386d834bb617f0ab..1a14f1882e8d1f595ff5de0b6a38b2512b1c1b3f 100644 (file)
@@ -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<String>()
           .forMetric((Metric<String>) 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<Integer, Integer> mergeCoverageLineMetric(Metric<?> metric, String value1, String value2) {
+  static String mergeCoverageLineMetric(Metric<?> metric, String value1, String value2) {
     Map<Integer, Integer> data1 = KeyValueFormat.parseIntInt(value1);
     Map<Integer, Integer> 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)));
     }
   }
 
index c78affbaa9bc56dafe297d771e8540d9755dc4fe..715c779c7e131a353b26f32c80db5853d56268f4 100644 (file)
@@ -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");
+  }
+
 }