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.Rule;
24 import org.junit.Test;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.api.rule.Severity;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
30 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
31 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
32 import org.sonar.ce.task.projectanalysis.measure.Measure;
33 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
34 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
35 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
36 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
37 import org.sonar.core.issue.DefaultIssue;
38 import org.sonar.server.rule.CommonRuleKeys;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
44 public class CommentDensityRuleTest {
46 private static final String PLUGIN_KEY = "java";
47 private static final String QP_KEY = "qp1";
49 static RuleKey RULE_KEY = RuleKey.of(CommonRuleKeys.commonRepositoryForLang(PLUGIN_KEY), CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY);
51 static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
52 .setFileAttributes(new FileAttributes(false, PLUGIN_KEY, 1))
55 static ReportComponent TEST_FILE = ReportComponent.builder(Component.Type.FILE, 1)
56 .setFileAttributes(new FileAttributes(true, PLUGIN_KEY, 1))
61 public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule();
64 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
65 .add(CoreMetrics.COMMENT_LINES_DENSITY)
66 .add(CoreMetrics.COMMENT_LINES)
67 .add(CoreMetrics.NCLOC);
70 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(DUMB_PROJECT);
73 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
75 CommentDensityRule underTest = new CommentDensityRule(activeRuleHolder, measureRepository, metricRepository);
78 public void no_issues_if_enough_comments() {
79 activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, PLUGIN_KEY, QP_KEY));
80 measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(90.0, 1));
82 DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY);
84 assertThat(issue).isNull();
88 public void issue_if_not_enough_comments() {
89 prepareForIssue("25", FILE, 10.0, 40, 360);
91 DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY);
93 assertThat(issue.ruleKey()).isEqualTo(RULE_KEY);
94 assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
95 // min_comments = (min_percent * ncloc) / (1 - min_percent)
96 // -> threshold of 25% for 360 ncloc is 120 comment lines. 40 are already written.
97 assertThat(issue.gap()).isEqualTo(120.0 - 40.0);
98 assertThat(issue.message()).isEqualTo("80 more comment lines need to be written to reach the minimum threshold of 25.0% comment density.");
102 public void no_issues_on_tests() {
103 prepareForIssue("25", TEST_FILE, 10.0, 40, 360);
105 DefaultIssue issue = underTest.processFile(TEST_FILE, PLUGIN_KEY);
107 assertThat(issue).isNull();
111 public void issue_if_not_enough_comments__test_ceil() {
112 prepareForIssue("25", FILE, 0.0, 0, 1);
114 DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY);
116 assertThat(issue.ruleKey()).isEqualTo(RULE_KEY);
117 assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
118 // 1 ncloc requires 1 comment line to reach 25% of comment density
119 assertThat(issue.gap()).isEqualTo(1.0);
120 assertThat(issue.message()).isEqualTo("1 more comment lines need to be written to reach the minimum threshold of 25.0% comment density.");
127 public void fail_if_min_density_is_100() {
128 assertThatThrownBy(() -> {
129 prepareForIssue("100", FILE, 0.0, 0, 1);
130 underTest.processFile(FILE, PLUGIN_KEY);
132 .isInstanceOf(IllegalStateException.class)
133 .hasMessage("Minimum density of rule [common-java:InsufficientCommentDensity] is incorrect. Got [100] but must be strictly less than 100.");
136 private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) {
137 activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, PLUGIN_KEY, QP_KEY));
138 measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(commentLineDensity, 1));
139 measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_KEY, Measure.newMeasureBuilder().create(commentLines));
140 measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.NCLOC_KEY, Measure.newMeasureBuilder().create(ncloc));