3 * Copyright (C) 2009-2023 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.issue.commonrule;
22 import java.util.Optional;
23 import org.sonar.ce.task.projectanalysis.component.Component;
24 import org.sonar.ce.task.projectanalysis.measure.Measure;
25 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
26 import org.sonar.ce.task.projectanalysis.metric.Metric;
27 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
28 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
30 public abstract class AbstractCoverageRule extends CommonRule {
32 private final MeasureRepository measureRepository;
33 private final Metric coverageMetric;
34 private final Metric uncoveredMetric;
35 private final Metric toCoverMetric;
36 private final String minPropertyKey;
38 public AbstractCoverageRule(ActiveRulesHolder activeRulesHolder, String ruleKey, String minPropertyKey,
39 MeasureRepository measureRepository,
40 Metric coverageMetric, Metric uncoveredMetric, Metric toCoverMetric) {
41 super(activeRulesHolder, ruleKey);
42 this.minPropertyKey = minPropertyKey;
43 this.measureRepository = measureRepository;
44 this.coverageMetric = coverageMetric;
45 this.uncoveredMetric = uncoveredMetric;
46 this.toCoverMetric = toCoverMetric;
50 protected CommonRuleIssue doProcessFile(Component file, ActiveRule activeRule) {
51 Optional<Measure> coverageMeasure = measureRepository.getRawMeasure(file, coverageMetric);
52 if (!file.getFileAttributes().isUnitTest() && coverageMeasure.isPresent()) {
53 double coverage = coverageMeasure.get().getDoubleValue();
54 double minimumCoverage = getMinDensityParam(activeRule, minPropertyKey);
55 if (coverage < minimumCoverage) {
56 return generateIssue(file, minimumCoverage);
62 private CommonRuleIssue generateIssue(Component file, double minimumCoverage) {
63 Optional<Measure> uncoveredMeasure = measureRepository.getRawMeasure(file, uncoveredMetric);
64 Optional<Measure> toCoverMeasure = measureRepository.getRawMeasure(file, toCoverMetric);
65 double uncovered = uncoveredMeasure.isPresent() ? uncoveredMeasure.get().getIntValue() : 0.0;
66 double toCover = toCoverMeasure.isPresent() ? toCoverMeasure.get().getIntValue() : 0.0;
68 // effort to fix is the number of lines/conditions to cover for reaching threshold
69 int effortToFix = (int) Math.ceil((toCover * minimumCoverage / 100) - (toCover - uncovered));
71 return new CommonRuleIssue(effortToFix, formatMessage(effortToFix, minimumCoverage));
74 protected abstract String formatMessage(int effortToFix, double minCoverage);