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.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.utils.SonarException;
28 import java.util.List;
30 import static org.fest.assertions.Assertions.assertThat;
32 public class PatternDecoderTest {
34 private PatternDecoder decoder = new PatternDecoder();
37 public ExpectedException thrown = ExpectedException.none();
40 public void shouldReadString() {
41 String patternsList = "# a comment followed by a blank line\n\n" +
42 "# suppress all violations\n" +
44 "# exclude a Java file\n" +
45 "com.foo.Bar;*;*\n\n" +
46 "# exclude a Java package\n" +
48 "# exclude a specific rule\n" +
49 "*;checkstyle:IllegalRegexp;*\n\n" +
50 "# exclude a specific rule on a specific file\n" +
51 "com.foo.Bar;checkstyle:IllegalRegexp;*\n";
52 List<IssuePattern> patterns = decoder.decode(patternsList);
54 assertThat(patterns).hasSize(5);
58 public void shouldCheckFormatOfResource() {
59 assertThat(PatternDecoder.isResource("")).isFalse();
60 assertThat(PatternDecoder.isResource("*")).isTrue();
61 assertThat(PatternDecoder.isResource("com.foo.*")).isTrue();
65 public void shouldCheckFormatOfRule() {
66 assertThat(PatternDecoder.isRule("")).isFalse();
67 assertThat(PatternDecoder.isRule("*")).isTrue();
68 assertThat(PatternDecoder.isRule("com.foo.*")).isTrue();
72 public void shouldCheckFormatOfLinesRange() {
73 assertThat(PatternDecoder.isLinesRange("")).isFalse();
74 assertThat(PatternDecoder.isLinesRange(" ")).isFalse();
75 assertThat(PatternDecoder.isLinesRange("12")).isFalse();
76 assertThat(PatternDecoder.isLinesRange("12,212")).isFalse();
78 assertThat(PatternDecoder.isLinesRange("*")).isTrue();
79 assertThat(PatternDecoder.isLinesRange("[]")).isTrue();
80 assertThat(PatternDecoder.isLinesRange("[13]")).isTrue();
81 assertThat(PatternDecoder.isLinesRange("[13,24]")).isTrue();
82 assertThat(PatternDecoder.isLinesRange("[13,24,25-500]")).isTrue();
83 assertThat(PatternDecoder.isLinesRange("[24-65]")).isTrue();
84 assertThat(PatternDecoder.isLinesRange("[13,24-65,84-89,122]")).isTrue();
88 public void shouldReadStarPatterns() {
89 IssuePattern pattern = decoder.decodeLine("*;*;*");
91 assertThat(pattern.getResourcePattern().toString()).isEqualTo("*");
92 assertThat(pattern.getRulePattern().toString()).isEqualTo("*");
93 assertThat(pattern.isCheckLines()).isFalse();
97 public void shouldReadLineIds() {
98 IssuePattern pattern = decoder.decodeLine("*;*;[10,25,98]");
100 assertThat(pattern.isCheckLines()).isTrue();
101 assertThat(pattern.getAllLines()).containsOnly(10, 25, 98);
105 public void shouldReadRangeOfLineIds() {
106 IssuePattern pattern = decoder.decodeLine("*;*;[10-12,25,97-100]");
108 assertThat(pattern.isCheckLines()).isTrue();
109 assertThat(pattern.getAllLines()).containsOnly(10, 11, 12, 25, 97, 98, 99, 100);
113 public void shouldNotExcludeLines() {
114 // [] is different than *
115 // - all violations are excluded on *
116 // * no violations are excluded on []
117 IssuePattern pattern = decoder.decodeLine("*;*;[]");
119 assertThat(pattern.isCheckLines()).isTrue();
120 assertThat(pattern.getAllLines()).isEmpty();
124 public void shouldReadBlockPattern() {
125 IssuePattern pattern = decoder.decodeLine("SONAR-OFF;SONAR-ON");
127 assertThat(pattern.getResourcePattern()).isNull();
128 assertThat(pattern.getBeginBlockRegexp()).isEqualTo("SONAR-OFF");
129 assertThat(pattern.getEndBlockRegexp()).isEqualTo("SONAR-ON");
133 public void shouldReadAllFilePattern() {
134 IssuePattern pattern = decoder.decodeLine("SONAR-ALL-OFF");
136 assertThat(pattern.getResourcePattern()).isNull();
137 assertThat(pattern.getAllFileRegexp()).isEqualTo("SONAR-ALL-OFF");
141 public void shouldFailToReadUncorrectLine1() {
142 thrown.expect(SonarException.class);
143 thrown.expectMessage("Exclusions > Issues : Invalid format. The following line has more than 3 fields separated by comma");
145 decoder.decode(";;;;");
149 public void shouldFailToReadUncorrectLine3() {
150 thrown.expect(SonarException.class);
151 thrown.expectMessage("Exclusions > Issues : Invalid format. The first field does not define a resource pattern");
153 decoder.decode(";*;*");
157 public void shouldFailToReadUncorrectLine4() {
158 thrown.expect(SonarException.class);
159 thrown.expectMessage("Exclusions > Issues : Invalid format. The second field does not define a rule pattern");
161 decoder.decode("*;;*");
165 public void shouldFailToReadUncorrectLine5() {
166 thrown.expect(SonarException.class);
167 thrown.expectMessage("Exclusions > Issues : Invalid format. The third field does not define a range of lines");
169 decoder.decode("*;*;blabla");
173 public void shouldFailToReadUncorrectLine6() {
174 thrown.expect(SonarException.class);
175 thrown.expectMessage("Exclusions > Issues : Invalid format. The first field does not define a regular expression");
177 decoder.decode(";ON");
181 public void shouldAcceptEmptyEndBlockRegexp() {
182 IssuePattern pattern = decoder.decodeLine("OFF;");
184 assertThat(pattern.getResourcePattern()).isNull();
185 assertThat(pattern.getBeginBlockRegexp()).isEqualTo("OFF");
186 assertThat(pattern.getEndBlockRegexp()).isEmpty();