diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-01-24 17:32:28 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-27 16:26:30 +0100 |
commit | 291382160309e0543067c59c193d126710370dee (patch) | |
tree | bacef8cf02968d4911360db95fd6375049bccf3e /sonar-scanner-engine | |
parent | e606937326a6f2c9d83d7d2509d73bb237a94356 (diff) | |
download | sonarqube-291382160309e0543067c59c193d126710370dee.tar.gz sonarqube-291382160309e0543067c59c193d126710370dee.zip |
SONAR-8630 query by filename and ext (#1555)
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 118 insertions, 0 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java index 88c41a10d08..ae6c52d38c5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java @@ -19,6 +19,8 @@ */ package org.sonar.scanner.scan.filesystem; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.SetMultimap; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -35,6 +37,8 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import com.google.common.collect.Table; import com.google.common.collect.TreeBasedTable; +import org.sonar.api.batch.fs.internal.FileExtensionPredicate; +import org.sonar.api.batch.fs.internal.FilenamePredicate; /** * Store of all files and dirs. This cache is shared amongst all project modules. Inclusion and @@ -47,6 +51,8 @@ public class InputComponentStore { private final Table<String, String, InputDir> inputDirCache = TreeBasedTable.create(); private final Map<String, InputModule> inputModuleCache = new HashMap<>(); private final Map<String, InputComponent> inputComponents = new HashMap<>(); + private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create(); + private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create(); private InputModule root; public Collection<InputComponent> all() { @@ -104,6 +110,8 @@ public class InputComponentStore { DefaultInputFile file = (DefaultInputFile) inputFile; inputFileCache.put(file.moduleKey(), inputFile.relativePath(), inputFile); inputComponents.put(inputFile.key(), inputFile); + filesByNameCache.put(FilenamePredicate.getFilename(inputFile), inputFile); + filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile); return this; } @@ -134,4 +142,11 @@ public class InputComponentStore { inputModuleCache.put(inputModule.key(), inputModule); } + public Iterable<InputFile> getFilesByName(String filename) { + return filesByNameCache.get(filename); + } + + public Iterable<InputFile> getFilesByExtension(String extension) { + return filesByExtensionCache.get(extension); + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java index 9b4e5762fb1..dd518d9d152 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java @@ -70,4 +70,13 @@ public class ModuleInputComponentStore extends DefaultFileSystem.Cache { public InputModule module() { return inputComponentStore.getModule(moduleKey); } + + @Override + public Iterable<InputFile> getFilesByName(String filename) { + return inputComponentStore.getFilesByName(filename); + } + + @Override public Iterable<InputFile> getFilesByExtension(String extension) { + return inputComponentStore.getFilesByExtension(extension); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java new file mode 100644 index 00000000000..fad8bae936d --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.filesystem; + +import java.io.IOException; +import org.junit.Test; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.InputModule; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class ModuleInputComponentStoreTest { + @Test + public void should_cache_files_by_filename() throws IOException { + ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), new InputComponentStore()); + + String filename = "some name"; + String moduleKey = "dummy key"; + InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/" + filename).build(); + store.doAdd(inputFile1); + + InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/" + filename).build(); + store.doAdd(inputFile2); + + InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/Dummy.java").build(); + store.doAdd(dummyInputFile); + + assertThat(store.getFilesByName(filename)).containsOnly(inputFile1, inputFile2); + } + + @Test + public void should_cache_files_by_extension() throws IOException { + ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), new InputComponentStore()); + + String moduleKey = "dummy key"; + InputFile inputFile1 = new TestInputFileBuilder(moduleKey, "some/path/Program.java").build(); + store.doAdd(inputFile1); + + InputFile inputFile2 = new TestInputFileBuilder(moduleKey, "other/path/Utils.java").build(); + store.doAdd(inputFile2); + + InputFile dummyInputFile = new TestInputFileBuilder(moduleKey, "some/path/NotJava.cpp").build(); + store.doAdd(dummyInputFile); + + assertThat(store.getFilesByExtension("java")).containsOnly(inputFile1, inputFile2); + } + + @Test + public void should_not_cache_duplicates() throws IOException { + ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), new InputComponentStore()); + + String ext = "java"; + String filename = "Program." + ext; + InputFile inputFile = new TestInputFileBuilder("dummy key", "some/path/" + filename).build(); + store.doAdd(inputFile); + store.doAdd(inputFile); + store.doAdd(inputFile); + + assertThat(store.getFilesByName(filename)).containsOnly(inputFile); + assertThat(store.getFilesByExtension(ext)).containsOnly(inputFile); + } + + @Test + public void should_get_empty_iterable_on_cache_miss() { + ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), new InputComponentStore()); + + String ext = "java"; + String filename = "Program." + ext; + InputFile inputFile = new TestInputFileBuilder("dummy key", "some/path/" + filename).build(); + store.doAdd(inputFile); + + assertThat(store.getFilesByName("nonexistent")).isEmpty(); + assertThat(store.getFilesByExtension("nonexistent")).isEmpty(); + } +} |