From 70961f0058aaf7eddf739cba89d19f0b86ad5c85 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 25 Feb 2011 11:44:52 +0100 Subject: [PATCH] API: fix CoverageMeasuresBuilder for Cobertura --- .../cobertura/CoberturaSensorTest.java | 20 ------- .../api/measures/CoverageMeasuresBuilder.java | 59 +++++++------------ .../measures/CoverageMeasuresBuilderTest.java | 40 +++++++------ 3 files changed, 43 insertions(+), 76 deletions(-) diff --git a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java index 8b5c57f7e3f..c14f11e160a 100644 --- a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java +++ b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java @@ -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()); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java index dfc37496a54..6d75c729bc1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java @@ -35,8 +35,8 @@ public final class CoverageMeasuresBuilder { */ public static final List 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 builder = new PropertiesBuilder(CoreMetrics.BRANCH_COVERAGE_HITS_DATA); - for (Map.Entry 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(); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java index 12c43709c37..1d72a1b194f 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java @@ -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(); -- 2.39.5