aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-11-06 13:00:26 +0100
committerDavid Gageot <david@gageot.net>2012-11-06 13:00:37 +0100
commit14de16c62104cff7d8cb0a272b1f3abb29458f6e (patch)
tree0624553e93ee99058d7fa3efb7e177c2963b3fe5 /sonar-plugin-api
parentb80f9a00426d7337fa50e2258f3c833dbe045bbe (diff)
downloadsonarqube-14de16c62104cff7d8cb0a272b1f3abb29458f6e.tar.gz
sonarqube-14de16c62104cff7d8cb0a272b1f3abb29458f6e.zip
SONAR-3885 Exclusion patterns should support negation
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java60
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java22
2 files changed, 75 insertions, 7 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 a7e8c7bd326..812eeab7d6e 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
@@ -362,9 +362,7 @@ public class Project extends Resource {
* @since 3.3 also applies exclusions in general settings page and global exclusions.
*/
public String[] getExclusionPatterns() {
- return trimExclusions(ImmutableList.<String> builder()
- .add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY))
- .add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build());
+ return removeNegativePatterns(exclusionPatterns());
}
/**
@@ -374,6 +372,34 @@ public class Project extends Resource {
* @since 3.3
*/
public String[] getTestExclusionPatterns() {
+ return removeNegativePatterns(testExclusionPatterns());
+ }
+
+ /**
+ * Patterns of resource inclusion as defined in project settings page.
+ *
+ * @since 3.4
+ */
+ public String[] getInclusionPatterns() {
+ return onlyNegativePatterns(exclusionPatterns());
+ }
+
+ /**
+ * Patterns of test inclusion as defined in project settings page.
+ *
+ * @since 3.4
+ */
+ public String[] getTestInclusionPatterns() {
+ return onlyNegativePatterns(testExclusionPatterns());
+ }
+
+ private List<String> exclusionPatterns() {
+ return trimExclusions(ImmutableList.<String> builder()
+ .add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY))
+ .add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build());
+ }
+
+ private List<String> testExclusionPatterns() {
String[] globalTestExclusions = configuration.getStringArray(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY);
if (globalTestExclusions.length == 0) {
globalTestExclusions = new String[] {CoreProperties.GLOBAL_TEST_EXCLUSIONS_DEFAULT};
@@ -384,13 +410,33 @@ public class Project extends Resource {
.add(globalTestExclusions).build());
}
+ private static String[] removeNegativePatterns(List<String> patterns) {
+ List<String> filtered = Lists.newArrayList();
+ for (String pattern : patterns) {
+ if (!pattern.startsWith("!")) {
+ filtered.add(pattern);
+ }
+ }
+ return filtered.toArray(new String[filtered.size()]);
+ }
+
+ private static String[] onlyNegativePatterns(List<String> patterns) {
+ List<String> filtered = Lists.newArrayList();
+ for (String pattern : patterns) {
+ if (pattern.startsWith("!")) {
+ filtered.add(pattern.substring(1));
+ }
+ }
+ return filtered.toArray(new String[filtered.size()]);
+ }
+
// http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be trimmed
- private static String[] trimExclusions(List<String> exclusions) {
+ private static List<String> trimExclusions(List<String> patterns) {
List<String> trimmed = Lists.newArrayList();
- for (String exclusion : exclusions) {
- trimmed.add(StringUtils.trim(exclusion));
+ for (String pattern : patterns) {
+ trimmed.add(StringUtils.trim(pattern));
}
- return trimmed.toArray(new String[trimmed.size()]);
+ return trimmed;
}
/**
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 81c3f209886..2ff1a1a6ec0 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
@@ -103,4 +103,26 @@ public class ProjectTest {
assertThat(project.getExclusionPatterns()).containsOnly("**/*Foo.java", "**/*Bar.java");
}
+
+ @Test
+ public void should_support_inclusion_patterns() {
+ conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "!**/*foo.java, **/*bar.java");
+ conf.setProperty(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, "!**/*quix.java");
+
+ Project project = new Project("key").setConfiguration(conf);
+
+ assertThat(project.getExclusionPatterns()).containsOnly("**/*bar.java");
+ assertThat(project.getInclusionPatterns()).containsOnly("**/*foo.java", "**/*quix.java");
+ }
+
+ @Test
+ public void should_support_test_inclusion_patterns() {
+ conf.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "!**/*Test.java, **/*FunctionalTest.java");
+ conf.setProperty(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, "!**/*IntegrationTest.java");
+
+ Project project = new Project("key").setConfiguration(conf);
+
+ assertThat(project.getTestExclusionPatterns()).containsOnly("**/*FunctionalTest.java");
+ assertThat(project.getTestInclusionPatterns()).containsOnly("**/*Test.java", "**/*IntegrationTest.java");
+ }
}