3 * Copyright (C) 2009-2017 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.server.computation.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.core.issue.DefaultIssue;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
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.server.computation.task.projectanalysis.component.Component.Type.FILE;
42 import static org.sonar.server.computation.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").setPath(PATH1).build();
60 static final Component COMPONENT_2 = builder(FILE, 2).setKey("File2").setPath(PATH2).build();
61 static final Component COMPONENT_3 = builder(FILE, 3).setKey("File3").setPath(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 SettingsRepository settingsRepository = mock(SettingsRepository.class);
73 public void accept_everything_when_no_filter_properties() throws Exception {
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() throws Exception {
83 IssueFilter underTest = newIssueFilter(newSettings(asList("*", "**"), Collections.<String>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() throws Exception {
92 IssueFilter underTest = newIssueFilter(newSettings(asList("xoo:x1", "**/xoo/File1*"), Collections.<String>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() throws Exception {
102 IssueFilter underTest = newIssueFilter(newSettings(
103 asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*"),
104 Collections.<String>emptyList())
107 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
108 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isTrue();
109 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isFalse();
110 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
114 public void include_all() throws Exception {
115 IssueFilter underTest = newIssueFilter(newSettings(Collections.<String>emptyList(), asList("*", "**")));
117 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
118 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
119 assertThat(underTest.accept(ISSUE_3, COMPONENT_1)).isTrue();
123 public void include_some_rule_and_component() throws Exception {
124 IssueFilter underTest = newIssueFilter(newSettings(Collections.<String>emptyList(), asList("xoo:x1", "**/xoo/File1*")));
126 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
127 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
128 // Issues on other rule are accepted
129 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
130 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
134 public void ignore_and_include_same_rule_and_component() throws Exception {
135 IssueFilter underTest = newIssueFilter(newSettings(
136 asList("xoo:x1", "**/xoo/File1*"),
137 asList("xoo:x1", "**/xoo/File1*")));
139 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
140 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
141 // Issues on other rule are accepted
142 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
143 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
147 public void include_many_rules() throws Exception {
148 IssueFilter underTest = newIssueFilter(newSettings(
149 Collections.<String>emptyList(),
150 asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*")
153 assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
154 assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
155 assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
156 assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isFalse();
160 public void accept_project_issues() throws Exception {
161 IssueFilter underTest = newIssueFilter(newSettings(
162 asList("xoo:x1", "**/xoo/File1*"),
163 asList("xoo:x1", "**/xoo/File1*")));
165 assertThat(underTest.accept(ISSUE_1, PROJECT)).isTrue();
166 assertThat(underTest.accept(ISSUE_2, PROJECT)).isTrue();
170 public void fail_when_only_rule_key_parameter() throws Exception {
171 expectedException.expect(IllegalArgumentException.class);
172 expectedException.expectMessage("File path pattern cannot be empty. Please check 'sonar.issue.ignore.multicriteria' settings");
174 newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.<String>emptyList()));
178 public void fail_when_only_path_parameter() throws Exception {
179 expectedException.expect(IllegalArgumentException.class);
180 expectedException.expectMessage("Rule key pattern cannot be empty. Please check 'sonar.issue.enforce.multicriteria' settings");
182 newIssueFilter(newSettings(Collections.<String>emptyList(), asList("", "**")));
185 private IssueFilter newIssueFilter(Settings settings) {
186 when(settingsRepository.getSettings(PROJECT)).thenReturn(settings);
187 return new IssueFilter(treeRootHolder, settingsRepository);
190 private static Settings newSettings(List<String> exclusionsProperties, List<String> inclusionsProperties) {
191 Settings settings = new MapSettings();
192 if (!exclusionsProperties.isEmpty()) {
193 addProperties(exclusionsProperties, "ignore", settings);
195 if (!inclusionsProperties.isEmpty()) {
196 addProperties(inclusionsProperties, "enforce", settings);
201 private static void addProperties(List<String> properties, String property, Settings settings) {
202 if (!properties.isEmpty()) {
203 List<Integer> indexes = new ArrayList<>();
205 for (int i = 0; i < properties.size(); i += 2) {
206 settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".ruleKey", properties.get(i));
207 settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".resourceKey", properties.get(i + 1));
211 settings.setProperty("sonar.issue." + property + ".multicriteria", Joiner.on(",").join(indexes));