3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.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.core.issue.DefaultIssue;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
32 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
34 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
35 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
36 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
37 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRule;
38 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
43 public abstract class CoverageRuleTest {
45 static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
46 .setFileAttributes(new FileAttributes(false, "java", 1))
50 public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule();
53 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
54 .add(CoreMetrics.LINE_COVERAGE)
55 .add(CoreMetrics.LINES_TO_COVER)
56 .add(CoreMetrics.UNCOVERED_LINES)
57 .add(CoreMetrics.BRANCH_COVERAGE)
58 .add(CoreMetrics.CONDITIONS_TO_COVER)
59 .add(CoreMetrics.UNCOVERED_CONDITIONS);
62 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
65 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
67 CommonRule underTest = createRule();
71 treeRootHolder.setRoot(DUMB_PROJECT);
74 protected abstract CommonRule createRule();
76 protected abstract RuleKey getRuleKey();
78 protected abstract String getMinPropertyKey();
80 protected abstract String getCoverageMetricKey();
82 protected abstract String getToCoverMetricKey();
84 protected abstract String getUncoveredMetricKey();
87 public void no_issue_if_enough_coverage() {
88 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65")));
89 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1));
91 DefaultIssue issue = underTest.processFile(FILE, "java");
93 assertThat(issue).isNull();
97 public void issue_if_coverage_is_too_low() {
98 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65")));
99 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
100 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40));
101 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50));
103 DefaultIssue issue = underTest.processFile(FILE, "java");
105 assertThat(issue.ruleKey()).isEqualTo(getRuleKey());
106 assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
108 assertThat(issue.effortToFix()).isEqualTo(23.0);
109 assertThat(issue.message()).isEqualTo(getExpectedIssueMessage());
112 protected abstract String getExpectedIssueMessage();
115 public void no_issue_if_coverage_is_not_set() {
116 activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65")));
118 DefaultIssue issue = underTest.processFile(FILE, "java");
120 assertThat(issue).isNull();
124 public void ignored_if_rule_is_deactivated() {
125 // coverage is too low, but rule is not activated
126 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
128 DefaultIssue issue = underTest.processFile(FILE, "java");
130 assertThat(issue).isNull();