]> source.dussan.org Git - sonarqube.git/blob
e3a322e69384cf78c971296e250ee9ba3fa30560
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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 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;
33
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;
43 import java.util.Set;
44 import java.util.regex.Pattern;
45
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;
50
51 public class IssueExclusionsRegexpScannerTest {
52   private String javaFile;
53
54   @Mock
55   private IssueExclusionPatternInitializer patternsInitializer;
56   @Mock
57   private PatternMatcher patternMatcher;
58
59   private List<Pattern> allFilePatterns;
60   private List<DoubleRegexpMatcher> blockPatterns;
61   private IssueExclusionsRegexpScanner regexpScanner;
62   private FileMetadata fileMetadata = new FileMetadata();
63
64   @Before
65   public void init() {
66     MockitoAnnotations.initMocks(this);
67
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"))
71     });
72     allFilePatterns = Collections.singletonList(Pattern.compile("@SONAR-IGNORE-ALL"));
73
74     javaFile = "org.sonar.test.MyFile";
75     regexpScanner = new IssueExclusionsRegexpScanner(javaFile, allFilePatterns, blockPatterns, patternMatcher);
76   }
77
78   @Test
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);
82
83     verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
84     verifyNoMoreInteractions(patternMatcher);
85   }
86
87   @Test
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);
91
92     verifyNoMoreInteractions(patternMatcher);
93   }
94
95   @Test
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);
99
100     verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
101     verifyNoMoreInteractions(patternMatcher);
102   }
103
104   @Test
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);
108
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);
114   }
115
116   @Test
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);
120
121     Set<LineRange> lineRanges = new HashSet<>();
122     lineRanges.add(new LineRange(21, 25));
123     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
124     verifyNoMoreInteractions(patternMatcher);
125   }
126
127   @Test
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);
131
132     Set<LineRange> lineRanges = new HashSet<>();
133     lineRanges.add(new LineRange(21, 34));
134     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
135     verifyNoMoreInteractions(patternMatcher);
136   }
137
138   @Test
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);
142
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);
148   }
149
150   @Test
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);
154
155     Set<LineRange> lineRanges = new HashSet<>();
156     lineRanges.add(new LineRange(25, 35));
157     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
158     verifyNoMoreInteractions(patternMatcher);
159   }
160
161   @Test
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);
165
166     Set<LineRange> lineRanges = new HashSet<>();
167     lineRanges.add(new LineRange(21, 29));
168     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
169     verifyNoMoreInteractions(patternMatcher);
170   }
171
172   private Path getResource(String fileName) throws URISyntaxException {
173     return Paths.get(Resources.getResource("org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest/" + fileName).toURI());
174   }
175
176 }