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.Before;
24 import org.junit.Test;
25 import org.sonar.api.config.PropertyDefinitions;
26 import org.sonar.api.config.Settings;
27 import org.sonar.api.utils.SonarException;
28 import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration;
30 import static org.fest.assertions.Assertions.assertThat;
32 public class ExclusionPatternInitializerTest {
34 private ExclusionPatternInitializer patternsInitializer;
36 private Settings settings;
40 settings = new Settings(new PropertyDefinitions(IgnoreIssuesConfiguration.getPropertyDefinitions()));
41 patternsInitializer = new ExclusionPatternInitializer(settings);
45 public void testNoConfiguration() {
46 patternsInitializer.initPatterns();
47 assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse();
48 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
52 public void shouldHavePatternsBasedOnMulticriteriaPattern() {
53 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY, "1,2");
54 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RESOURCE_KEY, "org/foo/Bar.java");
55 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RULE_KEY, "*");
56 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".2." + IgnoreIssuesConfiguration.RESOURCE_KEY, "org/foo/Hello.java");
57 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".2." + IgnoreIssuesConfiguration.RULE_KEY, "checkstyle:MagicNumber");
58 patternsInitializer.initPatterns();
60 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
61 assertThat(patternsInitializer.hasFileContentPattern()).isFalse();
62 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isTrue();
63 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(2);
64 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
65 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
67 patternsInitializer.initializePatternsForPath("org/foo/Bar.java", "org.foo.Bar");
68 patternsInitializer.initializePatternsForPath("org/foo/Baz.java", "org.foo.Baz");
69 patternsInitializer.initializePatternsForPath("org/foo/Hello.java", "org.foo.Hello");
71 assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Bar")).hasSize(1);
72 assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Baz")).hasSize(0);
73 assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Hello")).hasSize(1);
77 @Test(expected = SonarException.class)
78 public void shouldLogInvalidResourceKey() {
79 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY, "1");
80 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RESOURCE_KEY, "");
81 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RULE_KEY, "*");
82 patternsInitializer.initPatterns();
85 @Test(expected = SonarException.class)
86 public void shouldLogInvalidRuleKey() {
87 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY, "1");
88 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RESOURCE_KEY, "*");
89 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY + ".1." + IgnoreIssuesConfiguration.RULE_KEY, "");
90 patternsInitializer.initPatterns();
94 public void shouldReturnBlockPattern() {
95 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY, "1,2,3");
96 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
97 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "// SONAR-ON");
98 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".2." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// FOO-OFF");
99 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".2." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "// FOO-ON");
100 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".3." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// IGNORE-TO-EOF");
101 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".3." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "");
102 patternsInitializer.loadFileContentPatterns();
104 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
105 assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
106 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
107 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
108 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(3);
109 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
112 @Test(expected = SonarException.class)
113 public void shouldLogInvalidStartBlockPattern() {
114 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY, "1");
115 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "");
116 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "// SONAR-ON");
117 patternsInitializer.loadFileContentPatterns();
121 public void shouldReturnAllFilePattern() {
122 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY, "1,2");
123 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY + ".1." + IgnoreIssuesConfiguration.FILE_REGEXP, "@SONAR-IGNORE-ALL");
124 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY + ".2." + IgnoreIssuesConfiguration.FILE_REGEXP, "//FOO-IGNORE-ALL");
125 patternsInitializer.loadFileContentPatterns();
127 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
128 assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
129 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
130 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
131 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
132 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
135 @Test(expected = SonarException.class)
136 public void shouldLogInvalidAllFilePattern() {
137 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY, "1");
138 settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY + ".1." + IgnoreIssuesConfiguration.FILE_REGEXP, "");
139 patternsInitializer.loadFileContentPatterns();