diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-25 11:44:52 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-25 11:44:52 +0100 |
commit | 70961f0058aaf7eddf739cba89d19f0b86ad5c85 (patch) | |
tree | 81f80d26ecd778d61f72306243170e14b168bfe1 /sonar-plugin-api/src | |
parent | 5633520d8220bd9635fc8c0ef5939170442d7db4 (diff) | |
download | sonarqube-70961f0058aaf7eddf739cba89d19f0b86ad5c85.tar.gz sonarqube-70961f0058aaf7eddf739cba89d19f0b86ad5c85.zip |
API: fix CoverageMeasuresBuilder for Cobertura
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java | 59 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/measures/CoverageMeasuresBuilderTest.java | 40 |
2 files changed, 43 insertions, 56 deletions
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<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(); } 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(); |