3 * Copyright (C) 2009-2019 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.filter;
22 import com.google.common.base.Joiner;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.config.Settings;
30 import org.sonar.api.config.internal.MapSettings;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.ce.task.projectanalysis.component.Component;
33 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
34 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
35 import org.sonar.core.issue.DefaultIssue;
37 import static java.util.Arrays.asList;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
42 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
44 public class IssueFilterTest {
47 public ExpectedException expectedException = ExpectedException.none();
49 static final RuleKey XOO_X1 = RuleKey.of("xoo", "x1");
50 static final RuleKey XOO_X2 = RuleKey.of("xoo", "x2");
51 static final RuleKey XOO_X3 = RuleKey.of("xoo", "x3");
53 static final String PATH1 = "src/main/xoo/File1.xoo";
54 static final String PATH2 = "src/main/xoo/File2.xoo";
55 static final String PATH3 = "src/main/xoo/File3.xoo";
57 static final Component PROJECT = builder(Component.Type.PROJECT, 10).build();
59 static final Component COMPONENT_1 = builder(FILE, 1).setKey("File1").setName(PATH1).build();
60 static final Component COMPONENT_2 = builder(FILE, 2).setKey("File2").setName(PATH2).build();
61 static final Component COMPONENT_3 = builder(FILE, 3).setKey("File3").setName(PATH3).build();
63 static final DefaultIssue ISSUE_1 = new DefaultIssue().setRuleKey(XOO_X1);
64 static final DefaultIssue ISSUE_2 = new DefaultIssue().setRuleKey(XOO_X2);
65 static final DefaultIssue ISSUE_3 = new DefaultIssue().setRuleKey(XOO_X3);
68 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
70 ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
73 public void accept_everything_when_no_filter_properties() {
74 IssueFilter underTest = newIssueFilter(new MapSettings());
76 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
77 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
78 assertThat(underTest.accept(ISSUE_3, COMPONENT_3)).isTrue();
82 public void ignore_all() {
83 IssueFilter underTest = newIssueFilter(newSettings(asList("*", "**"), Collections.emptyList()));
85 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
86 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isFalse();
87 assertThat(underTest.accept(ISSUE_3, COMPONENT_1)).isFalse();
91 public void ignore_some_rule_and_component() {
92 IssueFilter underTest = newIssueFilter(newSettings(asList("xoo:x1", "**/xoo/File1*"), Collections.emptyList()));
94 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
95 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isTrue();
96 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
97 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
101 public void ignore_many_rules() {
102 IssueFilter underTest = newIssueFilter(newSettings(
103 asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*"),
104 Collections.emptyList()));
106 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
107 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isTrue();
108 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isFalse();
109 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
113 public void include_all() {
114 IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("*", "**")));
116 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
117 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
118 assertThat(underTest.accept(ISSUE_3, COMPONENT_1)).isTrue();
122 public void include_some_rule_and_component() {
123 IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("xoo:x1", "**/xoo/File1*")));
125 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
126 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
127 // Issues on other rule are accepted
128 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
129 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
133 public void ignore_and_include_same_rule_and_component() {
134 IssueFilter underTest = newIssueFilter(newSettings(
135 asList("xoo:x1", "**/xoo/File1*"),
136 asList("xoo:x1", "**/xoo/File1*")));
138 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
139 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
140 // Issues on other rule are accepted
141 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
142 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
146 public void include_many_rules() {
147 IssueFilter underTest = newIssueFilter(newSettings(
148 Collections.emptyList(),
149 asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*")));
151 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
152 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
153 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
154 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isFalse();
158 public void accept_project_issues() {
159 IssueFilter underTest = newIssueFilter(newSettings(
160 asList("xoo:x1", "**/xoo/File1*"),
161 asList("xoo:x1", "**/xoo/File1*")));
163 assertThat(underTest.accept(ISSUE_1, PROJECT)).isTrue();
164 assertThat(underTest.accept(ISSUE_2, PROJECT)).isTrue();
168 public void fail_when_only_rule_key_parameter() {
169 expectedException.expect(IllegalArgumentException.class);
170 expectedException.expectMessage("File path pattern cannot be empty. Please check 'sonar.issue.ignore.multicriteria' settings");
172 newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.emptyList()));
176 public void fail_when_only_path_parameter() {
177 expectedException.expect(IllegalArgumentException.class);
178 expectedException.expectMessage("Rule key pattern cannot be empty. Please check 'sonar.issue.enforce.multicriteria' settings");
180 newIssueFilter(newSettings(Collections.emptyList(), asList("", "**")));
183 private IssueFilter newIssueFilter(MapSettings settings) {
184 when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig());
185 return new IssueFilter(settingsRepository);
188 private static MapSettings newSettings(List<String> exclusionsProperties, List<String> inclusionsProperties) {
189 MapSettings settings = new MapSettings();
190 if (!exclusionsProperties.isEmpty()) {
191 addProperties(exclusionsProperties, "ignore", settings);
193 if (!inclusionsProperties.isEmpty()) {
194 addProperties(inclusionsProperties, "enforce", settings);
199 private static void addProperties(List<String> properties, String property, Settings settings) {
200 if (!properties.isEmpty()) {
201 List<Integer> indexes = new ArrayList<>();
203 for (int i = 0; i < properties.size(); i += 2) {
204 settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".ruleKey", properties.get(i));
205 settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".resourceKey", properties.get(i + 1));
209 settings.setProperty("sonar.issue." + property + ".multicriteria", Joiner.on(",").join(indexes));