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.TestInputFileBuilder;
30 import org.sonar.api.scan.filesystem.PathResolver;
31 import org.sonar.scanner.sensor.SensorStrategy;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Matchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
39 public class ModuleInputComponentStoreTest {
41 public TemporaryFolder temp = new TemporaryFolder();
43 private InputComponentStore componentStore;
45 private final String moduleKey = "dummy key";
48 public void setUp() throws IOException {
49 componentStore = new InputComponentStore(new PathResolver());
50 componentStore.put(TestInputFileBuilder.newDefaultInputModule(moduleKey, temp.newFolder()));
54 public void should_cache_files_by_filename() throws IOException {
55 ModuleInputComponentStore store = newModuleInputComponentStore();
57 String filename = "some name";
58 InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
59 store.doAdd(inputFile1);
61 InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/" + filename).build();
62 store.doAdd(inputFile2);
64 InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/Dummy.java").build();
65 store.doAdd(dummyInputFile);
67 assertThat(store.getFilesByName(filename)).containsExactlyInAnyOrder(inputFile1, inputFile2);
71 public void should_cache_files_by_extension() throws IOException {
72 ModuleInputComponentStore store = newModuleInputComponentStore();
74 InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/Program.java").build();
75 store.doAdd(inputFile1);
77 InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/Utils.java").build();
78 store.doAdd(inputFile2);
80 InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/NotJava.cpp").build();
81 store.doAdd(dummyInputFile);
83 assertThat(store.getFilesByExtension("java")).containsExactlyInAnyOrder(inputFile1, inputFile2);
87 public void should_not_cache_duplicates() throws IOException {
88 ModuleInputComponentStore store = newModuleInputComponentStore();
91 String filename = "Program." + ext;
92 InputFile inputFile = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
93 store.doAdd(inputFile);
94 store.doAdd(inputFile);
95 store.doAdd(inputFile);
97 assertThat(store.getFilesByName(filename)).containsExactly(inputFile);
98 assertThat(store.getFilesByExtension(ext)).containsExactly(inputFile);
102 public void should_get_empty_iterable_on_cache_miss() {
103 ModuleInputComponentStore store = newModuleInputComponentStore();
106 String filename = "Program." + ext;
107 InputFile inputFile = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build();
108 store.doAdd(inputFile);
110 assertThat(store.getFilesByName("nonexistent")).isEmpty();
111 assertThat(store.getFilesByExtension("nonexistent")).isEmpty();
114 private ModuleInputComponentStore newModuleInputComponentStore() {
115 return new ModuleInputComponentStore(mock(InputModule.class), componentStore, mock(SensorStrategy.class));
119 public void should_find_module_components_with_non_global_strategy() {
120 InputComponentStore inputComponentStore = mock(InputComponentStore.class);
121 SensorStrategy strategy = new SensorStrategy();
122 ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
125 verify(inputComponentStore).filesByModule(any(String.class));
127 String relativePath = "somepath";
128 store.inputFile(relativePath);
129 verify(inputComponentStore).getFile(any(String.class), eq(relativePath));
131 store.inputDir(relativePath);
132 verify(inputComponentStore).getDir(any(String.class), eq(relativePath));
135 verify(inputComponentStore).getLanguages(any(String.class));
139 public void should_find_all_components_with_global_strategy() {
140 InputComponentStore inputComponentStore = mock(InputComponentStore.class);
141 SensorStrategy strategy = new SensorStrategy();
142 ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
144 strategy.setGlobal(true);
147 verify(inputComponentStore).allFiles();
149 String relativePath = "somepath";
150 store.inputFile(relativePath);
151 verify(inputComponentStore).getFile(relativePath);
153 store.inputDir(relativePath);
154 verify(inputComponentStore).getDir(relativePath);
157 verify(inputComponentStore).getLanguages();