]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2266 ProjectFileSystem should return only existing source and test directories
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 11:39:59 +0000 (15:39 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 11:40:08 +0000 (15:40 +0400)
sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java
sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/pom.xml [new file with mode: 0644]
sonar-plugin-api/src/test/resources/org/sonar/api/resources/DefaultProjectFileSystemTest/nonexistent-dirs/src/main/java [new file with mode: 0644]

index 1270074570c3d85cb97c5d9f7e7ad365100f6ed9..00c85b67ccb0ebf95fcb6cc3b664e815edc3f824 100644 (file)
@@ -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<File> getSourceDirs() {
+    List<File> 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<File> getTestDirs() {
+    List<File> 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));
   }
 
   /**
index 6b3737ac39d2aee6b976197962e9621cc50c5053..40a0915ce48d00da307d39ae1a3f652b527ef381 100644 (file)
@@ -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<File> DIRECTORY_EXISTS = new Predicate<File>() {
+    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<IOFileFilter> filters = Lists.newArrayList();
@@ -92,7 +102,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
    * Maven can modify source directories during Sonar execution - see MavenPhaseExecutor.
    */
   public List<File> 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<File> getTestDirs() {
-    return resolvePaths(project.getPom().getTestCompileSourceRoots());
+    return ImmutableList.copyOf(Iterables.filter(resolvePaths(project.getPom().getTestCompileSourceRoots()), DIRECTORY_EXISTS));
   }
 
   /**
index c6840a40c91fc2cd3fef5f70d78108be848bede2..60022bbe72b77a7cac494502bea9c9d493b3d03a 100644 (file)
@@ -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<File> 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<File> getTestDirs();
 
index 72e88d2979e52d9d4f0bc1f8db060c3ef94b358a..a867392245b7d83f3c49c8bc7ec479cb5328ca91 100644 (file)
@@ -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 (file)
index 0000000..f219368
--- /dev/null
@@ -0,0 +1,8 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>foo</groupId>
+  <artifactId>foo</artifactId>
+  <packaging>jar</packaging>
+
+</project>
\ 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 (file)
index 0000000..e69de29