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.scanner;
23 import com.google.common.collect.Sets;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.sonar.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
29 import org.sonar.plugins.core.issue.ignore.pattern.IssuePattern;
30 import org.sonar.plugins.core.issue.ignore.pattern.LineRange;
31 import org.sonar.plugins.core.issue.ignore.pattern.PatternMatcher;
32 import org.sonar.test.TestUtils;
34 import java.io.IOException;
35 import java.util.Arrays;
38 import static com.google.common.base.Charsets.UTF_8;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42 import static org.mockito.Mockito.when;
44 public class IgnoreIssuesRegexpScannerTest {
46 private IgnoreIssuesRegexpScanner regexpScanner;
48 private String javaFile;
50 private ExclusionPatternInitializer patternsInitializer;
52 private PatternMatcher patternMatcher;
54 private IssuePattern allFilePattern;
56 private IssuePattern blockPattern1;
58 private IssuePattern blockPattern2;
62 MockitoAnnotations.initMocks(this);
64 when(allFilePattern.getAllFileRegexp()).thenReturn("@SONAR-IGNORE-ALL");
65 when(blockPattern1.getBeginBlockRegexp()).thenReturn("// SONAR-OFF");
66 when(blockPattern1.getEndBlockRegexp()).thenReturn("// SONAR-ON");
67 when(blockPattern2.getBeginBlockRegexp()).thenReturn("// FOO-OFF");
68 when(blockPattern2.getEndBlockRegexp()).thenReturn("// FOO-ON");
69 when(patternsInitializer.getAllFilePatterns()).thenReturn(Arrays.asList(allFilePattern));
70 when(patternsInitializer.getBlockPatterns()).thenReturn(Arrays.asList(blockPattern1, blockPattern2));
71 when(patternsInitializer.getPatternMatcher()).thenReturn(patternMatcher);
73 regexpScanner = new IgnoreIssuesRegexpScanner(patternsInitializer);
74 verify(patternsInitializer, times(1)).getAllFilePatterns();
75 verify(patternsInitializer, times(1)).getBlockPatterns();
77 javaFile = "org.sonar.test.MyFile";
81 public void shouldDoNothing() throws IOException {
82 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-no-regexp.txt"), UTF_8);
84 verifyNoMoreInteractions(patternsInitializer);
88 public void shouldAddPatternToExcludeFile() throws IOException {
89 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-single-regexp.txt"), UTF_8);
91 verify(patternsInitializer).getPatternMatcher();
92 verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
93 verifyNoMoreInteractions(patternsInitializer);
97 public void shouldAddPatternToExcludeFileEvenIfAlsoDoubleRegexps() throws IOException {
98 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-single-regexp-and-double-regexp.txt"), UTF_8);
100 verify(patternsInitializer).getPatternMatcher();
101 verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
102 verifyNoMoreInteractions(patternsInitializer);
106 public void shouldAddPatternToExcludeLines() throws IOException {
107 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp.txt"), UTF_8);
109 Set<LineRange> lineRanges = Sets.newHashSet();
110 lineRanges.add(new LineRange(21, 25));
111 verify(patternsInitializer).getPatternMatcher();
112 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
113 verifyNoMoreInteractions(patternsInitializer);
117 public void shouldAddPatternToExcludeLinesTillTheEnd() throws IOException {
118 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-unfinished.txt"), UTF_8);
120 Set<LineRange> lineRanges = Sets.newHashSet();
121 lineRanges.add(new LineRange(21, 34));
122 verify(patternsInitializer).getPatternMatcher();
123 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
124 verifyNoMoreInteractions(patternsInitializer);
128 public void shouldAddPatternToExcludeSeveralLineRanges() throws IOException {
129 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-twice.txt"), UTF_8);
131 Set<LineRange> lineRanges = Sets.newHashSet();
132 lineRanges.add(new LineRange(21, 25));
133 lineRanges.add(new LineRange(29, 33));
134 verify(patternsInitializer).getPatternMatcher();
135 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
136 verifyNoMoreInteractions(patternsInitializer);
140 public void shouldAddPatternToExcludeLinesWithWrongOrder() throws IOException {
141 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-wrong-order.txt"), UTF_8);
143 Set<LineRange> lineRanges = Sets.newHashSet();
144 lineRanges.add(new LineRange(25, 35));
145 verify(patternsInitializer).getPatternMatcher();
146 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
147 verifyNoMoreInteractions(patternsInitializer);
151 public void shouldAddPatternToExcludeLinesWithMess() throws IOException {
152 regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-mess.txt"), UTF_8);
154 Set<LineRange> lineRanges = Sets.newHashSet();
155 lineRanges.add(new LineRange(21, 29));
156 verify(patternsInitializer).getPatternMatcher();
157 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
158 verifyNoMoreInteractions(patternsInitializer);