3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package org.sonar.server.computation.task.projectanalysis.step;
23 import java.util.Arrays;
24 import javax.annotation.Nullable;
25 import org.assertj.core.data.Offset;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
29 import org.sonar.server.computation.task.projectanalysis.duplication.DuplicationRepositoryRule;
30 import org.sonar.server.computation.task.projectanalysis.duplication.TextBlock;
31 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
32 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
33 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
34 import org.sonar.server.computation.task.projectanalysis.period.Period;
35 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.scm.Changeset;
37 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepositoryRule;
39 import static com.google.common.base.Preconditions.checkArgument;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.guava.api.Assertions.assertThat;
42 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKS_DUPLICATED;
43 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKS_DUPLICATED_KEY;
44 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES;
45 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
46 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
47 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.NEW_LINES;
49 import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY;
50 import static org.sonar.api.utils.DateUtils.parseDate;
51 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
52 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
53 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
54 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
55 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
57 public class NewSizeMeasuresStepTest {
59 private static final Offset<Double> DEFAULT_OFFSET = Offset.offset(0.1d);
61 private static final int ROOT_REF = 1;
62 private static final int MODULE_REF = 12;
63 private static final int SUB_MODULE_1_REF = 123;
64 private static final int SUB_MODULE_2_REF = 126;
65 private static final int DIRECTORY_REF = 1234;
66 private static final int DIRECTORY_2_REF = 1235;
67 private static final int FILE_1_REF = 12341;
68 private static final int FILE_2_REF = 12342;
69 private static final int FILE_3_REF = 1261;
70 private static final int FILE_4_REF = 1262;
71 private static final String SOME_FILE_KEY = "some file key";
74 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
76 builder(PROJECT, ROOT_REF)
78 builder(MODULE, MODULE_REF)
80 builder(MODULE, SUB_MODULE_1_REF)
82 builder(DIRECTORY, DIRECTORY_REF)
84 builder(FILE, FILE_1_REF).build(),
85 builder(FILE, FILE_2_REF).build())
87 builder(DIRECTORY, DIRECTORY_2_REF).build())
89 builder(MODULE, SUB_MODULE_2_REF)
91 builder(FILE, FILE_3_REF).build(),
92 builder(FILE, FILE_4_REF).build())
98 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule().setPeriods(
99 new Period(2, "mode_p_1", null, parseDate("2009-12-25").getTime(), "u1"),
100 new Period(5, "mode_p_5", null, parseDate("2011-02-18").getTime(), "u2"));
103 public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
106 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
108 .add(NEW_DUPLICATED_LINES)
109 .add(NEW_DUPLICATED_LINES_DENSITY)
110 .add(NEW_BLOCKS_DUPLICATED);
113 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
116 public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
118 NewSizeMeasuresStep underTest = new NewSizeMeasuresStep(treeRootHolder, periodsHolder, metricRepository, measureRepository, scmInfoRepository,
119 duplicationRepository);
122 public void compute_new_lines() {
123 setChangesets(FILE_1_REF, FILE_2_REF, FILE_4_REF);
127 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_LINES_KEY, 11);
128 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_2_REF, NEW_LINES_KEY, 11);
129 assertNoRawMeasure(FILE_3_REF, NEW_LINES_KEY);
130 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_4_REF, NEW_LINES_KEY, 11);
131 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(DIRECTORY_REF, NEW_LINES_KEY, 22);
132 assertNoRawMeasure(DIRECTORY_2_REF, NEW_LINES_KEY);
133 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_1_REF, NEW_LINES_KEY, 22);
134 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_2_REF, NEW_LINES_KEY, 11);
135 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(MODULE_REF, NEW_LINES_KEY, 33);
136 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(ROOT_REF, NEW_LINES_KEY, 33);
140 public void does_not_compute_new_lines_when_no_changeset() {
143 assertNoRawMeasures(NEW_LINES_KEY);
147 public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_of_a_single_line() {
148 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), new TextBlock(2, 2));
149 setChangesets(FILE_1_REF);
153 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 2d);
157 public void compute_duplicated_lines_counts_lines_from_original_and_ignores_InProjectDuplicate() {
158 TextBlock original = new TextBlock(1, 1);
159 duplicationRepository.addDuplication(FILE_1_REF, original, FILE_2_REF, new TextBlock(2, 2));
160 setChangesets(FILE_1_REF);
164 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 1d);
168 public void compute_duplicated_lines_counts_lines_from_original_and_ignores_CrossProjectDuplicate() {
169 TextBlock original = new TextBlock(1, 1);
170 duplicationRepository.addDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
171 setChangesets(FILE_1_REF);
175 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 1d);
179 public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate() {
180 TextBlock original = new TextBlock(1, 5);
181 duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(10, 11));
182 setChangesets(FILE_1_REF);
186 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 6d);
190 public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_only_once() {
191 TextBlock original = new TextBlock(1, 10);
192 duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(10, 11), new TextBlock(11, 12));
193 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(2, 2), new TextBlock(4, 4));
194 setChangesets(FILE_1_REF);
198 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 11d);
202 public void compute_new_duplicated_lines_on_different_periods() {
203 TextBlock original = new TextBlock(1, 1);
204 duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(2, 2));
205 scmInfoRepository.setScmInfo(FILE_1_REF,
206 Changeset.newChangesetBuilder().setDate(parseDate("2012-01-01").getTime()).setRevision("rev-1").build(),
207 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-2").build());
211 assertRawMeasureValue(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 2d, 1d);
215 public void compute_and_aggregate_duplicated_lines() {
216 addDuplicatedBlock(FILE_1_REF, 2);
217 addDuplicatedBlock(FILE_3_REF, 10);
218 addDuplicatedBlock(FILE_4_REF, 12);
219 setChangesets(FILE_1_REF);
220 setChangesets(FILE_2_REF);
221 setChangesets(FILE_3_REF);
222 setChangesets(FILE_4_REF);
226 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 2d);
227 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_2_REF, NEW_DUPLICATED_LINES_KEY, 0d);
228 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_3_REF, NEW_DUPLICATED_LINES_KEY, 9d);
229 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_4_REF, NEW_DUPLICATED_LINES_KEY, 11d);
230 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(DIRECTORY_REF, NEW_DUPLICATED_LINES_KEY, 2d);
231 assertNoRawMeasure(DIRECTORY_2_REF, NEW_DUPLICATED_LINES_KEY);
232 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_1_REF, NEW_DUPLICATED_LINES_KEY, 2d);
233 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_2_REF, NEW_DUPLICATED_LINES_KEY, 20d);
234 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(MODULE_REF, NEW_DUPLICATED_LINES_KEY, 22d);
235 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(ROOT_REF, NEW_DUPLICATED_LINES_KEY, 22d);
239 public void compute_and_aggregate_zero_duplicated_line_when_no_duplication() {
240 setChangesets(FILE_1_REF);
241 setChangesets(FILE_2_REF);
242 setChangesets(FILE_3_REF);
243 setChangesets(FILE_4_REF);
247 assertComputedAndAggregatedToZeroInt(NEW_DUPLICATED_LINES_KEY);
251 public void compute_duplicated_blocks_one_for_original_one_for_each_InnerDuplicate() {
252 TextBlock original = new TextBlock(1, 1);
253 duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(2, 2), new TextBlock(4, 4), new TextBlock(3, 4));
254 setChangesets(FILE_1_REF);
258 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 4);
262 public void compute_duplicated_blocks_does_not_count_blocks_only_once_it_assumes_consistency_from_duplication_data() {
263 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), new TextBlock(4, 4));
264 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(2, 2), new TextBlock(4, 4));
265 setChangesets(FILE_1_REF);
269 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 4);
273 public void compute_duplicated_blocks_one_for_original_and_ignores_InProjectDuplicate() {
274 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), FILE_2_REF, new TextBlock(2, 2));
275 setChangesets(FILE_1_REF);
279 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 1);
283 public void compute_duplicated_blocks_one_for_original_and_ignores_CrossProjectDuplicate() {
284 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
285 setChangesets(FILE_1_REF);
289 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 1);
293 public void compute_and_aggregate_duplicated_blocks_from_single_duplication() {
294 addDuplicatedBlock(FILE_1_REF, 11);
295 addDuplicatedBlock(FILE_2_REF, 2);
296 addDuplicatedBlock(FILE_4_REF, 7);
297 setChangesets(FILE_1_REF, FILE_2_REF, FILE_3_REF, FILE_4_REF);
301 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 10);
302 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_2_REF, NEW_BLOCKS_DUPLICATED_KEY, 2);
303 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_3_REF, NEW_BLOCKS_DUPLICATED_KEY, 0);
304 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_4_REF, NEW_BLOCKS_DUPLICATED_KEY, 6);
305 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(DIRECTORY_REF, NEW_BLOCKS_DUPLICATED_KEY, 12);
306 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 12);
307 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_2_REF, NEW_BLOCKS_DUPLICATED_KEY, 6);
308 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(MODULE_REF, NEW_BLOCKS_DUPLICATED_KEY, 18);
309 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(ROOT_REF, NEW_BLOCKS_DUPLICATED_KEY, 18);
313 public void compute_and_aggregate_duplicated_blocks_to_zero_when_no_duplication() {
314 setChangesets(FILE_1_REF, FILE_2_REF, FILE_3_REF, FILE_4_REF);
318 assertComputedAndAggregatedToZeroInt(NEW_BLOCKS_DUPLICATED_KEY);
322 public void compute_new_duplicated_lines_density() {
323 setChangesets(FILE_1_REF, FILE_2_REF, FILE_4_REF);
324 addDuplicatedBlock(FILE_1_REF, 2);
325 addDuplicatedBlock(FILE_3_REF, 10);
326 addDuplicatedBlock(FILE_4_REF, 12);
330 assertRawMeasureValue(FILE_1_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 18.2d, null);
331 assertRawMeasureValue(FILE_2_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 0d, null);
332 assertNoRawMeasure(FILE_3_REF, NEW_DUPLICATED_LINES_DENSITY_KEY);
333 assertRawMeasureValue(FILE_4_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 100d, null);
334 assertRawMeasureValue(DIRECTORY_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 9.1d, null);
335 assertNoRawMeasure(DIRECTORY_2_REF, NEW_DUPLICATED_LINES_DENSITY_KEY);
336 assertRawMeasureValue(SUB_MODULE_1_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 9.1d, null);
337 assertRawMeasureValue(SUB_MODULE_2_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 100d, null);
338 assertRawMeasureValue(MODULE_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 39.4d, null);
339 assertRawMeasureValue(ROOT_REF, NEW_DUPLICATED_LINES_DENSITY_KEY, 39.4d, null);
343 public void compute_no_new_duplicated_lines_density_when_no_lines() {
346 assertNoRawMeasures(NEW_DUPLICATED_LINES_DENSITY_KEY);
350 * Adds duplication blocks of a single line (each line is specific to its block).
352 * This is a very simple use case, convenient for unit tests but more realistic and complex use cases must be tested separately.
354 private void addDuplicatedBlock(int fileRef, int blockCount) {
355 checkArgument(blockCount > 1, "BlockCount can not be less than 2");
356 TextBlock original = new TextBlock(1, 1);
357 TextBlock[] duplicates = new TextBlock[blockCount - 1];
358 for (int i = 2; i < blockCount + 1; i++) {
359 duplicates[i - 2] = new TextBlock(i, i);
361 duplicationRepository.addDuplication(fileRef, original, duplicates);
364 private void setChangesets(int... componentRefs) {
365 Arrays.stream(componentRefs)
366 .forEach(componentRef -> scmInfoRepository.setScmInfo(componentRef,
367 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
368 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
369 // line 3 is older, part of no period
370 Changeset.newChangesetBuilder().setDate(parseDate("2007-01-15").getTime()).setRevision("rev-2").build(),
371 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
372 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
373 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
374 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
375 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
376 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
377 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
378 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build(),
379 Changeset.newChangesetBuilder().setDate(parseDate("2011-01-01").getTime()).setRevision("rev-1").build()));
382 private void assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(int componentRef, String metricKey, double period2Value) {
383 assertRawMeasureValue(componentRef, metricKey, period2Value, 0d);
386 private void assertRawMeasureValue(int componentRef, String metricKey, double period2Value, @Nullable Double period5Value) {
387 MeasureVariations variations = measureRepository.getAddedRawMeasure(componentRef, metricKey).get().getVariations();
388 assertThat(variations.getVariation2()).isEqualTo(period2Value, DEFAULT_OFFSET);
389 if (period5Value != null) {
390 assertThat(variations.getVariation5()).isEqualTo(period5Value, DEFAULT_OFFSET);
392 assertThat(variations.hasVariation5()).isFalse();
396 private void assertComputedAndAggregatedToZeroInt(String metricKey) {
397 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_1_REF, metricKey, 0);
398 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_2_REF, metricKey, 0);
399 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_3_REF, metricKey, 0);
400 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(FILE_4_REF, metricKey, 0);
401 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(DIRECTORY_REF, metricKey, 0);
402 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_1_REF, metricKey, 0);
403 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(SUB_MODULE_2_REF, metricKey, 0);
404 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(MODULE_REF, metricKey, 0);
405 assertRawMeasureValueOnPeriod2AndZeroOnPeriod5(ROOT_REF, metricKey, 0);
408 private void assertNoRawMeasure(int componentRef, String metricKey) {
409 assertThat(measureRepository.getAddedRawMeasure(componentRef, metricKey)).isAbsent();
412 private void assertNoRawMeasures(String metricKey) {
413 assertThat(measureRepository.getAddedRawMeasures(FILE_1_REF).get(metricKey)).isEmpty();
414 assertThat(measureRepository.getAddedRawMeasures(FILE_2_REF).get(metricKey)).isEmpty();
415 assertThat(measureRepository.getAddedRawMeasures(FILE_3_REF).get(metricKey)).isEmpty();
416 assertThat(measureRepository.getAddedRawMeasures(FILE_4_REF).get(metricKey)).isEmpty();
417 assertThat(measureRepository.getAddedRawMeasures(DIRECTORY_REF).get(metricKey)).isEmpty();
418 assertThat(measureRepository.getAddedRawMeasures(SUB_MODULE_1_REF).get(metricKey)).isEmpty();
419 assertThat(measureRepository.getAddedRawMeasures(SUB_MODULE_2_REF).get(metricKey)).isEmpty();
420 assertThat(measureRepository.getAddedRawMeasures(MODULE_REF).get(metricKey)).isEmpty();
421 assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(metricKey)).isEmpty();