]> source.dussan.org Git - sonarqube.git/blob
639d078ff082fa01fc8dc2925fdf0c0495945348
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
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;
27 import java.util.Set;
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;
51
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;
58
59 /**
60  * Computes measures related to the New Coverage. These measures do not have values, only variations.
61  */
62 public class NewCoverageMeasuresStep implements ComputationStep {
63
64   private static final List<Formula> FORMULAS = ImmutableList.of(
65     // UT coverage
66     new NewCoverageFormula(),
67     new NewBranchCoverageFormula(),
68     new NewLineCoverageFormula());
69
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;
75
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;
83   }
84
85   @Override
86   public void execute(ComputationStep.Context context) {
87     new PathAwareCrawler<>(
88       FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository)
89         .buildFor(
90           Iterables.concat(NewLinesAndConditionsCoverageFormula.from(newLinesRepository, reportReader), FORMULAS)))
91             .visit(treeRootHolder.getRoot());
92   }
93
94   @Override
95   public String getDescription() {
96     return "Compute new coverage";
97   }
98
99   private static class NewCoverageFormula extends LinesAndConditionsWithUncoveredVariationFormula {
100     NewCoverageFormula() {
101       super(
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);
106     }
107   }
108
109   private static class NewBranchCoverageFormula extends SingleWithUncoveredVariationFormula {
110     NewBranchCoverageFormula() {
111       super(
112         new SingleWithUncoveredMetricKeys(NEW_CONDITIONS_TO_COVER_KEY, NEW_UNCOVERED_CONDITIONS_KEY),
113         CoreMetrics.NEW_BRANCH_COVERAGE_KEY);
114     }
115   }
116
117   private static class NewLineCoverageFormula extends SingleWithUncoveredVariationFormula {
118     NewLineCoverageFormula() {
119       super(
120         new SingleWithUncoveredMetricKeys(NEW_LINES_TO_COVER_KEY, NEW_UNCOVERED_LINES_KEY),
121         CoreMetrics.NEW_LINE_COVERAGE_KEY);
122     }
123   }
124
125   public static class NewLinesAndConditionsCoverageFormula implements Formula<NewCoverageCounter> {
126     private final NewLinesRepository newLinesRepository;
127     private final BatchReportReader reportReader;
128
129     private NewLinesAndConditionsCoverageFormula(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
130       this.newLinesRepository = newLinesRepository;
131       this.reportReader = reportReader;
132     }
133
134     public static Iterable<Formula<NewCoverageCounter>> from(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
135       return Collections.singleton(new NewLinesAndConditionsCoverageFormula(newLinesRepository, reportReader));
136     }
137
138     @Override
139     public NewCoverageCounter createNewCounter() {
140       return new NewCoverageCounter(newLinesRepository, reportReader);
141     }
142
143     @Override
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());
148       }
149       return Optional.empty();
150     }
151
152     private static int computeValueForMetric(NewCoverageCounter counter, Metric metric) {
153       if (metric.getKey().equals(NEW_LINES_TO_COVER_KEY)) {
154         return counter.getNewLines();
155       }
156       if (metric.getKey().equals(NEW_UNCOVERED_LINES_KEY)) {
157         return counter.getNewLines() - counter.getNewCoveredLines();
158       }
159       if (metric.getKey().equals(NEW_CONDITIONS_TO_COVER_KEY)) {
160         return counter.getNewConditions();
161       }
162       if (metric.getKey().equals(NEW_UNCOVERED_CONDITIONS_KEY)) {
163         return counter.getNewConditions() - counter.getNewCoveredConditions();
164       }
165       throw new IllegalArgumentException("Unsupported metric " + metric.getKey());
166     }
167
168     @Override
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
175       };
176     }
177   }
178
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;
186
187     NewCoverageCounter(NewLinesRepository newLinesRepository, BatchReportReader reportReader) {
188       this.newLinesRepository = newLinesRepository;
189       this.reportReader = reportReader;
190     }
191
192     @Override
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);
198     }
199
200     @Override
201     public void initialize(CounterInitializationContext context) {
202       Component component = context.getLeaf();
203       if (component.getType() != Component.Type.FILE) {
204         return;
205       }
206       Optional<Set<Integer>> newLinesSet = newLinesRepository.getNewLines(component);
207       if (!newLinesSet.isPresent()) {
208         return;
209       }
210
211       newLines.increment(0);
212       newCoveredLines.increment(0);
213       newConditions.increment(0);
214       newCoveredConditions.increment(0);
215
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);
225               }
226             }
227             if (line.getHasCoveredConditionsCase() == ScannerReport.LineCoverage.HasCoveredConditionsCase.COVERED_CONDITIONS) {
228               newConditions.increment(line.getConditions());
229               newCoveredConditions.increment(min(line.getCoveredConditions(), line.getConditions()));
230             }
231           }
232         }
233       }
234
235     }
236
237     boolean hasNewCode() {
238       return newLines.isSet();
239     }
240
241     int getNewLines() {
242       return newLines.getValue();
243     }
244
245     int getNewCoveredLines() {
246       return newCoveredLines.getValue();
247     }
248
249     int getNewConditions() {
250       return newConditions.getValue();
251     }
252
253     int getNewCoveredConditions() {
254       return newCoveredConditions.getValue();
255     }
256   }
257
258 }