aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-11-11 15:20:42 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-11-11 15:31:15 +0100
commitd13d88263cb63e09c6e471b64c7c5f87116292a6 (patch)
treede75147c047733ac39a65a8936bf712609fc1dbc /sonar-plugin-api/src
parentb3b22dd430216c707a9936804f251d015e4d94be (diff)
downloadsonarqube-d13d88263cb63e09c6e471b64c7c5f87116292a6.tar.gz
sonarqube-d13d88263cb63e09c6e471b64c7c5f87116292a6.zip
Fix loading of file exclusions - do not cache exclusions in Project
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java13
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java17
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java45
3 files changed, 67 insertions, 8 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java
index 99432b0313f..f2ef02f5746 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java
@@ -101,7 +101,6 @@ public class Project extends Resource {
private String languageKey;
private Date analysisDate;
private AnalysisType analysisType;
- private String[] exclusionPatterns;
private String analysisVersion;
private boolean latestAnalysis;
@@ -363,15 +362,21 @@ public class Project extends Resource {
* Patterns of resource exclusion as defined in project settings page.
*/
public String[] getExclusionPatterns() {
- return exclusionPatterns;
+ String[] exclusions = configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
+ for (int index=0 ; index<exclusions.length ; index++) {
+ // http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be trimmed
+ exclusions[index]=StringUtils.trim(exclusions[index]);
+ }
+ return exclusions;
}
/**
* Set exclusion patterns. Configuration is not saved, so this method must be used ONLY IN UNIT TESTS.
+ * @deprecated
*/
+ @Deprecated
public Project setExclusionPatterns(String[] s) {
- this.exclusionPatterns = s;
- return this;
+ throw new UnsupportedOperationException("Unsupported since version 2.12");
}
/**
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 22731536df3..867464ce822 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
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.project.MavenProject;
@@ -78,7 +79,9 @@ public class DefaultProjectFileSystemTest {
final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
assertThat(fs.hasJavaSourceFiles(), is(true));
- project.setExclusionPatterns(new String[] { "**/*.java" });
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.exclusions", "**/*.java");
+ project.setConfiguration(conf);
assertThat(fs.hasJavaSourceFiles(), is(false));
}
@@ -92,7 +95,9 @@ public class DefaultProjectFileSystemTest {
@Test
public void applyExclusionPatternsToSourceFiles() {
- project.setExclusionPatterns(new String[] { "**/B*.java" });
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.exclusions", "**/B*.java");
+ project.setConfiguration(conf);
final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
@@ -105,7 +110,9 @@ public class DefaultProjectFileSystemTest {
*/
@Test
public void exclusionPatternOnAjFiles() {
- project.setExclusionPatterns(new String[] { "**/*.aj" });
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.exclusions", "**/*.aj");
+ project.setConfiguration(conf);
final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
@@ -116,7 +123,9 @@ public class DefaultProjectFileSystemTest {
@Test
public void doNotApplyExclusionPatternsToTestFiles() {
- project.setExclusionPatterns(new String[] { "**/B*.java" });
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.exclusions", "**/B*.java");
+ project.setConfiguration(conf);
final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
index 8df12c56673..5b3e9bfe6f6 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java
@@ -19,13 +19,20 @@
*/
package org.sonar.api.resources;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.core.Is;
import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
import org.sonar.api.test.MavenTestUtils;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
public class ProjectTest {
@Test
@@ -47,4 +54,42 @@ public class ProjectTest {
Project project = Project.createFromMavenIds("my", "artifact");
assertThat(project.getKey(), is("my:artifact"));
}
+
+ /**
+ * See http://jira.codehaus.org/browse/SONAR-2261
+ * Note that several exclusions separated by comma would be correctly trimmed by commons-configuration library.
+ * So issue is only with a single pattern, which contains spaces.
+ */
+ @Test
+ public void shouldTrimExclusionPatterns() {
+ Configuration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.exclusions", " **/*Foo.java , **/Bar.java ");
+ Project project = new Project("foo").setConfiguration(conf);
+
+ String[] exclusions = project.getExclusionPatterns();
+
+ assertThat(exclusions.length, Is.is(2));
+ assertThat(exclusions[0], Is.is("**/*Foo.java"));
+ assertThat(exclusions[1], Is.is("**/Bar.java"));
+ }
+
+ @Test
+ public void testNoExclusionPatterns() {
+ Project project = new Project("key").setConfiguration(new PropertiesConfiguration());
+
+ MatcherAssert.assertThat(project.getExclusionPatterns().length, Is.is(0));
+ }
+
+ @Test
+ public void testManyExclusionPatterns() {
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*,foo,*/bar");
+
+ Project project = new Project("key").setConfiguration(conf);
+
+ MatcherAssert.assertThat(project.getExclusionPatterns().length, Is.is(3));
+ MatcherAssert.assertThat(project.getExclusionPatterns()[0], Is.is("**/*"));
+ MatcherAssert.assertThat(project.getExclusionPatterns()[1], Is.is("foo"));
+ MatcherAssert.assertThat(project.getExclusionPatterns()[2], Is.is("*/bar"));
+ }
}