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.step;
22 import org.junit.Before;
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.ViewsComponent;
28 import org.sonar.server.computation.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
29 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
30 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
31 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
32 import org.sonar.server.computation.task.projectanalysis.period.Period;
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.utils.DateUtils.parseDate;
38 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
39 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
40 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
41 import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.builder;
42 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
43 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
44 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
46 public class ViewsNewCoverageMeasuresStepTest {
48 private static final int ROOT_REF = 1;
49 private static final int SUBVIEW_REF = 11;
50 private static final int SUB_SUBVIEW_1_REF = 111;
51 private static final int PROJECT_VIEW_1_REF = 1111;
52 private static final int SUB_SUBVIEW_2_REF = 112;
53 private static final int PROJECT_VIEW_2_REF = 1121;
54 private static final int PROJECT_VIEW_3_REF = 1122;
55 private static final int PROJECT_VIEW_4_REF = 12;
57 private static final ViewsComponent VIEWS_TREE = builder(VIEW, ROOT_REF)
59 builder(SUBVIEW, SUBVIEW_REF)
61 builder(SUBVIEW, SUB_SUBVIEW_1_REF)
63 builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build())
65 builder(SUBVIEW, SUB_SUBVIEW_2_REF)
67 builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build(),
68 builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
70 builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build())
75 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
77 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
79 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
80 .add(CoreMetrics.COVERAGE_LINE_HITS_DATA)
81 .add(CoreMetrics.CONDITIONS_BY_LINE)
82 .add(CoreMetrics.COVERED_CONDITIONS_BY_LINE)
83 .add(CoreMetrics.NEW_LINES_TO_COVER)
84 .add(CoreMetrics.NEW_UNCOVERED_LINES)
85 .add(CoreMetrics.NEW_CONDITIONS_TO_COVER)
86 .add(CoreMetrics.NEW_UNCOVERED_CONDITIONS)
87 .add(CoreMetrics.NEW_COVERAGE)
88 .add(CoreMetrics.NEW_BRANCH_COVERAGE)
89 .add(CoreMetrics.NEW_LINE_COVERAGE);
91 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
93 private NewCoverageMeasuresStep underTest = new NewCoverageMeasuresStep(treeRootHolder, periodsHolder,
94 measureRepository, metricRepository);
98 periodsHolder.setPeriod(new Period("mode_p_1", null, parseDate("2009-12-25").getTime(), "U1"));
102 public void verify_aggregation_of_measures_for_new_conditions() {
103 String newLinesToCover = CoreMetrics.NEW_LINES_TO_COVER_KEY;
104 String newUncoveredLines = CoreMetrics.NEW_UNCOVERED_LINES_KEY;
105 String newConditionsToCover = CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY;
106 String newUncoveredConditions = CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY;
108 MetricKeys metricKeys = new MetricKeys(newLinesToCover, newUncoveredLines, newConditionsToCover, newUncoveredConditions);
110 treeRootHolder.setRoot(VIEWS_TREE);
111 // PROJECT_VIEW_1_REF has no measure
112 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newLinesToCover, createMeasure(1d));
113 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredLines, createMeasure(10d));
114 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newConditionsToCover, createMeasure(4d));
115 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredConditions, createMeasure(40d));
116 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newLinesToCover, createMeasure(11d));
117 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredLines, createMeasure(20d));
118 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newConditionsToCover, createMeasure(14d));
119 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredConditions, createMeasure(50d));
120 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newLinesToCover, createMeasure(21d));
121 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredLines, createMeasure(30d));
122 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newConditionsToCover, createMeasure(24d));
123 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredConditions, createMeasure(60d));
127 assertNoAddedRawMeasureOnProjectViews();
128 assertNoAddedRawMeasures(SUB_SUBVIEW_1_REF);
129 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
130 entryOf(metricKeys.newLinesToCover, createMeasure(12d)),
131 entryOf(metricKeys.newUncoveredLines, createMeasure(30d)),
132 entryOf(metricKeys.newConditionsToCover, createMeasure(18d)),
133 entryOf(metricKeys.newUncoveredConditions, createMeasure(90d)));
134 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
135 entryOf(metricKeys.newLinesToCover, createMeasure(33d)),
136 entryOf(metricKeys.newUncoveredLines, createMeasure(60d)),
137 entryOf(metricKeys.newConditionsToCover, createMeasure(42d)),
138 entryOf(metricKeys.newUncoveredConditions, createMeasure(150d)));
142 public void verify_aggregates_variations_for_new_code_line_and_branch_Coverage() {
143 LinesAndConditionsWithUncoveredMetricKeys metricKeys = new LinesAndConditionsWithUncoveredMetricKeys(
144 CoreMetrics.NEW_LINES_TO_COVER_KEY, CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY,
145 CoreMetrics.NEW_UNCOVERED_LINES_KEY, CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY);
146 String codeCoverageKey = CoreMetrics.NEW_COVERAGE_KEY;
147 String lineCoverageKey = CoreMetrics.NEW_LINE_COVERAGE_KEY;
148 String branchCoverageKey = CoreMetrics.NEW_BRANCH_COVERAGE_KEY;
150 verify_aggregates_variations(metricKeys, codeCoverageKey, lineCoverageKey, branchCoverageKey);
153 private void verify_aggregates_variations(LinesAndConditionsWithUncoveredMetricKeys metricKeys, String codeCoverageKey, String lineCoverageKey, String branchCoverageKey) {
154 treeRootHolder.setRoot(VIEWS_TREE);
156 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getLines(), createMeasure(3000d))
157 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getConditions(), createMeasure(300d))
158 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredLines(), createMeasure(30d))
159 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredConditions(), createMeasure(9d))
160 // PROJECT_VIEW_2_REF
161 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getLines(), createMeasure(2000d))
162 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getConditions(), createMeasure(400d))
163 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredLines(), createMeasure(200d))
164 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredConditions(), createMeasure(16d))
165 // PROJECT_VIEW_3_REF has no measure
166 // PROJECT_VIEW_4_REF
167 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getLines(), createMeasure(1000d))
168 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getConditions(), createMeasure(300d))
169 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredLines(), createMeasure(100d))
170 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredConditions(), createMeasure(6d));
174 assertNoAddedRawMeasureOnProjectViews();
176 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_1_REF))).contains(
177 entryOf(codeCoverageKey, createMeasure(98.8d)),
178 entryOf(lineCoverageKey, createMeasure(99d)),
179 entryOf(branchCoverageKey, createMeasure(97d)));
180 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
181 entryOf(codeCoverageKey, createMeasure(91d)),
182 entryOf(lineCoverageKey, createMeasure(90d)),
183 entryOf(branchCoverageKey, createMeasure(96d)));
184 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUBVIEW_REF))).contains(
185 entryOf(codeCoverageKey, createMeasure(94.8d)),
186 entryOf(lineCoverageKey, createMeasure(94.5d)),
187 entryOf(branchCoverageKey, createMeasure(96.9d)));
188 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
189 entryOf(codeCoverageKey, createMeasure(94.8d)),
190 entryOf(lineCoverageKey, createMeasure(94.5d)),
191 entryOf(branchCoverageKey, createMeasure(96.9d)));
194 private static final class MetricKeys {
195 private final String newLinesToCover;
196 private final String newUncoveredLines;
197 private final String newConditionsToCover;
198 private final String newUncoveredConditions;
200 public MetricKeys(String newLinesToCover, String newUncoveredLines, String newConditionsToCover, String newUncoveredConditions) {
201 this.newLinesToCover = newLinesToCover;
202 this.newUncoveredLines = newUncoveredLines;
203 this.newConditionsToCover = newConditionsToCover;
204 this.newUncoveredConditions = newUncoveredConditions;
208 private static Measure createMeasure(Double expectedVariation) {
209 return newMeasureBuilder()
210 .setVariation(expectedVariation)
214 private void assertNoAddedRawMeasureOnProjectViews() {
215 assertNoAddedRawMeasures(PROJECT_VIEW_1_REF);
216 assertNoAddedRawMeasures(PROJECT_VIEW_2_REF);
217 assertNoAddedRawMeasures(PROJECT_VIEW_3_REF);
220 private void assertNoAddedRawMeasures(int componentRef) {
221 assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();