]> source.dussan.org Git - sonarqube.git/blob
bfbe6c6aee021c7078355431c98e4bf682489bd1
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube 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  * SonarQube 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
21 package org.sonar.plugins.core.issue.ignore.scanner;
22
23 import com.google.common.collect.Sets;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.sonar.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
29 import org.sonar.plugins.core.issue.ignore.pattern.IssuePattern;
30 import org.sonar.plugins.core.issue.ignore.pattern.LineRange;
31 import org.sonar.plugins.core.issue.ignore.pattern.PatternMatcher;
32 import org.sonar.test.TestUtils;
33
34 import java.io.IOException;
35 import java.util.Arrays;
36 import java.util.Set;
37
38 import static com.google.common.base.Charsets.UTF_8;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42 import static org.mockito.Mockito.when;
43
44 public class IgnoreIssuesRegexpScannerTest {
45
46   private IgnoreIssuesRegexpScanner regexpScanner;
47
48   private String javaFile;
49   @Mock
50   private ExclusionPatternInitializer patternsInitializer;
51   @Mock
52   private PatternMatcher patternMatcher;
53   @Mock
54   private IssuePattern allFilePattern;
55   @Mock
56   private IssuePattern blockPattern1;
57   @Mock
58   private IssuePattern blockPattern2;
59
60   @Before
61   public void init() {
62     MockitoAnnotations.initMocks(this);
63
64     when(allFilePattern.getAllFileRegexp()).thenReturn("@SONAR-IGNORE-ALL");
65     when(blockPattern1.getBeginBlockRegexp()).thenReturn("// SONAR-OFF");
66     when(blockPattern1.getEndBlockRegexp()).thenReturn("// SONAR-ON");
67     when(blockPattern2.getBeginBlockRegexp()).thenReturn("// FOO-OFF");
68     when(blockPattern2.getEndBlockRegexp()).thenReturn("// FOO-ON");
69     when(patternsInitializer.getAllFilePatterns()).thenReturn(Arrays.asList(allFilePattern));
70     when(patternsInitializer.getBlockPatterns()).thenReturn(Arrays.asList(blockPattern1, blockPattern2));
71     when(patternsInitializer.getPatternMatcher()).thenReturn(patternMatcher);
72
73     regexpScanner = new IgnoreIssuesRegexpScanner(patternsInitializer);
74     verify(patternsInitializer, times(1)).getAllFilePatterns();
75     verify(patternsInitializer, times(1)).getBlockPatterns();
76
77     javaFile = "org.sonar.test.MyFile";
78   }
79
80   @Test
81   public void shouldDoNothing() throws IOException {
82     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-no-regexp.txt"), UTF_8);
83
84     verifyNoMoreInteractions(patternsInitializer);
85   }
86
87   @Test
88   public void shouldAddPatternToExcludeFile() throws IOException {
89     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-single-regexp.txt"), UTF_8);
90
91     verify(patternsInitializer).getPatternMatcher();
92     verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
93     verifyNoMoreInteractions(patternsInitializer);
94   }
95
96   @Test
97   public void shouldAddPatternToExcludeFileEvenIfAlsoDoubleRegexps() throws IOException {
98     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-single-regexp-and-double-regexp.txt"), UTF_8);
99
100     verify(patternsInitializer).getPatternMatcher();
101     verify(patternMatcher, times(1)).addPatternToExcludeResource(javaFile);
102     verifyNoMoreInteractions(patternsInitializer);
103   }
104
105   @Test
106   public void shouldAddPatternToExcludeLines() throws IOException {
107     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp.txt"), UTF_8);
108
109     Set<LineRange> lineRanges = Sets.newHashSet();
110     lineRanges.add(new LineRange(21, 25));
111     verify(patternsInitializer).getPatternMatcher();
112     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
113     verifyNoMoreInteractions(patternsInitializer);
114   }
115
116   @Test
117   public void shouldAddPatternToExcludeLinesTillTheEnd() throws IOException {
118     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-unfinished.txt"), UTF_8);
119
120     Set<LineRange> lineRanges = Sets.newHashSet();
121     lineRanges.add(new LineRange(21, 34));
122     verify(patternsInitializer).getPatternMatcher();
123     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
124     verifyNoMoreInteractions(patternsInitializer);
125   }
126
127   @Test
128   public void shouldAddPatternToExcludeSeveralLineRanges() throws IOException {
129     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-twice.txt"), UTF_8);
130
131     Set<LineRange> lineRanges = Sets.newHashSet();
132     lineRanges.add(new LineRange(21, 25));
133     lineRanges.add(new LineRange(29, 33));
134     verify(patternsInitializer).getPatternMatcher();
135     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
136     verifyNoMoreInteractions(patternsInitializer);
137   }
138
139   @Test
140   public void shouldAddPatternToExcludeLinesWithWrongOrder() throws IOException {
141     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-wrong-order.txt"), UTF_8);
142
143     Set<LineRange> lineRanges = Sets.newHashSet();
144     lineRanges.add(new LineRange(25, 35));
145     verify(patternsInitializer).getPatternMatcher();
146     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
147     verifyNoMoreInteractions(patternsInitializer);
148   }
149
150   @Test
151   public void shouldAddPatternToExcludeLinesWithMess() throws IOException {
152     regexpScanner.scan(javaFile, TestUtils.getResource(getClass(), "file-with-double-regexp-mess.txt"), UTF_8);
153
154     Set<LineRange> lineRanges = Sets.newHashSet();
155     lineRanges.add(new LineRange(21, 29));
156     verify(patternsInitializer).getPatternMatcher();
157     verify(patternMatcher, times(1)).addPatternToExcludeLines(javaFile, lineRanges);
158     verifyNoMoreInteractions(patternsInitializer);
159   }
160
161 }