3 * Copyright (C) 2009-2020 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.sonar.api.batch.fs.internal.DefaultInputFile;
36 import org.sonar.api.batch.fs.internal.FileMetadata;
37 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
38 import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
39 import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader.DoubleRegexpMatcher;
41 import static java.nio.charset.StandardCharsets.UTF_8;
42 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
43 import static org.mockito.Mockito.mock;
45 public class IssueExclusionsRegexpScannerTest {
46 private DefaultInputFile javaFile;
47 private IssueExclusionPatternInitializer patternsInitializer = mock(IssueExclusionPatternInitializer.class);
49 private List<Pattern> allFilePatterns;
50 private List<DoubleRegexpMatcher> blockPatterns;
51 private IssueExclusionsRegexpScanner regexpScanner;
52 private FileMetadata fileMetadata = new FileMetadata();
56 blockPatterns = Arrays.asList(new DoubleRegexpMatcher[] {
57 new DoubleRegexpMatcher(Pattern.compile("// SONAR-OFF"), Pattern.compile("// SONAR-ON")),
58 new DoubleRegexpMatcher(Pattern.compile("// FOO-OFF"), Pattern.compile("// FOO-ON"))
60 allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
62 javaFile = TestInputFileBuilder.create("foo", "src/Foo.java").build();
63 regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns);
67 public void shouldDetectPatternLastLine() throws URISyntaxException, IOException {
68 Path filePath = getResource("file-with-single-regexp-last-line.txt");
69 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
71 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
75 public void shouldDoNothing() throws Exception {
76 Path filePath = getResource("file-with-no-regexp.txt");
77 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
79 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
83 public void shouldExcludeAllIssues() throws Exception {
84 Path filePath = getResource("file-with-single-regexp.txt");
85 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
87 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
91 public void shouldExcludeAllIssuesEvenIfAlsoDoubleRegexps() throws Exception {
92 Path filePath = getResource("file-with-single-regexp-and-double-regexp.txt");
93 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
95 assertThat(javaFile.isIgnoreAllIssues()).isTrue();
99 public void shouldExcludeLines() throws Exception {
100 Path filePath = getResource("file-with-double-regexp.txt");
101 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
103 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
104 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
105 assertThat(IntStream.rangeClosed(21, 25).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
106 assertThat(IntStream.rangeClosed(26, 34).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
110 public void shouldAddPatternToExcludeLinesTillTheEnd() throws Exception {
111 Path filePath = getResource("file-with-double-regexp-unfinished.txt");
112 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
114 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
115 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
116 assertThat(IntStream.rangeClosed(21, 34).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
120 public void shouldAddPatternToExcludeSeveralLineRanges() throws Exception {
121 Path filePath = getResource("file-with-double-regexp-twice.txt");
122 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
124 assertThat(javaFile.isIgnoreAllIssues()).isFalse();
125 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
126 assertThat(IntStream.rangeClosed(21, 25).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
127 assertThat(IntStream.rangeClosed(26, 28).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
128 assertThat(IntStream.rangeClosed(29, 33).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
132 public void shouldAddPatternToExcludeLinesWithWrongOrder() throws Exception {
133 Path filePath = getResource("file-with-double-regexp-wrong-order.txt");
134 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
136 assertThat(IntStream.rangeClosed(1, 24).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
137 assertThat(IntStream.rangeClosed(25, 35).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
141 public void shouldAddPatternToExcludeLinesWithMess() throws Exception {
142 Path filePath = getResource("file-with-double-regexp-mess.txt");
143 fileMetadata.readMetadata(Files.newInputStream(filePath), UTF_8, filePath.toString(), regexpScanner);
145 assertThat(IntStream.rangeClosed(1, 20).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
146 assertThat(IntStream.rangeClosed(21, 29).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
147 assertThat(IntStream.rangeClosed(30, 37).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
150 private Path getResource(String fileName) throws URISyntaxException {
151 return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());