]> source.dussan.org Git - sonarqube.git/blob
a6f08ac2361a4e342b42bc613cbd1401b417b8f1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.step;
21
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.server.computation.task.projectanalysis.component.TreeRootHolderRule;
27 import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
28 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry;
29 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
30 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
31
32 import static com.google.common.base.Predicates.notNull;
33 import static com.google.common.collect.FluentIterable.from;
34 import static com.google.common.collect.Iterables.concat;
35 import static java.util.Arrays.asList;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
38 import static org.sonar.api.measures.CoreMetrics.DIRECTORIES_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.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
46 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
47 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
48 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
49 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
50 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
51 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
52 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
53
54 public class ReportSizeMeasuresStepTest {
55
56   private static final String LANGUAGE_DOES_NOT_MATTER_HERE = null;
57   private static final int ROOT_REF = 1;
58   private static final int MODULE_REF = 12;
59   private static final int SUB_MODULE_REF = 123;
60   private static final int DIRECTORY_1_REF = 1234;
61   private static final int DIRECTORY_2_REF = 1235;
62   private static final int DIRECTORY_3_REF = 1236;
63   private static final int FILE_1_REF = 12341;
64   private static final int FILE_2_REF = 12343;
65   private static final int FILE_3_REF = 12351;
66   private static final int UNIT_TEST_1_REF = 12352;
67   private static final int UNIT_TEST_2_REF = 12361;
68   private static final Integer NO_METRIC = null;
69
70   @Rule
71   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
72     builder(PROJECT, ROOT_REF)
73       .addChildren(
74         builder(MODULE, MODULE_REF)
75           .addChildren(
76             builder(MODULE, SUB_MODULE_REF)
77               .addChildren(
78                 builder(DIRECTORY, DIRECTORY_1_REF)
79                   .addChildren(
80                     builder(FILE, FILE_1_REF).build(),
81                     builder(FILE, FILE_2_REF).build())
82                   .build(),
83                 builder(DIRECTORY, DIRECTORY_2_REF)
84                   .addChildren(
85                     builder(FILE, FILE_3_REF).build(),
86                     builder(FILE, UNIT_TEST_1_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE)).build())
87                   .build(),
88                 builder(DIRECTORY, DIRECTORY_3_REF)
89                   .addChildren(
90                     builder(FILE, UNIT_TEST_2_REF).setFileAttributes(new FileAttributes(true, LANGUAGE_DOES_NOT_MATTER_HERE)).build())
91                   .build())
92               .build())
93           .build())
94       .build());
95   @Rule
96   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
97     .add(CoreMetrics.FILES)
98     .add(CoreMetrics.DIRECTORIES)
99     .add(CoreMetrics.LINES)
100     .add(CoreMetrics.GENERATED_LINES)
101     .add(CoreMetrics.NCLOC)
102     .add(CoreMetrics.GENERATED_NCLOC)
103     .add(CoreMetrics.FUNCTIONS)
104     .add(CoreMetrics.STATEMENTS)
105     .add(CoreMetrics.CLASSES)
106     .add(CoreMetrics.ACCESSORS);
107   @Rule
108   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
109
110   private SizeMeasuresStep underTest = new SizeMeasuresStep(treeRootHolder, metricRepository, measureRepository);
111
112   @Test
113   public void verify_FILE_and_DIRECTORY_computation_and_aggregation() {
114     underTest.execute();
115
116     verifyMeasuresOnFile(FILE_1_REF, 1);
117     verifyMeasuresOnFile(FILE_2_REF, 1);
118     verifyMeasuresOnFile(FILE_3_REF, 1);
119     verifyNoMeasure(UNIT_TEST_1_REF);
120     verifyNoMeasure(UNIT_TEST_2_REF);
121     verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1);
122     verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1);
123     verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC);
124     verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2);
125     verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2);
126     verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2);
127   }
128
129   @Test
130   public void verify_LINE_related_measures_aggregation() {
131     verifyTwoMeasureAggregation(LINES_KEY, GENERATED_LINES_KEY);
132   }
133
134   private void verifyTwoMeasureAggregation(String metric1Key, String metric2Key) {
135     measureRepository.addRawMeasure(FILE_1_REF, metric1Key, newMeasureBuilder().create(1));
136     measureRepository.addRawMeasure(FILE_1_REF, metric2Key, newMeasureBuilder().create(10));
137     // FILE_2_REF has no metric2 measure
138     measureRepository.addRawMeasure(FILE_2_REF, metric1Key, newMeasureBuilder().create(6));
139     // FILE_3_REF has no measure at all
140     // UNIT_TEST_1_REF has no metric1
141     measureRepository.addRawMeasure(UNIT_TEST_1_REF, metric2Key, newMeasureBuilder().create(90));
142
143     underTest.execute();
144
145     verifyMeasuresOnFile(FILE_1_REF, 1);
146     verifyMeasuresOnFile(FILE_2_REF, 1);
147     verifyMeasuresOnFile(FILE_3_REF, 1);
148     verifyNoMeasure(UNIT_TEST_1_REF);
149     verifyNoMeasure(UNIT_TEST_2_REF);
150     verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1,
151       entryOf(metric1Key, newMeasureBuilder().create(7)), entryOf(metric2Key, newMeasureBuilder().create(10)));
152     verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1,
153       entryOf(metric2Key, newMeasureBuilder().create(90)));
154     MeasureRepoEntry[] subModuleAndAboveEntries = {
155       entryOf(metric1Key, newMeasureBuilder().create(7)),
156       entryOf(metric2Key, newMeasureBuilder().create(100))
157     };
158     verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC);
159     verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2, subModuleAndAboveEntries);
160     verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2, subModuleAndAboveEntries);
161     verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2, subModuleAndAboveEntries);
162   }
163
164   @Test
165   public void verify_NCLOC_measure_aggregation() {
166     verifyMetricAggregation(NCLOC_KEY);
167   }
168
169   private void verifyMetricAggregation(String metricKey) {
170     measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create(10));
171     measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create(6));
172     measureRepository.addRawMeasure(UNIT_TEST_1_REF, metricKey, newMeasureBuilder().create(3));
173
174     underTest.execute();
175
176     verifyMeasuresOnFile(FILE_1_REF, 1);
177     verifyMeasuresOnFile(FILE_2_REF, 1);
178     verifyMeasuresOnFile(FILE_3_REF, 1);
179     verifyNoMeasure(UNIT_TEST_1_REF);
180     verifyNoMeasure(UNIT_TEST_2_REF);
181     verifyMeasuresOnOtherComponent(DIRECTORY_1_REF, 2, 1, entryOf(metricKey, newMeasureBuilder().create(16)));
182     verifyMeasuresOnOtherComponent(DIRECTORY_2_REF, 1, 1, entryOf(metricKey, newMeasureBuilder().create(3)));
183     verifyMeasuresOnOtherComponent(DIRECTORY_3_REF, NO_METRIC, NO_METRIC);
184     verifyMeasuresOnOtherComponent(SUB_MODULE_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19)));
185     verifyMeasuresOnOtherComponent(MODULE_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19)));
186     verifyMeasuresOnOtherComponent(ROOT_REF, 3, 2, entryOf(metricKey, newMeasureBuilder().create(19)));
187   }
188
189   @Test
190   public void verify_FUNCTIONS_and_STATEMENT_measure_aggregation() {
191     verifyTwoMeasureAggregation(FUNCTIONS_KEY, STATEMENTS_KEY);
192   }
193
194   @Test
195   public void verify_CLASSES_measure_aggregation() {
196     verifyMetricAggregation(CLASSES_KEY);
197   }
198
199   private void verifyMeasuresOnFile(int componentRef, int fileCount) {
200     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
201       .containsOnly(entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)));
202   }
203
204   private void verifyMeasuresOnOtherComponent(int componentRef, @Nullable Integer fileCount, @Nullable Integer directoryCount, MeasureRepoEntry... otherMeasures) {
205     MeasureRepoEntry[] measureRepoEntries = concatIntoArray(
206       otherMeasures,
207       fileCount == null ? null : entryOf(FILES_KEY, newMeasureBuilder().create(fileCount)),
208       directoryCount == null ? null : entryOf(DIRECTORIES_KEY, newMeasureBuilder().create(directoryCount))
209     );
210     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef)))
211       .containsOnly(measureRepoEntries);
212   }
213
214   private static MeasureRepoEntry[] concatIntoArray(MeasureRepoEntry[] otherMeasures, MeasureRepoEntry... measureRepoEntries) {
215     return from(concat(
216       asList(otherMeasures),
217       from(asList(measureRepoEntries)).filter(notNull())))
218         .toArray(MeasureRepoEntry.class);
219   }
220
221   private void verifyNoMeasure(int componentRef) {
222     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty();
223   }
224 }