diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 01:19:57 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 01:19:57 +0200 |
commit | 7cdb8cbfb54bcb028300674f18ce08ea3ee35858 (patch) | |
tree | f3bffef9fac71d13c4e48553e7b4bc2c49625efc /sonar-plugin-api | |
parent | c2a0f4f06564c1d62f08d4e20489f70f0f4ad223 (diff) | |
download | sonarqube-7cdb8cbfb54bcb028300674f18ce08ea3ee35858.tar.gz sonarqube-7cdb8cbfb54bcb028300674f18ce08ea3ee35858.zip |
SONAR-3677 refactor API
Diffstat (limited to 'sonar-plugin-api')
13 files changed, 511 insertions, 215 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 3135b7c27cf..e4be84847af 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 @@ -37,7 +37,7 @@ import java.util.Set; */ public class FileQuery { - // TODO better builders, for example FileQuery.ALL + // TODO REFACTOR - better builders, for example FileQuery.ALL public static FileQuery on(FileType... types) { FileQuery query = new FileQuery(); @@ -62,7 +62,7 @@ public class FileQuery { private FileQuery() { } - private FileQuery on(String attribute, String... values) { + public FileQuery on(String attribute, String... values) { for (String value : values) { attributes.put(attribute, value); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java index 31f3938da22..8100c473f09 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java @@ -21,7 +21,7 @@ package org.sonar.api.scan.filesystem; /** * @since 3.5 - * @deprecated in 4.0. Replaced by more flexible {@link InputFile} attributes. + * @deprecated in 4.0. Replaced by more flexible {@link org.sonar.api.scan.filesystem.InputFile} attributes. */ @Deprecated public enum FileType { 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 c6638353224..dd358dff6e2 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 @@ -1,141 +1,94 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.api.scan.filesystem; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; - import javax.annotation.CheckForNull; import java.io.File; import java.io.Serializable; -import java.util.HashMap; -import java.util.List; import java.util.Map; -/** - * @since 4.0 - */ -public class InputFile implements Serializable { +public interface InputFile extends Serializable { + + /** + * Canonical path of source directory. + * Example: <code>/path/to/module/src/main/java</code> or <code>C:\path\to\module\src\main\java</code> + */ + String ATTRIBUTE_SOURCEDIR_PATH = "srcDirPath"; - // TODO refactor attribute constants as classes or enums ? + /** + * Relative path from source directory. File separator is the forward slash ('/'), + * even on MSWindows. + */ + String ATTRIBUTE_SOURCE_RELATIVE_PATH = "srcRelPath"; /** - * Path relative to module base directory. + * Detected language */ - public static final String ATTRIBUTE_BASE_RELATIVE_PATH = "baseRelPath"; - - // TODO ambiguity of term "source" with sourceDir versus testDir properties - // Here it does not depend on type. - public static final String ATTRIBUTE_SOURCEDIR_PATH = "srcDirPath"; - public static final String ATTRIBUTE_SOURCE_RELATIVE_PATH = "srcRelPath"; - - public static final String ATTRIBUTE_CANONICAL_PATH = "canonicalPath"; - public static final String ATTRIBUTE_LANGUAGE = "lang"; - public static final String ATTRIBUTE_TYPE = "type"; - public static final String ATTRIBUTE_STATUS = "status"; - public static final String STATUS_SAME = "same"; - public static final String STATUS_CHANGED = "changed"; - public static final String STATUS_ADDED = "added"; - public static final String ATTRIBUTE_HASH = "checksum"; - public static final String ATTRIBUTE_EXTENSION = "extension"; - public static final String TYPE_SOURCE = "source"; - public static final String TYPE_TEST = "test"; - - // TODO limitation of persistit -> add unit test - private transient File transientFile; - private Map<String, String> attributes; - - private InputFile(File file, Map<String, String> attributes) { - this.transientFile = file; - this.attributes = attributes; - } + String ATTRIBUTE_LANGUAGE = "lang"; /** - * Plugins should not build their own instances of {@link InputFile}. This method - * aims to be used by unit tests. - * // TODO provide builder ? + * */ - public static InputFile create(File file, String baseRelativePath, Map<String, String> attributes) { - Map<String,String> copy = new HashMap<String, String>(attributes); - copy.put(InputFile.ATTRIBUTE_BASE_RELATIVE_PATH, baseRelativePath); - return new InputFile(file, copy); - } + 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_HASH = "hash"; + String ATTRIBUTE_EXTENSION = "extension"; + /** * Path from module base directory. Path is unique and identifies file within given - * <code>{@link ModuleFileSystem}</code>. File separator is the forward slash ('/'), + * <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> + * <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>. + * <p/> + * Returned relative path is never null. */ - public String path() { - return attribute(ATTRIBUTE_BASE_RELATIVE_PATH); - } + String relativePath(); - public File file() { - if (transientFile == null) { - transientFile = new File(attribute(ATTRIBUTE_CANONICAL_PATH)); - } - return transientFile; - } + /** + * Canonical path. + */ + String path(); - public String name() { - return file().getName(); - } + /** + * Not-null related {@link java.io.File} + */ + File file(); - public boolean has(String attribute, String value) { - return StringUtils.equals(attributes.get(attribute), value); - } + /** + * Not-null filename, including extension + */ - @CheckForNull - public String attribute(String key) { - return attributes.get(key); - } - - public Map<String, String> attributes() { - return attributes; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - InputFile inputFile = (InputFile) o; - return attribute(ATTRIBUTE_CANONICAL_PATH).equals(inputFile.attribute(ATTRIBUTE_CANONICAL_PATH)); - } - - @Override - public int hashCode() { - return path().hashCode(); - } - - public static List<File> toFiles(Iterable<InputFile> inputFiles) { - List<File> files = Lists.newArrayList(); - for (InputFile inputFile : inputFiles) { - files.add(inputFile.file()); - } - return files; - } + String name(); + /** + * Does the given attribute have the given value ? + */ + boolean has(String attribute, String value); + + /** + * See list of attribute keys in constants starting with ATTRIBUTE_. + */ + @CheckForNull + String attribute(String key); + Map<String, String> attributes(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java index 2b166827db8..a09f5df6c17 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java @@ -23,6 +23,7 @@ import org.sonar.api.BatchExtension; /** * TODO document lifecycle -> executed when initializing project + * * @since 4.0 */ public interface InputFileFilter extends BatchExtension { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFiles.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFiles.java new file mode 100644 index 00000000000..a6e2fc8bd6d --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFiles.java @@ -0,0 +1,42 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem; + +import com.google.common.collect.Lists; + +import java.io.File; +import java.util.List; + +/** + * @since 4.0 + */ +public class InputFiles { + InputFiles() { + // static methods only + } + + public static List<File> toFiles(Iterable<InputFile> inputFiles) { + List<File> files = Lists.newArrayList(); + for (InputFile inputFile : inputFiles) { + files.add(inputFile.file()); + } + return files; + } +} 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 f8c8257141f..7f5dacc9d87 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 @@ -20,7 +20,6 @@ package org.sonar.api.scan.filesystem; import org.sonar.api.BatchComponent; -import org.sonar.api.resources.InputFile; import javax.annotation.CheckForNull; import java.io.File; @@ -82,6 +81,11 @@ public interface ModuleFileSystem extends BatchComponent { List<File> files(FileQuery query); /** + * @since 4.0 + */ + Iterable<InputFile> inputFiles(FileQuery query); + + /** * Charset of source and test files. If it's not defined, then return the platform default charset. */ Charset sourceCharset(); 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 new file mode 100644 index 00000000000..5c9a2dd5423 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java @@ -0,0 +1,114 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem.internal; + +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.scan.filesystem.InputFile; + +import javax.annotation.CheckForNull; +import java.io.File; +import java.io.IOException; +import java.util.Map; + +/** + * PLUGINS MUST NOT USE THIS CLASS. + * + * @since 4.0 + */ +public class DefaultInputFile implements InputFile { + + private final String path; + private final String relativePath; + private final Map<String, String> attributes; + + private DefaultInputFile(File file, String relativePath, Map<String, String> attributes) { + try { + this.path = file.getCanonicalPath(); + this.relativePath = FilenameUtils.separatorsToUnix(relativePath); + this.attributes = attributes; + } catch (IOException e) { + throw new IllegalStateException("Fail to get canonical path of: " + file, e); + } + } + + /** + * Plugins must not build their own instances of {@link InputFile}. + * {@link org.sonar.api.scan.filesystem.ModuleFileSystem} must be used to search for files to scan. + * <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); + } + + @Override + public String relativePath() { + return relativePath; + } + + @Override + public String path() { + return path; + } + + @Override + public File file() { + return new File(path); + } + + @Override + public String name() { + return file().getName(); + } + + @Override + public boolean has(String attribute, String value) { + return StringUtils.equals(attributes.get(attribute), value); + } + + @Override + @CheckForNull + public String attribute(String key) { + return attributes.get(key); + } + + @Override + public Map<String, String> attributes() { + return attributes; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DefaultInputFile other = (DefaultInputFile) o; + return path.equals(other.path); + } + + @Override + public int hashCode() { + return path.hashCode(); + } +} 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 new file mode 100644 index 00000000000..60d0af7567e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/InputFileBuilder.java @@ -0,0 +1,87 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem.internal; + +import org.sonar.api.scan.filesystem.InputFile; + +import javax.annotation.Nullable; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * FOR UNIT-TESTING ONLY + * + * @since 4.0 + */ +public class InputFileBuilder { + + private final Map<String, String> attributes = new HashMap<String, String>(); + private final File file; + private final String relativePath; + + public static void _FOR_UNIT_TESTING_ONLY_() { + // For those who don't read javadoc + } + + public InputFileBuilder(File file, String relativePath) { + this.file = file; + this.relativePath = relativePath; + } + + public InputFileBuilder attribute(String key, @Nullable String value) { + if (value != null) { + attributes.put(key, value); + } + return this; + } + + public InputFileBuilder type(@Nullable String type) { + 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); + } + + public InputFileBuilder hash(@Nullable String hash) { + return attribute(InputFile.ATTRIBUTE_HASH, hash); + } + + public InputFileBuilder status(@Nullable String status) { + return attribute(InputFile.ATTRIBUTE_STATUS, status); + } + + public InputFileBuilder sourceDir(File dir) { + return sourceDir(dir.getAbsolutePath()); + } + + public InputFileBuilder sourceDir(@Nullable String path) { + return attribute(InputFile.ATTRIBUTE_SOURCEDIR_PATH, path); + } + + public DefaultInputFile build() { + return DefaultInputFile.create(file, relativePath, attributes); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/package-info.java new file mode 100644 index 00000000000..2018417ac1c --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.api.scan.filesystem.internal; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/InputFilesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/InputFilesTest.java new file mode 100644 index 00000000000..cb1f210b454 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/InputFilesTest.java @@ -0,0 +1,46 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem; + +import com.google.common.collect.Lists; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.scan.filesystem.internal.InputFileBuilder; + +import java.io.File; + +import static org.fest.assertions.Assertions.assertThat; + +public class InputFilesTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void test_toFiles() throws Exception { + File file1 = temp.newFile(); + File file2 = temp.newFile(); + InputFile input1 = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); + InputFile input2 = new InputFileBuilder(file2, "src/main/java/Bar.java").build(); + + assertThat(InputFiles.toFiles(Lists.newArrayList(input1, input2))).containsOnly(file1, file2); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java deleted file mode 100644 index 4d821c0bf8e..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.api.scan.filesystem; - -import com.google.common.collect.Lists; -import org.apache.commons.lang.CharEncoding; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.Collections; -import java.util.List; - -/** - * @since 3.5 - */ -public class SimpleModuleFileSystem implements ModuleFileSystem { - private File baseDir; - private File buildDir; - private List<File> sourceDirs = Lists.newArrayList(); - private List<File> testDirs = Lists.newArrayList(); - private List<File> binaryDirs = Lists.newArrayList(); - - public SimpleModuleFileSystem(File baseDir) { - this.baseDir = baseDir; - this.buildDir = new File(baseDir, "build"); - } - - @Override - public String moduleKey() { - return null; - } - - public File baseDir() { - return baseDir; - } - - public File buildDir() { - return buildDir; - } - - public List<File> sourceDirs() { - return sourceDirs; - } - - public List<File> testDirs() { - return testDirs; - } - - public List<File> binaryDirs() { - return binaryDirs; - } - - public SimpleModuleFileSystem addSourceDir(File d) { - sourceDirs.add(d); - return this; - } - - public SimpleModuleFileSystem addTestDir(File d) { - testDirs.add(d); - return this; - } - - public SimpleModuleFileSystem addBinaryDir(File d) { - binaryDirs.add(d); - return this; - } - - public List<File> files(FileQuery query) { - return Collections.emptyList(); - } - - public Charset sourceCharset() { - return Charset.forName(CharEncoding.UTF_8); - } - - public File workingDir() { - return new File(baseDir, "work"); - } -} 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 new file mode 100644 index 00000000000..a418721f796 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/DefaultInputFileTest.java @@ -0,0 +1,92 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem.internal; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.scan.filesystem.InputFile; + +import java.io.File; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultInputFileTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void test_attributes() throws IOException { + File file = temp.newFile(); + 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.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); + + assertThat(input.has(InputFile.ATTRIBUTE_LANGUAGE, "java")).isTrue(); + assertThat(input.has(InputFile.ATTRIBUTE_LANGUAGE, "php")).isFalse(); + assertThat(input.has("unknown", "xxx")).isFalse(); + } + + @Test + public void test_file() throws Exception { + File sourceDir = temp.newFolder(); + File file = temp.newFile("Foo.java"); + InputFile input = new InputFileBuilder(file, "src/main/java/Foo.java") + .sourceDir(sourceDir) + .build(); + + assertThat(input.name()).isEqualTo("Foo.java"); + assertThat(input.file()).isEqualTo(file); + assertThat(input.attribute(InputFile.ATTRIBUTE_SOURCEDIR_PATH)).isEqualTo(sourceDir.getAbsolutePath()); + assertThat(input.relativePath()).isEqualTo("src/main/java/Foo.java"); + assertThat(input.path()).isEqualTo(file.getCanonicalPath()); + } + + @Test + public void test_equals_and_hashCode() throws Exception { + File file1 = temp.newFile(); + InputFile input1 = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); + InputFile input1a = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); + InputFile input2 = new InputFileBuilder(temp.newFile(), "src/main/java/Bar.java").build(); + + assertThat(input1.equals(input1)).isTrue(); + assertThat(input1.equals(input1a)).isTrue(); + assertThat(input1.equals(input2)).isFalse(); + assertThat(input1.hashCode()).isEqualTo(input1.hashCode()); + assertThat(input1.hashCode()).isEqualTo(input1a.hashCode()); + + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/InputFileBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/InputFileBuilderTest.java new file mode 100644 index 00000000000..6e70f14fa1d --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/internal/InputFileBuilderTest.java @@ -0,0 +1,30 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.scan.filesystem.internal; + +import org.junit.Test; + +public class InputFileBuilderTest { + @Test + public void just_for_coverage() throws Exception { + InputFileBuilder._FOR_UNIT_TESTING_ONLY_(); + // do not fail + } +} |