1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2011 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.measures;
import org.junit.Test;
import java.util.Collection;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class CoverageMeasuresBuilderTest {
@Test
public void shouldNotCreateIfNoValues() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
assertThat(builder.createMeasures().size(), is(0));
}
@Test
public void shouldCreateHitsByLineData() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setHits(1, 0);
builder.setHits(2, 3);
builder.setHits(4, 2);
assertThat(find(builder.createMeasures(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY).getData(), is("1=0;2=3;4=2"));
}
@Test
public void shouldCreateUncoveredLines() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setHits(1, 0);
builder.setHits(2, 3);
builder.setHits(3, 0);
assertThat(find(builder.createMeasures(), CoreMetrics.UNCOVERED_LINES_KEY).getIntValue(), is(2));
}
@Test
public void shouldCreateConditionsByLineData() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setConditions(1, 2, 2);
builder.setConditions(2, 1, 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
public void shouldCreateNumberOfConditionsToCover() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setConditions(1, 2, 2);
builder.setConditions(2, 1, 0);
assertThat(find(builder.createMeasures(), CoreMetrics.CONDITIONS_TO_COVER_KEY).getIntValue(), is(3));
}
@Test
public void shouldCreateNumberOfUncoveredConditions() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setConditions(1, 2, 2);
builder.setConditions(2, 1, 0);
builder.setConditions(3, 3, 1);
assertThat(find(builder.createMeasures(), CoreMetrics.UNCOVERED_CONDITIONS_KEY).getIntValue(), is(3));
}
@Test
public void shouldSetOnlyPositiveConditions() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
builder.setConditions(1, 0, 0);
builder.setConditions(2, 1, 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
public void shouldIgnoreDuplicatedSetHits() {
CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
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();
builder.setHits(1, 4);
builder.setConditions(1, 3, 1);
builder.reset();
assertThat(builder.getConditions(), is(0));
assertThat(builder.getCoveredConditions(), is(0));
assertThat(builder.getCoveredLines(), is(0));
assertThat(builder.getHitsByLine().size(), is(0));
assertThat(builder.getConditionsByLine().size(), is(0));
assertThat(builder.getCoveredConditionsByLine().size(), is(0));
}
private Measure find(Collection<Measure> measures, String metricKey) {
for (Measure measure : measures) {
if (metricKey.equals(measure.getMetricKey())) {
return measure;
}
}
return null;
}
}
|