]> source.dussan.org Git - sonarqube.git/commitdiff
API: fix CoverageMeasuresBuilder for Cobertura
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 25 Feb 2011 10:44:52 +0000 (11:44 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 25 Feb 2011 10:44:52 +0000 (11:44 +0100)
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java
sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java

index 8b5c57f7e3f34db25a383374a5deba7a779f3d0b..c14f11e160a1a6b3d2ab161fb2d8e8051f3f667b 100644 (file)
@@ -62,7 +62,6 @@ public class CoberturaSensorTest {
     new CoberturaSensor().parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
-    verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE_HITS_DATA)));
   }
 
   @Test
@@ -201,25 +200,6 @@ public class CoberturaSensorTest {
             "48=117;56=234;66=0;67=0;68=0;84=999;86=999;98=318;111=18;121=0;122=0;125=0;126=0;127=0;128=0;131=0;133=0")));
   }
 
-  @Test
-  public void collectFileBranchHitsData() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
-
-    // no conditions
-    verify(context, never()).saveMeasure(
-        eq(new JavaFile("org.apache.commons.chain.config.ConfigRuleSet")),
-        argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE_HITS_DATA)));
-
-    verify(context).saveMeasure(
-        eq(new JavaFile("org.apache.commons.chain.config.ConfigParser")),
-        argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE_HITS_DATA, "73=50%;76=50%;93=100%")));
-
-    verify(context).saveMeasure(
-        eq(new JavaFile("org.apache.commons.chain.generic.CopyCommand")),
-        argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE_HITS_DATA, "132=0%;136=0%")));
-  }
 
   private File getCoverageReport() throws URISyntaxException {
     return new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/commons-chain-coverage.xml").toURI());
index dfc37496a54946248c78860b784c023aa0a8085f..6d75c729bc1cf3524279a9b30dab7c44ef470e11 100644 (file)
@@ -35,8 +35,8 @@ public final class CoverageMeasuresBuilder {
    */
   public static final List<Metric> METRICS = Arrays.asList(
       CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.COVERAGE_LINE_HITS_DATA,
-      CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS, CoreMetrics.CONDITIONS_BY_LINE_DATA,
-      CoreMetrics.COVERED_CONDITIONS_BY_LINE_DATA, CoreMetrics.BRANCH_COVERAGE_HITS_DATA);
+      CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS, CoreMetrics.CONDITIONS_BY_LINE,
+      CoreMetrics.COVERED_CONDITIONS_BY_LINE);
 
 
   private int totalCoveredLines = 0, totalConditions = 0, totalCoveredConditions = 0;
@@ -59,25 +59,23 @@ public final class CoverageMeasuresBuilder {
   }
 
   public CoverageMeasuresBuilder setHits(int lineId, int hits) {
-    if (hitsByLine.containsKey(lineId)) {
-      throw new IllegalArgumentException("Line " + lineId + " is count twice (hits=" + hits + ")");
-    }
-    hitsByLine.put(lineId, hits);
-    if (hits > 0) {
-      totalCoveredLines += 1;
+    if (!hitsByLine.containsKey(lineId)) {
+      hitsByLine.put(lineId, hits);
+      if (hits > 0) {
+        totalCoveredLines += 1;
+      }
     }
     return this;
   }
 
   public CoverageMeasuresBuilder setConditions(int lineId, int conditions, int coveredConditions) {
-    if (conditionsByLine.containsKey(lineId)) {
-      throw new IllegalArgumentException("Line " + lineId + " is count twice (conditions=" + conditions + ")");
-    }
-    if (conditions > 0) {
-      totalConditions += conditions;
-      totalCoveredConditions += coveredConditions;
-      conditionsByLine.put(lineId, conditions);
-      coveredConditionsByLine.put(lineId, coveredConditions);
+    if (!conditionsByLine.containsKey(lineId)) {
+      if (conditions > 0) {
+        totalConditions += conditions;
+        totalCoveredConditions += coveredConditions;
+        conditionsByLine.put(lineId, conditions);
+        coveredConditionsByLine.put(lineId, coveredConditions);
+      }
     }
     return this;
   }
@@ -120,41 +118,24 @@ public final class CoverageMeasuresBuilder {
     if (getConditions() > 0) {
       measures.add(new Measure(CoreMetrics.CONDITIONS_TO_COVER, (double) getConditions()));
       measures.add(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) (getConditions() - getCoveredConditions())));
-      measures.add(createConditionsByLineData());
-      measures.add(createCoveredConditionsByLineData());
-      measures.add(createBranchCoverageByLine());
+      measures.add(createConditionsByLine());
+      measures.add(createCoveredConditionsByLine());
     }
     return measures;
   }
 
-  private Measure createCoveredConditionsByLineData() {
-    return new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE_DATA)
+  private Measure createCoveredConditionsByLine() {
+    return new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)
         .setData(KeyValueFormat.format(coveredConditionsByLine))
         .setPersistenceMode(PersistenceMode.DATABASE);
   }
 
-  private Measure createConditionsByLineData() {
-    return new Measure(CoreMetrics.CONDITIONS_BY_LINE_DATA)
+  private Measure createConditionsByLine() {
+    return new Measure(CoreMetrics.CONDITIONS_BY_LINE)
         .setData(KeyValueFormat.format(conditionsByLine))
         .setPersistenceMode(PersistenceMode.DATABASE);
   }
 
-  private Measure createBranchCoverageByLine() {
-    PropertiesBuilder<Integer, String> builder = new PropertiesBuilder<Integer, String>(CoreMetrics.BRANCH_COVERAGE_HITS_DATA);
-    for (Map.Entry<Integer, Integer> entry : conditionsByLine.entrySet()) {
-      Integer lineId = entry.getKey();
-      int conditions = entry.getValue();
-      int coveredConditions = coveredConditionsByLine.get(lineId);
-      builder.add(lineId, formatBranchCoverage(conditions, coveredConditions));
-    }
-    return builder.build().setPersistenceMode(PersistenceMode.DATABASE);
-  }
-
-  static String formatBranchCoverage(int conditions, int coveredConditions) {
-    long branchCoverage = Math.round(100.0 * coveredConditions / conditions);
-    return branchCoverage + "%";
-  }
-
   public static CoverageMeasuresBuilder create() {
     return new CoverageMeasuresBuilder();
   }
index 12c43709c37c43c7308784da2db53f6b3463ead8..1d72a1b194f4eda4c68648e04aa844d2284cef45 100644 (file)
@@ -43,13 +43,6 @@ public class CoverageMeasuresBuilderTest {
     assertThat(find(builder.createMeasures(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY).getData(), is("1=0;2=3;4=2"));
   }
 
-  @Test(expected = IllegalArgumentException.class)
-  public void shouldFailIfDuplicatedLineHits() {
-    CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
-    builder.setHits(1, 0);
-    builder.setHits(1, 3);
-  }
-
   @Test
   public void shouldCreateUncoveredLines() {
     CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
@@ -64,9 +57,8 @@ public class CoverageMeasuresBuilderTest {
     CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
     builder.setConditions(1, 2, 2);
     builder.setConditions(2, 1, 0);
-    assertThat(find(builder.createMeasures(), CoreMetrics.CONDITIONS_BY_LINE_DATA_KEY).getData(), is("1=2;2=1"));
-    assertThat(find(builder.createMeasures(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_DATA_KEY).getData(), is("1=2;2=0"));
-    assertThat(find(builder.createMeasures(), CoreMetrics.BRANCH_COVERAGE_HITS_DATA_KEY).getData(), is("1=100%;2=0%"));
+    assertThat(find(builder.createMeasures(), CoreMetrics.CONDITIONS_BY_LINE_KEY).getData(), is("1=2;2=1"));
+    assertThat(find(builder.createMeasures(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY).getData(), is("1=2;2=0"));
   }
 
   @Test
@@ -91,18 +83,32 @@ public class CoverageMeasuresBuilderTest {
     CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
     builder.setConditions(1, 0, 0);
     builder.setConditions(2, 1, 0);
-    assertThat(find(builder.createMeasures(), CoreMetrics.CONDITIONS_BY_LINE_DATA_KEY).getData(), is("2=1"));
-    assertThat(find(builder.createMeasures(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_DATA_KEY).getData(), is("2=0"));
-    assertThat(find(builder.createMeasures(), CoreMetrics.BRANCH_COVERAGE_HITS_DATA_KEY).getData(), is("2=0%"));
+    assertThat(find(builder.createMeasures(), CoreMetrics.CONDITIONS_BY_LINE_KEY).getData(), is("2=1"));
+    assertThat(find(builder.createMeasures(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY).getData(), is("2=0"));
   }
 
-  @Test(expected = IllegalArgumentException.class)
-  public void shouldFailIfDuplicatedLineConditions() {
+  @Test
+  public void shouldIgnoreDuplicatedSetHits() {
     CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
-    builder.setConditions(1, 3, 0);
-    builder.setConditions(1, 3, 1);
+    builder.setHits(2, 3);
+    builder.setHits(2, 5);// to ignore
+    assertThat(builder.getLinesToCover(), is(1));
+    assertThat(builder.getCoveredLines(), is(1));
+    assertThat(builder.getHitsByLine().get(2), is(3));
+  }
+
+  @Test
+  public void shouldIgnoreDuplicatedSetConditions() {
+    CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
+    builder.setConditions(1, 3, 2);
+    builder.setConditions(1, 1, 0);// to ignore
+    assertThat(builder.getConditions(), is(3));
+    assertThat(builder.getCoveredConditions(), is(2));
+    assertThat(builder.getConditionsByLine().get(1), is(3));
+    assertThat(builder.getCoveredConditionsByLine().get(1), is(2));
   }
 
+
   @Test
   public void shouldResetFields() {
     CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();