You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssueFilterTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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. import com.google.common.base.Joiner;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.sonar.api.config.internal.Settings;
  29. import org.sonar.api.config.internal.MapSettings;
  30. import org.sonar.api.rule.RuleKey;
  31. import org.sonar.ce.task.projectanalysis.component.Component;
  32. import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
  33. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  34. import org.sonar.core.issue.DefaultIssue;
  35. import static java.util.Arrays.asList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  40. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  41. public class IssueFilterTest {
  42. @Rule
  43. public ExpectedException expectedException = ExpectedException.none();
  44. static final RuleKey XOO_X1 = RuleKey.of("xoo", "x1");
  45. static final RuleKey XOO_X2 = RuleKey.of("xoo", "x2");
  46. static final RuleKey XOO_X3 = RuleKey.of("xoo", "x3");
  47. static final String PATH1 = "src/main/xoo/File1.xoo";
  48. static final String PATH2 = "src/main/xoo/File2.xoo";
  49. static final String PATH3 = "src/main/xoo/File3.xoo";
  50. static final Component PROJECT = builder(Component.Type.PROJECT, 10).build();
  51. static final Component COMPONENT_1 = builder(FILE, 1).setKey("File1").setName(PATH1).build();
  52. static final Component COMPONENT_2 = builder(FILE, 2).setKey("File2").setName(PATH2).build();
  53. static final Component COMPONENT_3 = builder(FILE, 3).setKey("File3").setName(PATH3).build();
  54. static final DefaultIssue ISSUE_1 = new DefaultIssue().setRuleKey(XOO_X1);
  55. static final DefaultIssue ISSUE_2 = new DefaultIssue().setRuleKey(XOO_X2);
  56. static final DefaultIssue ISSUE_3 = new DefaultIssue().setRuleKey(XOO_X3);
  57. @Rule
  58. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
  59. ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
  60. @Test
  61. public void accept_everything_when_no_filter_properties() {
  62. IssueFilter underTest = newIssueFilter(new MapSettings());
  63. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
  64. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
  65. assertThat(underTest.accept(ISSUE_3, COMPONENT_3)).isTrue();
  66. }
  67. @Test
  68. public void ignore_all() {
  69. IssueFilter underTest = newIssueFilter(newSettings(asList("*", "**"), Collections.emptyList()));
  70. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
  71. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isFalse();
  72. assertThat(underTest.accept(ISSUE_3, COMPONENT_1)).isFalse();
  73. }
  74. @Test
  75. public void ignore_some_rule_and_component() {
  76. IssueFilter underTest = newIssueFilter(newSettings(asList("xoo:x1", "**/xoo/File1*"), Collections.emptyList()));
  77. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
  78. assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isTrue();
  79. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
  80. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
  81. }
  82. @Test
  83. public void ignore_many_rules() {
  84. IssueFilter underTest = newIssueFilter(newSettings(
  85. asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*"),
  86. Collections.emptyList()));
  87. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
  88. assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isTrue();
  89. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isFalse();
  90. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
  91. }
  92. @Test
  93. public void include_all() {
  94. IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("*", "**")));
  95. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
  96. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
  97. assertThat(underTest.accept(ISSUE_3, COMPONENT_1)).isTrue();
  98. }
  99. @Test
  100. public void include_some_rule_and_component() {
  101. IssueFilter underTest = newIssueFilter(newSettings(Collections.emptyList(), asList("xoo:x1", "**/xoo/File1*")));
  102. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
  103. assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
  104. // Issues on other rule are accepted
  105. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
  106. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
  107. }
  108. @Test
  109. public void ignore_and_include_same_rule_and_component() {
  110. IssueFilter underTest = newIssueFilter(newSettings(
  111. asList("xoo:x1", "**/xoo/File1*"),
  112. asList("xoo:x1", "**/xoo/File1*")));
  113. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isFalse();
  114. assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
  115. // Issues on other rule are accepted
  116. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
  117. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isTrue();
  118. }
  119. @Test
  120. public void include_many_rules() {
  121. IssueFilter underTest = newIssueFilter(newSettings(
  122. Collections.emptyList(),
  123. asList("xoo:x1", "**/xoo/File1*", "xoo:x2", "**/xoo/File1*")));
  124. assertThat(underTest.accept(ISSUE_1, COMPONENT_1)).isTrue();
  125. assertThat(underTest.accept(ISSUE_1, COMPONENT_2)).isFalse();
  126. assertThat(underTest.accept(ISSUE_2, COMPONENT_1)).isTrue();
  127. assertThat(underTest.accept(ISSUE_2, COMPONENT_2)).isFalse();
  128. }
  129. @Test
  130. public void accept_project_issues() {
  131. IssueFilter underTest = newIssueFilter(newSettings(
  132. asList("xoo:x1", "**/xoo/File1*"),
  133. asList("xoo:x1", "**/xoo/File1*")));
  134. assertThat(underTest.accept(ISSUE_1, PROJECT)).isTrue();
  135. assertThat(underTest.accept(ISSUE_2, PROJECT)).isTrue();
  136. }
  137. @Test
  138. public void fail_when_only_rule_key_parameter() {
  139. expectedException.expect(IllegalArgumentException.class);
  140. expectedException.expectMessage("File path pattern cannot be empty. Please check 'sonar.issue.ignore.multicriteria' settings");
  141. newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.emptyList()));
  142. }
  143. @Test
  144. public void fail_when_only_path_parameter() {
  145. expectedException.expect(IllegalArgumentException.class);
  146. expectedException.expectMessage("Rule key pattern cannot be empty. Please check 'sonar.issue.enforce.multicriteria' settings");
  147. newIssueFilter(newSettings(Collections.emptyList(), asList("", "**")));
  148. }
  149. private IssueFilter newIssueFilter(MapSettings settings) {
  150. when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig());
  151. return new IssueFilter(settingsRepository);
  152. }
  153. private static MapSettings newSettings(List<String> exclusionsProperties, List<String> inclusionsProperties) {
  154. MapSettings settings = new MapSettings();
  155. if (!exclusionsProperties.isEmpty()) {
  156. addProperties(exclusionsProperties, "ignore", settings);
  157. }
  158. if (!inclusionsProperties.isEmpty()) {
  159. addProperties(inclusionsProperties, "enforce", settings);
  160. }
  161. return settings;
  162. }
  163. private static void addProperties(List<String> properties, String property, Settings settings) {
  164. if (!properties.isEmpty()) {
  165. List<Integer> indexes = new ArrayList<>();
  166. int index = 1;
  167. for (int i = 0; i < properties.size(); i += 2) {
  168. settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".ruleKey", properties.get(i));
  169. settings.setProperty("sonar.issue." + property + ".multicriteria." + index + ".resourceKey", properties.get(i + 1));
  170. indexes.add(index);
  171. index++;
  172. }
  173. settings.setProperty("sonar.issue." + property + ".multicriteria", Joiner.on(",").join(indexes));
  174. }
  175. }
  176. }