diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-22 09:27:34 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-22 12:35:56 +0200 |
commit | a1defc3dd106409930d9323d7bc055f816c2bcd3 (patch) | |
tree | f07e884153f38592a4359d9a2be3cf6270c2ab1c /sonar-batch/src | |
parent | 6d5f68a9de9c3c0365921ad95304ea7051640c7b (diff) | |
download | sonarqube-a1defc3dd106409930d9323d7bc055f816c2bcd3.tar.gz sonarqube-a1defc3dd106409930d9323d7bc055f816c2bcd3.zip |
SONAR-5389 Add InputDir concept in batch API
Diffstat (limited to 'sonar-batch/src')
10 files changed, 94 insertions, 71 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index 5eaa1ee13a4..394a2d2688a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -39,7 +39,7 @@ import org.sonar.batch.protocol.input.GlobalReferentials; import org.sonar.batch.protocol.input.ProjectReferentials; import org.sonar.batch.referential.GlobalReferentialsLoader; import org.sonar.batch.referential.ProjectReferentialsLoader; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.batch.scan2.AnalyzerIssueCache; import org.sonar.batch.scan2.AnalyzerMeasureCache; import org.sonar.batch.scan2.ProjectScanContainer; @@ -212,7 +212,7 @@ public class BatchMediumTester { measures.add(measure); } - InputFileCache inputFileCache = container.getComponentByType(InputFileCache.class); + InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class); for (InputFile inputFile : inputFileCache.all()) { inputFiles.add(inputFile); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index 9c2b46bf9b8..e3fde9a8c0f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -67,7 +67,7 @@ import org.sonar.batch.phases.GraphPersister; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; import org.sonar.batch.referential.ProjectReferentialsProvider; import org.sonar.batch.rule.RulesProvider; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.batch.scan.maven.FakeMavenPluginExecutor; import org.sonar.batch.scan.maven.MavenPluginExecutor; import org.sonar.batch.scan.measure.MeasureCache; @@ -153,7 +153,7 @@ public class ProjectScanContainer extends ComponentContainer { DefaultUserFinder.class, // file system - InputFileCache.class, + InputPathCache.class, PathResolver.class, // issues diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java index bdd0da25555..a33bbc50358 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java @@ -46,7 +46,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; /** - * Index input files into {@link InputFileCache}. + * Index input files into {@link InputPathCache}. */ public class FileIndexer implements BatchComponent { @@ -56,18 +56,18 @@ public class FileIndexer implements BatchComponent { private static final IOFileFilter FILE_FILTER = HiddenFileFilter.VISIBLE; private final List<InputFileFilter> filters; - private final InputFileCache fileCache; + private final InputPathCache fileCache; private final boolean isAggregator; private final ExclusionFilters exclusionFilters; private final InputFileBuilderFactory inputFileBuilderFactory; public FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, - InputFileCache cache, ProjectDefinition def) { + InputPathCache cache, ProjectDefinition def) { this(filters, exclusionFilters, inputFileBuilderFactory, cache, !def.getSubProjects().isEmpty()); } private FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, - InputFileCache cache, boolean isAggregator) { + InputPathCache cache, boolean isAggregator) { this.filters = filters; this.exclusionFilters = exclusionFilters; this.inputFileBuilderFactory = inputFileBuilderFactory; @@ -83,7 +83,7 @@ public class FileIndexer implements BatchComponent { LOG.info("Index files"); exclusionFilters.prepare(); - Progress progress = new Progress(fileCache.byModule(fileSystem.moduleKey())); + Progress progress = new Progress(fileCache.filesByModule(fileSystem.moduleKey())); InputFileBuilder inputFileBuilder = inputFileBuilderFactory.create(fileSystem); indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.sources(), InputFile.Type.MAIN); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java index 3b12d8ba5f6..842ecab6484 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java @@ -20,7 +20,9 @@ package org.sonar.batch.scan.filesystem; import org.sonar.api.BatchComponent; +import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.InputPath; import org.sonar.batch.index.Cache; import org.sonar.batch.index.Caches; @@ -30,45 +32,50 @@ import javax.annotation.CheckForNull; * Cache of all files. This cache is shared amongst all project modules. Inclusion and * exclusion patterns are already applied. */ -public class InputFileCache implements BatchComponent { +public class InputPathCache implements BatchComponent { - // [path type | module key | path] -> InputFile + private static final String DIR = "DIR"; + private static final String FILE = "FILE"; + // [module key | type | path] -> InputPath // For example: - // [rel | struts-core | src/main/java/Action.java] -> InputFile - // [rel | struts-core | src/main/java/Filter.java] -> InputFile - // [abs | struts-core | /absolute/path/to/src/main/java/Action.java] -> InputFile - // [abs | struts-core | /absolute/path/to/src/main/java/Filter.java] -> InputFile - private final Cache<InputFile> cache; + // [struts-core | FILE | src/main/java/Action.java] -> InputFile + // [struts-core | FILE | src/main/java/Filter.java] -> InputFile + // [struts-core | DIR | src/main/java] -> InputDir + private final Cache<InputPath> cache; - public InputFileCache(Caches caches) { + public InputPathCache(Caches caches) { cache = caches.createCache("inputFiles"); } - public Iterable<InputFile> all() { + public Iterable<InputPath> all() { return cache.values(); } - public Iterable<InputFile> byModule(String moduleKey) { - return cache.values(moduleKey); + public Iterable<InputFile> filesByModule(String moduleKey) { + return (Iterable) cache.values(moduleKey, FILE); } - public InputFileCache removeModule(String moduleKey) { + public InputPathCache removeModule(String moduleKey) { cache.clear(moduleKey); return this; } - public InputFileCache remove(String moduleKey, InputFile inputFile) { - cache.remove(moduleKey, inputFile.relativePath()); + public InputPathCache remove(String moduleKey, InputFile inputFile) { + cache.remove(moduleKey, FILE, inputFile.relativePath()); return this; } - public InputFileCache put(String moduleKey, InputFile inputFile) { - cache.put(moduleKey, inputFile.relativePath(), inputFile); + public InputPathCache put(String moduleKey, InputFile inputFile) { + cache.put(moduleKey, FILE, inputFile.relativePath(), inputFile); return this; } @CheckForNull - public InputFile get(String moduleKey, String relativePath) { - return cache.get(moduleKey, relativePath); + public InputFile getFile(String moduleKey, String relativePath) { + return (InputFile) cache.get(moduleKey, FILE, relativePath); + } + + public InputDir getDir(String moduleKey, String relativePath) { + return (InputDir) cache.get(moduleKey, DIR, relativePath); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java index d555074db79..c11d585a9c5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java @@ -21,37 +21,34 @@ package org.sonar.batch.scan.filesystem; import org.sonar.api.BatchComponent; import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.RelativePathPredicate; -import org.sonar.api.resources.Project; public class ModuleInputFileCache extends DefaultFileSystem.Cache implements BatchComponent { private final String moduleKey; - private final InputFileCache projectCache; + private final InputPathCache projectCache; - public ModuleInputFileCache(Project module, ProjectDefinition projectDef, InputFileCache projectCache) { - this.moduleKey = module.getKey(); - this.projectCache = projectCache; - } - - /** - * Used by scan2 - */ - public ModuleInputFileCache(ProjectDefinition projectDef, InputFileCache projectCache) { - this.moduleKey = projectDef.getKey(); + public ModuleInputFileCache(ProjectDefinition projectDef, InputPathCache projectCache) { + this.moduleKey = projectDef.getKeyWithBranch(); this.projectCache = projectCache; } @Override protected Iterable<InputFile> inputFiles() { - return projectCache.byModule(moduleKey); + return projectCache.filesByModule(moduleKey); } @Override protected InputFile inputFile(RelativePathPredicate predicate) { - return projectCache.get(moduleKey, predicate.path()); + return projectCache.getFile(moduleKey, predicate.path()); + } + + @Override + protected InputDir inputDir(String relativePath) { + return projectCache.getDir(moduleKey, relativePath); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java index fe8ddc7826d..a5334ace9d6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java @@ -26,8 +26,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; +import org.sonar.api.batch.fs.InputPath; +import org.sonar.api.batch.fs.internal.DefaultInputDir; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.config.Settings; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.platform.Server; @@ -43,9 +46,13 @@ import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.events.BatchStepEvent; import org.sonar.batch.events.EventBus; import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; -import java.io.*; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -67,11 +74,11 @@ public class JsonReport implements BatchComponent { private final EventBus eventBus; private final AnalysisMode analysisMode; private final UserFinder userFinder; - private final InputFileCache fileCache; + private final InputPathCache fileCache; private final Project rootModule; public JsonReport(Settings settings, FileSystem fileSystem, Server server, RuleFinder ruleFinder, IssueCache issueCache, - EventBus eventBus, AnalysisMode analysisMode, UserFinder userFinder, Project rootModule, InputFileCache fileCache) { + EventBus eventBus, AnalysisMode analysisMode, UserFinder userFinder, Project rootModule, InputPathCache fileCache) { this.settings = settings; this.fileSystem = fileSystem; this.server = server; @@ -167,16 +174,28 @@ public class JsonReport implements BatchComponent { json.name("components").beginArray(); // Dump modules writeJsonModuleComponents(json, rootModule); - // TODO we need to dump directories - for (InputFile inputFile : fileCache.all()) { - String key = ((DeprecatedDefaultInputFile) inputFile).key(); - json - .beginObject() - .prop("key", key) - .prop("path", inputFile.relativePath()) - .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) - .prop("status", inputFile.status().name()) - .endObject(); + for (InputPath inputPath : fileCache.all()) { + if (inputPath instanceof InputFile) { + InputFile inputFile = (InputFile) inputPath; + String key = ((DefaultInputFile) inputFile).key(); + json + .beginObject() + .prop("key", key) + .prop("path", inputFile.relativePath()) + .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) + .prop("status", inputFile.status().name()) + .endObject(); + } else { + InputDir inputDir = (InputDir) inputPath; + String key = ((DefaultInputDir) inputDir).key(); + json + .beginObject() + .prop("key", key) + .prop("path", inputDir.relativePath()) + .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) + .endObject(); + } + } json.endArray(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java index addc6e18e3c..603cf47bb3f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java @@ -26,16 +26,16 @@ import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.measures.FileLinesContext; import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.resources.Resource; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; public class DefaultFileLinesContextFactory implements FileLinesContextFactory { private final AnalyzerMeasureCache measureCache; private final MetricFinder metricFinder; private final ProjectDefinition def; - private InputFileCache fileCache; + private InputPathCache fileCache; - public DefaultFileLinesContextFactory(InputFileCache fileCache, FileSystem fs, MetricFinder metricFinder, AnalyzerMeasureCache measureCache, + public DefaultFileLinesContextFactory(InputPathCache fileCache, FileSystem fs, MetricFinder metricFinder, AnalyzerMeasureCache measureCache, ProjectDefinition def) { this.fileCache = fileCache; this.metricFinder = metricFinder; @@ -50,7 +50,7 @@ public class DefaultFileLinesContextFactory implements FileLinesContextFactory { @Override public FileLinesContext createFor(InputFile inputFile) { - if (fileCache.get(def.getKey(), inputFile.relativePath()) == null) { + if (fileCache.getFile(def.getKey(), inputFile.relativePath()) == null) { throw new IllegalStateException("InputFile is not indexed: " + inputFile); } return new DefaultFileLinesContext(metricFinder, measureCache, def.getKey(), inputFile); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java index 91b11ce80da..b0738e420e4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java @@ -41,7 +41,7 @@ import org.sonar.batch.referential.ProjectReferentialsLoader; import org.sonar.batch.referential.ProjectReferentialsProvider; import org.sonar.batch.scan.ProjectReactorBuilder; import org.sonar.batch.scan.ProjectSettings; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.batch.scan.maven.FakeMavenPluginExecutor; import org.sonar.batch.scan.maven.MavenPluginExecutor; @@ -98,7 +98,7 @@ public class ProjectScanContainer extends ComponentContainer { AnalyzerMeasureCache.class, // file system - InputFileCache.class, + InputPathCache.class, PathResolver.class, // issues diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java index 3da4eeba2c0..6a81b667c7c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java @@ -32,7 +32,7 @@ import org.sonar.batch.index.CachesTest; import static org.fest.assertions.Assertions.assertThat; -public class InputFileCacheTest { +public class InputPathCacheTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -52,16 +52,16 @@ public class InputFileCacheTest { @Test public void should_add_input_file() throws Exception { - InputFileCache cache = new InputFileCache(caches); + InputPathCache cache = new InputPathCache(caches); DefaultInputFile fooFile = new DefaultInputFile("src/main/java/Foo.java").setFile(temp.newFile("Foo.java")); cache.put("struts", fooFile); cache.put("struts-core", new DeprecatedDefaultInputFile("src/main/java/Bar.java").setFile(temp.newFile("Bar.java"))); - assertThat(cache.get("struts", "src/main/java/Foo.java").relativePath()) + assertThat(cache.getFile("struts", "src/main/java/Foo.java").relativePath()) .isEqualTo("src/main/java/Foo.java"); - assertThat(cache.byModule("struts")).hasSize(1); - assertThat(cache.byModule("struts-core")).hasSize(1); + assertThat(cache.filesByModule("struts")).hasSize(1); + assertThat(cache.filesByModule("struts-core")).hasSize(1); assertThat(cache.all()).hasSize(2); for (InputFile inputFile : cache.all()) { assertThat(inputFile.relativePath()).startsWith("src/main/java/"); @@ -71,8 +71,8 @@ public class InputFileCacheTest { assertThat(cache.all()).hasSize(1); cache.removeModule("struts"); - assertThat(cache.byModule("struts")).hasSize(0); - assertThat(cache.byModule("struts-core")).hasSize(1); + assertThat(cache.filesByModule("struts")).hasSize(0); + assertThat(cache.filesByModule("struts-core")).hasSize(1); assertThat(cache.all()).hasSize(1); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java index 6f4f306a0c5..59092657927 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java @@ -44,7 +44,7 @@ import org.sonar.api.user.UserFinder; import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.events.EventBus; import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.scan.filesystem.InputFileCache; +import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.core.user.DefaultUser; import java.io.File; @@ -88,7 +88,7 @@ public class JsonReportTest { DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile("src/main/java/org/apache/struts/Action.java"); inputFile.setKey("struts:src/main/java/org/apache/struts/Action.java"); inputFile.setStatus(InputFile.Status.CHANGED); - InputFileCache fileCache = mock(InputFileCache.class); + InputPathCache fileCache = mock(InputPathCache.class); when(fileCache.all()).thenReturn(Arrays.<InputFile>asList(inputFile)); Project rootModule = new Project("struts"); Project moduleA = new Project("struts-core"); |