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
"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());
*/
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;
}
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;
}
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();
}
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();
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
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();