From: Evgeny Mandrikov Date: Wed, 27 Apr 2011 11:39:59 +0000 (+0400) Subject: SONAR-2266 ProjectFileSystem should return only existing source and test directories X-Git-Tag: 2.8~110 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=70c03e6ac1e0f24277655f9e7666142d9418d270;p=sonarqube.git SONAR-2266 ProjectFileSystem should return only existing source and test directories --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java index 1270074570c..00c85b67ccb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java @@ -19,6 +19,8 @@ */ package org.sonar.batch; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import org.apache.commons.io.FileUtils; import org.apache.maven.project.MavenProject; import org.sonar.api.resources.DefaultProjectFileSystem; @@ -83,12 +85,14 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem { } public List getSourceDirs() { + List unfiltered; if (pom != null) { // Maven can modify source directories during Sonar execution - see MavenPhaseExecutor. - return resolvePaths(pom.getCompileSourceRoots()); + unfiltered = resolvePaths(pom.getCompileSourceRoots()); } else { - return resolvePaths(def.getSourceDirs()); + unfiltered = resolvePaths(def.getSourceDirs()); } + return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS)); } /** @@ -111,12 +115,14 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem { * Maven can modify test directories during Sonar execution - see MavenPhaseExecutor. */ public List getTestDirs() { + List unfiltered; if (pom != null) { // Maven can modify test directories during Sonar execution - see MavenPhaseExecutor. - return resolvePaths(pom.getTestCompileSourceRoots()); + unfiltered = resolvePaths(pom.getTestCompileSourceRoots()); } else { - return resolvePaths(def.getTestDirs()); + unfiltered = resolvePaths(def.getTestDirs()); } + return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS)); } /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index 6b3737ac39d..40a0915ce48 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -19,6 +19,9 @@ */ package org.sonar.api.resources; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; @@ -48,6 +51,13 @@ import java.util.List; @Deprecated public class DefaultProjectFileSystem implements ProjectFileSystem { + protected static final Predicate DIRECTORY_EXISTS = new Predicate() { + public boolean apply(File input) { + System.out.println(input.toString() + " " + input.exists()); + return input.exists() && input.isDirectory(); + } + }; + private Project project; private Languages languages; private List filters = Lists.newArrayList(); @@ -92,7 +102,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { * Maven can modify source directories during Sonar execution - see MavenPhaseExecutor. */ public List getSourceDirs() { - return resolvePaths(project.getPom().getCompileSourceRoots()); + return ImmutableList.copyOf(Iterables.filter(resolvePaths(project.getPom().getCompileSourceRoots()), DIRECTORY_EXISTS)); } /** @@ -111,7 +121,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { * Maven can modify test directories during Sonar execution - see MavenPhaseExecutor. */ public List getTestDirs() { - return resolvePaths(project.getPom().getTestCompileSourceRoots()); + return ImmutableList.copyOf(Iterables.filter(resolvePaths(project.getPom().getTestCompileSourceRoots()), DIRECTORY_EXISTS)); } /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java index c6840a40c91..60022bbe72b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java @@ -53,7 +53,7 @@ public interface ProjectFileSystem extends BatchComponent { File getBuildOutputDir(); /** - * The list of directories for sources + * The list of existing directories with sources */ List getSourceDirs(); @@ -68,7 +68,7 @@ public interface ProjectFileSystem extends BatchComponent { ProjectFileSystem addSourceDir(File dir); /** - * The list of directories for tests + * The list of existing directories with tests */ List getTestDirs(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java index 72e88d2979e..a867392245b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java @@ -50,6 +50,18 @@ public class DefaultProjectFileSystemTest { project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample/pom.xml"); } + /** + * See http://jira.codehaus.org/browse/SONAR-2266 + */ + @Test + public void shouldReturnOnlyExistingSourceAndTestDirectories() { + // in this example : "src/main/java" is a file, "src/test/java" doesn't exists + project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "nonexistent-dirs/pom.xml"); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); + assertThat(fs.getSourceDirs().size(), is(0)); + assertThat(fs.getTestDirs().size(), is(0)); + } + @Test public void getJavaSourceFiles() { final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/pom.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/pom.xml new file mode 100644 index 00000000000..f219368e824 --- /dev/null +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/pom.xml @@ -0,0 +1,8 @@ + + 4.0.0 + foo + foo + jar + + \ No newline at end of file diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/src/main/java b/sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/src/main/java new file mode 100644 index 00000000000..e69de29bb2d