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.pattern;
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;
28 import static org.fest.assertions.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 public class IssuePatternTest {
35 public void shouldMatchLines() {
36 IssuePattern pattern = new IssuePattern("*", "*");
37 pattern.addLine(12).addLine(15).addLineRange(20, 25);
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();
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();
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();
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();
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();
77 public void shouldMatchViolation() {
78 Rule rule = Rule.create("checkstyle", "IllegalRegexp", "");
79 String javaFile = "org.foo.Bar";
81 IssuePattern pattern = new IssuePattern("*", "*");
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();
91 private Issue create(Rule rule, String component, Integer line) {
92 Issue mockIssue = mock(Issue.class);
93 RuleKey ruleKey = null;
95 ruleKey = rule.ruleKey();
97 when(mockIssue.ruleKey()).thenReturn(ruleKey);
98 when(mockIssue.componentKey()).thenReturn(component);
99 when(mockIssue.line()).thenReturn(line);
104 public void shouldNotMatchNullRule() {
105 assertThat(new IssuePattern("*", "*").matchRule(null)).isFalse();
109 public void shouldPrintPatternToString() {
110 IssuePattern pattern = new IssuePattern("*", "checkstyle:*");
112 assertThat(pattern.toString()).isEqualTo("IssuePattern[resourcePattern=*,rulePattern=checkstyle:*,lines=[],lineRanges=[],beginBlockRegexp=<null>,endBlockRegexp=<null>,allFileRegexp=<null>,checkLines=true]");