3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info 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.
20 package org.sonar.ce.task.projectanalysis.step;
22 import javax.annotation.Nullable;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
27 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
28 import org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
31 import org.sonar.ce.task.step.TestComputationStepContext;
33 import static com.google.common.base.Predicates.notNull;
34 import static com.google.common.collect.FluentIterable.from;
35 import static com.google.common.collect.Iterables.concat;
36 import static java.util.Arrays.asList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
39 import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
40 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
41 import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
42 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
43 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
44 import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
45 import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
46 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
47 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
48 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
49 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
50 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
51 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
53 public class ReportSizeMeasuresStepTest {
55 private static final String LANGUAGE_DOES_NOT_MATTER_HERE = null;
56 private static final int ROOT_REF = 1;
57 private static final int DIRECTORY_1_REF = 1234;
58 private static final int DIRECTORY_2_REF = 1235;
59 private static final int DIRECTORY_3_REF = 1236;
60 private static final int DIRECTORY_4_REF = 1237;
61 private static final int DIRECTORY_4_1_REF = 1238;
62 private static final int DIRECTORY_4_2_REF = 1239;
63 private static final int DIRECTORY_4_3_REF = 1240;
64 private static final int FILE_1_REF = 12341;
65 private static final int FILE_2_REF = 12343;
66 private static final int FILE_3_REF = 12351;
67 private static final int FILE_4_REF = 12371;
68 private static final int FILE_5_REF = 12372;
69 private static final int UNIT_TEST_1_REF = 12352;
70 private static final int UNIT_TEST_2_REF = 12361;
71 private static final int UNIT_TEST_3_REF = 12362;
72 private static final Integer NO_METRIC = null;
75 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
76 builder(PROJECT, ROOT_REF)
78 builder(DIRECTORY, DIRECTORY_1_REF)
80 builder(FILE, FILE_1_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_DOES_NOT_MATTER_HERE, 1)).build(),
81 builder(FILE, FILE_2_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_DOES_NOT_MATTER_HERE, 2)).build())
83 builder(DIRECTORY, DIRECTORY_2_REF)
85 builder(FILE, FILE_3_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_DOES_NOT_MATTER_HERE, 7)).build(),
86 builder(FILE, UNIT_TEST_1_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE, 4)).build())
88 builder(DIRECTORY, DIRECTORY_3_REF)
90 builder(FILE, UNIT_TEST_2_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE, 10)).build())
92 builder(DIRECTORY, DIRECTORY_4_REF)
94 builder(DIRECTORY, DIRECTORY_4_1_REF)
96 builder(FILE, FILE_4_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_DOES_NOT_MATTER_HERE, 9)).build()).build(),
97 builder(DIRECTORY, DIRECTORY_4_2_REF)
99 builder(FILE, FILE_5_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_DOES_NOT_MATTER_HERE, 5)).build()).build(),
100 builder(DIRECTORY, DIRECTORY_4_3_REF)
102 builder(FILE, UNIT_TEST_3_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE, 6)).build()).build()
107 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
108 .add(CoreMetrics.FILES)
109 .add(CoreMetrics.DIRECTORIES)
110 .add(CoreMetrics.LINES)
111 .add(CoreMetrics.GENERATED_LINES)
112 .add(CoreMetrics.NCLOC)
113 .add(CoreMetrics.GENERATED_NCLOC)
114 .add(CoreMetrics.FUNCTIONS)
115 .add(CoreMetrics.STATEMENTS)
116 .add(CoreMetrics.CLASSES);
118 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
120 private SizeMeasuresStep underTest = new SizeMeasuresStep(treeRootHolder, metricRepository, measureRepository);
123 public void verify_LINES_and_FILE_and_DIRECTORY_computation_and_aggregation() {
124 underTest.execute(new TestComputationStepContext());
126 verifyMeasuresOnFile(FILE_1_REF, 1, 1);
127 verifyMeasuresOnFile(FILE_2_REF, 2, 1);
128 verifyMeasuresOnFile(FILE_3_REF, 7, 1);
129 verifyMeasuresOnFile(FILE_4_REF, 9, 1);
130 verifyMeasuresOnFile(FILE_5_REF, 5, 1);
131 verifyNoMeasure(UNIT_TEST_1_REF);
132 verifyNoMeasure(UNIT_TEST_2_REF);
133 verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 3, 2, NO_METRIC);
134 verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 7, 1, NO_METRIC);
135 verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC, NO_METRIC);
136 verifyMeasuresOnOtherComponent(DIRECTORY_4_REF, 14, 2, 2);
137 verifyMeasuresOnOtherComponent(ROOT_REF, 24, 5, 5);
141 public void verify_GENERATED_LINES_related_measures_aggregation() {
142 verifyMetricAggregation(GENERATED_LINES_KEY);
146 public void verify_NCLOC_measure_aggregation() {
147 verifyMetricAggregation(NCLOC_KEY);
150 private void verifyMetricAggregation(String metricKey) {
151 measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create(10));
152 measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create(6));
153 measureRepository.addRawMeasure(UNIT_TEST_1_REF, metricKey, newMeasureBuilder().create(3));
155 underTest.execute(new TestComputationStepContext());
157 verifyMeasuresOnFile(FILE_1_REF, 1, 1);
158 verifyMeasuresOnFile(FILE_2_REF, 2, 1);
159 verifyMeasuresOnFile(FILE_3_REF, 7, 1);
160 verifyMeasuresOnFile(FILE_4_REF, 9, 1);
161 verifyMeasuresOnFile(FILE_5_REF, 5, 1);
162 verifyNoMeasure(UNIT_TEST_1_REF);
163 verifyNoMeasure(UNIT_TEST_2_REF);
164 verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 3, 2, NO_METRIC, entryOf(metricKey, newMeasureBuilder().create(16)));
165 verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 7, 1, NO_METRIC, entryOf(metricKey, newMeasureBuilder().create(3)));
166 verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC, NO_METRIC);
167 verifyMeasuresOnOtherComponent(DIRECTORY_4_REF, 14, 2, 2);
168 verifyMeasuresOnOtherComponent(ROOT_REF, 24, 5, 5, entryOf(metricKey, newMeasureBuilder().create(19)));
171 private void verifyTwoMeasureAggregation(String metric1Key, String metric2Key) {
172 measureRepository.addRawMeasure(FILE_1_REF, metric1Key, newMeasureBuilder().create(1));
173 measureRepository.addRawMeasure(FILE_1_REF, metric2Key, newMeasureBuilder().create(10));
174 // FILE_2_REF has no metric2 measure
175 measureRepository.addRawMeasure(FILE_2_REF, metric1Key, newMeasureBuilder().create(6));
176 // FILE_3_REF has no measure at all
177 // UNIT_TEST_1_REF has no metric1
178 measureRepository.addRawMeasure(UNIT_TEST_1_REF, metric2Key, newMeasureBuilder().create(90));
179 measureRepository.addRawMeasure(FILE_4_REF, metric1Key, newMeasureBuilder().create(2));
180 measureRepository.addRawMeasure(FILE_4_REF, metric2Key, newMeasureBuilder().create(3));
182 underTest.execute(new TestComputationStepContext());
184 verifyMeasuresOnFile(FILE_1_REF, 1, 1);
185 verifyMeasuresOnFile(FILE_2_REF, 2, 1);
186 verifyMeasuresOnFile(FILE_3_REF, 7, 1);
187 verifyNoMeasure(UNIT_TEST_1_REF);
188 verifyNoMeasure(UNIT_TEST_2_REF);
189 verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 3, 2, NO_METRIC,
190 entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(10)));
191 verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 7, 1, NO_METRIC,
192 entryOf(metric2Key, newMeasureBuilder().create(90)));
193 MeasureRepoEntry[] subModuleAndAboveEntries = {
194 entryOf(metric1Key, newMeasureBuilder().create(9)),
195 entryOf(metric2Key, newMeasureBuilder().create(103))
197 verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC, NO_METRIC);
198 verifyMeasuresOnOtherComponent(ROOT_REF, 24, 5, 5, subModuleAndAboveEntries);
202 public void verify_FUNCTIONS_and_STATEMENT_measure_aggregation() {
203 verifyTwoMeasureAggregation(FUNCTIONS_KEY, STATEMENTS_KEY);
207 public void verify_CLASSES_measure_aggregation() {
208 verifyMetricAggregation(CLASSES_KEY);
211 private void verifyMeasuresOnFile(int componentRef, int linesCount, int fileCount) {
212 assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
214 entryOf(LINES_KEY, newMeasureBuilder().create(linesCount)),
215 entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)));
218 private void verifyMeasuresOnOtherComponent(int componentRef, @Nullable Integer linesCount, @Nullable Integer fileCount, @Nullable Integer directoryCount,
219 MeasureRepoEntry... otherMeasures) {
220 MeasureRepoEntry[] measureRepoEntries = concatIntoArray(
222 linesCount == null ? null : entryOf(LINES_KEY, newMeasureBuilder().create(linesCount)),
223 fileCount == null ? null : entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)));
224 assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
225 .containsOnly(measureRepoEntries);
228 private static MeasureRepoEntry[] concatIntoArray(MeasureRepoEntry[] otherMeasures, MeasureRepoEntry... measureRepoEntries) {
230 asList(otherMeasures),
231 from(asList(measureRepoEntries)).filter(notNull())))
232 .toArray(MeasureRepoEntry.class);
235 private void verifyNoMeasure(int componentRef) {
236 assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty();