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 javax.annotation.Nullable;
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.ViewsComponent;
29 import org.sonar.server.computation.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
30 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
31 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
32 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
33 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
34 import org.sonar.server.computation.task.projectanalysis.period.Period;
35 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolderRule;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.guava.api.Assertions.assertThat;
39 import static org.sonar.api.utils.DateUtils.parseDate;
40 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
41 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
42 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
43 import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.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 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations.newMeasureVariationsBuilder;
49 public class ViewsNewCoverageMeasuresStepTest {
51 private static final int ROOT_REF = 1;
52 private static final int SUBVIEW_REF = 11;
53 private static final int SUB_SUBVIEW_1_REF = 111;
54 private static final int PROJECT_VIEW_1_REF = 1111;
55 private static final int SUB_SUBVIEW_2_REF = 112;
56 private static final int PROJECT_VIEW_2_REF = 1121;
57 private static final int PROJECT_VIEW_3_REF = 1122;
58 private static final int PROJECT_VIEW_4_REF = 12;
60 private static final ViewsComponent VIEWS_TREE = builder(VIEW, ROOT_REF)
62 builder(SUBVIEW, SUBVIEW_REF)
64 builder(SUBVIEW, SUB_SUBVIEW_1_REF)
66 builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build())
68 builder(SUBVIEW, SUB_SUBVIEW_2_REF)
70 builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build(),
71 builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
73 builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build())
76 private static final Double NO_PERIOD_4_OR_5_IN_VIEWS = null;
79 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
81 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
83 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
84 .add(CoreMetrics.COVERAGE_LINE_HITS_DATA)
85 .add(CoreMetrics.CONDITIONS_BY_LINE)
86 .add(CoreMetrics.COVERED_CONDITIONS_BY_LINE)
87 .add(CoreMetrics.NEW_LINES_TO_COVER)
88 .add(CoreMetrics.NEW_UNCOVERED_LINES)
89 .add(CoreMetrics.NEW_CONDITIONS_TO_COVER)
90 .add(CoreMetrics.NEW_UNCOVERED_CONDITIONS)
91 .add(CoreMetrics.NEW_COVERAGE)
92 .add(CoreMetrics.NEW_BRANCH_COVERAGE)
93 .add(CoreMetrics.NEW_LINE_COVERAGE);
95 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
97 private NewCoverageMeasuresStep underTest = new NewCoverageMeasuresStep(treeRootHolder, periodsHolder,
98 measureRepository, metricRepository);
101 public void setUp() {
102 periodsHolder.setPeriods(
103 new Period(2, "mode_p_1", null, parseDate("2009-12-25").getTime(), "U1"),
104 new Period(5, "mode_p_5", null, parseDate("2011-02-18").getTime(), "U2"));
108 public void verify_aggregation_of_measures_for_new_conditions() {
109 String newLinesToCover = CoreMetrics.NEW_LINES_TO_COVER_KEY;
110 String newUncoveredLines = CoreMetrics.NEW_UNCOVERED_LINES_KEY;
111 String newConditionsToCover = CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY;
112 String newUncoveredConditions = CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY;
114 MetricKeys metricKeys = new MetricKeys(newLinesToCover, newUncoveredLines, newConditionsToCover, newUncoveredConditions);
116 treeRootHolder.setRoot(VIEWS_TREE);
117 // PROJECT_VIEW_1_REF has no measure
118 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newLinesToCover, createMeasure(1d, 2d));
119 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredLines, createMeasure(10d, 20d));
120 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newConditionsToCover, createMeasure(4d, 5d));
121 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredConditions, createMeasure(40d, 50d));
122 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newLinesToCover, createMeasure(11d, 12d));
123 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredLines, createMeasure(20d, 30d));
124 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newConditionsToCover, createMeasure(14d, 15d));
125 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredConditions, createMeasure(50d, 60d));
126 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newLinesToCover, createMeasure(21d, 22d));
127 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredLines, createMeasure(30d, 40d));
128 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newConditionsToCover, createMeasure(24d, 25d));
129 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredConditions, createMeasure(60d, 70d));
133 assertNoAddedRawMeasureOnProjectViews();
134 assertNoAddedRawMeasures(SUB_SUBVIEW_1_REF);
135 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
136 entryOf(metricKeys.newLinesToCover, createMeasure(12d, NO_PERIOD_4_OR_5_IN_VIEWS)),
137 entryOf(metricKeys.newUncoveredLines, createMeasure(30d, NO_PERIOD_4_OR_5_IN_VIEWS)),
138 entryOf(metricKeys.newConditionsToCover, createMeasure(18d, NO_PERIOD_4_OR_5_IN_VIEWS)),
139 entryOf(metricKeys.newUncoveredConditions, createMeasure(90d, NO_PERIOD_4_OR_5_IN_VIEWS)));
140 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
141 entryOf(metricKeys.newLinesToCover, createMeasure(33d, NO_PERIOD_4_OR_5_IN_VIEWS)),
142 entryOf(metricKeys.newUncoveredLines, createMeasure(60d, NO_PERIOD_4_OR_5_IN_VIEWS)),
143 entryOf(metricKeys.newConditionsToCover, createMeasure(42d, NO_PERIOD_4_OR_5_IN_VIEWS)),
144 entryOf(metricKeys.newUncoveredConditions, createMeasure(150d, NO_PERIOD_4_OR_5_IN_VIEWS)));
148 public void verify_aggregates_variations_for_new_code_line_and_branch_Coverage() {
149 LinesAndConditionsWithUncoveredMetricKeys metricKeys = new LinesAndConditionsWithUncoveredMetricKeys(
150 CoreMetrics.NEW_LINES_TO_COVER_KEY, CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY,
151 CoreMetrics.NEW_UNCOVERED_LINES_KEY, CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY);
152 String codeCoverageKey = CoreMetrics.NEW_COVERAGE_KEY;
153 String lineCoverageKey = CoreMetrics.NEW_LINE_COVERAGE_KEY;
154 String branchCoverageKey = CoreMetrics.NEW_BRANCH_COVERAGE_KEY;
156 verify_aggregates_variations(metricKeys, codeCoverageKey, lineCoverageKey, branchCoverageKey);
159 private void verify_aggregates_variations(LinesAndConditionsWithUncoveredMetricKeys metricKeys, String codeCoverageKey, String lineCoverageKey, String branchCoverageKey) {
160 treeRootHolder.setRoot(VIEWS_TREE);
162 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getLines(), createMeasure(3000d, 2000d))
163 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getConditions(), createMeasure(300d, 400d))
164 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredLines(), createMeasure(30d, 200d))
165 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredConditions(), createMeasure(9d, 16d))
166 // PROJECT_VIEW_2_REF
167 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getLines(), createMeasure(2000d, 3000d))
168 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getConditions(), createMeasure(400d, 300d))
169 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredLines(), createMeasure(200d, 30d))
170 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredConditions(), createMeasure(16d, 9d))
171 // PROJECT_VIEW_3_REF has no measure
172 // PROJECT_VIEW_4_REF
173 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getLines(), createMeasure(1000d, 2000d))
174 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getConditions(), createMeasure(300d, 200d))
175 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredLines(), createMeasure(100d, 20d))
176 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredConditions(), createMeasure(6d, 9d));
180 assertNoAddedRawMeasureOnProjectViews();
182 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_1_REF))).contains(
183 entryOf(codeCoverageKey, createMeasure(98.8d, NO_PERIOD_4_OR_5_IN_VIEWS)),
184 entryOf(lineCoverageKey, createMeasure(99d, NO_PERIOD_4_OR_5_IN_VIEWS)),
185 entryOf(branchCoverageKey, createMeasure(97d, NO_PERIOD_4_OR_5_IN_VIEWS)));
186 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
187 entryOf(codeCoverageKey, createMeasure(91d, NO_PERIOD_4_OR_5_IN_VIEWS)),
188 entryOf(lineCoverageKey, createMeasure(90d, NO_PERIOD_4_OR_5_IN_VIEWS)),
189 entryOf(branchCoverageKey, createMeasure(96d, NO_PERIOD_4_OR_5_IN_VIEWS)));
190 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUBVIEW_REF))).contains(
191 entryOf(codeCoverageKey, createMeasure(94.8d, NO_PERIOD_4_OR_5_IN_VIEWS)),
192 entryOf(lineCoverageKey, createMeasure(94.5d, NO_PERIOD_4_OR_5_IN_VIEWS)),
193 entryOf(branchCoverageKey, createMeasure(96.9d, NO_PERIOD_4_OR_5_IN_VIEWS)));
194 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
195 entryOf(codeCoverageKey, createMeasure(94.8d, NO_PERIOD_4_OR_5_IN_VIEWS)),
196 entryOf(lineCoverageKey, createMeasure(94.5d, NO_PERIOD_4_OR_5_IN_VIEWS)),
197 entryOf(branchCoverageKey, createMeasure(96.9d, NO_PERIOD_4_OR_5_IN_VIEWS)));
200 private static final class MetricKeys {
201 private final String newLinesToCover;
202 private final String newUncoveredLines;
203 private final String newConditionsToCover;
204 private final String newUncoveredConditions;
206 public MetricKeys(String newLinesToCover, String newUncoveredLines, String newConditionsToCover, String newUncoveredConditions) {
207 this.newLinesToCover = newLinesToCover;
208 this.newUncoveredLines = newUncoveredLines;
209 this.newConditionsToCover = newConditionsToCover;
210 this.newUncoveredConditions = newUncoveredConditions;
214 private static Measure createMeasure(@Nullable Double variationPeriod2, @Nullable Double variationPeriod5) {
215 MeasureVariations.Builder variationBuilder = newMeasureVariationsBuilder();
216 if (variationPeriod2 != null) {
217 variationBuilder.setVariation(new Period(2, "", null, 1L, "U2"), variationPeriod2);
219 if (variationPeriod5 != null) {
220 variationBuilder.setVariation(new Period(5, "", null, 1L, "U2"), variationPeriod5);
222 return newMeasureBuilder()
223 .setVariations(variationBuilder.build())
227 private void assertNoAddedRawMeasureOnProjectViews() {
228 assertNoAddedRawMeasures(PROJECT_VIEW_1_REF);
229 assertNoAddedRawMeasures(PROJECT_VIEW_2_REF);
230 assertNoAddedRawMeasures(PROJECT_VIEW_3_REF);
233 private void assertNoAddedRawMeasures(int componentRef) {
234 assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();