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.
20 package org.sonar.server.computation.task.projectanalysis.formula;
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;
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;
48 public class AverageFormulaExecutionTest {
51 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
53 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
54 .add(CoreMetrics.FUNCTION_COMPLEXITY)
55 .add(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)
56 .add(CoreMetrics.FUNCTIONS);
58 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
60 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
62 FormulaExecutorComponentVisitor underTest;
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)
76 public void add_measures() {
77 ReportComponent project = builder(PROJECT, 1)
81 builder(DIRECTORY, 111)
83 builder(Component.Type.FILE, 1111).build(),
84 builder(Component.Type.FILE, 1112).build()
89 builder(DIRECTORY, 121)
91 builder(Component.Type.FILE, 1211).build()
96 treeRootHolder.setRoot(project);
98 measureRepository.addRawMeasure(1111, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(5));
99 measureRepository.addRawMeasure(1111, FUNCTIONS_KEY, newMeasureBuilder().create(2));
101 measureRepository.addRawMeasure(1112, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(1));
102 measureRepository.addRawMeasure(1112, FUNCTIONS_KEY, newMeasureBuilder().create(1));
104 measureRepository.addRawMeasure(1211, COMPLEXITY_IN_FUNCTIONS_KEY, newMeasureBuilder().create(9));
105 measureRepository.addRawMeasure(1211, FUNCTIONS_KEY, newMeasureBuilder().create(2));
107 new PathAwareCrawler<>(underTest).visit(project);
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)));
120 public void not_add_measures_when_no_data_on_file() {
121 ReportComponent project = builder(PROJECT, 1)
125 builder(DIRECTORY, 111)
127 builder(Component.Type.FILE, 1111).build()
132 treeRootHolder.setRoot(project);
134 new PathAwareCrawler<>(underTest).visit(project);
136 assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
137 assertThat(measureRepository.getAddedRawMeasures(11)).isEmpty();
138 assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty();
139 assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty();