diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 23:17:11 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 23:17:11 +0200 |
commit | 7feff9997bfa2df921684cc83cd7f02b1f6204e8 (patch) | |
tree | a1209c38a0903d25a921389d4c909bf5b66b8000 /sonar-batch | |
parent | cdd15cb557eecbb39c09bedb64defb363671394b (diff) | |
download | sonarqube-7feff9997bfa2df921684cc83cd7f02b1f6204e8.tar.gz sonarqube-7feff9997bfa2df921684cc83cd7f02b1f6204e8.zip |
SONAR-3677 Improve API
Diffstat (limited to 'sonar-batch')
9 files changed, 43 insertions, 40 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/JsonReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/JsonReport.java index 49bfcc5fa02..224989eb951 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/JsonReport.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/JsonReport.java @@ -160,6 +160,8 @@ public class JsonReport implements BatchComponent { json .beginObject() .name("key").value(componentKey) + // module + // path .endObject(); } json.endArray(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java index 9045cb7b8f0..571088ac7cb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java @@ -141,7 +141,6 @@ public class DefaultModuleFileSystem implements ModuleFileSystem, Startable { @Override public Iterable<InputFile> inputFiles(FileQuery query) { List<InputFile> result = Lists.newArrayList(); - FileQueryFilter filter = new FileQueryFilter(settings, query); for (InputFile input : index.inputFiles(moduleKey)) { if (filter.accept(input)) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java index c4bbcdd9804..c529db12a39 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java @@ -78,7 +78,7 @@ public class DeprecatedFileFilters implements InputFileFilter { @Override public String canonicalPath() { - return inputFile.path(); + return inputFile.absolutePath(); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java index 091e8790413..c625b4a4ae2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java @@ -21,7 +21,6 @@ package org.sonar.batch.scan.filesystem; import com.google.common.collect.Maps; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.HiddenFileFilter; import org.apache.commons.io.filefilter.IOFileFilter; @@ -52,15 +51,15 @@ public class FileIndex implements BatchComponent { private static class Progress { private int count = 0; - private final Set<String> removedRelativePaths; + private final Set<String> removedPaths; - Progress(Set<String> removedRelativePaths) { - this.removedRelativePaths = removedRelativePaths; + Progress(Set<String> removedPaths) { + this.removedPaths = removedPaths; } void markAsIndexed(String relativePath) { count++; - removedRelativePaths.remove(relativePath); + removedPaths.remove(relativePath); } } @@ -109,8 +108,8 @@ public class FileIndex implements BatchComponent { } // Remove files that have been removed since previous indexation - for (String relativePath : progress.removedRelativePaths) { - cache.remove(fileSystem.moduleKey(), relativePath); + for (String path : progress.removedPaths) { + cache.remove(fileSystem.moduleKey(), path); } logger.info(String.format("%d files indexed", progress.count)); @@ -139,41 +138,37 @@ public class FileIndex implements BatchComponent { } private void indexFile(ModuleFileSystem fileSystem, Progress status, File sourceDir, File file, String type) { - String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file); - if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) { + String path = pathResolver.relativePath(fileSystem.baseDir(), file); + if (!cache.containsFile(fileSystem.moduleKey(), path)) { // Not indexed yet - InputFile input = newInputFile(fileSystem, sourceDir, type, file, relativePath); + InputFile input = newInputFile(fileSystem, sourceDir, type, file, path); if (input != null && accept(input)) { cache.put(fileSystem.moduleKey(), input); } } - status.markAsIndexed(relativePath); + status.markAsIndexed(path); } @CheckForNull - private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file, String baseRelativePath) { + private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file, String path) { // File extension must be kept case-sensitive - String extension = FilenameUtils.getExtension(file.getName()); - String lang = languageRecognizer.ofExtension(extension); + String lang = languageRecognizer.of(file); if (lang == null) { return null; } - Map<String, String> attributes = Maps.newHashMap(); - set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension); - set(attributes, InputFile.ATTRIBUTE_TYPE, type); - set(attributes, InputFile.ATTRIBUTE_LANGUAGE, lang); + Map<String, String> attributes = Maps.newHashMap(); + set(attributes, InputFile.ATTRIBUTE_TYPE, type); + set(attributes, InputFile.ATTRIBUTE_LANGUAGE, lang); - // paths - set(attributes, InputFile.ATTRIBUTE_SOURCEDIR_PATH, PathUtils.canonicalPath(file)); - set(attributes, InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, pathResolver.relativePath(sourceDir, file)); - - // hash + status - initStatus(file, fileSystem.sourceCharset(), baseRelativePath, attributes); - - return DefaultInputFile.create(file, baseRelativePath, attributes); + // paths + set(attributes, InputFile.ATTRIBUTE_SOURCEDIR_PATH, PathUtils.canonicalPath(sourceDir)); + set(attributes, InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, pathResolver.relativePath(sourceDir, file)); + // hash + status + initStatus(file, fileSystem.sourceCharset(), path, attributes); + return DefaultInputFile.create(file, path, attributes); } private void initStatus(File file, Charset charset, String baseRelativePath, Map<String, String> attributes) { 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/InputFileCache.java index bdae94fb996..18ee293adab 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/InputFileCache.java @@ -66,7 +66,7 @@ public class InputFileCache implements BatchComponent { } public InputFileCache put(String moduleKey, InputFile file) { - cache.put(moduleKey, file.relativePath(), file); + cache.put(moduleKey, file.path(), file); return this; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageRecognizer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageRecognizer.java index 98e30d690e5..e5c6ebc03fc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageRecognizer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageRecognizer.java @@ -20,12 +20,14 @@ package org.sonar.batch.scan.filesystem; import com.google.common.collect.Maps; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; import org.picocontainer.Startable; import org.sonar.api.BatchComponent; import org.sonar.api.resources.Language; import javax.annotation.CheckForNull; +import java.io.File; import java.util.Map; /** @@ -68,9 +70,10 @@ public class LanguageRecognizer implements BatchComponent, Startable { // TODO what about cobol files without extension ? @CheckForNull - String ofExtension(String fileExtension) { - if (StringUtils.isNotBlank(fileExtension)) { - return byExtensions.get(StringUtils.lowerCase(fileExtension)); + String of(File file) { + String extension = FilenameUtils.getExtension(file.getName()); + if (StringUtils.isNotBlank(extension)) { + return byExtensions.get(StringUtils.lowerCase(extension)); } return null; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/PathPattern.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/PathPattern.java index 0f3240a1159..459c579c06c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/PathPattern.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/PathPattern.java @@ -61,7 +61,7 @@ abstract class PathPattern { @Override boolean match(InputFile inputFile) { - return pattern.match(inputFile.path()); + return pattern.match(inputFile.absolutePath()); } @Override 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/InputFileCacheTest.java index 3b576e8a3fc..a2474ac5ce8 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/InputFileCacheTest.java @@ -58,7 +58,7 @@ public class InputFileCacheTest { assertThat(cache.byModule("struts-core")).hasSize(1); assertThat(cache.all()).hasSize(2); for (InputFile inputFile : cache.all()) { - assertThat(inputFile.relativePath()).startsWith("src/main/java"); + assertThat(inputFile.path()).startsWith("src/main/java"); } assertThat(cache.fileRelativePaths("struts-core")).containsOnly("src/main/java/Foo.java"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java index 5a53538434e..c4eab635eeb 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java @@ -19,7 +19,9 @@ */ package org.sonar.batch.scan.filesystem; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.resources.Java; import org.sonar.api.resources.Language; @@ -28,6 +30,9 @@ import static org.fest.assertions.Fail.fail; public class LanguageRecognizerTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void test_sanitizeExtension() throws Exception { assertThat(LanguageRecognizer.sanitizeExtension(".cbl")).isEqualTo("cbl"); @@ -42,12 +47,11 @@ public class LanguageRecognizerTest { LanguageRecognizer recognizer = new LanguageRecognizer(languages); recognizer.start(); - assertThat(recognizer.ofExtension("java")).isEqualTo(Java.KEY); - assertThat(recognizer.ofExtension("cbl")).isEqualTo("cobol"); - assertThat(recognizer.ofExtension("CBL")).isEqualTo("cobol"); - assertThat(recognizer.ofExtension("php")).isNull(); - assertThat(recognizer.ofExtension("")).isNull(); - assertThat(recognizer.ofExtension(null)).isNull(); + assertThat(recognizer.of(temp.newFile("Foo.java"))).isEqualTo(Java.KEY); + assertThat(recognizer.of(temp.newFile("abc.cbl"))).isEqualTo("cobol"); + assertThat(recognizer.of(temp.newFile("abc.CBL"))).isEqualTo("cobol"); + assertThat(recognizer.of(temp.newFile("abc.php"))).isNull(); + assertThat(recognizer.of(temp.newFile("abc"))).isNull(); recognizer.stop(); } |