]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2827 The property sonar.exclusions can not be changed by extensions
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 26 Sep 2011 10:16:31 +0000 (12:16 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 26 Sep 2011 10:17:00 +0000 (12:17 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ExcludedResourceFilter.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/batch/ExcludedResourceFilterTest.java

index f594d19483536ef9bea7bf8eae9e913b98da09bd..f7bc914d5fe4be848729e5df157f593eeb709821 100644 (file)
@@ -19,8 +19,9 @@
  */
 package org.sonar.plugins.core.batch;
 
+import org.apache.commons.configuration.Configuration;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.ResourceFilter;
-import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 
@@ -29,14 +30,10 @@ import org.sonar.api.resources.ResourceUtils;
  */
 public class ExcludedResourceFilter implements ResourceFilter {
 
-  private String[] exclusionPatterns;
+  private Configuration conf;
 
-  public ExcludedResourceFilter(Project project) {
-    this(project.getExclusionPatterns());
-  }
-
-  protected ExcludedResourceFilter(String[] exclusionPatterns) {
-    this.exclusionPatterns = (exclusionPatterns==null ? new String[0] : exclusionPatterns);
+  public ExcludedResourceFilter(Configuration conf) {
+    this.conf = conf;
   }
 
   public boolean isIgnored(Resource resource) {
@@ -45,11 +42,18 @@ public class ExcludedResourceFilter implements ResourceFilter {
       return false;
     }
 
-    for (String pattern : exclusionPatterns) {
-      if (resource.matchFilePattern(pattern)) {
-        return true;
+    String[] patterns = getExclusionPatterns();
+    if (patterns != null) {
+      for (String pattern : patterns) {
+        if (resource.matchFilePattern(pattern)) {
+          return true;
+        }
       }
     }
     return false;
   }
+
+  String[] getExclusionPatterns() {
+    return conf.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
+  }
 }
\ No newline at end of file
index 2079725ed5efa94f128597472697a9d2206afc0c..dbbe8fd9e13d640f4a8a40e7a1c300a26fa41196 100644 (file)
@@ -19,7 +19,9 @@
  */
 package org.sonar.plugins.core.batch;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.Test;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Resource;
 
@@ -32,13 +34,16 @@ public class ExcludedResourceFilterTest {
 
   @Test
   public void doNotFailIfNoPatterns() {
-    ExcludedResourceFilter filter = new ExcludedResourceFilter((String[]) null);
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    ExcludedResourceFilter filter = new ExcludedResourceFilter(conf);
     assertThat(filter.isIgnored(mock(Resource.class)), is(false));
   }
 
   @Test
   public void noPatternsMatch() {
-    ExcludedResourceFilter filter = new ExcludedResourceFilter(new String[]{"**/foo/*.java", "**/bar/*"});
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"});
+    ExcludedResourceFilter filter = new ExcludedResourceFilter(conf);
     assertThat(filter.isIgnored(mock(Resource.class)), is(false));
   }
 
@@ -47,7 +52,9 @@ public class ExcludedResourceFilterTest {
    */
   @Test
   public void ignoreResourceIfMatchesPattern() {
-    ExcludedResourceFilter filter = new ExcludedResourceFilter(new String[]{"**/foo/*.java", "**/bar/*"});
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"});
+    ExcludedResourceFilter filter = new ExcludedResourceFilter(conf);
 
     Resource resource = mock(Resource.class);
     when(resource.matchFilePattern("**/bar/*")).thenReturn(true);
@@ -57,7 +64,9 @@ public class ExcludedResourceFilterTest {
 
   @Test
   public void doNotExcludeUnitTestFiles() {
-    ExcludedResourceFilter filter = new ExcludedResourceFilter(new String[]{"**/foo/*.java", "**/bar/*"});
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"});
+    ExcludedResourceFilter filter = new ExcludedResourceFilter(conf);
 
     Resource unitTest = mock(Resource.class);
     when(unitTest.getQualifier()).thenReturn(Qualifiers.UNIT_TEST_FILE);