3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.scan.filesystem;
22 import java.io.IOException;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.TemporaryFolder;
27 import org.sonar.api.batch.fs.InputFile;
28 import org.sonar.api.batch.fs.InputModule;
29 import org.sonar.api.batch.fs.internal.DefaultInputModule;
30 import org.sonar.api.batch.fs.internal.SensorStrategy;
31 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
32 import org.sonar.scanner.analysis.DefaultAnalysisMode;
33 import org.sonar.scanner.scan.branch.BranchConfiguration;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Matchers.any;
37 import static org.mockito.Matchers.eq;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verify;
41 public class ModuleInputComponentStoreTest {
43 public TemporaryFolder temp = new TemporaryFolder();
45 private InputComponentStore componentStore;
47 private final String moduleKey = "dummy key";
50 public void setUp() throws IOException {
51 DefaultInputModule root = TestInputFileBuilder.newDefaultInputModule(moduleKey, temp.newFolder());
52 componentStore = new InputComponentStore(root, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class));
56 public void should_cache_files_by_filename() throws IOException {
57 ModuleInputComponentStore store = newModuleInputComponentStore();
59 String filename = "some name";
60 InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
61 store.doAdd(inputFile1);
63 InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/" + filename).build();
64 store.doAdd(inputFile2);
66 InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/Dummy.java").build();
67 store.doAdd(dummyInputFile);
69 assertThat(store.getFilesByName(filename)).containsExactlyInAnyOrder(inputFile1, inputFile2);
73 public void should_cache_files_by_extension() throws IOException {
74 ModuleInputComponentStore store = newModuleInputComponentStore();
76 InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/Program.java").build();
77 store.doAdd(inputFile1);
79 InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/Utils.java").build();
80 store.doAdd(inputFile2);
82 InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/NotJava.cpp").build();
83 store.doAdd(dummyInputFile);
85 assertThat(store.getFilesByExtension("java")).containsExactlyInAnyOrder(inputFile1, inputFile2);
89 public void should_not_cache_duplicates() throws IOException {
90 ModuleInputComponentStore store = newModuleInputComponentStore();
93 String filename = "Program." + ext;
94 InputFile inputFile = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
95 store.doAdd(inputFile);
96 store.doAdd(inputFile);
97 store.doAdd(inputFile);
99 assertThat(store.getFilesByName(filename)).containsExactly(inputFile);
100 assertThat(store.getFilesByExtension(ext)).containsExactly(inputFile);
104 public void should_get_empty_iterable_on_cache_miss() {
105 ModuleInputComponentStore store = newModuleInputComponentStore();
108 String filename = "Program." + ext;
109 InputFile inputFile = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
110 store.doAdd(inputFile);
112 assertThat(store.getFilesByName("nonexistent")).isEmpty();
113 assertThat(store.getFilesByExtension("nonexistent")).isEmpty();
116 private ModuleInputComponentStore newModuleInputComponentStore() {
117 return new ModuleInputComponentStore(mock(InputModule.class), componentStore, mock(SensorStrategy.class));
121 public void should_find_module_components_with_non_global_strategy() {
122 InputComponentStore inputComponentStore = mock(InputComponentStore.class);
123 SensorStrategy strategy = new SensorStrategy();
124 ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
127 verify(inputComponentStore).filesByModule(any(String.class));
129 String relativePath = "somepath";
130 store.inputFile(relativePath);
131 verify(inputComponentStore).getFile(any(String.class), eq(relativePath));
133 store.inputDir(relativePath);
134 verify(inputComponentStore).getDir(any(String.class), eq(relativePath));
137 verify(inputComponentStore).getLanguages(any(String.class));
141 public void should_find_all_components_with_global_strategy() {
142 InputComponentStore inputComponentStore = mock(InputComponentStore.class);
143 SensorStrategy strategy = new SensorStrategy();
144 ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
146 strategy.setGlobal(true);
149 verify(inputComponentStore).allFiles();
151 String relativePath = "somepath";
152 store.inputFile(relativePath);
153 verify(inputComponentStore).getFile(relativePath);
155 store.inputDir(relativePath);
156 verify(inputComponentStore).getDir(relativePath);
159 verify(inputComponentStore).getLanguages();