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 com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.sonar.api.config.PropertyDefinitions;
28 import org.sonar.api.config.Settings;
29 import org.sonar.plugins.core.issue.ignore.Constants;
30 import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration;
34 import static org.fest.assertions.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 public class PatternsInitializerTest {
40 private PatternsInitializer patternsInitializer;
42 private Settings settings;
46 settings = new Settings(new PropertyDefinitions(IgnoreIssuesConfiguration.getPropertyDefinitions()));
47 patternsInitializer = new PatternsInitializer(settings);
51 public void testNoConfiguration() {
52 patternsInitializer.initPatterns();
53 assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse();
54 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
58 public void shouldReturnExtraPatternForResource() {
60 patternsInitializer.addPatternToExcludeResource(file);
62 Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0);
63 assertThat(extraPattern.matchResource(file)).isTrue();
64 assertThat(extraPattern.isCheckLines()).isFalse();
68 public void shouldReturnExtraPatternForLinesOfResource() {
70 Set<LineRange> lineRanges = Sets.newHashSet();
71 lineRanges.add(new LineRange(25, 28));
72 patternsInitializer.addPatternToExcludeLines(file, lineRanges);
74 Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0);
75 assertThat(extraPattern.matchResource(file)).isTrue();
76 assertThat(extraPattern.getAllLines()).isEqualTo(Sets.newHashSet(25, 26, 27, 28));
80 public void shouldReturnMulticriteriaPattern() {
81 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1,2");
82 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org/foo/Bar.java");
83 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*");
84 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "*");
85 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org/foo/Hello.java");
86 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.RULE_KEY, "checkstyle:MagicNumber");
87 settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "[15-200]");
88 patternsInitializer.initPatterns();
90 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
91 assertThat(patternsInitializer.hasFileContentPattern()).isFalse();
92 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isTrue();
93 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(2);
94 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
95 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
99 public void shouldReturnBlockPattern() {
100 settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1,2");
101 settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
102 settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.END_BLOCK_REGEXP, "// SONAR-ON");
103 settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".2." + Constants.BEGIN_BLOCK_REGEXP, "// FOO-OFF");
104 settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".2." + Constants.END_BLOCK_REGEXP, "// FOO-ON");
105 patternsInitializer.initPatterns();
107 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
108 assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
109 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
110 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
111 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(2);
112 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
116 public void shouldReturnAllFilePattern() {
117 settings.setProperty(Constants.PATTERNS_ALLFILE_KEY, "1,2");
118 settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".1." + Constants.FILE_REGEXP, "@SONAR-IGNORE-ALL");
119 settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".2." + Constants.FILE_REGEXP, "//FOO-IGNORE-ALL");
120 patternsInitializer.initPatterns();
122 assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
123 assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
124 assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
125 assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
126 assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
127 assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
131 public void shouldConfigurePatternsForComponents() {
132 String componentKey = "groupId:artifactId:org.foo.Bar";
133 String path = "org/foo/Bar.java";
135 Pattern matching1, matching2, notMatching;
136 matching1 = mock(Pattern.class);
137 when(matching1.matchResource(path)).thenReturn(true);
138 matching2 = mock(Pattern.class);
139 when(matching2.matchResource(path)).thenReturn(true);
140 notMatching = mock(Pattern.class);
141 when(notMatching.matchResource(path)).thenReturn(false);
143 patternsInitializer.initPatterns();
144 patternsInitializer.getMulticriteriaPatterns().addAll(Lists.newArrayList(matching1, matching2, notMatching));
145 patternsInitializer.configurePatternsForComponent(componentKey, path);
147 assertThat(patternsInitializer.getPatternsForComponent(componentKey).size()).isEqualTo(2);
148 assertThat(patternsInitializer.getPatternsForComponent("other").size()).isEqualTo(0);