]> source.dussan.org Git - sonarqube.git/blob
98ec8ec805cf78c77f6475b22f3bbf159a0a975f
[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.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;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.Arrays;
45
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;
52
53 public class SourceScannerTest {
54
55   private SourceScanner scanner;
56
57   @Mock
58   private RegexpScanner regexpScanner;
59   @Mock
60   private InclusionPatternInitializer inclusionPatternInitializer;
61   @Mock
62   private ExclusionPatternInitializer exclusionPatternInitializer;
63   @Mock
64   private PatternMatcher patternMatcher;
65   @Mock
66   private DefaultModuleFileSystem fs;
67
68   private Project project;
69
70   @Rule
71   public ExpectedException thrown = ExpectedException.none();
72
73   @Rule
74   public TemporaryFolder temp = new TemporaryFolder();
75
76   private File baseDir;
77
78   @Before
79   public void init() throws IOException {
80     baseDir = temp.newFolder();
81     MockitoAnnotations.initMocks(this);
82
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);
87
88     scanner = new SourceScanner(regexpScanner, exclusionPatternInitializer, inclusionPatternInitializer, fs);
89   }
90
91   @Test
92   public void testToString() throws Exception {
93     assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner");
94   }
95
96   @Test
97   public void shouldExecute() throws IOException {
98     when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
99     when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
100     assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
101
102     when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
103     when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
104     assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
105
106     when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
107     when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(true);
108     assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
109
110     when(exclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
111     when(inclusionPatternInitializer.hasConfiguredPatterns()).thenReturn(false);
112     assertThat(scanner.shouldExecuteOnProject(null)).isFalse();
113
114   }
115
116   @Test
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")
123         .build(),
124       new InputFileBuilder(javaTestFile1, Charsets.UTF_8, "src/test/java/FooTest.java")
125         .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/test/java/FooTest.java")
126         .build()));
127
128     when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
129
130     scanner.analyse(project, null);
131
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);
138   }
139
140   @Test
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")
147         .build(),
148       new InputFileBuilder(javaTestFile1, Charsets.UTF_8, "src/test/java/FooTest.java")
149         .attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY, "polop:src/test/java/FooTest.java")
150         .build()));
151
152     when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(false);
153
154     scanner.analyse(project, null);
155
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);
161   }
162
163   @Test
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")
169         .build()));
170
171     when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
172     doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:src/Foo.php", phpFile1, UTF_8);
173
174     thrown.expect(SonarException.class);
175     thrown.expectMessage("Unable to read the source file");
176
177     scanner.analyse(project, null);
178   }
179 }