]> source.dussan.org Git - sonarqube.git/blob
01178f2275ed9aacb9ce8e0551cc3cff497ff156
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info 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.ce.task.projectanalysis.issue.filter;
21
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;
36
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;
43
44 public class IssueFilterTest {
45
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48
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");
52
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";
56
57   static final Component PROJECT = builder(Component.Type.PROJECT, 10).build();
58
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();
62
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);
66
67   @Rule
68   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
69
70   ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
71
72   @Test
73   public void accept_everything_when_no_filter_properties() {
74     IssueFilter underTest = newIssueFilter(new MapSettings());
75
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();
79   }
80
81   @Test
82   public void ignore_all() {
83     IssueFilter underTest = newIssueFilter(newSettings(asList("*", "**"), Collections.emptyList()));
84
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();
88   }
89
90   @Test
91   public void ignore_some_rule_and_component() {
92     IssueFilter underTest = newIssueFilter(newSettings(asList("xoo:x1", "**/xoo/File1*"), Collections.emptyList()));
93
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();
98   }
99
100   @Test
101   public void ignore_many_rules() {
102     IssueFilter underTest = newIssueFilter(newSettings(
103       asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*"),
104       Collections.emptyList()));
105
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();
110   }
111
112   @Test
113   public void include_all() {
114     IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("*", "**")));
115
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();
119   }
120
121   @Test
122   public void include_some_rule_and_component() {
123     IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("xoo:x1", "**/xoo/File1*")));
124
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();
130   }
131
132   @Test
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*")));
137
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();
143   }
144
145   @Test
146   public void include_many_rules() {
147     IssueFilter underTest = newIssueFilter(newSettings(
148       Collections.emptyList(),
149       asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*")));
150
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();
155   }
156
157   @Test
158   public void accept_project_issues() {
159     IssueFilter underTest = newIssueFilter(newSettings(
160       asList("xoo:x1", "**/xoo/File1*"),
161       asList("xoo:x1", "**/xoo/File1*")));
162
163     assertThat(underTest.accept(ISSUE_1, PROJECT)).isTrue();
164     assertThat(underTest.accept(ISSUE_2, PROJECT)).isTrue();
165   }
166
167   @Test
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");
171
172     newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.emptyList()));
173   }
174
175   @Test
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");
179
180     newIssueFilter(newSettings(Collections.emptyList(), asList("", "**")));
181   }
182
183   private IssueFilter newIssueFilter(MapSettings settings) {
184     when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig());
185     return new IssueFilter(settingsRepository);
186   }
187
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);
192     }
193     if (!inclusionsProperties.isEmpty()) {
194       addProperties(inclusionsProperties, "enforce", settings);
195     }
196     return settings;
197   }
198
199   private static void addProperties(List<String> properties, String property, Settings settings) {
200     if (!properties.isEmpty()) {
201       List<Integer> indexes = new ArrayList<>();
202       int index = 1;
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));
206         indexes.add(index);
207         index++;
208       }
209       settings.setProperty("sonar.issue." + property + ".multicriteria", Joiner.on(",").join(indexes));
210     }
211   }
212
213 }