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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 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.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
import static org.fest.assertions.Assertions.assertThat;
public class MeasureTest {
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void valueCanBeNull() {
Measure measure = new Measure("metric_key").setValue(null);
assertThat(measure.getValue()).isNull();
}
@Test
public void valueShouldNotBeNaN() {
thrown.expect(IllegalArgumentException.class);
new Measure("metric_key").setValue(Double.NaN);
}
@Test
public void scaleValue() {
assertThat(new Measure(CoreMetrics.COVERAGE, 80.666666).getValue()).isEqualTo(80.7);
assertThat(new Measure(CoreMetrics.COVERAGE, 80.666666, 2).getValue()).isEqualTo(80.67);
}
@Test
public void defaultPersistenceModeIsFull() {
assertThat(new Measure(CoreMetrics.LINES, 32.0).getPersistenceMode()).isEqualTo(PersistenceMode.FULL);
}
@Test
public void persistenceModeIsDatabaseForBigDataMeasures() {
Measure bigDataMeasure = new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "long data")
.setPersistenceMode(PersistenceMode.DATABASE);
assertThat(bigDataMeasure.getPersistenceMode()).isEqualTo(PersistenceMode.DATABASE);
}
@Test
public void measureWithLevelValue() {
assertThat(new Measure(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR).getData()).isEqualTo("ERROR");
assertThat(new Measure(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR).getDataAsLevel()).isEqualTo(Metric.Level.ERROR);
assertThat(new Measure(CoreMetrics.ALERT_STATUS).setData(Metric.Level.ERROR).getDataAsLevel()).isEqualTo(Metric.Level.ERROR);
}
@Test
public void measureWithIntegerValue() {
assertThat(new Measure(CoreMetrics.LINES).setIntValue(3).getValue()).isEqualTo(3.0);
assertThat(new Measure(CoreMetrics.LINES).setIntValue(null).getValue()).isNull();
assertThat(new Measure(CoreMetrics.LINES).setIntValue(3).getIntValue()).isEqualTo(3);
assertThat(new Measure(CoreMetrics.LINES).setIntValue(null).getIntValue()).isNull();
assertThat(new Measure(CoreMetrics.LINES).setValue(3.6).getIntValue()).isEqualTo(3);
}
@Test
public void valuesAreRoundUp() {
assertThat(new Measure(CoreMetrics.COVERAGE, 5.22222222).getValue()).isEqualTo(5.2);
assertThat(new Measure(CoreMetrics.COVERAGE, 5.7777777).getValue()).isEqualTo(5.8);
assertThat(new Measure(CoreMetrics.COVERAGE, 5.22222222, 3).getValue()).isEqualTo(5.222);
assertThat(new Measure(CoreMetrics.COVERAGE, 5.7777777, 3).getValue()).isEqualTo(5.778);
}
/**
* Proper definition of equality for measures is important, because used to store them.
*/
@Test
public void equalsAndHashCode() {
Measure measure1 = new Measure();
Measure measure2 = new Measure();
assertThat(measure1.equals(null)).isFalse();
// another class
assertThat(measure1.equals("")).isFalse();
// same instance
assertThat(measure1.equals(measure1)).isTrue();
assertThat(measure1.hashCode()).isEqualTo(measure2.hashCode());
// same key - null
assertThat(measure1.equals(measure2)).isTrue();
assertThat(measure2.equals(measure1)).isTrue();
assertThat(measure1.hashCode()).isEqualTo(measure2.hashCode());
// different keys
measure1.setMetric(CoreMetrics.COVERAGE);
assertThat(measure1.equals(measure2)).isFalse();
assertThat(measure2.equals(measure1)).isFalse();
assertThat(measure1.hashCode()).isNotEqualTo(measure2.hashCode());
measure2.setMetric(CoreMetrics.LINES);
assertThat(measure1.equals(measure2)).isFalse();
assertThat(measure2.equals(measure1)).isFalse();
assertThat(measure1.hashCode()).isNotEqualTo(measure2.hashCode());
// same key
measure2.setMetric(CoreMetrics.COVERAGE);
assertThat(measure1.equals(measure2)).isTrue();
assertThat(measure2.equals(measure1)).isTrue();
assertThat(measure1.hashCode()).isEqualTo(measure2.hashCode());
// different committer
measure1.setPersonId(1);
assertThat(measure1.equals(measure2)).isFalse();
assertThat(measure2.equals(measure1)).isFalse();
assertThat(measure1.hashCode()).isNotEqualTo(measure2.hashCode());
measure2.setPersonId(2);
assertThat(measure1.equals(measure2)).isFalse();
assertThat(measure2.equals(measure1)).isFalse();
assertThat(measure1.hashCode()).isNotEqualTo(measure2.hashCode());
// same committer
measure2.setPersonId(1);
assertThat(measure1.equals(measure2)).isTrue();
assertThat(measure2.equals(measure1)).isTrue();
assertThat(measure1.hashCode()).isEqualTo(measure2.hashCode());
// value doesn't matter
measure1.setValue(1.0);
measure2.setValue(2.0);
assertThat(measure1.equals(measure2)).isTrue();
assertThat(measure2.equals(measure1)).isTrue();
assertThat(measure1.hashCode()).isEqualTo(measure2.hashCode());
}
@Test
public void longDataForDataMetric() {
new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, StringUtils.repeat("x", Measure.MAX_TEXT_SIZE + 1));
}
@Test
public void shouldGetAndSetVariations() {
Measure measure = new Measure(CoreMetrics.LINES).setVariation1(1d).setVariation2(2d).setVariation3(3d);
assertThat(measure.getVariation1()).isEqualTo(1d);
assertThat(measure.getVariation2()).isEqualTo(2d);
assertThat(measure.getVariation3()).isEqualTo(3d);
}
@Test
public void shouldSetVariationsWithIndex() {
Measure measure = new Measure(CoreMetrics.LINES).setVariation(2, 3.3);
assertThat(measure.getVariation1()).isNull();
assertThat(measure.getVariation2()).isEqualTo(3.3);
assertThat(measure.getVariation3()).isNull();
}
@Test
public void notEqualRuleMeasures() {
Measure measure = new Measure(CoreMetrics.VIOLATIONS, 30.0);
RuleMeasure ruleMeasure = new RuleMeasure(CoreMetrics.VIOLATIONS, new Rule("foo", "bar"), RulePriority.CRITICAL, 3);
assertThat(measure.equals(ruleMeasure)).isFalse();
assertThat(ruleMeasure.equals(measure)).isFalse();
}
@Test
public void shouldUnsetData() {
String data = "1=10;21=456";
Measure measure = new Measure(CoreMetrics.CONDITIONS_BY_LINE).setData(data);
assertThat(measure.hasData()).isTrue();
assertThat(measure.getData()).isEqualTo(data);
measure.unsetData();
assertThat(measure.hasData()).isFalse();
assertThat(measure.getData()).isNull();
}
}
|