]> source.dussan.org Git - sonarqube.git/blob
e6c757a92d37abded0d74cea3dd8fea52e2c30c7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.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;
42
43 import static java.nio.charset.StandardCharsets.UTF_8;
44 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
45
46 public class IssueExclusionsRegexpScannerTest {
47   private DefaultInputFile javaFile;
48
49   @Mock
50   private IssueExclusionPatternInitializer patternsInitializer;
51
52   private List<Pattern> allFilePatterns;
53   private List<DoubleRegexpMatcher> blockPatterns;
54   private IssueExclusionsRegexpScanner regexpScanner;
55   private FileMetadata fileMetadata = new FileMetadata();
56
57   @Before
58   public void init() {
59     MockitoAnnotations.initMocks(this);
60
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"))
64     });
65     allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
66
67     javaFile = TestInputFileBuilder.create("foo", "src/Foo.java").build();
68     regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns);
69   }
70
71   @Test
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);
75
76     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
77   }
78
79   @Test
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);
83
84     assertThat(javaFile.isIgnoreAllIssues()).isFalse();
85   }
86
87   @Test
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);
91
92     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
93   }
94
95   @Test
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);
99
100     assertThat(javaFile.isIgnoreAllIssues()).isTrue();
101   }
102
103   @Test
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);
107
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();
112   }
113
114   @Test
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);
118
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();
122   }
123
124   @Test
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);
128
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();
134   }
135
136   @Test
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);
140
141     assertThat(IntStream.rangeClosed(1, 24).noneMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
142     assertThat(IntStream.rangeClosed(25, 35).allMatch(javaFile::isIgnoreAllIssuesOnLine)).isTrue();
143   }
144
145   @Test
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);
149
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();
153   }
154
155   private Path getResource(String fileName) throws URISyntaxException {
156     return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());
157   }
158
159 }