]> source.dussan.org Git - sonarqube.git/blob
70e73f63fb3b30fc46a1a8b6bcf1ff8a53ede172
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
22 import javax.annotation.Nullable;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
26 import org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry;
27 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
28 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
29 import org.sonar.ce.task.step.TestComputationStepContext;
30
31 import static com.google.common.base.Predicates.notNull;
32 import static com.google.common.collect.FluentIterable.from;
33 import static com.google.common.collect.Iterables.concat;
34 import static java.util.Arrays.asList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.sonar.api.measures.CoreMetrics.CLASSES;
37 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
38 import static org.sonar.api.measures.CoreMetrics.DIRECTORIES;
39 import static org.sonar.api.measures.CoreMetrics.FILES;
40 import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
41 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS;
42 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
43 import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES;
44 import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
45 import static org.sonar.api.measures.CoreMetrics.GENERATED_NCLOC;
46 import static org.sonar.api.measures.CoreMetrics.LINES;
47 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.NCLOC;
49 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
50 import static org.sonar.api.measures.CoreMetrics.STATEMENTS;
51 import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
52 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
53 import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
54 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
55 import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
56 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
57 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
58 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
59
60 public class ViewsSizeMeasuresStepTest {
61
62   private static final int ROOT_REF = 1;
63   private static final int SUBVIEW_1_REF = 12;
64   private static final int SUBVIEW_2_REF = 13;
65   private static final int SUB_SUBVIEW_1_REF = 121;
66   private static final int SUB_SUBVIEW_2_REF = 122;
67   private static final int SUB_SUBVIEW_3_REF = 123;
68   private static final int PROJECTVIEW_1_REF = 1231;
69   private static final int PROJECTVIEW_2_REF = 1232;
70   private static final int PROJECTVIEW_3_REF = 1241;
71   private static final int PROJECTVIEW_4_REF = 1251;
72   private static final int PROJECTVIEW_5_REF = 14;
73   private static final Integer NO_METRIC = null;
74
75   @Rule
76   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
77     builder(VIEW, ROOT_REF)
78       .addChildren(
79         builder(SUBVIEW, SUBVIEW_1_REF)
80           .addChildren(
81             builder(SUBVIEW, SUB_SUBVIEW_1_REF)
82               .addChildren(
83                 builder(PROJECT_VIEW, PROJECTVIEW_1_REF).build(),
84                 builder(PROJECT_VIEW, PROJECTVIEW_2_REF).build())
85               .build(),
86             builder(SUBVIEW, SUB_SUBVIEW_2_REF)
87               .addChildren(
88                 builder(PROJECT_VIEW, PROJECTVIEW_3_REF).build())
89               .build(),
90             builder(SUBVIEW, SUB_SUBVIEW_3_REF).addChildren(
91               builder(PROJECT_VIEW, PROJECTVIEW_4_REF).build())
92               .build())
93           .build(),
94         builder(SUBVIEW, SUBVIEW_2_REF).build(),
95         builder(PROJECT_VIEW, PROJECTVIEW_5_REF).build())
96       .build());
97   @Rule
98   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
99     .add(FILES)
100     .add(DIRECTORIES)
101     .add(LINES)
102     .add(GENERATED_LINES)
103     .add(NCLOC)
104     .add(GENERATED_NCLOC)
105     .add(FUNCTIONS)
106     .add(STATEMENTS)
107     .add(CLASSES);
108   @Rule
109   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository)
110     .addRawMeasure(PROJECTVIEW_1_REF, LINES_KEY, newMeasureBuilder().create(1))
111     .addRawMeasure(PROJECTVIEW_2_REF, LINES_KEY, newMeasureBuilder().create(2))
112     .addRawMeasure(PROJECTVIEW_3_REF, LINES_KEY, newMeasureBuilder().create(5))
113     // PROJECTVIEW_4_REF has no lines metric
114     .addRawMeasure(PROJECTVIEW_5_REF, LINES_KEY, newMeasureBuilder().create(5))
115
116     .addRawMeasure(PROJECTVIEW_1_REF, FILES_KEY, newMeasureBuilder().create(1))
117     .addRawMeasure(PROJECTVIEW_2_REF, FILES_KEY, newMeasureBuilder().create(2))
118     .addRawMeasure(PROJECTVIEW_3_REF, FILES_KEY, newMeasureBuilder().create(3))
119     // PROJECTVIEW_4_REF has no file metric
120     .addRawMeasure(PROJECTVIEW_5_REF, FILES_KEY, newMeasureBuilder().create(5));
121   // PROJECTVIEW_3_REF has no directory metric
122
123   private SizeMeasuresStep underTest = new SizeMeasuresStep(treeRootHolder, metricRepository, measureRepository);
124
125   @Test
126   public void verify_FILE_and_DIRECTORY_computation_and_aggregation() {
127     underTest.execute(new TestComputationStepContext());
128
129     verifyNoMeasure(PROJECTVIEW_1_REF);
130     verifyNoMeasure(PROJECTVIEW_2_REF);
131     verifyNoMeasure(PROJECTVIEW_3_REF);
132     verifyNoMeasure(PROJECTVIEW_4_REF);
133     verifyNoMeasure(PROJECTVIEW_5_REF);
134     verifyMeasures(SUB_SUBVIEW_1_REF, 3, 3, 3);
135     verifyMeasures(SUB_SUBVIEW_2_REF, 5, 3, 0);
136     verifyMeasures(SUB_SUBVIEW_3_REF, NO_METRIC, NO_METRIC, NO_METRIC);
137     verifyMeasures(SUBVIEW_1_REF, 8, 6, 7);
138     verifyMeasures(SUBVIEW_2_REF, NO_METRIC, NO_METRIC, NO_METRIC);
139     verifyMeasures(ROOT_REF, 13, 11, 12);
140   }
141
142   @Test
143   public void verify_GENERATED_LINES_related_measures_aggregation() {
144     verifyMetricAggregation(GENERATED_LINES_KEY);
145   }
146
147   @Test
148   public void verify_NCLOC_measure_aggregation() {
149     verifyMetricAggregation(NCLOC_KEY);
150   }
151
152   private void verifyMetricAggregation(String metricKey) {
153     measureRepository.addRawMeasure(PROJECTVIEW_1_REF, metricKey, newMeasureBuilder().create(10));
154     measureRepository.addRawMeasure(PROJECTVIEW_2_REF, metricKey, newMeasureBuilder().create(6));
155     measureRepository.addRawMeasure(PROJECTVIEW_4_REF, metricKey, newMeasureBuilder().create(3));
156     measureRepository.addRawMeasure(PROJECTVIEW_5_REF, metricKey, newMeasureBuilder().create(7));
157
158     underTest.execute(new TestComputationStepContext());
159
160     verifyNoMeasure(PROJECTVIEW_1_REF);
161     verifyNoMeasure(PROJECTVIEW_2_REF);
162     verifyNoMeasure(PROJECTVIEW_3_REF);
163     verifyNoMeasure(PROJECTVIEW_4_REF);
164     verifyNoMeasure(PROJECTVIEW_5_REF);
165     verifyMeasures(SUB_SUBVIEW_1_REF, 3, 3, 3, entryOf(metricKey, newMeasureBuilder().create(16)));
166     verifyMeasures(SUB_SUBVIEW_2_REF, 5, 3, 0);
167     verifyMeasures(SUB_SUBVIEW_3_REF, NO_METRIC, NO_METRIC, NO_METRIC, entryOf(metricKey, newMeasureBuilder().create(3)));
168     verifyMeasures(SUBVIEW_1_REF, 8, 6, 7, entryOf(metricKey, newMeasureBuilder().create(19)));
169     verifyMeasures(SUBVIEW_2_REF, NO_METRIC, NO_METRIC, NO_METRIC);
170     verifyMeasures(ROOT_REF, 13, 11, 12, entryOf(metricKey, newMeasureBuilder().create(26)));
171   }
172
173   private void verifyTwoMeasureAggregation(String metric1Key, String metric2Key) {
174     measureRepository.addRawMeasure(PROJECTVIEW_1_REF, metric1Key, newMeasureBuilder().create(1));
175     measureRepository.addRawMeasure(PROJECTVIEW_1_REF, metric2Key, newMeasureBuilder().create(10));
176     // PROJECTVIEW_2_REF has no metric2 measure
177     measureRepository.addRawMeasure(PROJECTVIEW_2_REF, metric1Key, newMeasureBuilder().create(6));
178     // PROJECTVIEW_3_REF has no measure at all
179     // PROJECTVIEW_4_REF has no metric1
180     measureRepository.addRawMeasure(PROJECTVIEW_4_REF, metric2Key, newMeasureBuilder().create(90));
181     measureRepository.addRawMeasure(PROJECTVIEW_5_REF, metric1Key, newMeasureBuilder().create(3));
182     measureRepository.addRawMeasure(PROJECTVIEW_5_REF, metric2Key, newMeasureBuilder().create(7));
183
184     underTest.execute(new TestComputationStepContext());
185
186     verifyNoMeasure(PROJECTVIEW_1_REF);
187     verifyNoMeasure(PROJECTVIEW_2_REF);
188     verifyNoMeasure(PROJECTVIEW_3_REF);
189     verifyNoMeasure(PROJECTVIEW_4_REF);
190     verifyNoMeasure(PROJECTVIEW_5_REF);
191     verifyNoMeasure(PROJECTVIEW_4_REF);
192     verifyMeasures(SUB_SUBVIEW_1_REF, 3, 3, 3,
193       entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(10)));
194     verifyMeasures(SUB_SUBVIEW_2_REF, 5, 3, 0);
195     verifyMeasures(SUB_SUBVIEW_3_REF, NO_METRIC, NO_METRIC, NO_METRIC,
196       entryOf(metric2Key, newMeasureBuilder().create(90)));
197     verifyMeasures(SUBVIEW_1_REF, 8, 6, 7,
198       entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(100)));
199     verifyMeasures(SUBVIEW_2_REF, NO_METRIC, NO_METRIC, NO_METRIC);
200     verifyMeasures(ROOT_REF, 13, 11, 12,
201       entryOf(metric1Key, newMeasureBuilder().create(10)), entryOf(metric2Key, newMeasureBuilder().create(107)));
202   }
203
204   @Test
205   public void verify_FUNCTIONS_and_STATEMENT_measure_aggregation() {
206     verifyTwoMeasureAggregation(FUNCTIONS_KEY, STATEMENTS_KEY);
207   }
208
209   @Test
210   public void verify_CLASSES_measure_aggregation() {
211     verifyMetricAggregation(CLASSES_KEY);
212   }
213
214   private void verifyMeasures(int componentRef, @Nullable Integer linesCount, @Nullable Integer fileCount, @Nullable Integer directoryCount, MeasureRepoEntry... otherMeasures) {
215     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
216       .containsOnly(
217         concatIntoArray(otherMeasures, createFileAndDirectoryEntries(linesCount, fileCount, directoryCount)));
218   }
219
220   private static MeasureRepoEntry[] createFileAndDirectoryEntries(@Nullable Integer linesCount, @Nullable Integer fileCount, @Nullable Integer directoryCount) {
221     return new MeasureRepoEntry[] {
222       linesCount == null ? null : entryOf(LINES_KEY, newMeasureBuilder().create(linesCount)),
223       fileCount == null ? null : entryOf(FILES_KEY, newMeasureBuilder().create(fileCount))
224     };
225   }
226
227   private static MeasureRepoEntry[] concatIntoArray(MeasureRepoEntry[] otherMeasures, MeasureRepoEntry... measureRepoEntries) {
228     return from(concat(
229       asList(otherMeasures),
230       from(asList(measureRepoEntries)).filter(notNull())))
231       .toArray(MeasureRepoEntry.class);
232   }
233
234   private void verifyNoMeasure(int componentRef) {
235     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty();
236   }
237 }