]> source.dussan.org Git - sonarqube.git/blob
d9c9baec0bd1302147961a94fde40499cea1aa24
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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 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;
34
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;
43
44 /**
45  * Computes coverage measures on files and then aggregates them on higher components.
46  */
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());
56
57   private final TreeRootHolder treeRootHolder;
58   private final MetricRepository metricRepository;
59   private final MeasureRepository measureRepository;
60
61   public CoverageMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
62     this.treeRootHolder = treeRootHolder;
63     this.metricRepository = metricRepository;
64     this.measureRepository = measureRepository;
65   }
66
67   @Override
68   public void execute(ComputationStep.Context context) {
69     new PathAwareCrawler<>(
70       FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(COVERAGE_FORMULAS))
71         .visit(treeRootHolder.getReportTreeRoot());
72   }
73
74   private static class CodeCoverageFormula extends LinesAndConditionsWithUncoveredFormula {
75     public CodeCoverageFormula() {
76       super(
77         new LinesAndConditionsWithUncoveredMetricKeys(
78           LINES_TO_COVER_KEY, CONDITIONS_TO_COVER_KEY,
79           UNCOVERED_LINES_KEY, UNCOVERED_CONDITIONS_KEY),
80         COVERAGE_KEY);
81     }
82   }
83
84   private static class BranchCoverageFormula extends SingleWithUncoveredFormula {
85     public BranchCoverageFormula() {
86       super(
87         new SingleWithUncoveredMetricKeys(
88           CONDITIONS_TO_COVER_KEY, UNCOVERED_CONDITIONS_KEY),
89         BRANCH_COVERAGE_KEY);
90     }
91   }
92
93   private static class LineCoverageFormula extends SingleWithUncoveredFormula {
94     public LineCoverageFormula() {
95       super(
96         new SingleWithUncoveredMetricKeys(LINES_TO_COVER_KEY, UNCOVERED_LINES_KEY),
97         LINE_COVERAGE_KEY);
98     }
99   }
100
101   @Override
102   public String getDescription() {
103     return "Compute coverage measures";
104   }
105 }