]> source.dussan.org Git - sonarqube.git/blob
e6caaed3a0567b1e0598544a2489af6b4a88ee30
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.issue.commonrule;
21
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;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
42
43 public abstract class CoverageRuleTest {
44
45   static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
46     .setFileAttributes(new FileAttributes(false, "java", 1))
47     .build();
48
49   @Rule
50   public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule();
51
52   @Rule
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);
60
61   @Rule
62   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
63
64   @Rule
65   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
66
67   CommonRule underTest = createRule();
68
69   @Before
70   public void setUp() {
71     treeRootHolder.setRoot(DUMB_PROJECT);
72   }
73
74   protected abstract CommonRule createRule();
75
76   protected abstract RuleKey getRuleKey();
77
78   protected abstract String getMinPropertyKey();
79
80   protected abstract String getCoverageMetricKey();
81
82   protected abstract String getToCoverMetricKey();
83
84   protected abstract String getUncoveredMetricKey();
85
86   @Test
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));
90
91     DefaultIssue issue = underTest.processFile(FILE, "java");
92
93     assertThat(issue).isNull();
94   }
95
96   @Test
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));
102
103     DefaultIssue issue = underTest.processFile(FILE, "java");
104
105     assertThat(issue.ruleKey()).isEqualTo(getRuleKey());
106     assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
107     // FIXME explain
108     assertThat(issue.effortToFix()).isEqualTo(23.0);
109     assertThat(issue.message()).isEqualTo(getExpectedIssueMessage());
110   }
111
112   protected abstract String getExpectedIssueMessage();
113
114   @Test
115   public void no_issue_if_coverage_is_not_set() {
116     activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65")));
117
118     DefaultIssue issue = underTest.processFile(FILE, "java");
119
120     assertThat(issue).isNull();
121   }
122
123   @Test
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));
127
128     DefaultIssue issue = underTest.processFile(FILE, "java");
129
130     assertThat(issue).isNull();
131   }
132 }