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 com.google.common.collect.ImmutableMap;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
31 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
33 import org.sonar.ce.task.projectanalysis.measure.Measure;
34 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
35 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
36 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
37 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
38 import org.sonar.core.issue.DefaultIssue;
40 import static org.assertj.core.api.Assertions.assertThat;
42 public abstract class CoverageRuleTest {
44 private static final String PLUGIN_KEY = "java";
45 private static final String QP_KEY = "qp1";
47 static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
48 .setFileAttributes(new FileAttributes(false, "java", 1))
52 public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule();
55 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
56 .add(CoreMetrics.LINE_COVERAGE)
57 .add(CoreMetrics.LINES_TO_COVER)
58 .add(CoreMetrics.UNCOVERED_LINES)
59 .add(CoreMetrics.BRANCH_COVERAGE)
60 .add(CoreMetrics.CONDITIONS_TO_COVER)
61 .add(CoreMetrics.UNCOVERED_CONDITIONS);
64 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
67 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
69 CommonRule underTest = createRule();
73 treeRootHolder.setRoot(ReportComponent.DUMB_PROJECT);
76 protected abstract CommonRule createRule();
78 protected abstract RuleKey getRuleKey();
80 protected abstract String getMinPropertyKey();
82 protected abstract String getCoverageMetricKey();
84 protected abstract String getToCoverMetricKey();
86 protected abstract String getUncoveredMetricKey();
89 public void no_issue_if_enough_coverage() {
90 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
91 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1));
93 DefaultIssue issue = underTest.processFile(FILE, "java");
95 assertThat(issue).isNull();
99 public void issue_if_coverage_is_too_low() {
100 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
101 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
102 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40));
103 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50));
105 DefaultIssue issue = underTest.processFile(FILE, "java");
107 assertThat(issue.ruleKey()).isEqualTo(getRuleKey());
108 assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
110 assertThat(issue.gap()).isEqualTo(23.0);
111 assertThat(issue.message()).isEqualTo(getExpectedIssueMessage());
114 protected abstract String getExpectedIssueMessage();
117 public void no_issue_if_coverage_is_not_set() {
118 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
120 DefaultIssue issue = underTest.processFile(FILE, "java");
122 assertThat(issue).isNull();
126 public void ignored_if_rule_is_deactivated() {
127 // coverage is too low, but rule is not activated
128 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
130 DefaultIssue issue = underTest.processFile(FILE, "java");
132 assertThat(issue).isNull();