]> source.dussan.org Git - sonarqube.git/blob
2b357840099e0e736f08471eecebd802b0544c11
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.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.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;
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.server.computation.task.projectanalysis.component.Component.Type.FILE;
42 import static org.sonar.server.computation.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").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();
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   SettingsRepository settingsRepository = mock(SettingsRepository.class);
71
72   @Test
73   public void accept_everything_when_no_filter_properties() throws Exception {
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() throws Exception {
83     IssueFilter underTest = newIssueFilter(newSettings(asList("*", "**"), Collections.<String>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() throws Exception {
92     IssueFilter underTest = newIssueFilter(newSettings(asList("xoo:x1", "**/xoo/File1*"), Collections.<String>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() throws Exception {
102     IssueFilter underTest = newIssueFilter(newSettings(
103       asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*"),
104       Collections.<String>emptyList())
105     );
106
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();
111   }
112
113   @Test
114   public void include_all() throws Exception {
115     IssueFilter underTest = newIssueFilter(newSettings(Collections.<String>emptyList(), asList("*", "**")));
116
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();
120   }
121
122   @Test
123   public void include_some_rule_and_component() throws Exception {
124     IssueFilter underTest = newIssueFilter(newSettings(Collections.<String>emptyList(), asList("xoo:x1", "**/xoo/File1*")));
125
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();
131   }
132
133   @Test
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*")));
138
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();
144   }
145
146   @Test
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*")
151     ));
152
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();
157   }
158
159   @Test
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*")));
164
165     assertThat(underTest.accept(ISSUE_1, PROJECT)).isTrue();
166     assertThat(underTest.accept(ISSUE_2, PROJECT)).isTrue();
167   }
168
169   @Test
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");
173
174     newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.<String>emptyList()));
175   }
176
177   @Test
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");
181
182     newIssueFilter(newSettings(Collections.<String>emptyList(), asList("", "**")));
183   }
184
185   private IssueFilter newIssueFilter(Settings settings) {
186     when(settingsRepository.getSettings(PROJECT)).thenReturn(settings);
187     return new IssueFilter(treeRootHolder, settingsRepository);
188   }
189
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);
194     }
195     if (!inclusionsProperties.isEmpty()) {
196       addProperties(inclusionsProperties, "enforce", settings);
197     }
198     return settings;
199   }
200
201   private static void addProperties(List<String> properties, String property, Settings settings) {
202     if (!properties.isEmpty()) {
203       List<Integer> indexes = new ArrayList<>();
204       int index = 1;
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));
208         indexes.add(index);
209         index++;
210       }
211       settings.setProperty("sonar.issue." + property + ".multicriteria", Joiner.on(",").join(indexes));
212     }
213   }
214
215 }