3 * Copyright (C) 2009-2019 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 java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.regex.Pattern;
32 import java.util.stream.IntStream;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.sonar.api.impl.fs.DefaultInputFile;
38 import org.sonar.api.impl.fs.FileMetadata;
39 import org.sonar.api.impl.fs.TestInputFileBuilder;
40 import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
41 import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader.DoubleRegexpMatcher;
43 import static java.nio.charset.StandardCharsets.UTF_8;
44 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
46 public class IssueExclusionsRegexpScannerTest {
47 private DefaultInputFile javaFile;
50 private IssueExclusionPatternInitializer patternsInitializer;
52 private List<Pattern> allFilePatterns;
53 private List<DoubleRegexpMatcher> blockPatterns;
54 private IssueExclusionsRegexpScanner regexpScanner;
55 private FileMetadata fileMetadata = new FileMetadata();
59 MockitoAnnotations.initMocks(this);
61 blockPatterns = Arrays.asList(new DoubleRegexpMatcher[] {
62 new DoubleRegexpMatcher(Pattern.compile("// SONAR-OFF"), Pattern.compile("// SONAR-ON")),
63 new DoubleRegexpMatcher(Pattern.compile("// FOO-OFF"), Pattern.compile("// FOO-ON"))
65 allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
67 javaFile = TestInputFileBuilder.create("foo", "src/Foo.java").build();
68 regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns);
72 public void shouldDetectPatternLastLine() throws URISyntaxException, IOException {
73 Path filePath = getResource("file-with-single-regexp-last-line.txt");
74 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
76 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
80 public void shouldDoNothing() throws Exception {
81 Path filePath = getResource("file-with-no-regexp.txt");
82 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
84 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
88 public void shouldExcludeAllIssues() throws Exception {
89 Path filePath = getResource("file-with-single-regexp.txt");
90 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
92 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
96 public void shouldExcludeAllIssuesEvenIfAlsoDoubleRegexps() throws Exception {
97 Path filePath = getResource("file-with-single-regexp-and-double-regexp.txt");
98 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
100 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
104 public void shouldExcludeLines() throws Exception {
105 Path filePath = getResource("file-with-double-regexp.txt");
106 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
108 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
109 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
110 assertThat(IntStream.rangeClosed(21, 25).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
111 assertThat(IntStream.rangeClosed(26, 34).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
115 public void shouldAddPatternToExcludeLinesTillTheEnd() throws Exception {
116 Path filePath = getResource("file-with-double-regexp-unfinished.txt");
117 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
119 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
120 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
121 assertThat(IntStream.rangeClosed(21, 34).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
125 public void shouldAddPatternToExcludeSeveralLineRanges() throws Exception {
126 Path filePath = getResource("file-with-double-regexp-twice.txt");
127 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
129 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
130 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
131 assertThat(IntStream.rangeClosed(21, 25).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
132 assertThat(IntStream.rangeClosed(26, 28).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
133 assertThat(IntStream.rangeClosed(29, 33).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
137 public void shouldAddPatternToExcludeLinesWithWrongOrder() throws Exception {
138 Path filePath = getResource("file-with-double-regexp-wrong-order.txt");
139 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
141 assertThat(IntStream.rangeClosed(1, 24).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
142 assertThat(IntStream.rangeClosed(25, 35).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
146 public void shouldAddPatternToExcludeLinesWithMess() throws Exception {
147 Path filePath = getResource("file-with-double-regexp-mess.txt");
148 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
150 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
151 assertThat(IntStream.rangeClosed(21, 29).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
152 assertThat(IntStream.rangeClosed(30, 37).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
155 private Path getResource(String fileName) throws URISyntaxException {
156 return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());