]> source.dussan.org Git - sonarqube.git/blob
c9fbc50229e583057f1741a481780e74c618bb05
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program 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  * This program 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 package org.sonar.scanner.issue.ignore.pattern;
21
22
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.core.config.IssueExclusionProperties;
29 import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
33 public class IssueExclusionPatternInitializerTest {
34
35   private IssueExclusionPatternInitializer patternsInitializer;
36
37   private Settings settings;
38
39   @Before
40   public void init() {
41     settings = new Settings(new PropertyDefinitions(IssueExclusionProperties.all()));
42     patternsInitializer = new IssueExclusionPatternInitializer(settings);
43   }
44
45   @Test
46   public void testNoConfiguration() {
47     patternsInitializer.initPatterns();
48     assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse();
49     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
50   }
51
52   @Test
53   public void shouldHavePatternsBasedOnMulticriteriaPattern() {
54     settings.setProperty("sonar.issue.ignore" + ".multicriteria", "1,2");
55     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "resourceKey", "org/foo/Bar.java");
56     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "ruleKey", "*");
57     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".2." + "resourceKey", "org/foo/Hello.java");
58     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".2." + "ruleKey", "checkstyle:MagicNumber");
59     patternsInitializer.initPatterns();
60
61     assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
62     assertThat(patternsInitializer.hasFileContentPattern()).isFalse();
63     assertThat(patternsInitializer.hasMulticriteriaPatterns()).isTrue();
64     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(2);
65     assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
66     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
67
68     patternsInitializer.initializePatternsForPath("org/foo/Bar.java", "org.foo.Bar");
69     patternsInitializer.initializePatternsForPath("org/foo/Baz.java", "org.foo.Baz");
70     patternsInitializer.initializePatternsForPath("org/foo/Hello.java", "org.foo.Hello");
71
72     assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Bar")).hasSize(1);
73     assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Baz")).hasSize(0);
74     assertThat(patternsInitializer.getPatternMatcher().getPatternsForComponent("org.foo.Hello")).hasSize(1);
75
76   }
77
78   @Test(expected = SonarException.class)
79   public void shouldLogInvalidResourceKey() {
80     settings.setProperty("sonar.issue.ignore" + ".multicriteria", "1");
81     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "resourceKey", "");
82     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "ruleKey", "*");
83     patternsInitializer.initPatterns();
84   }
85
86   @Test(expected = SonarException.class)
87   public void shouldLogInvalidRuleKey() {
88     settings.setProperty("sonar.issue.ignore" + ".multicriteria", "1");
89     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "resourceKey", "*");
90     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "ruleKey", "");
91     patternsInitializer.initPatterns();
92   }
93
94   @Test
95   public void shouldReturnBlockPattern() {
96     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY, "1,2,3");
97     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
98     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.END_BLOCK_REGEXP, "// SONAR-ON");
99     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".2." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// FOO-OFF");
100     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".2." + IssueExclusionProperties.END_BLOCK_REGEXP, "// FOO-ON");
101     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".3." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// IGNORE-TO-EOF");
102     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".3." + IssueExclusionProperties.END_BLOCK_REGEXP, "");
103     patternsInitializer.loadFileContentPatterns();
104
105     assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
106     assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
107     assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
108     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
109     assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(3);
110     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
111   }
112
113   @Test(expected = SonarException.class)
114   public void shouldLogInvalidStartBlockPattern() {
115     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY, "1");
116     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "");
117     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.END_BLOCK_REGEXP, "// SONAR-ON");
118     patternsInitializer.loadFileContentPatterns();
119   }
120
121   @Test
122   public void shouldReturnAllFilePattern() {
123     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY, "1,2");
124     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".1." + IssueExclusionProperties.FILE_REGEXP, "@SONAR-IGNORE-ALL");
125     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".2." + IssueExclusionProperties.FILE_REGEXP, "//FOO-IGNORE-ALL");
126     patternsInitializer.loadFileContentPatterns();
127
128     assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
129     assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
130     assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
131     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
132     assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
133     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
134   }
135
136   @Test(expected = SonarException.class)
137   public void shouldLogInvalidAllFilePattern() {
138     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY, "1");
139     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".1." + IssueExclusionProperties.FILE_REGEXP, "");
140     patternsInitializer.loadFileContentPatterns();
141   }
142 }