3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.issue.ignore.scanner;
22 import com.google.common.io.Resources;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.sonar.api.batch.fs.internal.FileMetadata;
28 import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
29 import org.sonar.scanner.issue.ignore.pattern.LineRange;
30 import org.sonar.scanner.issue.ignore.pattern.PatternMatcher;
31 import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsRegexpScanner;
32 import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader.DoubleRegexpMatcher;
34 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.List;
44 import java.util.regex.Pattern;
46 import static java.nio.charset.StandardCharsets.UTF_8;
47 import static org.mockito.Mockito.times;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.verifyNoMoreInteractions;
51 public class IssueExclusionsRegexpScannerTest {
52 private String javaFile;
55 private IssueExclusionPatternInitializer patternsInitializer;
57 private PatternMatcher patternMatcher;
59 private List<Pattern> allFilePatterns;
60 private List<DoubleRegexpMatcher> blockPatterns;
61 private IssueExclusionsRegexpScanner regexpScanner;
62 private FileMetadata fileMetadata = new FileMetadata();
66 MockitoAnnotations.initMocks(this);
68 blockPatterns = Arrays.asList(new DoubleRegexpMatcher[] {
69 new DoubleRegexpMatcher(Pattern.compile("// SONAR-OFF"), Pattern.compile("// SONAR-ON")),
70 new DoubleRegexpMatcher(Pattern.compile("// FOO-OFF"), Pattern.compile("// FOO-ON"))
72 allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
74 javaFile = "org.sonar.test.MyFile";
75 regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns, patternMatcher);
79 public void shouldDetectPatternLastLine() throws URISyntaxException, IOException {
80 Path filePath = getResource("file-with-single-regexp-last-line.txt");
81 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
83 verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
84 verifyNoMoreInteractions(patternMatcher);
88 public void shouldDoNothing() throws Exception {
89 Path filePath = getResource("file-with-no-regexp.txt");
90 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
92 verifyNoMoreInteractions(patternMatcher);
96 public void shouldAddPatternToExcludeFile() throws Exception {
97 Path filePath = getResource("file-with-single-regexp.txt");
98 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
100 verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
101 verifyNoMoreInteractions(patternMatcher);
105 public void shouldAddPatternToExcludeFileEvenIfAlsoDoubleRegexps() throws Exception {
106 Path filePath = getResource("file-with-single-regexp-and-double-regexp.txt");
107 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
109 Set<LineRange> lineRanges = new HashSet<>();
110 lineRanges.add(new LineRange(5, 26));
111 verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
112 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
113 verifyNoMoreInteractions(patternMatcher);
117 public void shouldAddPatternToExcludeLines() throws Exception {
118 Path filePath = getResource("file-with-double-regexp.txt");
119 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
121 Set<LineRange> lineRanges = new HashSet<>();
122 lineRanges.add(new LineRange(21, 25));
123 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
124 verifyNoMoreInteractions(patternMatcher);
128 public void shouldAddPatternToExcludeLinesTillTheEnd() throws Exception {
129 Path filePath = getResource("file-with-double-regexp-unfinished.txt");
130 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
132 Set<LineRange> lineRanges = new HashSet<>();
133 lineRanges.add(new LineRange(21, 34));
134 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
135 verifyNoMoreInteractions(patternMatcher);
139 public void shouldAddPatternToExcludeSeveralLineRanges() throws Exception {
140 Path filePath = getResource("file-with-double-regexp-twice.txt");
141 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
143 Set<LineRange> lineRanges = new HashSet<>();
144 lineRanges.add(new LineRange(21, 25));
145 lineRanges.add(new LineRange(29, 33));
146 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
147 verifyNoMoreInteractions(patternMatcher);
151 public void shouldAddPatternToExcludeLinesWithWrongOrder() throws Exception {
152 Path filePath = getResource("file-with-double-regexp-wrong-order.txt");
153 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
155 Set<LineRange> lineRanges = new HashSet<>();
156 lineRanges.add(new LineRange(25, 35));
157 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
158 verifyNoMoreInteractions(patternMatcher);
162 public void shouldAddPatternToExcludeLinesWithMess() throws Exception {
163 Path filePath = getResource("file-with-double-regexp-mess.txt");
164 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
166 Set<LineRange> lineRanges = new HashSet<>();
167 lineRanges.add(new LineRange(21, 29));
168 verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
169 verifyNoMoreInteractions(patternMatcher);
172 private Path getResource(String fileName) throws URISyntaxException {
173 return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());