]> source.dussan.org Git - sonarqube.git/blob
b0982ee2de216edb24ac6c14d7a0358b695e2995
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.scan.filesystem;
21
22 import com.google.common.collect.Lists;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.Charset;
26 import java.util.Arrays;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.rules.TemporaryFolder;
32 import org.mockito.Mockito;
33 import org.sonar.api.CoreProperties;
34 import org.sonar.api.batch.fs.InputFile;
35 import org.sonar.api.batch.fs.InputFile.Status;
36 import org.sonar.api.batch.fs.internal.DefaultInputFile;
37 import org.sonar.api.config.Settings;
38 import org.sonar.api.resources.Project;
39 import org.sonar.scanner.analysis.DefaultAnalysisMode;
40 import org.sonar.scanner.scan.filesystem.ComponentIndexer;
41 import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem;
42 import org.sonar.scanner.scan.filesystem.FileIndexer;
43 import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer;
44 import org.sonar.scanner.scan.filesystem.ModuleInputFileCache;
45
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.verifyZeroInteractions;
50 import static org.mockito.Mockito.when;
51
52 public class DefaultModuleFileSystemTest {
53
54   @Rule
55   public TemporaryFolder temp = new TemporaryFolder();
56
57   @Rule
58   public ExpectedException thrown = ExpectedException.none();
59
60   private Settings settings;
61   private FileIndexer fileIndexer;
62   private ModuleFileSystemInitializer initializer;
63   private ComponentIndexer componentIndexer;
64   private ModuleInputFileCache moduleInputFileCache;
65   private DefaultAnalysisMode mode;
66
67   @Before
68   public void setUp() {
69     settings = new Settings();
70     fileIndexer = mock(FileIndexer.class);
71     initializer = mock(ModuleFileSystemInitializer.class, Mockito.RETURNS_DEEP_STUBS);
72     componentIndexer = mock(ComponentIndexer.class);
73     moduleInputFileCache = mock(ModuleInputFileCache.class);
74     mode = mock(DefaultAnalysisMode.class);
75   }
76
77   @Test
78   public void test_equals_and_hashCode() throws Exception {
79     DefaultModuleFileSystem foo1 = new DefaultModuleFileSystem(moduleInputFileCache,
80       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
81     DefaultModuleFileSystem foo2 = new DefaultModuleFileSystem(moduleInputFileCache,
82       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
83     DefaultModuleFileSystem bar = new DefaultModuleFileSystem(moduleInputFileCache,
84       new Project("bar"), settings, fileIndexer, initializer, componentIndexer, mode);
85     DefaultModuleFileSystem branch = new DefaultModuleFileSystem(moduleInputFileCache,
86       new Project("bar", "branch", "My project"), settings, fileIndexer, initializer, componentIndexer, mode);
87
88     assertThat(foo1.moduleKey()).isEqualTo("foo");
89     assertThat(branch.moduleKey()).isEqualTo("bar:branch");
90     assertThat(foo1.equals(foo1)).isTrue();
91     assertThat(foo1.equals(foo2)).isTrue();
92     assertThat(foo1.equals(bar)).isFalse();
93     assertThat(foo1.equals("foo")).isFalse();
94     assertThat(foo1.hashCode()).isEqualTo(foo1.hashCode());
95     assertThat(foo1.hashCode()).isEqualTo(foo2.hashCode());
96   }
97
98   @Test
99   public void default_source_encoding() {
100     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
101       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
102
103     assertThat(fs.sourceCharset()).isEqualTo(Charset.defaultCharset());
104     assertThat(fs.isDefaultJvmEncoding()).isTrue();
105   }
106
107   @Test
108   public void source_encoding_is_set() {
109     settings.setProperty(CoreProperties.ENCODING_PROPERTY, "Cp1124");
110     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
111       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
112
113     assertThat(fs.encoding()).isEqualTo(Charset.forName("Cp1124"));
114     assertThat(fs.sourceCharset()).isEqualTo(Charset.forName("Cp1124"));
115
116     // This test fails when default Java encoding is "IBM AIX Ukraine". Sorry for that.
117     assertThat(fs.isDefaultJvmEncoding()).isFalse();
118   }
119
120   @Test
121   public void default_predicate_scan_only_changed() throws IOException {
122     when(mode.scanAllFiles()).thenReturn(false);
123
124     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
125       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
126
127     File baseDir = temp.newFile();
128     InputFile mainInput = new DefaultInputFile("foo", "Main.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.MAIN);
129     InputFile testInput = new DefaultInputFile("foo", "Test.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.TEST);
130     InputFile mainSameInput = new DefaultInputFile("foo", "MainSame.java").setModuleBaseDir(baseDir.toPath())
131       .setType(InputFile.Type.TEST).setStatus(Status.SAME);
132     when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput, mainSameInput));
133
134     fs.index();
135     Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().all());
136     assertThat(inputFiles).containsOnly(mainInput, testInput);
137
138     Iterable<InputFile> allInputFiles = fs.inputFiles();
139     assertThat(allInputFiles).containsOnly(mainInput, mainSameInput, testInput);
140   }
141
142   @Test
143   public void test_dirs() throws IOException {
144     File basedir = temp.newFolder("base");
145     File buildDir = temp.newFolder("build");
146     File workingDir = temp.newFolder("work");
147     File additionalFile = temp.newFile("Main.java");
148     File additionalTest = temp.newFile("Test.java");
149     when(initializer.baseDir()).thenReturn(basedir);
150     when(initializer.buildDir()).thenReturn(buildDir);
151     when(initializer.workingDir()).thenReturn(workingDir);
152     File javaSrc = new File(basedir, "src/main/java");
153     javaSrc.mkdirs();
154     File groovySrc = new File(basedir, "src/main/groovy");
155     groovySrc.mkdirs();
156     when(initializer.sources()).thenReturn(Arrays.asList(javaSrc, groovySrc, additionalFile));
157     File javaTest = new File(basedir, "src/test/java");
158     javaTest.mkdirs();
159     when(initializer.tests()).thenReturn(Arrays.asList(javaTest, additionalTest));
160
161     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
162       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
163
164     assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
165     assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workingDir.getCanonicalPath());
166     assertThat(fs.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath());
167     assertThat(fs.sourceDirs()).hasSize(2);
168     assertThat(fs.testDirs()).hasSize(1);
169   }
170
171   @Test
172   public void should_search_input_files() throws Exception {
173     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
174       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
175
176     File baseDir = temp.newFile();
177     InputFile mainInput = new DefaultInputFile("foo", "Main.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.MAIN);
178     InputFile testInput = new DefaultInputFile("foo", "Test.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.TEST);
179     when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput));
180
181     fs.index();
182     Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN));
183     assertThat(inputFiles).containsOnly(mainInput);
184
185     Iterable<File> files = fs.files(fs.predicates().hasType(InputFile.Type.MAIN));
186     assertThat(files).containsOnly(new File(baseDir, "Main.java"));
187   }
188
189   @Test
190   public void should_index() {
191     DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
192       new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
193
194     verifyZeroInteractions(fileIndexer);
195
196     fs.index();
197     verify(fileIndexer).index(fs);
198     verify(componentIndexer).execute(fs);
199   }
200
201 }