From 875f335cfa7c8bd07ca84de6b42d3bc57c85c71c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 15 Oct 2014 09:52:24 +0200 Subject: [PATCH] Fix issue related to SONAR-5419 --- .../batch/bootstrap/ProjectDefinition.java | 19 ++++- .../bootstrap/ProjectDefinitionTest.java | 71 +++++++++++++++++-- 2 files changed, 82 insertions(+), 8 deletions(-) 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 ec3bb5efa07..65ac737e31e 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 @@ -313,7 +313,7 @@ public class ProjectDefinition { public ProjectDefinition addSourceFiles(String... paths) { // Hack for visual studio project builder that used to add baseDir first as source dir List sourceDirs = getSourceDirs(); - if (sourceDirs.size() == 1 && new File(sourceDirs.get(0)).isDirectory()) { + if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) { resetSources(); } return addSources(paths); @@ -326,7 +326,7 @@ public class ProjectDefinition { public ProjectDefinition addSourceFiles(File... files) { // Hack for visual studio project builder that used to add baseDir first as source dir List sourceDirs = getSourceDirs(); - if (sourceDirs.size() == 1 && new File(sourceDirs.get(0)).isDirectory()) { + if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) { resetSources(); } return addSources(files); @@ -436,17 +436,30 @@ public class ProjectDefinition { public ProjectDefinition addTestFiles(String... paths) { // Hack for visual studio project builder that used to add baseDir first as test dir List testDirs = getTestDirs(); - if (testDirs.size() == 1 && new File(testDirs.get(0)).isDirectory()) { + 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 testDirs = getTestDirs(); + if (testDirs.size() == 1 && isDirectory(testDirs.get(0))) { + resetTests(); + } return addTests(files); } 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 b9265129605..28802165afc 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 @@ -19,19 +19,26 @@ */ package org.sonar.api.batch.bootstrap; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import org.junit.Rule; +import org.junit.Test; +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 org.junit.Test; -import org.sonar.api.CoreProperties; +import static org.fest.assertions.Assertions.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; public class ProjectDefinitionTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void shouldSetKey() { ProjectDefinition def = ProjectDefinition.create(); @@ -183,6 +190,60 @@ public class ProjectDefinitionTest { 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"); + } + + @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 paths, String... values) { assertThat(paths.size(), is(values.length)); for (int i = 0; i < values.length; i++) { -- 2.39.5