-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SizeMeasuresStepTest.java
222 lines (200 loc) · 10.1 KB
/
SizeMeasuresStepTest.java
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.step;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.FileAttributes;
import org.sonar.server.computation.measure.MeasureRepoEntry;
import org.sonar.server.computation.measure.MeasureRepositoryRule;
import org.sonar.server.computation.metric.MetricRepositoryRule;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
import static org.sonar.api.measures.CoreMetrics.DIRECTORIES_KEY;
import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
import static org.sonar.server.computation.component.Component.Type.DIRECTORY;
import static org.sonar.server.computation.component.Component.Type.FILE;
import static org.sonar.server.computation.component.Component.Type.MODULE;
import static org.sonar.server.computation.component.Component.Type.PROJECT;
import static org.sonar.server.computation.component.DumbComponent.builder;
import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf;
import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries;
public class SizeMeasuresStepTest {
private static final String LANGUAGE_DOES_NOT_MATTER_HERE = null;
private static final int ROOT_REF = 1;
private static final int MODULE_REF = 12;
private static final int SUB_MODULE_REF = 123;
private static final int DIRECTORY_1_REF = 1234;
private static final int DIRECTORY_2_REF = 1235;
private static final int DIRECTORY_3_REF = 1236;
private static final int FILE_1_REF = 12341;
private static final int FILE_2_REF = 12343;
private static final int FILE_3_REF = 12351;
private static final int UNIT_TEST_1_REF = 12352;
private static final int UNIT_TEST_2_REF = 12361;
private static final Integer NO_FILE_METRIC = null;
@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
builder(PROJECT, ROOT_REF)
.addChildren(
builder(MODULE, MODULE_REF)
.addChildren(
builder(MODULE, SUB_MODULE_REF)
.addChildren(
builder(DIRECTORY, DIRECTORY_1_REF)
.addChildren(
builder(FILE, FILE_1_REF).build(),
builder(FILE, FILE_2_REF).build())
.build(),
builder(DIRECTORY, DIRECTORY_2_REF)
.addChildren(
builder(FILE, FILE_3_REF).build(),
builder(FILE, UNIT_TEST_1_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE)).build())
.build(),
builder(DIRECTORY, DIRECTORY_3_REF)
.addChildren(
builder(FILE, UNIT_TEST_2_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE)).build())
.build())
.build())
.build())
.build());
@Rule
public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
.add(CoreMetrics.FILES)
.add(CoreMetrics.DIRECTORIES)
.add(CoreMetrics.LINES)
.add(CoreMetrics.GENERATED_LINES)
.add(CoreMetrics.NCLOC)
.add(CoreMetrics.GENERATED_NCLOC)
.add(CoreMetrics.FUNCTIONS)
.add(CoreMetrics.STATEMENTS)
.add(CoreMetrics.CLASSES)
.add(CoreMetrics.ACCESSORS);
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
private SizeMeasuresStep underTest = new SizeMeasuresStep(treeRootHolder, metricRepository, measureRepository);
@Test
public void verify_FILE_and_DIRECTORY_computation_and_aggregation() {
underTest.execute();
verifyMeasuresOnFile(FILE_1_REF, 1);
verifyMeasuresOnFile(FILE_2_REF, 1);
verifyMeasuresOnFile(FILE_3_REF, 1);
verifyNoMeasure(UNIT_TEST_1_REF);
verifyNoMeasure(UNIT_TEST_2_REF);
verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1);
verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1);
verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_FILE_METRIC, 1);
verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 3);
verifyMeasuresOnOtherComponent(MODULE_REF, 3, 3);
verifyMeasuresOnOtherComponent(ROOT_REF, 3, 3);
}
@Test
public void verify_LINE_related_measures_aggregation() {
verifyTwoMeasureAggregation(LINES_KEY, GENERATED_LINES_KEY);
}
private void verifyTwoMeasureAggregation(String metric1Key, String metric2Key) {
measureRepository.addRawMeasure(FILE_1_REF, metric1Key, newMeasureBuilder().create(1));
measureRepository.addRawMeasure(FILE_1_REF, metric2Key, newMeasureBuilder().create(10));
// FILE_2_REF has no metric2 measure
measureRepository.addRawMeasure(FILE_2_REF, metric1Key, newMeasureBuilder().create(6));
// FILE_3_REF has no measure at all
// UNIT_TEST_1_REF has no metric1
measureRepository.addRawMeasure(UNIT_TEST_1_REF, metric2Key, newMeasureBuilder().create(90));
underTest.execute();
verifyMeasuresOnFile(FILE_1_REF, 1);
verifyMeasuresOnFile(FILE_2_REF, 1);
verifyMeasuresOnFile(FILE_3_REF, 1);
verifyNoMeasure(UNIT_TEST_1_REF);
verifyNoMeasure(UNIT_TEST_2_REF);
verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1,
entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(10)));
verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1,
entryOf(metric2Key, newMeasureBuilder().create(90)));
MeasureRepoEntry[] subModuleAndAboveEntries = {
entryOf(metric1Key, newMeasureBuilder().create(7)),
entryOf(metric2Key, newMeasureBuilder().create(100))
};
verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_FILE_METRIC, 1);
verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 3, subModuleAndAboveEntries);
verifyMeasuresOnOtherComponent(MODULE_REF, 3, 3, subModuleAndAboveEntries);
verifyMeasuresOnOtherComponent(ROOT_REF, 3, 3, subModuleAndAboveEntries);
}
@Test
public void verify_NCLOC_measure_aggregation() {
verifyMetricAggregation(NCLOC_KEY);
}
private void verifyMetricAggregation(String metricKey) {
measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create(10));
measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create(6));
measureRepository.addRawMeasure(UNIT_TEST_1_REF, metricKey, newMeasureBuilder().create(3));
underTest.execute();
verifyMeasuresOnFile(FILE_1_REF, 1);
verifyMeasuresOnFile(FILE_2_REF, 1);
verifyMeasuresOnFile(FILE_3_REF, 1);
verifyNoMeasure(UNIT_TEST_1_REF);
verifyNoMeasure(UNIT_TEST_2_REF);
verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1, entryOf(metricKey, newMeasureBuilder().create(16)));
verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1, entryOf(metricKey, newMeasureBuilder().create(3)));
verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_FILE_METRIC, 1);
verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 3, entryOf(metricKey, newMeasureBuilder().create(19)));
verifyMeasuresOnOtherComponent(MODULE_REF, 3, 3, entryOf(metricKey, newMeasureBuilder().create(19)));
verifyMeasuresOnOtherComponent(ROOT_REF, 3, 3, entryOf(metricKey, newMeasureBuilder().create(19)));
}
@Test
public void verify_FUNCTIONS_and_STATEMENT_measure_aggregation() {
verifyTwoMeasureAggregation(FUNCTIONS_KEY, STATEMENTS_KEY);
}
@Test
public void verify_CLASSES_measure_aggregation() {
verifyMetricAggregation(CLASSES_KEY);
}
private void verifyMeasuresOnFile(int componentRef, int fileCount) {
assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
.containsOnly(entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)));
}
private void verifyMeasuresOnOtherComponent(int componentRef, @Nullable Integer fileCount, int directoryCount, MeasureRepoEntry... otherMeasures) {
MeasureRepoEntry[] measureRepoEntries = concatIntoArray(
otherMeasures,
fileCount == null ? null : entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)), entryOf(DIRECTORIES_KEY, newMeasureBuilder().create(directoryCount)));
assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
.containsOnly(measureRepoEntries);
}
private static MeasureRepoEntry[] concatIntoArray(MeasureRepoEntry[] otherMeasures, MeasureRepoEntry... measureRepoEntries) {
return from(concat(
asList(otherMeasures),
from(asList(measureRepoEntries)).filter(notNull())))
.toArray(MeasureRepoEntry.class);
}
private void verifyNoMeasure(int componentRef) {
assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty();
}
}