3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.step;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
26 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
27 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
32 import org.sonar.ce.task.step.TestComputationStepContext;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.guava.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
38 import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
39 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
40 import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
41 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
42 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
43 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
45 public class ViewsNewCoverageMeasuresStepTest {
47 private static final int ROOT_REF = 1;
48 private static final int SUBVIEW_REF = 11;
49 private static final int SUB_SUBVIEW_1_REF = 111;
50 private static final int PROJECT_VIEW_1_REF = 1111;
51 private static final int SUB_SUBVIEW_2_REF = 112;
52 private static final int PROJECT_VIEW_2_REF = 1121;
53 private static final int PROJECT_VIEW_3_REF = 1122;
54 private static final int PROJECT_VIEW_4_REF = 12;
56 private static final ViewsComponent VIEWS_TREE = builder(VIEW, ROOT_REF)
58 builder(SUBVIEW, SUBVIEW_REF)
60 builder(SUBVIEW, SUB_SUBVIEW_1_REF)
62 builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build())
64 builder(SUBVIEW, SUB_SUBVIEW_2_REF)
66 builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build(),
67 builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
69 builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build())
74 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
77 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
78 .add(CoreMetrics.COVERAGE_LINE_HITS_DATA)
79 .add(CoreMetrics.CONDITIONS_BY_LINE)
80 .add(CoreMetrics.COVERED_CONDITIONS_BY_LINE)
81 .add(CoreMetrics.NEW_LINES_TO_COVER)
82 .add(CoreMetrics.NEW_UNCOVERED_LINES)
83 .add(CoreMetrics.NEW_CONDITIONS_TO_COVER)
84 .add(CoreMetrics.NEW_UNCOVERED_CONDITIONS)
85 .add(CoreMetrics.NEW_COVERAGE)
86 .add(CoreMetrics.NEW_BRANCH_COVERAGE)
87 .add(CoreMetrics.NEW_LINE_COVERAGE);
89 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
91 private NewCoverageMeasuresStep underTest = new NewCoverageMeasuresStep(treeRootHolder, measureRepository, metricRepository);
94 public void verify_aggregation_of_measures_for_new_conditions() {
95 String newLinesToCover = CoreMetrics.NEW_LINES_TO_COVER_KEY;
96 String newUncoveredLines = CoreMetrics.NEW_UNCOVERED_LINES_KEY;
97 String newConditionsToCover = CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY;
98 String newUncoveredConditions = CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY;
100 MetricKeys metricKeys = new MetricKeys(newLinesToCover, newUncoveredLines, newConditionsToCover, newUncoveredConditions);
102 treeRootHolder.setRoot(VIEWS_TREE);
103 // PROJECT_VIEW_1_REF has no measure
104 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newLinesToCover, createMeasure(1d));
105 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredLines, createMeasure(10d));
106 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newConditionsToCover, createMeasure(4d));
107 measureRepository.addRawMeasure(PROJECT_VIEW_2_REF, newUncoveredConditions, createMeasure(40d));
108 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newLinesToCover, createMeasure(11d));
109 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredLines, createMeasure(20d));
110 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newConditionsToCover, createMeasure(14d));
111 measureRepository.addRawMeasure(PROJECT_VIEW_3_REF, newUncoveredConditions, createMeasure(50d));
112 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newLinesToCover, createMeasure(21d));
113 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredLines, createMeasure(30d));
114 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newConditionsToCover, createMeasure(24d));
115 measureRepository.addRawMeasure(PROJECT_VIEW_4_REF, newUncoveredConditions, createMeasure(60d));
117 underTest.execute(new TestComputationStepContext());
119 assertNoAddedRawMeasureOnProjectViews();
120 assertNoAddedRawMeasures(SUB_SUBVIEW_1_REF);
121 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
122 entryOf(metricKeys.newLinesToCover, createMeasure(12d)),
123 entryOf(metricKeys.newUncoveredLines, createMeasure(30d)),
124 entryOf(metricKeys.newConditionsToCover, createMeasure(18d)),
125 entryOf(metricKeys.newUncoveredConditions, createMeasure(90d)));
126 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
127 entryOf(metricKeys.newLinesToCover, createMeasure(33d)),
128 entryOf(metricKeys.newUncoveredLines, createMeasure(60d)),
129 entryOf(metricKeys.newConditionsToCover, createMeasure(42d)),
130 entryOf(metricKeys.newUncoveredConditions, createMeasure(150d)));
134 public void verify_aggregates_variations_for_new_code_line_and_branch_Coverage() {
135 LinesAndConditionsWithUncoveredMetricKeys metricKeys = new LinesAndConditionsWithUncoveredMetricKeys(
136 CoreMetrics.NEW_LINES_TO_COVER_KEY, CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY,
137 CoreMetrics.NEW_UNCOVERED_LINES_KEY, CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY);
138 String codeCoverageKey = CoreMetrics.NEW_COVERAGE_KEY;
139 String lineCoverageKey = CoreMetrics.NEW_LINE_COVERAGE_KEY;
140 String branchCoverageKey = CoreMetrics.NEW_BRANCH_COVERAGE_KEY;
142 verify_aggregates_variations(metricKeys, codeCoverageKey, lineCoverageKey, branchCoverageKey);
145 private void verify_aggregates_variations(LinesAndConditionsWithUncoveredMetricKeys metricKeys, String codeCoverageKey, String lineCoverageKey, String branchCoverageKey) {
146 treeRootHolder.setRoot(VIEWS_TREE);
148 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getLines(), createMeasure(3000d))
149 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getConditions(), createMeasure(300d))
150 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredLines(), createMeasure(30d))
151 .addRawMeasure(PROJECT_VIEW_1_REF, metricKeys.getUncoveredConditions(), createMeasure(9d))
152 // PROJECT_VIEW_2_REF
153 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getLines(), createMeasure(2000d))
154 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getConditions(), createMeasure(400d))
155 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredLines(), createMeasure(200d))
156 .addRawMeasure(PROJECT_VIEW_2_REF, metricKeys.getUncoveredConditions(), createMeasure(16d))
157 // PROJECT_VIEW_3_REF has no measure
158 // PROJECT_VIEW_4_REF
159 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getLines(), createMeasure(1000d))
160 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getConditions(), createMeasure(300d))
161 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredLines(), createMeasure(100d))
162 .addRawMeasure(PROJECT_VIEW_4_REF, metricKeys.getUncoveredConditions(), createMeasure(6d));
164 underTest.execute(new TestComputationStepContext());
166 assertNoAddedRawMeasureOnProjectViews();
168 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_1_REF))).contains(
169 entryOf(codeCoverageKey, createMeasure(98.8d)),
170 entryOf(lineCoverageKey, createMeasure(99d)),
171 entryOf(branchCoverageKey, createMeasure(97d)));
172 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_2_REF))).contains(
173 entryOf(codeCoverageKey, createMeasure(91d)),
174 entryOf(lineCoverageKey, createMeasure(90d)),
175 entryOf(branchCoverageKey, createMeasure(96d)));
176 assertThat(toEntries(measureRepository.getAddedRawMeasures(SUBVIEW_REF))).contains(
177 entryOf(codeCoverageKey, createMeasure(94.8d)),
178 entryOf(lineCoverageKey, createMeasure(94.5d)),
179 entryOf(branchCoverageKey, createMeasure(96.9d)));
180 assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(
181 entryOf(codeCoverageKey, createMeasure(94.8d)),
182 entryOf(lineCoverageKey, createMeasure(94.5d)),
183 entryOf(branchCoverageKey, createMeasure(96.9d)));
186 private static final class MetricKeys {
187 private final String newLinesToCover;
188 private final String newUncoveredLines;
189 private final String newConditionsToCover;
190 private final String newUncoveredConditions;
192 public MetricKeys(String newLinesToCover, String newUncoveredLines, String newConditionsToCover, String newUncoveredConditions) {
193 this.newLinesToCover = newLinesToCover;
194 this.newUncoveredLines = newUncoveredLines;
195 this.newConditionsToCover = newConditionsToCover;
196 this.newUncoveredConditions = newUncoveredConditions;
200 private static Measure createMeasure(Double expectedVariation) {
201 return newMeasureBuilder()
202 .setVariation(expectedVariation)
206 private void assertNoAddedRawMeasureOnProjectViews() {
207 assertNoAddedRawMeasures(PROJECT_VIEW_1_REF);
208 assertNoAddedRawMeasures(PROJECT_VIEW_2_REF);
209 assertNoAddedRawMeasures(PROJECT_VIEW_3_REF);
212 private void assertNoAddedRawMeasures(int componentRef) {
213 assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();