2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.plugins.core.issue.ignore.scanner;
23 import com.google.common.base.Charsets;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.rules.TemporaryFolder;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.sonar.api.resources.Project;
33 import org.sonar.api.scan.filesystem.FileQuery;
34 import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
35 import org.sonar.api.scan.filesystem.internal.InputFileBuilder;
36 import org.sonar.api.utils.SonarException;
37 import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
38 import org.sonar.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
39 import org.sonar.plugins.core.issue.ignore.pattern.InclusionPatternInitializer;
40 import org.sonar.plugins.core.issue.ignore.pattern.PatternMatcher;
43 import java.io.IOException;
44 import java.util.Arrays;
46 import static com.google.common.base.Charsets.UTF_8;
47 import static org.fest.assertions.Assertions.assertThat;
48 import static org.mockito.Mockito.doThrow;
49 import static org.mockito.Mockito.verify;
50 import static org.mockito.Mockito.verifyZeroInteractions;
51 import static org.mockito.Mockito.when;
53 public class SourceScannerTest {
55 private SourceScanner scanner;
58 private RegexpScanner regexpScanner;
60 private InclusionPatternInitializer inclusionPatternInitializer;
62 private ExclusionPatternInitializer exclusionPatternInitializer;
64 private PatternMatcher patternMatcher;
66 private DefaultModuleFileSystem fs;
68 private Project project;
71 public ExpectedException thrown = ExpectedException.none();
74 public TemporaryFolder temp = new TemporaryFolder();
79 public void init() throws IOException {
80 baseDir = temp.newFolder();
81 MockitoAnnotations.initMocks(this);
83 Project realProject = new Project("polop");
84 project = Mockito.spy(realProject);
85 Mockito.doReturn("java").when(project).getLanguageKey();
86 when(fs.sourceCharset()).thenReturn(UTF_8);
88 scanner = new SourceScanner(regexpScanner, exclusionPatternInitializer, inclusionPatternInitializer, fs);
92 public void testToString() throws Exception {
93 assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner");
97 public void shouldExecute() throws IOException {
98 when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
99 when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
100 assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
102 when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
103 when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
104 assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
106 when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
107 when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
108 assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
110 when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
111 when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
112 assertThat(scanner.shouldExecuteOnProject(null)).isFalse();
117 public void shouldAnalyseProject() throws IOException {
118 File javaFile1 = new File(baseDir, "src/main/java/Foo.java");
119 File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java");
120 when(fs.inputFiles(FileQuery.all())).thenReturn((Iterable) Arrays.asList(
121 new InputFileBuilder(javaFile1, Charsets.UTF_8, "src/main/java/Foo.java")
122 .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/main/java/Foo.java")
124 new InputFileBuilder(javaTestFile1, Charsets.UTF_8, "src/test/java/FooTest.java")
125 .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/test/java/FooTest.java")
128 when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
130 scanner.analyse(project, null);
132 verify(inclusionPatternInitializer).initializePatternsForPath("src/main/java/Foo.java", "polop:src/main/java/Foo.java");
133 verify(inclusionPatternInitializer).initializePatternsForPath("src/test/java/FooTest.java", "polop:src/test/java/FooTest.java");
134 verify(exclusionPatternInitializer).initializePatternsForPath("src/main/java/Foo.java", "polop:src/main/java/Foo.java");
135 verify(exclusionPatternInitializer).initializePatternsForPath("src/test/java/FooTest.java", "polop:src/test/java/FooTest.java");
136 verify(regexpScanner).scan("polop:src/main/java/Foo.java", javaFile1, UTF_8);
137 verify(regexpScanner).scan("polop:src/test/java/FooTest.java", javaTestFile1, UTF_8);
141 public void shouldAnalyseFilesOnlyWhenRegexConfigured() throws IOException {
142 File javaFile1 = new File(baseDir, "src/main/java/Foo.java");
143 File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java");
144 when(fs.inputFiles(FileQuery.all())).thenReturn((Iterable) Arrays.asList(
145 new InputFileBuilder(javaFile1, Charsets.UTF_8, "src/main/java/Foo.java")
146 .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/main/java/Foo.java")
148 new InputFileBuilder(javaTestFile1, Charsets.UTF_8, "src/test/java/FooTest.java")
149 .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/test/java/FooTest.java")
152 when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(false);
154 scanner.analyse(project, null);
156 verify(inclusionPatternInitializer).initializePatternsForPath("src/main/java/Foo.java", "polop:src/main/java/Foo.java");
157 verify(inclusionPatternInitializer).initializePatternsForPath("src/test/java/FooTest.java", "polop:src/test/java/FooTest.java");
158 verify(exclusionPatternInitializer).initializePatternsForPath("src/main/java/Foo.java", "polop:src/main/java/Foo.java");
159 verify(exclusionPatternInitializer).initializePatternsForPath("src/test/java/FooTest.java", "polop:src/test/java/FooTest.java");
160 verifyZeroInteractions(regexpScanner);
164 public void shouldReportFailure() throws IOException {
165 File phpFile1 = new File(baseDir, "src/Foo.php");
166 when(fs.inputFiles(FileQuery.all())).thenReturn((Iterable) Arrays.asList(
167 new InputFileBuilder(phpFile1, Charsets.UTF_8, "src/Foo.php")
168 .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/Foo.php")
171 when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
172 doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:src/Foo.php", phpFile1, UTF_8);
174 thrown.expect(SonarException.class);
175 thrown.expectMessage("Unable to read the source file");
177 scanner.analyse(project, null);