]> source.dussan.org Git - sonarqube.git/blob
b47aea3b827861541fef2da67c4429a14da33fda
[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.formula;
21
22 import com.google.common.collect.Lists;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.PathAwareCrawler;
30 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
31 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
32 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
33 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolderRule;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.guava.api.Assertions.assertThat;
37 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
38 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
39 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
40 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
41 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
42 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
43 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
44 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
45 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
46 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
47
48 public class AverageFormulaExecutionTest {
49
50   @Rule
51   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52   @Rule
53   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
54     .add(CoreMetrics.FUNCTION_COMPLEXITY)
55     .add(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)
56     .add(CoreMetrics.FUNCTIONS);
57   @Rule
58   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
59   @Rule
60   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
61
62   FormulaExecutorComponentVisitor underTest;
63
64   @Before
65   public void setUp() throws Exception {
66     underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository)
67       .buildFor(Lists.<Formula>newArrayList(
68         AverageFormula.Builder.newBuilder()
69           .setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
70           .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
71           .setByMetricKey(FUNCTIONS_KEY)
72           .build()));
73   }
74
75   @Test
76   public void add_measures() {
77     ReportComponent project = builder(PROJECT, 1)
78       .addChildren(
79         builder(MODULE, 11)
80           .addChildren(
81             builder(DIRECTORY, 111)
82               .addChildren(
83                 builder(Component.Type.FILE, 1111).build(),
84                 builder(Component.Type.FILE, 1112).build()
85               ).build()
86           ).build(),
87         builder(MODULE, 12)
88           .addChildren(
89             builder(DIRECTORY, 121)
90               .addChildren(
91                 builder(Component.Type.FILE, 1211).build()
92               ).build()
93           ).build()
94       ).build();
95
96     treeRootHolder.setRoot(project);
97
98     measureRepository.addRawMeasure(1111, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(5));
99     measureRepository.addRawMeasure(1111, FUNCTIONS_KEY, newMeasureBuilder().create(2));
100
101     measureRepository.addRawMeasure(1112, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(1));
102     measureRepository.addRawMeasure(1112, FUNCTIONS_KEY, newMeasureBuilder().create(1));
103
104     measureRepository.addRawMeasure(1211, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(9));
105     measureRepository.addRawMeasure(1211, FUNCTIONS_KEY, newMeasureBuilder().create(2));
106
107     new PathAwareCrawler<>(underTest).visit(project);
108
109     assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(3d, 1)));
110     assertThat(toEntries(measureRepository.getAddedRawMeasures(11))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(2d, 1)));
111     assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(2d, 1)));
112     assertThat(toEntries(measureRepository.getAddedRawMeasures(1111))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(2.5d, 1)));
113     assertThat(toEntries(measureRepository.getAddedRawMeasures(1112))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(1d, 1)));
114     assertThat(toEntries(measureRepository.getAddedRawMeasures(12))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(4.5d, 1)));
115     assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(4.5d, 1)));
116     assertThat(toEntries(measureRepository.getAddedRawMeasures(1211))).containsOnly(entryOf(FUNCTION_COMPLEXITY_KEY, newMeasureBuilder().create(4.5d, 1)));
117   }
118
119   @Test
120   public void not_add_measures_when_no_data_on_file() {
121     ReportComponent project = builder(PROJECT, 1)
122       .addChildren(
123         builder(MODULE, 11)
124           .addChildren(
125             builder(DIRECTORY, 111)
126               .addChildren(
127                 builder(Component.Type.FILE, 1111).build()
128               ).build()
129           ).build()
130       ).build();
131
132     treeRootHolder.setRoot(project);
133
134     new PathAwareCrawler<>(underTest).visit(project);
135
136     assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
137     assertThat(measureRepository.getAddedRawMeasures(11)).isEmpty();
138     assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty();
139     assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty();
140   }
141
142 }