3 * Copyright (C) 2009-2019 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.DefaultInputProject;
30 import org.sonar.api.batch.fs.internal.SensorStrategy;
31 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
32 import org.sonar.scanner.scan.branch.BranchConfiguration;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.eq;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
41 public class ModuleInputComponentStoreTest {
43 public TemporaryFolder temp = new TemporaryFolder();
45 private InputComponentStore componentStore;
47 private final String projectKey = "dummy key";
50 public void setUp() throws IOException {
51 DefaultInputProject root = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder());
52 componentStore = new InputComponentStore(root, 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(projectKey, "some/path/" + filename).build();
61 store.doAdd(inputFile1);
63 InputFile inputFile2 = new TestInputFileBuilder(projectKey, "other/path/" + filename).build();
64 store.doAdd(inputFile2);
66 InputFile dummyInputFile = new TestInputFileBuilder(projectKey, "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(projectKey, "some/path/Program.java").build();
77 store.doAdd(inputFile1);
79 InputFile inputFile2 = new TestInputFileBuilder(projectKey, "other/path/Utils.java").build();
80 store.doAdd(inputFile2);
82 InputFile dummyInputFile = new TestInputFileBuilder(projectKey, "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(projectKey, "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(projectKey, "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 InputModule module = mock(InputModule.class);
125 when(module.key()).thenReturn("foo");
126 ModuleInputComponentStore store = new ModuleInputComponentStore(module, inputComponentStore, strategy);
129 verify(inputComponentStore).filesByModule("foo");
131 String relativePath = "somepath";
132 store.inputFile(relativePath);
133 verify(inputComponentStore).getFile(any(String.class), eq(relativePath));
135 store.inputDir(relativePath);
136 verify(inputComponentStore).getDir(any(String.class), eq(relativePath));
139 verify(inputComponentStore).getLanguages(any(String.class));
143 public void should_find_all_components_with_global_strategy() {
144 InputComponentStore inputComponentStore = mock(InputComponentStore.class);
145 SensorStrategy strategy = new SensorStrategy();
146 ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
148 strategy.setGlobal(true);
151 verify(inputComponentStore).allFiles();
153 String relativePath = "somepath";
154 store.inputFile(relativePath);
155 verify(inputComponentStore).getFile(relativePath);
157 store.inputDir(relativePath);
158 verify(inputComponentStore).getDir(relativePath);
161 verify(inputComponentStore).getLanguages();