]> source.dussan.org Git - sonarqube.git/blob
3cba13dbd786120878d462e202581a4614901ca0
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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
21 package org.sonar.plugins.core.issue.ignore;
22
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;
32
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;
38
39 public class EnforceIssuesFilterTest {
40
41   private InclusionPatternInitializer exclusionPatternInitializer;
42   private EnforceIssuesFilter ignoreFilter;
43   private Issue issue;
44   private IssueFilterChain chain;
45
46   @Before
47   public void init() {
48     exclusionPatternInitializer = mock(InclusionPatternInitializer.class);
49     issue = mock(Issue.class);
50     chain = mock(IssueFilterChain.class);
51     when(chain.accept(issue)).thenReturn(true);
52
53     ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer);
54   }
55
56   @Test
57   public void shouldPassToChainIfNoConfiguredPatterns() {
58     assertThat(ignoreFilter.accept(issue, chain)).isTrue();
59     verify(chain).accept(issue);
60   }
61
62   @Test
63   public void shouldPassToChainIfRuleDoesNotMatch() {
64     String rule = "rule";
65     RuleKey ruleKey = mock(RuleKey.class);
66     when(ruleKey.toString()).thenReturn(rule);
67     when(issue.ruleKey()).thenReturn(ruleKey);
68
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));
74
75     assertThat(ignoreFilter.accept(issue, chain)).isTrue();
76     verify(chain).accept(issue);
77   }
78
79   @Test
80   public void shouldAcceptIssueIfFullyMatched() {
81     String rule = "rule";
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);
88
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);
98
99     assertThat(ignoreFilter.accept(issue, chain)).isTrue();
100     verifyZeroInteractions(chain);
101   }
102
103   @Test
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);
112
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);
122
123     assertThat(ignoreFilter.accept(issue, chain)).isFalse();
124     verifyZeroInteractions(chain);
125   }
126
127   @Test
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);
136
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);
146
147     assertThat(ignoreFilter.accept(issue, chain)).isFalse();
148     verifyZeroInteractions(chain);
149   }
150 }