3 * Copyright (C) 2009-2021 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 com.google.common.collect.ImmutableList;
23 import com.google.common.collect.Iterables;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
33 import org.sonar.ce.task.projectanalysis.formula.Counter;
34 import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext;
35 import org.sonar.ce.task.projectanalysis.formula.CreateMeasureContext;
36 import org.sonar.ce.task.projectanalysis.formula.Formula;
37 import org.sonar.ce.task.projectanalysis.formula.FormulaExecutorComponentVisitor;
38 import org.sonar.ce.task.projectanalysis.formula.counter.IntValue;
39 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
40 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredVariationFormula;
41 import org.sonar.ce.task.projectanalysis.formula.coverage.SingleWithUncoveredMetricKeys;
42 import org.sonar.ce.task.projectanalysis.formula.coverage.SingleWithUncoveredVariationFormula;
43 import org.sonar.ce.task.projectanalysis.measure.Measure;
44 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
45 import org.sonar.ce.task.projectanalysis.metric.Metric;
46 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
47 import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
48 import org.sonar.ce.task.step.ComputationStep;
49 import org.sonar.core.util.CloseableIterator;
50 import org.sonar.scanner.protocol.output.ScannerReport;
52 import static java.lang.Math.min;
53 import static org.sonar.api.measures.CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY;
54 import static org.sonar.api.measures.CoreMetrics.NEW_LINES_TO_COVER_KEY;
55 import static org.sonar.api.measures.CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY;
56 import static org.sonar.api.measures.CoreMetrics.NEW_UNCOVERED_LINES_KEY;
57 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
60 * Computes measures related to the New Coverage. These measures do not have values, only variations.
62 public class NewCoverageMeasuresStep implements ComputationStep {
64 private static final List<Formula> FORMULAS = ImmutableList.of(
66 new NewCoverageFormula(),
67 new NewBranchCoverageFormula(),
68 new NewLineCoverageFormula());
70 private final TreeRootHolder treeRootHolder;
71 private final MetricRepository metricRepository;
72 private final MeasureRepository measureRepository;
73 private final NewLinesRepository newLinesRepository;
74 private final BatchReportReader reportReader;
76 public NewCoverageMeasuresStep(TreeRootHolder treeRootHolder,
77 MeasureRepository measureRepository, MetricRepository metricRepository, NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
78 this.treeRootHolder = treeRootHolder;
79 this.metricRepository = metricRepository;
80 this.measureRepository = measureRepository;
81 this.newLinesRepository = newLinesRepository;
82 this.reportReader = reportReader;
86 public void execute(ComputationStep.Context context) {
87 new PathAwareCrawler<>(
88 FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository)
90 Iterables.concat(NewLinesAndConditionsCoverageFormula.from(newLinesRepository, reportReader), FORMULAS)))
91 .visit(treeRootHolder.getRoot());
95 public String getDescription() {
96 return "Compute new coverage";
99 private static class NewCoverageFormula extends LinesAndConditionsWithUncoveredVariationFormula {
100 NewCoverageFormula() {
102 new LinesAndConditionsWithUncoveredMetricKeys(
103 NEW_LINES_TO_COVER_KEY, NEW_CONDITIONS_TO_COVER_KEY,
104 NEW_UNCOVERED_LINES_KEY, NEW_UNCOVERED_CONDITIONS_KEY),
105 CoreMetrics.NEW_COVERAGE_KEY);
109 private static class NewBranchCoverageFormula extends SingleWithUncoveredVariationFormula {
110 NewBranchCoverageFormula() {
112 new SingleWithUncoveredMetricKeys(NEW_CONDITIONS_TO_COVER_KEY, NEW_UNCOVERED_CONDITIONS_KEY),
113 CoreMetrics.NEW_BRANCH_COVERAGE_KEY);
117 private static class NewLineCoverageFormula extends SingleWithUncoveredVariationFormula {
118 NewLineCoverageFormula() {
120 new SingleWithUncoveredMetricKeys(NEW_LINES_TO_COVER_KEY, NEW_UNCOVERED_LINES_KEY),
121 CoreMetrics.NEW_LINE_COVERAGE_KEY);
125 public static class NewLinesAndConditionsCoverageFormula implements Formula<NewCoverageCounter> {
126 private final NewLinesRepository newLinesRepository;
127 private final BatchReportReader reportReader;
129 private NewLinesAndConditionsCoverageFormula(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
130 this.newLinesRepository = newLinesRepository;
131 this.reportReader = reportReader;
134 public static Iterable<Formula<NewCoverageCounter>> from(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
135 return Collections.singleton(new NewLinesAndConditionsCoverageFormula(newLinesRepository, reportReader));
139 public NewCoverageCounter createNewCounter() {
140 return new NewCoverageCounter(newLinesRepository, reportReader);
144 public Optional<Measure> createMeasure(NewCoverageCounter counter, CreateMeasureContext context) {
145 if (counter.hasNewCode()) {
146 int value = computeValueForMetric(counter, context.getMetric());
147 return Optional.of(newMeasureBuilder().setVariation(value).createNoValue());
149 return Optional.empty();
152 private static int computeValueForMetric(NewCoverageCounter counter, Metric metric) {
153 if (metric.getKey().equals(NEW_LINES_TO_COVER_KEY)) {
154 return counter.getNewLines();
156 if (metric.getKey().equals(NEW_UNCOVERED_LINES_KEY)) {
157 return counter.getNewLines() - counter.getNewCoveredLines();
159 if (metric.getKey().equals(NEW_CONDITIONS_TO_COVER_KEY)) {
160 return counter.getNewConditions();
162 if (metric.getKey().equals(NEW_UNCOVERED_CONDITIONS_KEY)) {
163 return counter.getNewConditions() - counter.getNewCoveredConditions();
165 throw new IllegalArgumentException("Unsupported metric " + metric.getKey());
169 public String[] getOutputMetricKeys() {
170 return new String[] {
171 NEW_LINES_TO_COVER_KEY,
172 NEW_UNCOVERED_LINES_KEY,
173 NEW_CONDITIONS_TO_COVER_KEY,
174 NEW_UNCOVERED_CONDITIONS_KEY
179 public static final class NewCoverageCounter implements Counter<NewCoverageCounter> {
180 private final IntValue newLines = new IntValue();
181 private final IntValue newCoveredLines = new IntValue();
182 private final IntValue newConditions = new IntValue();
183 private final IntValue newCoveredConditions = new IntValue();
184 private final NewLinesRepository newLinesRepository;
185 private final BatchReportReader reportReader;
187 NewCoverageCounter(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
188 this.newLinesRepository = newLinesRepository;
189 this.reportReader = reportReader;
193 public void aggregate(NewCoverageCounter counter) {
194 newLines.increment(counter.newLines);
195 newCoveredLines.increment(counter.newCoveredLines);
196 newConditions.increment(counter.newConditions);
197 newCoveredConditions.increment(counter.newCoveredConditions);
201 public void initialize(CounterInitializationContext context) {
202 Component component = context.getLeaf();
203 if (component.getType() != Component.Type.FILE) {
206 Optional<Set<Integer>> newLinesSet = newLinesRepository.getNewLines(component);
207 if (!newLinesSet.isPresent()) {
211 newLines.increment(0);
212 newCoveredLines.increment(0);
213 newConditions.increment(0);
214 newCoveredConditions.increment(0);
216 try (CloseableIterator<ScannerReport.LineCoverage> lineCoverage = reportReader.readComponentCoverage(component.getReportAttributes().getRef())) {
217 while (lineCoverage.hasNext()) {
218 final ScannerReport.LineCoverage line = lineCoverage.next();
219 int lineId = line.getLine();
220 if (newLinesSet.get().contains(lineId)) {
221 if (line.getHasHitsCase() == ScannerReport.LineCoverage.HasHitsCase.HITS) {
222 newLines.increment(1);
223 if (line.getHits()) {
224 newCoveredLines.increment(1);
227 if (line.getHasCoveredConditionsCase() == ScannerReport.LineCoverage.HasCoveredConditionsCase.COVERED_CONDITIONS) {
228 newConditions.increment(line.getConditions());
229 newCoveredConditions.increment(min(line.getCoveredConditions(), line.getConditions()));
237 boolean hasNewCode() {
238 return newLines.isSet();
242 return newLines.getValue();
245 int getNewCoveredLines() {
246 return newCoveredLines.getValue();
249 int getNewConditions() {
250 return newConditions.getValue();
253 int getNewCoveredConditions() {
254 return newCoveredConditions.getValue();