]> source.dussan.org Git - sonarqube.git/blob
7fadd593235e7aa23d017e9ae5592ae403fc8717
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info 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 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.config.PropertyDefinitions;
25 import org.sonar.api.config.Settings;
26 import org.sonar.api.config.MapSettings;
27 import org.sonar.core.config.IssueExclusionProperties;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30
31 public class IssueExclusionPatternInitializerTest {
32   private IssueExclusionPatternInitializer patternsInitializer;
33   private Settings settings;
34
35   @Before
36   public void init() {
37     settings = new MapSettings(new PropertyDefinitions(IssueExclusionProperties.all()));
38   }
39
40   @Test
41   public void testNoConfiguration() {
42     patternsInitializer = new IssueExclusionPatternInitializer(settings);
43     assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse();
44     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
45   }
46
47   @Test(expected = IllegalStateException.class)
48   public void shouldLogInvalidResourceKey() {
49     settings.setProperty("sonar.issue.ignore" + ".multicriteria", "1");
50     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "resourceKey", "");
51     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "ruleKey", "*");
52     patternsInitializer = new IssueExclusionPatternInitializer(settings);
53   }
54
55   @Test(expected = IllegalStateException.class)
56   public void shouldLogInvalidRuleKey() {
57     settings.setProperty("sonar.issue.ignore" + ".multicriteria", "1");
58     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "resourceKey", "*");
59     settings.setProperty("sonar.issue.ignore" + ".multicriteria" + ".1." + "ruleKey", "");
60     patternsInitializer = new IssueExclusionPatternInitializer(settings);
61   }
62
63   @Test
64   public void shouldReturnBlockPattern() {
65     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY, "1,2,3");
66     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
67     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.END_BLOCK_REGEXP, "// SONAR-ON");
68     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".2." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// FOO-OFF");
69     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".2." + IssueExclusionProperties.END_BLOCK_REGEXP, "// FOO-ON");
70     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".3." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "// IGNORE-TO-EOF");
71     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".3." + IssueExclusionProperties.END_BLOCK_REGEXP, "");
72     patternsInitializer = new IssueExclusionPatternInitializer(settings);
73
74     assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
75     assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
76     assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
77     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
78     assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(3);
79     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
80   }
81
82   @Test(expected = IllegalStateException.class)
83   public void shouldLogInvalidStartBlockPattern() {
84     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY, "1");
85     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.BEGIN_BLOCK_REGEXP, "");
86     settings.setProperty(IssueExclusionProperties.PATTERNS_BLOCK_KEY + ".1." + IssueExclusionProperties.END_BLOCK_REGEXP, "// SONAR-ON");
87     patternsInitializer = new IssueExclusionPatternInitializer(settings);
88   }
89
90   @Test
91   public void shouldReturnAllFilePattern() {
92     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY, "1,2");
93     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".1." + IssueExclusionProperties.FILE_REGEXP, "@SONAR-IGNORE-ALL");
94     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".2." + IssueExclusionProperties.FILE_REGEXP, "//FOO-IGNORE-ALL");
95     patternsInitializer = new IssueExclusionPatternInitializer(settings);
96
97     assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
98     assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
99     assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
100     assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
101     assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
102     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
103   }
104
105   @Test(expected = IllegalStateException.class)
106   public void shouldLogInvalidAllFilePattern() {
107     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY, "1");
108     settings.setProperty(IssueExclusionProperties.PATTERNS_ALLFILE_KEY + ".1." + IssueExclusionProperties.FILE_REGEXP, "");
109     patternsInitializer = new IssueExclusionPatternInitializer(settings);
110   }
111 }