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-plugin-api | |
parent | cdd15cb557eecbb39c09bedb64defb363671394b (diff) | |
download | sonarqube-7feff9997bfa2df921684cc83cd7f02b1f6204e8.tar.gz sonarqube-7feff9997bfa2df921684cc83cd7f02b1f6204e8.zip |
SONAR-3677 Improve API
Diffstat (limited to 'sonar-plugin-api')
6 files changed, 64 insertions, 60 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java index e4be84847af..6e858ee2511 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java @@ -42,7 +42,7 @@ public class FileQuery { public static FileQuery on(FileType... types) { FileQuery query = new FileQuery(); for (FileType type : types) { - query.on(InputFile.ATTRIBUTE_TYPE, type.name().toLowerCase()); + query.on(InputFile.ATTRIBUTE_TYPE, type.name()); } return query; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java index 890985ae5e2..c89e3f78d21 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java @@ -46,59 +46,50 @@ public interface InputFile extends Serializable { /** * */ - String ATTRIBUTE_TYPE = "type"; - String TYPE_SOURCE = "source"; - String TYPE_TEST = "test"; + String ATTRIBUTE_TYPE = "TYPE"; + String TYPE_SOURCE = "SOURCE"; + String TYPE_TEST = "TEST"; - String ATTRIBUTE_STATUS = "status"; - String STATUS_SAME = "same"; - String STATUS_CHANGED = "changed"; - String STATUS_ADDED = "added"; + String ATTRIBUTE_STATUS = "STATUS"; + String STATUS_SAME = "SAME"; + String STATUS_CHANGED = "CHANGED"; + String STATUS_ADDED = "ADDED"; - String ATTRIBUTE_HASH = "hash"; - String ATTRIBUTE_EXTENSION = "extension"; + String ATTRIBUTE_HASH = "HASH"; /** - * Path from module base directory. Path is unique and identifies file within given - * <code>{@link org.sonar.api.scan.filesystem.ModuleFileSystem}</code>. File separator is the forward slash ('/'), - * even on MSWindows. - * <p/> - * If: - * <ul> - * <li>Module base dir is <code>/absolute/path/to/module</code></li> - * <li>File is <code>/absolute/path/to/module/src/main/java/com/Foo.java</code></li> - * </ul> - * then the path is <code>src/main/java/com/Foo.java</code> + * Path is relative from module base directory. Path is unique and identifies file + * within given <code>{@link org.sonar.api.scan.filesystem.ModuleFileSystem}</code>. + * File separator is the forward slash ('/'), even on MSWindows. * <p/> - * On MSWindows, if: - * <ul> - * <li>Module base dir is <code>C:\absolute\path\to\module</code></li> - * <li>File is <code>C:\absolute\path\to\module\src\main\java\com\Foo.java</code></li> - * </ul> - * then the path is <code>src/main/java/com/Foo.java</code>. + * Returns <code>src/main/java/com/Foo.java</code> if module base dir is + * <code>/absolute/path/to/module</code> and if file is + * <code>/absolute/path/to/module/src/main/java/com/Foo.java</code>. * <p/> - * Returned relative path is never null. - */ - String relativePath(); - - /** - * Canonical path. + * Returned path is never null. */ String path(); /** - * Not-null related {@link java.io.File} + * Not-null canonical path. File separator is forward slash ('/'), even on MSWindows. */ + String absolutePath(); + File file(); /** * Not-null filename, including extension */ - String name(); /** + * Not-null type (is it a source file or a unit test file?). + * See constant values prefixed by <code>TYPE_</code>, for example {@link #TYPE_SOURCE}. + */ + String type(); + + /** * Does the given attribute have the given value ? */ boolean has(String attribute, String value); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java index dcd7cc1018d..f6bcadbb070 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java @@ -87,12 +87,14 @@ public interface ModuleFileSystem extends BatchComponent { Iterable<InputFile> inputFiles(FileQuery query); /** - * Charset of source and test files. If it's not defined, then return the platform default charset. + * Charset of source and test files. If it's not defined, then + * return the platform default charset. */ Charset sourceCharset(); /** - * Working directory used by Sonar. This directory can be used for example to store intermediary reports. + * Working directory used by Sonar. This directory can be used for example to + * store intermediary reports. */ File workingDir(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java index b56f99f2093..663fd097a93 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java @@ -29,20 +29,20 @@ import java.io.File; import java.util.Map; /** - * PLUGINS MUST NOT USE THIS CLASS. + * PLUGINS MUST NOT USE THIS CLASS, EVEN FOR UNIT TESTING. * * @since 4.0 */ public class DefaultInputFile implements InputFile { + private final String absolutePath; private final String path; - private final String relativePath; private final Map<String, String> attributes; - private DefaultInputFile(File file, String relativePath, Map<String, String> attributes) { - this.path = PathUtils.canonicalPath(file); - this.relativePath = FilenameUtils.separatorsToUnix(relativePath); - this.attributes = attributes; + private DefaultInputFile(File file, String path, Map<String, String> attributes) { + this.absolutePath = PathUtils.canonicalPath(file); + this.path = FilenameUtils.separatorsToUnix(path); + this.attributes = attributes; } /** @@ -51,23 +51,23 @@ public class DefaultInputFile implements InputFile { * <p/> * Usage: <code>InputFile.create(file, "src/main/java/com/Foo.java", attributes)</code> */ - public static DefaultInputFile create(File file, String relativePath, Map<String, String> attributes) { - return new DefaultInputFile(file, relativePath, attributes); + public static DefaultInputFile create(File file, String path, Map<String, String> attributes) { + return new DefaultInputFile(file, path, attributes); } @Override - public String relativePath() { - return relativePath; + public String path() { + return path; } @Override - public String path() { - return path; + public String absolutePath() { + return absolutePath; } @Override public File file() { - return new File(path); + return new File(absolutePath); } @Override @@ -76,6 +76,11 @@ public class DefaultInputFile implements InputFile { } @Override + public String type() { + return attribute(ATTRIBUTE_TYPE); + } + + @Override public boolean has(String attribute, String value) { return StringUtils.equals(attributes.get(attribute), value); } @@ -100,11 +105,16 @@ public class DefaultInputFile implements InputFile { return false; } DefaultInputFile other = (DefaultInputFile) o; - return path.equals(other.path); + return absolutePath.equals(other.absolutePath); } @Override public int hashCode() { - return path.hashCode(); + return absolutePath.hashCode(); + } + + @Override + public String toString() { + return String.format("[%s,%s]", path, type()); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/InputFileBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/InputFileBuilder.java index 8a83437fd56..2a081efe312 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/InputFileBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/InputFileBuilder.java @@ -58,10 +58,6 @@ public class InputFileBuilder { return attribute(InputFile.ATTRIBUTE_TYPE, type); } - public InputFileBuilder extension(@Nullable String extension) { - return attribute(InputFile.ATTRIBUTE_EXTENSION, extension); - } - public InputFileBuilder language(@Nullable String language) { return attribute(InputFile.ATTRIBUTE_LANGUAGE, language); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/DefaultInputFileTest.java index 203895e111a..afeb907394c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/DefaultInputFileTest.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.scan.filesystem.InputFile; +import org.sonar.api.utils.PathUtils; import java.io.File; import java.io.IOException; @@ -41,17 +42,15 @@ public class DefaultInputFileTest { InputFile input = new InputFileBuilder(file, "src/main/java/Foo.java") .attribute("foo", "bar") .type(InputFile.TYPE_TEST) - .extension("jav") .hash("ABC") .status(InputFile.STATUS_ADDED) .language("java") .build(); - assertThat(input.attributes()).hasSize(6); + assertThat(input.attributes()).hasSize(5); assertThat(input.attribute("unknown")).isNull(); assertThat(input.attribute("foo")).isEqualTo("bar"); assertThat(input.attribute(InputFile.ATTRIBUTE_TYPE)).isEqualTo(InputFile.TYPE_TEST); - assertThat(input.attribute(InputFile.ATTRIBUTE_EXTENSION)).isEqualTo("jav"); assertThat(input.attribute(InputFile.ATTRIBUTE_HASH)).isEqualTo("ABC"); assertThat(input.attribute(InputFile.ATTRIBUTE_LANGUAGE)).isEqualTo("java"); assertThat(input.attribute(InputFile.ATTRIBUTE_STATUS)).isEqualTo(InputFile.STATUS_ADDED); @@ -72,8 +71,8 @@ public class DefaultInputFileTest { assertThat(input.name()).isEqualTo("Foo.java"); assertThat(input.file()).isEqualTo(file); assertThat(input.attribute(InputFile.ATTRIBUTE_SOURCEDIR_PATH)).isEqualTo(FilenameUtils.separatorsToUnix(sourceDir.getAbsolutePath())); - assertThat(input.relativePath()).isEqualTo("src/main/java/Foo.java"); - assertThat(input.path()).isEqualTo(FilenameUtils.separatorsToUnix(file.getCanonicalPath())); + assertThat(input.path()).isEqualTo("src/main/java/Foo.java"); + assertThat(input.absolutePath()).isEqualTo(PathUtils.canonicalPath(file)); } @Test @@ -88,6 +87,12 @@ public class DefaultInputFileTest { assertThat(input1.equals(input2)).isFalse(); assertThat(input1.hashCode()).isEqualTo(input1.hashCode()); assertThat(input1.hashCode()).isEqualTo(input1a.hashCode()); + } + @Test + public void test_toString() throws Exception { + File file1 = temp.newFile(); + InputFile input = new InputFileBuilder(file1, "src/main/java/Foo.java").type(InputFile.TYPE_TEST).build(); + assertThat(input.toString()).isEqualTo("[src/main/java/Foo.java,TEST]"); } } |