2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.plugins.core.issue.ignore;
23 import com.google.common.collect.ImmutableList;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.issue.Issue;
27 import org.sonar.api.issue.batch.IssueFilterChain;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.utils.WildcardPattern;
30 import org.sonar.plugins.core.issue.ignore.pattern.InclusionPatternInitializer;
31 import org.sonar.plugins.core.issue.ignore.pattern.IssuePattern;
33 import static org.fest.assertions.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyZeroInteractions;
37 import static org.mockito.Mockito.when;
39 public class EnforceIssuesFilterTest {
41 private InclusionPatternInitializer exclusionPatternInitializer;
42 private EnforceIssuesFilter ignoreFilter;
44 private IssueFilterChain chain;
48 exclusionPatternInitializer = mock(InclusionPatternInitializer.class);
49 issue = mock(Issue.class);
50 chain = mock(IssueFilterChain.class);
51 when(chain.accept(issue)).thenReturn(true);
53 ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer);
57 public void shouldPassToChainIfNoConfiguredPatterns() {
58 assertThat(ignoreFilter.accept(issue, chain)).isTrue();
59 verify(chain).accept(issue);
63 public void shouldPassToChainIfRuleDoesNotMatch() {
65 RuleKey ruleKey = mock(RuleKey.class);
66 when(ruleKey.toString()).thenReturn(rule);
67 when(issue.ruleKey()).thenReturn(ruleKey);
69 IssuePattern matching = mock(IssuePattern.class);
70 WildcardPattern rulePattern = mock(WildcardPattern.class);
71 when(matching.getRulePattern()).thenReturn(rulePattern);
72 when(rulePattern.match(rule)).thenReturn(false);
73 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
75 assertThat(ignoreFilter.accept(issue, chain)).isTrue();
76 verify(chain).accept(issue);
80 public void shouldAcceptIssueIfFullyMatched() {
82 String path = "org/sonar/api/Issue.java";
83 String componentKey = "org.sonar.api.Issue";
84 RuleKey ruleKey = mock(RuleKey.class);
85 when(ruleKey.toString()).thenReturn(rule);
86 when(issue.ruleKey()).thenReturn(ruleKey);
87 when(issue.componentKey()).thenReturn(componentKey);
89 IssuePattern matching = mock(IssuePattern.class);
90 WildcardPattern rulePattern = mock(WildcardPattern.class);
91 when(matching.getRulePattern()).thenReturn(rulePattern);
92 when(rulePattern.match(rule)).thenReturn(true);
93 WildcardPattern pathPattern = mock(WildcardPattern.class);
94 when(matching.getResourcePattern()).thenReturn(pathPattern);
95 when(pathPattern.match(path)).thenReturn(true);
96 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
97 when(exclusionPatternInitializer.getPathForComponent(componentKey)).thenReturn(path);
99 assertThat(ignoreFilter.accept(issue, chain)).isTrue();
100 verifyZeroInteractions(chain);
104 public void shouldRefuseIssueIfRuleMatchesButNotPath() {
105 String rule = "rule";
106 String path = "org/sonar/api/Issue.java";
107 String componentKey = "org.sonar.api.Issue";
108 RuleKey ruleKey = mock(RuleKey.class);
109 when(ruleKey.toString()).thenReturn(rule);
110 when(issue.ruleKey()).thenReturn(ruleKey);
111 when(issue.componentKey()).thenReturn(componentKey);
113 IssuePattern matching = mock(IssuePattern.class);
114 WildcardPattern rulePattern = mock(WildcardPattern.class);
115 when(matching.getRulePattern()).thenReturn(rulePattern);
116 when(rulePattern.match(rule)).thenReturn(true);
117 WildcardPattern pathPattern = mock(WildcardPattern.class);
118 when(matching.getResourcePattern()).thenReturn(pathPattern);
119 when(pathPattern.match(path)).thenReturn(false);
120 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
121 when(exclusionPatternInitializer.getPathForComponent(componentKey)).thenReturn(path);
123 assertThat(ignoreFilter.accept(issue, chain)).isFalse();
124 verifyZeroInteractions(chain);
128 public void shouldRefuseIssueIfRuleMatchesAndPathUnknown() {
129 String rule = "rule";
130 String path = "org/sonar/api/Issue.java";
131 String componentKey = "org.sonar.api.Issue";
132 RuleKey ruleKey = mock(RuleKey.class);
133 when(ruleKey.toString()).thenReturn(rule);
134 when(issue.ruleKey()).thenReturn(ruleKey);
135 when(issue.componentKey()).thenReturn(componentKey);
137 IssuePattern matching = mock(IssuePattern.class);
138 WildcardPattern rulePattern = mock(WildcardPattern.class);
139 when(matching.getRulePattern()).thenReturn(rulePattern);
140 when(rulePattern.match(rule)).thenReturn(true);
141 WildcardPattern pathPattern = mock(WildcardPattern.class);
142 when(matching.getResourcePattern()).thenReturn(pathPattern);
143 when(pathPattern.match(path)).thenReturn(false);
144 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
145 when(exclusionPatternInitializer.getPathForComponent(componentKey)).thenReturn(null);
147 assertThat(ignoreFilter.accept(issue, chain)).isFalse();
148 verifyZeroInteractions(chain);