diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-08-24 13:30:14 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-08-24 13:30:14 +0200 |
commit | c66a8ff27bb4c208886780410738dd3c99828354 (patch) | |
tree | 4aee979f13d940311cbb1a71315a5ebbd28eeb7a /sonar-plugin-api | |
parent | d172a42d012bc511fdbf43d9de826d0c69388fe5 (diff) | |
download | sonarqube-c66a8ff27bb4c208886780410738dd3c99828354.tar.gz sonarqube-c66a8ff27bb4c208886780410738dd3c99828354.zip |
Revert "SONAR-6658 Drop validation on sonar.libraries and sonar.binaries"
This reverts commit 1dd1cd5c6517ffe91ae9b0b003d65c126d54b4b4.
Diffstat (limited to 'sonar-plugin-api')
5 files changed, 444 insertions, 33 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java new file mode 100644 index 00000000000..e9f71cc17d1 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java @@ -0,0 +1,90 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.batch; + +import com.google.common.collect.Lists; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; +import org.sonar.api.BatchComponent; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.resources.ProjectFileSystem; +import org.sonar.api.utils.SonarException; + +/** + * @since 2.2 + * @deprecated since 4.5 this is some Java specific stuff that should by handled by SQ Java plugin + */ +@Deprecated +public class ProjectClasspath implements BatchComponent { + + private final ProjectDefinition def; + private final ProjectFileSystem projectFileSystem; + private List<File> elements; + private URLClassLoader classloader; + + public ProjectClasspath(ProjectDefinition def, ProjectFileSystem projectFileSystem) { + this.def = def; + this.projectFileSystem = projectFileSystem; + } + + public URLClassLoader getClassloader() { + if (classloader == null) { + classloader = createClassLoader(); + } + return classloader; + } + + /** + * bytecode directory + JARs (dependencies) + */ + public List<File> getElements() { + if (elements == null) { + elements = createElements(); + } + return elements; + } + + protected URLClassLoader createClassLoader() { + try { + List<URL> urls = Lists.newArrayList(); + for (File file : getElements()) { + urls.add(file.toURI().toURL()); + } + return new URLClassLoader(urls.toArray(new URL[urls.size()]), null); + + } catch (MalformedURLException e) { + throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e); + } + } + + protected List<File> createElements() { + List<File> elements = Lists.newArrayList(); + for (String path : def.getBinaries()) { + elements.add(projectFileSystem.resolvePath(path)); + } + for (String path : def.getLibraries()) { + elements.add(projectFileSystem.resolvePath(path)); + } + return elements; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java index 49ec7b68fd0..88f80180cc9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java @@ -42,8 +42,38 @@ import org.sonar.api.CoreProperties; public class ProjectDefinition { public static final String SOURCES_PROPERTY = "sonar.sources"; + /** + * @deprecated since 4.5 use {@link #SOURCES_PROPERTY} + */ + @Deprecated + public static final String SOURCE_DIRS_PROPERTY = SOURCES_PROPERTY; + /** + * @deprecated since 4.5 use {@link #SOURCES_PROPERTY} + */ + @Deprecated + public static final String SOURCE_FILES_PROPERTY = "sonar.sourceFiles"; public static final String TESTS_PROPERTY = "sonar.tests"; + /** + * @deprecated since 4.5 use {@link #TESTS_PROPERTY} + */ + @Deprecated + public static final String TEST_DIRS_PROPERTY = TESTS_PROPERTY; + /** + * @deprecated since 4.5 use {@link #TESTS_PROPERTY} + */ + @Deprecated + public static final String TEST_FILES_PROPERTY = "sonar.testFiles"; + /** + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public static final String BINARIES_PROPERTY = "sonar.binaries"; + /** + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public static final String LIBRARIES_PROPERTY = "sonar.libraries"; public static final String BUILD_DIR_PROPERTY = "sonar.buildDir"; private static final char SEPARATOR = ','; @@ -227,6 +257,14 @@ public class ProjectDefinition { } /** + * @deprecated since 4.5 use {@link #sources()} + */ + @Deprecated + public List<String> getSourceDirs() { + return sources(); + } + + /** * @param paths paths to file or directory with main sources. * They can be absolute or relative to project base directory. */ @@ -237,6 +275,14 @@ public class ProjectDefinition { return this; } + /** + * @deprecated since 4.5 use {@link #addSources(String...)} + */ + @Deprecated + public ProjectDefinition addSourceDirs(String... paths) { + return addSources(paths); + } + public ProjectDefinition addSources(File... fileOrDirs) { for (File fileOrDir : fileOrDirs) { addSources(fileOrDir.getAbsolutePath()); @@ -244,16 +290,40 @@ public class ProjectDefinition { return this; } + /** + * @deprecated since 4.5 use {@link #addSources(File...)} + */ + @Deprecated + public ProjectDefinition addSourceDirs(File... dirs) { + return addSources(dirs); + } + public ProjectDefinition resetSources() { properties.remove(SOURCES_PROPERTY); return this; } + /** + * @deprecated since 4.5 use {@link #resetSources()} + */ + @Deprecated + public ProjectDefinition resetSourceDirs() { + return resetSources(); + } + public ProjectDefinition setSources(String... paths) { resetSources(); return addSources(paths); } + /** + * @deprecated since 4.5 use {@link #setSources(String...)} + */ + @Deprecated + public ProjectDefinition setSourceDirs(String... paths) { + return setSources(paths); + } + public ProjectDefinition setSources(File... filesOrDirs) { resetSources(); for (File fileOrDir : filesOrDirs) { @@ -262,12 +332,66 @@ public class ProjectDefinition { return this; } + /** + * @deprecated since 4.5 use {@link #setSources(File...)} + */ + @Deprecated + public ProjectDefinition setSourceDirs(File... dirs) { + resetSourceDirs(); + for (File dir : dirs) { + addSourceDirs(dir.getAbsolutePath()); + } + return this; + } + + /** + * @deprecated since 4.5 use {@link #addSources(File...)} + */ + @Deprecated + public ProjectDefinition addSourceFiles(String... paths) { + // Hack for visual studio project builder that used to add baseDir first as source dir + List<String> sourceDirs = getSourceDirs(); + if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) { + resetSources(); + } + return addSources(paths); + } + + /** + * @deprecated since 4.5 use {@link #addSources(File...)} + */ + @Deprecated + public ProjectDefinition addSourceFiles(File... files) { + // Hack for visual studio project builder that used to add baseDir first as source dir + List<String> sourceDirs = getSourceDirs(); + if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) { + resetSources(); + } + return addSources(files); + } + + /** + * @deprecated since 4.5 use {@link #sources()} + */ + @Deprecated + public List<String> getSourceFiles() { + return sources(); + } + public List<String> tests() { String sources = (String) ObjectUtils.defaultIfNull(properties.get(TESTS_PROPERTY), ""); return trim(StringUtils.split(sources, SEPARATOR)); } /** + * @deprecated since 4.5 use {@link #tests()} + */ + @Deprecated + public List<String> getTestDirs() { + return tests(); + } + + /** * @param paths path to files or directories with test sources. * It can be absolute or relative to project directory. */ @@ -278,6 +402,14 @@ public class ProjectDefinition { return this; } + /** + * @deprecated since 4.5 use {@link #addTests(String...)} + */ + @Deprecated + public ProjectDefinition addTestDirs(String... paths) { + return addTests(paths); + } + public ProjectDefinition addTests(File... fileOrDirs) { for (File fileOrDir : fileOrDirs) { addTests(fileOrDir.getAbsolutePath()); @@ -285,11 +417,27 @@ public class ProjectDefinition { return this; } + /** + * @deprecated since 4.5 use {@link #addTests(File...)} + */ + @Deprecated + public ProjectDefinition addTestDirs(File... dirs) { + return addTests(dirs); + } + public ProjectDefinition setTests(String... paths) { resetTests(); return addTests(paths); } + /** + * @deprecated since 4.5 use {@link #setTests(String...)} + */ + @Deprecated + public ProjectDefinition setTestDirs(String... paths) { + return setTests(paths); + } + public ProjectDefinition setTests(File... fileOrDirs) { resetTests(); for (File dir : fileOrDirs) { @@ -312,6 +460,103 @@ public class ProjectDefinition { } /** + * @deprecated since 4.5 use {@link #resetTests()} + */ + @Deprecated + public ProjectDefinition resetTestDirs() { + return resetTests(); + } + + /** + * @deprecated since 4.5 use {@link #addTests(String...)} + */ + @Deprecated + public ProjectDefinition addTestFiles(String... paths) { + // Hack for visual studio project builder that used to add baseDir first as test dir + List<String> testDirs = getTestDirs(); + if (testDirs.size() == 1 && isDirectory(testDirs.get(0))) { + resetTests(); + } + return addTests(paths); + } + + private boolean isDirectory(String relativeOrAbsoluteDir) { + File file = new File(relativeOrAbsoluteDir); + if (!file.isAbsolute()) { + file = new File(baseDir, relativeOrAbsoluteDir); + } + return file.isDirectory(); + } + + /** + * @deprecated since 4.5 use {@link #addTests(File...)} + */ + @Deprecated + public ProjectDefinition addTestFiles(File... files) { + // Hack for visual studio project builder that used to add baseDir first as test dir + List<String> testDirs = getTestDirs(); + if (testDirs.size() == 1 && isDirectory(testDirs.get(0))) { + resetTests(); + } + return addTests(files); + } + + /** + * @deprecated since 4.5 use {@link #tests()} + */ + @Deprecated + public List<String> getTestFiles() { + return tests(); + } + + /** + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public List<String> getBinaries() { + String sources = (String) ObjectUtils.defaultIfNull(properties.get(BINARIES_PROPERTY), ""); + return trim(StringUtils.split(sources, SEPARATOR)); + } + + /** + * @param path path to directory with compiled source. In case of Java this is directory with class files. + * It can be absolute or relative to project directory. + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public ProjectDefinition addBinaryDir(String path) { + appendProperty(BINARIES_PROPERTY, path); + return this; + } + + /** + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public ProjectDefinition addBinaryDir(File f) { + return addBinaryDir(f.getAbsolutePath()); + } + + /** + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public List<String> getLibraries() { + String sources = (String) ObjectUtils.defaultIfNull(properties.get(LIBRARIES_PROPERTY), ""); + return trim(StringUtils.split(sources, SEPARATOR)); + } + + /** + * @param path path to file with third-party library. In case of Java this is path to jar file. + * It can be absolute or relative to project directory. + * @deprecated since 4.5.1 use SonarQube Java specific API + */ + @Deprecated + public void addLibrary(String path) { + appendProperty(LIBRARIES_PROPERTY, path); + } + + /** * Adds an extension, which would be available in PicoContainer during analysis of this project. * * @since 2.8 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 dd88f5b9bfd..8979f73b7c1 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 @@ -63,7 +63,14 @@ public interface ModuleFileSystem { List<File> testDirs(); /** - * @deprecated since 4.2 - returns always empty list + * Optional directories that contain the compiled sources, for example java bytecode. + * Note that : + * <ul> + * <li>Maven projects have only a single binary directory, which is generally ${project.basedir}/target/classes</li> + * <li>Binary directories can be empty</li> + * <li>Test binary directories are not supported yet.</li> + * </ul> + * @deprecated since 4.2 sonar.binaries will be converted to java specific property */ List<File> binaryDirs(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java index 14280131018..7a13e851401 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java @@ -43,7 +43,7 @@ public class ProjectBuilderTest { ProjectDefinition root = projectReactor.getRoot(); assertThat(root.getName(), is("Name changed by plugin")); assertThat(root.getSubProjects().size(), is(1)); - assertThat(root.getSubProjects().get(0).sources(), hasItem("src")); + assertThat(root.getSubProjects().get(0).getSourceDirs(), hasItem("src")); } final static class ProjectBuilderSample extends ProjectBuilder { @@ -68,7 +68,7 @@ public class ProjectBuilderTest { subProject.setKey("groupId:parentProjectId"); subProject.setVersion(root.getVersion()); subProject.setName("Sub Project"); - subProject.setSources("src"); + subProject.setSourceDirs("src"); root.addSubProject(subProject); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java index db99a5f3fbf..14faa2ec0a0 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java @@ -25,9 +25,11 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Properties; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; @@ -79,8 +81,10 @@ public class ProjectDefinitionTest { @Test public void testDefaultValues() { ProjectDefinition def = ProjectDefinition.create(); - assertThat(def.sources().size(), is(0)); - assertThat(def.tests().size(), is(0)); + assertThat(def.getSourceDirs().size(), is(0)); + assertThat(def.getTestDirs().size(), is(0)); + assertThat(def.getBinaries().size(), is(0)); + assertThat(def.getLibraries().size(), is(0)); } /** @@ -89,43 +93,54 @@ public class ProjectDefinitionTest { @Test public void shouldTrimPaths() { ProjectDefinition def = ProjectDefinition.create(); - def.setProperty(ProjectDefinition.SOURCES_PROPERTY, "src1, src2 , with whitespace"); - def.setProperty(ProjectDefinition.TESTS_PROPERTY, "test1, test2 , with whitespace"); - - - assertFiles(def.sources(), "src1", "src2", "with whitespace"); - assertFiles(def.tests(), "test1", "test2", "with whitespace"); + def.setProperty(ProjectDefinition.SOURCE_DIRS_PROPERTY, "src1, src2 , with whitespace"); + def.setProperty(ProjectDefinition.TEST_DIRS_PROPERTY, "test1, test2 , with whitespace"); + def.setProperty(ProjectDefinition.BINARIES_PROPERTY, "bin1, bin2 , with whitespace"); + def.setProperty(ProjectDefinition.LIBRARIES_PROPERTY, "lib1, lib2 , with whitespace"); + + assertFiles(def.getSourceDirs(), "src1", "src2", "with whitespace"); + assertFiles(def.getTestDirs(), "test1", "test2", "with whitespace"); + assertFiles(def.getBinaries(), "bin1", "bin2", "with whitespace"); + assertFiles(def.getLibraries(), "lib1", "lib2", "with whitespace"); } @Test public void shouldAddDirectoriesAsPath() { ProjectDefinition def = ProjectDefinition.create(); - def.addSources("src/main/java", "src/main/java2"); - def.addTests("src/test/java"); - def.addTests("src/test/java2"); - - assertFiles(def.sources(), "src/main/java", "src/main/java2"); - assertFiles(def.tests(), "src/test/java", "src/test/java2"); + def.addSourceDirs("src/main/java", "src/main/java2"); + def.addTestDirs("src/test/java"); + def.addTestDirs("src/test/java2"); + def.addBinaryDir("target/classes"); + def.addBinaryDir("target/classes2"); + def.addLibrary("junit.jar"); + def.addLibrary("mockito.jar"); + + assertFiles(def.getSourceDirs(), "src/main/java", "src/main/java2"); + assertFiles(def.getTestDirs(), "src/test/java", "src/test/java2"); + assertFiles(def.getBinaries(), "target/classes", "target/classes2"); + assertFiles(def.getLibraries(), "junit.jar", "mockito.jar"); } @Test public void shouldAddDirectories() { ProjectDefinition def = ProjectDefinition.create(); - def.addSources(new File("src/main/java"), new File("src/main/java2")); - def.addTests(new File("src/test/java"), new File("src/test/java2")); + def.addSourceDirs(new File("src/main/java"), new File("src/main/java2")); + def.addTestDirs(new File("src/test/java"), new File("src/test/java2")); + def.addBinaryDir(new File("target/classes")); - assertThat(def.sources().size(), is(2)); - assertThat(def.tests().size(), is(2)); + assertThat(def.getSourceDirs().size(), is(2)); + assertThat(def.getTestDirs().size(), is(2)); + assertThat(def.getBinaries().size(), is(1)); } @Test public void shouldAddFiles() { ProjectDefinition def = ProjectDefinition.create(); - def.addSources("src/main/java/foo/Bar.java", "src/main/java/hello/World.java"); - def.addTests("src/test/java/foo/BarTest.java", "src/test/java/hello/WorldTest.java"); + def.addSourceFiles("src/main/java/foo/Bar.java", "src/main/java/hello/World.java"); + def.addTestFiles("src/test/java/foo/BarTest.java", "src/test/java/hello/WorldTest.java"); - assertFiles(def.sources(), "src/main/java/foo/Bar.java", "src/main/java/hello/World.java"); - assertFiles(def.tests(), "src/test/java/foo/BarTest.java", "src/test/java/hello/WorldTest.java"); + assertFiles(def.getSourceFiles(), "src/main/java/foo/Bar.java", "src/main/java/hello/World.java"); + assertFiles(def.getTestFiles(), "src/test/java/foo/BarTest.java", "src/test/java/hello/WorldTest.java"); } @Test @@ -144,21 +159,75 @@ public class ProjectDefinitionTest { @Test public void shouldResetSourceDirs() { ProjectDefinition root = ProjectDefinition.create(); - root.addSources("src", "src2/main"); - assertThat(root.sources().size(), is(2)); + root.addSourceDirs("src", "src2/main"); + assertThat(root.getSourceDirs().size(), is(2)); - root.resetSources(); - assertThat(root.sources().size(), is(0)); + root.resetSourceDirs(); + assertThat(root.getSourceDirs().size(), is(0)); } @Test public void shouldResetTestDirs() { ProjectDefinition root = ProjectDefinition.create(); - root.addTests("src", "src2/test"); - assertThat(root.tests().size(), is(2)); + root.addTestDirs("src", "src2/test"); + assertThat(root.getTestDirs().size(), is(2)); + + root.resetTestDirs(); + assertThat(root.getTestDirs().size(), is(0)); + } + + @Test + public void shouldResetSourceDirsWhenUsindDeprecatedMethod() throws IOException { + File baseDir = temp.newFolder(); + ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir); + File src = new File(baseDir, "src"); + src.mkdir(); + root.addSourceDirs("src"); + assertThat(root.getSourceDirs()).containsOnly("src"); + + root.addSourceFiles("foo.java"); + assertThat(root.getSourceDirs()).containsOnly("foo.java"); + } - root.resetTests(); - assertThat(root.tests().size(), is(0)); + @Test + public void shouldResetTestDirsWhenUsindDeprecatedMethod() throws IOException { + File baseDir = temp.newFolder(); + ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir); + File test = new File(baseDir, "test"); + test.mkdir(); + root.addTestDirs("test"); + assertThat(root.getTestDirs()).containsOnly("test"); + + root.addTestFiles("fooTest.java"); + assertThat(root.getTestDirs()).containsOnly("fooTest.java"); + } + + @Test + public void shouldResetSourceDirsWhenUsindDeprecatedFileMethod() throws IOException { + File baseDir = temp.newFolder(); + ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir); + File src = new File(baseDir, "src"); + src.mkdir(); + root.addSourceDirs("src"); + assertThat(root.getSourceDirs()).containsOnly("src"); + + File file = new File(src, "foo.java"); + root.addSourceFiles(file); + assertThat(root.getSourceDirs()).containsOnly(file.getAbsolutePath()); + } + + @Test + public void shouldResetTestDirsWhenUsindDeprecatedFileMethod() throws IOException { + File baseDir = temp.newFolder(); + ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir); + File test = new File(baseDir, "test"); + test.mkdir(); + root.addTestDirs("test"); + assertThat(root.getTestDirs()).containsOnly("test"); + + File file = new File(test, "fooTest.java"); + root.addTestFiles(file); + assertThat(root.getTestDirs()).containsOnly(file.getAbsolutePath()); } private static void assertFiles(List<String> paths, String... values) { |