]> source.dussan.org Git - sonarqube.git/blob
9a069bbb2a14861a9f66f026402ada570a88349b
[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.pattern;
22
23 import org.junit.Test;
24 import org.sonar.api.issue.Issue;
25 import org.sonar.api.rule.RuleKey;
26 import org.sonar.api.rules.Rule;
27
28 import static org.fest.assertions.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 public class IssuePatternTest {
33
34   @Test
35   public void shouldMatchLines() {
36     IssuePattern pattern = new IssuePattern("*", "*");
37     pattern.addLine(12).addLine(15).addLineRange(20, 25);
38
39     assertThat(pattern.matchLine(3)).isFalse();
40     assertThat(pattern.matchLine(12)).isTrue();
41     assertThat(pattern.matchLine(14)).isFalse();
42     assertThat(pattern.matchLine(21)).isTrue();
43     assertThat(pattern.matchLine(6599)).isFalse();
44   }
45
46   @Test
47   public void shouldMatchJavaFile() {
48     String javaFile = "org.foo.Bar";
49     assertThat(new IssuePattern("org.foo.Bar", "*").matchResource(javaFile)).isTrue();
50     assertThat(new IssuePattern("org.foo.*", "*").matchResource(javaFile)).isTrue();
51     assertThat(new IssuePattern("*Bar", "*").matchResource(javaFile)).isTrue();
52     assertThat(new IssuePattern("*", "*").matchResource(javaFile)).isTrue();
53     assertThat(new IssuePattern("org.*.?ar", "*").matchResource(javaFile)).isTrue();
54
55     assertThat(new IssuePattern("org.other.Hello", "*").matchResource(javaFile)).isFalse();
56     assertThat(new IssuePattern("org.foo.Hello", "*").matchResource(javaFile)).isFalse();
57     assertThat(new IssuePattern("org.*.??ar", "*").matchResource(javaFile)).isFalse();
58     assertThat(new IssuePattern("org.*.??ar", "*").matchResource(null)).isFalse();
59     assertThat(new IssuePattern("org.*.??ar", "*").matchResource("plop")).isFalse();
60   }
61
62   @Test
63   public void shouldMatchRule() {
64     RuleKey rule = Rule.create("checkstyle", "IllegalRegexp", "").ruleKey();
65     assertThat(new IssuePattern("*", "*").matchRule(rule)).isTrue();
66     assertThat(new IssuePattern("*", "checkstyle:*").matchRule(rule)).isTrue();
67     assertThat(new IssuePattern("*", "checkstyle:IllegalRegexp").matchRule(rule)).isTrue();
68     assertThat(new IssuePattern("*", "checkstyle:Illegal*").matchRule(rule)).isTrue();
69     assertThat(new IssuePattern("*", "*:*Illegal*").matchRule(rule)).isTrue();
70
71     assertThat(new IssuePattern("*", "pmd:IllegalRegexp").matchRule(rule)).isFalse();
72     assertThat(new IssuePattern("*", "pmd:*").matchRule(rule)).isFalse();
73     assertThat(new IssuePattern("*", "*:Foo*IllegalRegexp").matchRule(rule)).isFalse();
74   }
75
76   @Test
77   public void shouldMatchViolation() {
78     Rule rule = Rule.create("checkstyle", "IllegalRegexp", "");
79     String javaFile = "org.foo.Bar";
80
81     IssuePattern pattern = new IssuePattern("*", "*");
82     pattern.addLine(12);
83
84     assertThat(pattern.match(create(rule, javaFile, null))).isFalse();
85     assertThat(pattern.match(create(rule, javaFile, 12))).isTrue();
86     assertThat(pattern.match(create((Rule) null, javaFile, 5))).isFalse();
87     assertThat(pattern.match(create(rule, null, null))).isFalse();
88     assertThat(pattern.match(create((Rule) null, null, null))).isFalse();
89   }
90
91   private Issue create(Rule rule, String component, Integer line) {
92     Issue mockIssue = mock(Issue.class);
93     RuleKey ruleKey = null;
94     if (rule != null) {
95       ruleKey = rule.ruleKey();
96     }
97     when(mockIssue.ruleKey()).thenReturn(ruleKey);
98     when(mockIssue.componentKey()).thenReturn(component);
99     when(mockIssue.line()).thenReturn(line);
100     return mockIssue;
101   }
102
103   @Test
104   public void shouldNotMatchNullRule() {
105     assertThat(new IssuePattern("*", "*").matchRule(null)).isFalse();
106   }
107
108   @Test
109   public void shouldPrintPatternToString() {
110     IssuePattern pattern = new IssuePattern("*", "checkstyle:*");
111
112     assertThat(pattern.toString()).isEqualTo("IssuePattern[resourcePattern=*,rulePattern=checkstyle:*,lines=[],lineRanges=[],beginBlockRegexp=<null>,endBlockRegexp=<null>,allFileRegexp=<null>,checkLines=true]");
113   }
114 }