]> source.dussan.org Git - sonarqube.git/blob
5b82e07e82b2b65d71856e1cc43512e361525fc1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.scanner.issue.ignore.scanner;
21
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;
40
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;
44
45 public class IssueExclusionsRegexpScannerTest {
46   private DefaultInputFile javaFile;
47   private IssueExclusionPatternInitializer patternsInitializer = mock(IssueExclusionPatternInitializer.class);
48
49   private List<Pattern> allFilePatterns;
50   private List<DoubleRegexpMatcher> blockPatterns;
51   private IssueExclusionsRegexpScanner regexpScanner;
52   private FileMetadata fileMetadata = new FileMetadata();
53
54   @Before
55   public void init() {
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"))
59     });
60     allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
61
62     javaFile = TestInputFileBuilder.create("foo", "src/Foo.java").build();
63     regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns);
64   }
65
66   @Test
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);
70
71     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
72   }
73
74   @Test
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);
78
79     assertThat(javaFile.isIgnoreAllIssues()).isFalse();
80   }
81
82   @Test
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);
86
87     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
88   }
89
90   @Test
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);
94
95     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
96   }
97
98   @Test
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);
102
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();
107   }
108
109   @Test
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);
113
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();
117   }
118
119   @Test
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);
123
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();
129   }
130
131   @Test
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);
135
136     assertThat(IntStream.rangeClosed(1, 24).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
137     assertThat(IntStream.rangeClosed(25, 35).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
138   }
139
140   @Test
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);
144
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();
148   }
149
150   private Path getResource(String fileName) throws URISyntaxException {
151     return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());
152   }
153
154 }