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 com.google.common.collect.ImmutableList;
23 import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler;
24 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
25 import org.sonar.ce.task.projectanalysis.formula.Formula;
26 import org.sonar.ce.task.projectanalysis.formula.FormulaExecutorComponentVisitor;
27 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredFormula;
28 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
29 import org.sonar.ce.task.projectanalysis.formula.coverage.SingleWithUncoveredFormula;
30 import org.sonar.ce.task.projectanalysis.formula.coverage.SingleWithUncoveredMetricKeys;
31 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
32 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
33 import org.sonar.ce.task.step.ComputationStep;
35 import static org.sonar.api.measures.CoreMetrics.BRANCH_COVERAGE_KEY;
36 import static org.sonar.api.measures.CoreMetrics.CONDITIONS_TO_COVER_KEY;
37 import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
38 import static org.sonar.api.measures.CoreMetrics.LINES_TO_COVER_KEY;
39 import static org.sonar.api.measures.CoreMetrics.LINE_COVERAGE_KEY;
40 import static org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS_KEY;
41 import static org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES_KEY;
42 import static org.sonar.ce.task.projectanalysis.formula.SumFormula.createIntSumFormula;
45 * Computes coverage measures on files and then aggregates them on higher components.
47 public class CoverageMeasuresStep implements ComputationStep {
48 private static final ImmutableList<Formula> COVERAGE_FORMULAS = ImmutableList.of(
49 createIntSumFormula(LINES_TO_COVER_KEY),
50 createIntSumFormula(UNCOVERED_LINES_KEY),
51 createIntSumFormula(CONDITIONS_TO_COVER_KEY),
52 createIntSumFormula(UNCOVERED_CONDITIONS_KEY),
53 new CodeCoverageFormula(),
54 new BranchCoverageFormula(),
55 new LineCoverageFormula());
57 private final TreeRootHolder treeRootHolder;
58 private final MetricRepository metricRepository;
59 private final MeasureRepository measureRepository;
61 public CoverageMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
62 this.treeRootHolder = treeRootHolder;
63 this.metricRepository = metricRepository;
64 this.measureRepository = measureRepository;
68 public void execute(ComputationStep.Context context) {
69 new PathAwareCrawler<>(
70 FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(COVERAGE_FORMULAS))
71 .visit(treeRootHolder.getReportTreeRoot());
74 private static class CodeCoverageFormula extends LinesAndConditionsWithUncoveredFormula {
75 public CodeCoverageFormula() {
77 new LinesAndConditionsWithUncoveredMetricKeys(
78 LINES_TO_COVER_KEY, CONDITIONS_TO_COVER_KEY,
79 UNCOVERED_LINES_KEY, UNCOVERED_CONDITIONS_KEY),
84 private static class BranchCoverageFormula extends SingleWithUncoveredFormula {
85 public BranchCoverageFormula() {
87 new SingleWithUncoveredMetricKeys(
88 CONDITIONS_TO_COVER_KEY, UNCOVERED_CONDITIONS_KEY),
93 private static class LineCoverageFormula extends SingleWithUncoveredFormula {
94 public LineCoverageFormula() {
96 new SingleWithUncoveredMetricKeys(LINES_TO_COVER_KEY, UNCOVERED_LINES_KEY),
102 public String getDescription() {
103 return "Compute coverage measures";