]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2968 Do not force to add the root project to the property sonar.includedModules
authorsimonbrandhof <simon.brandhof@gmail.com>
Tue, 20 Dec 2011 19:56:20 +0000 (20:56 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Tue, 20 Dec 2011 19:56:20 +0000 (20:56 +0100)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectFilter.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectFilterTest.java

index 5ecf6606dc0f4ab64812ea404b58e913fc480818..e1e5c4d1fccd5b6b9cf767b850fe220e8bb6cf53 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.bootstrap;
 
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Project;
 
@@ -40,7 +41,7 @@ public class ProjectFilter {
   public boolean isExcluded(Project project) {
     Project p = project;
     while (p != null) {
-      if (isExcluded(getArtifactId(p))) {
+      if (isExcludedModule(getArtifactId(p), p.isRoot())) {
         return true;
       }
       p = p.getParent();
@@ -48,14 +49,21 @@ public class ProjectFilter {
     return false;
   }
 
-  private boolean isExcluded(String artifactId) {
+  private boolean isExcludedModule(String artifactId, boolean isRoot) {
     String[] includedArtifactIds = settings.getStringArray("sonar.includedModules");
-
-    if (includedArtifactIds.length > 0) {
-      return !ArrayUtils.contains(includedArtifactIds, artifactId);
+    boolean excluded = false;
+    if (!isRoot && includedArtifactIds.length > 0) {
+      excluded = !ArrayUtils.contains(includedArtifactIds, artifactId);
+    }
+    if (!excluded) {
+      String[] excludedArtifactIds = settings.getStringArray("sonar.skippedModules");
+      excluded = ArrayUtils.contains(excludedArtifactIds, artifactId);
+    }
+    if (excluded && isRoot) {
+      LoggerFactory.getLogger(getClass()).warn("The root module can't be excluded: " + artifactId);
+      excluded = false;
     }
-    String[] excludedArtifactIds = settings.getStringArray("sonar.skippedModules");
-    return ArrayUtils.contains(excludedArtifactIds, artifactId);
+    return excluded;
   }
 
   // TODO see http://jira.codehaus.org/browse/SONAR-2324
index d01f5150bf9db4f054be0032cd8bde54b568b9ff..3d5b4eaa545dbb794f5aee47cb3e248702bd7533 100644 (file)
@@ -22,51 +22,60 @@ package org.sonar.batch.bootstrap;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Project;
-import org.sonar.batch.config.ProjectSettings;
 
 import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 public class ProjectFilterTest {
 
+  private Project root = new Project("root");
+
   @Test
-  public void testSkippedModule() {
+  public void testSkippedModules() {
     Settings settings = new Settings();
     settings.setProperty("sonar.skippedModules", "foo,bar");
 
     ProjectFilter filter = new ProjectFilter(settings);
-    assertTrue(filter.isExcluded(new Project("my:foo")));
+    assertTrue(filter.isExcluded(new Project("foo").setParent(root)));
+    assertFalse(filter.isExcluded(new Project("other").setParent(root)));
   }
 
   @Test
-  public void testNotExcluded() {
+  public void shouldNotSkipRoot() {
     Settings settings = new Settings();
-    settings.setProperty("sonar.skippedModules", "foo,bar");
-
+    settings.setProperty("sonar.skippedModules", "root,foo,bar");
     ProjectFilter filter = new ProjectFilter(settings);
-    assertFalse(filter.isExcluded(new Project("my:other")));
+
+    assertFalse(filter.isExcluded(root));
   }
 
+
   @Test
   public void testNoSkippedModules() {
     Settings settings = new Settings();
-
     ProjectFilter filter = new ProjectFilter(settings);
-    assertFalse(filter.isExcluded(new Project("my:other")));
+
+    assertFalse(filter.isExcluded(new Project("foo").setParent(root)));
+    assertFalse(filter.isExcluded(root));
   }
 
   @Test
   public void testIncludedModules() {
     Settings settings = new Settings();
     settings.setProperty("sonar.includedModules", "foo");
+    ProjectFilter filter = new ProjectFilter(settings);
+
+    assertFalse(filter.isExcluded(new Project("foo").setParent(root)));
+    assertTrue(filter.isExcluded(new Project("bar").setParent(root)));
+  }
 
+  @Test
+  public void shouldNotIncludeRoot() {
+    Settings settings = new Settings();
+    settings.setProperty("sonar.includedModules", "foo,bar");
     ProjectFilter filter = new ProjectFilter(settings);
-    assertFalse(filter.isExcluded(new Project("my:foo")));
 
-    filter = new ProjectFilter(settings);
-    assertTrue(filter.isExcluded(new Project("my:bar")));
+    assertFalse(filter.isExcluded(root));
   }
 
   @Test
@@ -74,11 +83,11 @@ public class ProjectFilterTest {
     Settings settings = new Settings();
     settings.setProperty("sonar.skippedModules", "parent");
 
-    Project parent = new Project("my:parent");
-    Project child = new Project("my:child");
-    child.setParent(parent);
+    Project parent = new Project("parent").setParent(root);
+    Project child = new Project("child").setParent(parent);
 
     ProjectFilter filter = new ProjectFilter(settings);
+    assertTrue(filter.isExcluded(parent));
     assertTrue(filter.isExcluded(child));
   }