From 787a472b3de5e4fe60ac6407d2cd6aa5054ab863 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 13 Feb 2013 19:05:05 +0100 Subject: SONAR-2760 file exclusions by absolute path --- .../main/java/org/sonar/api/resources/Project.java | 6 +- .../sonar/api/scan/filesystem/FileExclusions.java | 82 ++++++++++++++++++++++ .../org/sonar/api/scan/filesystem/FileFilter.java | 9 +++ .../api/scan/filesystem/ModuleExclusions.java | 82 ---------------------- .../api/scan/filesystem/FileExclusionsTest.java | 61 ++++++++++++++++ .../api/scan/filesystem/ModuleExclusionsTest.java | 61 ---------------- 6 files changed, 155 insertions(+), 146 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleExclusions.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java (limited to 'sonar-plugin-api') 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 28d2b3a436a..7c99e49b36b 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 @@ -330,7 +330,7 @@ public class Project extends Resource { * Patterns of resource exclusion as defined in project settings page. * * @since 3.3 also applies exclusions in general settings page and global exclusions. - * @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleExclusions} in version 3.5 + * @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5 */ @Deprecated public String[] getExclusionPatterns() { @@ -344,7 +344,7 @@ public class Project extends Resource { * Also applies exclusions in general settings page and global exclusions. * * @since 3.3 - * @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleExclusions} in version 3.5 + * @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5 */ @Deprecated public String[] getTestExclusionPatterns() { @@ -369,7 +369,7 @@ public class Project extends Resource { /** * Set exclusion patterns. Configuration is not saved, so this method must be used ONLY IN UNIT TESTS. - * @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleExclusions} in version 3.5 + * @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5 */ @Deprecated public Project setExclusionPatterns(String[] s) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java new file mode 100644 index 00000000000..a79929f558e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java @@ -0,0 +1,82 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.scan.filesystem; + +import com.google.common.collect.Lists; +import com.google.common.collect.ObjectArrays; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.BatchComponent; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; + +import java.util.List; + +/** + * Configuration of file inclusions and exclusions + * + * @since 3.5 + */ +public class FileExclusions implements BatchComponent { + private final Settings settings; + + public FileExclusions(Settings settings) { + this.settings = settings; + } + + public String[] sourceInclusions() { + return inclusions(CoreProperties.PROJECT_INCLUSIONS_PROPERTY); + } + + public String[] testInclusions() { + return inclusions(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY); + } + + private String[] inclusions(String propertyKey) { + String[] patterns = sanitize(settings.getStringArray(propertyKey)); + List list = Lists.newArrayList(); + for (String pattern : patterns) { + if (!"**/*".equals(pattern) && !"file:**/*".equals(pattern)) { + list.add(pattern); + } + } + return list.toArray(new String[list.size()]); + } + + public String[] sourceExclusions() { + return exclusions(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY); + } + + public String[] testExclusions() { + return exclusions(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY); + } + + private String[] exclusions(String globalExclusionsProperty, String exclusionsProperty) { + String[] globalExclusions = settings.getStringArray(globalExclusionsProperty); + String[] exclusions = settings.getStringArray(exclusionsProperty); + return sanitize(ObjectArrays.concat(globalExclusions, exclusions, String.class)); + } + + private static String[] sanitize(String[] patterns) { + for (int i = 0; i < patterns.length; i++) { + patterns[i] = StringUtils.trim(patterns[i]); + } + return patterns; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileFilter.java index ec4067d8de4..7ff169087dc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileFilter.java @@ -37,7 +37,16 @@ public interface FileFilter extends BatchExtension { ModuleFileSystem fileSystem(); FileType fileType(); File sourceDir(); + + /** + * File path relative to source directory. Never return null. + */ String fileRelativePath(); + + /** + * Absolute file path. Never return null. + */ + String fileCanonicalPath(); } boolean accept(File file, Context context); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleExclusions.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleExclusions.java deleted file mode 100644 index 7331a984419..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleExclusions.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.scan.filesystem; - -import com.google.common.collect.Lists; -import com.google.common.collect.ObjectArrays; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.BatchComponent; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; - -import java.util.List; - -/** - * Configuration of file inclusions and exclusions - * - * @since 3.5 - */ -public class ModuleExclusions implements BatchComponent { - private final Settings settings; - - public ModuleExclusions(Settings settings) { - this.settings = settings; - } - - public String[] sourceInclusions() { - return inclusions(CoreProperties.PROJECT_INCLUSIONS_PROPERTY); - } - - public String[] testInclusions() { - return inclusions(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY); - } - - private String[] inclusions(String propertyKey) { - String[] patterns = sanitize(settings.getStringArray(propertyKey)); - List list = Lists.newArrayList(); - for (String pattern : patterns) { - if (!"**/*".equals(pattern)) { - list.add(pattern); - } - } - return list.toArray(new String[list.size()]); - } - - public String[] sourceExclusions() { - return exclusions(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY); - } - - public String[] testExclusions() { - return exclusions(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY); - } - - private String[] exclusions(String globalExclusionsProperty, String exclusionsProperty) { - String[] globalExclusions = settings.getStringArray(globalExclusionsProperty); - String[] exclusions = settings.getStringArray(exclusionsProperty); - return sanitize(ObjectArrays.concat(globalExclusions, exclusions, String.class)); - } - - private static String[] sanitize(String[] patterns) { - for (int i = 0; i < patterns.length; i++) { - patterns[i] = StringUtils.trim(patterns[i]); - } - return patterns; - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java new file mode 100644 index 00000000000..5f9d3acaeea --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java @@ -0,0 +1,61 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.scan.filesystem; + +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; + +import static org.fest.assertions.Assertions.assertThat; + +public class FileExclusionsTest { + @Test + public void ignore_inclusion_of_world() { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*"); + settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*"); + assertThat(new FileExclusions(settings).sourceInclusions()).isEmpty(); + assertThat(new FileExclusions(settings).testInclusions()).isEmpty(); + } + + @Test + public void load_inclusions() { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Foo.java"); + settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*FooTest.java"); + FileExclusions moduleExclusions = new FileExclusions(settings); + + assertThat(moduleExclusions.sourceInclusions()).containsOnly("**/*Foo.java"); + assertThat(moduleExclusions.testInclusions()).containsOnly("**/*FooTest.java"); + } + + @Test + public void load_exclusions() { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Foo.java"); + settings.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "**/*FooTest.java"); + FileExclusions moduleExclusions = new FileExclusions(settings); + + assertThat(moduleExclusions.sourceInclusions()).isEmpty(); + assertThat(moduleExclusions.sourceExclusions()).containsOnly("**/*Foo.java"); + assertThat(moduleExclusions.testInclusions()).isEmpty(); + assertThat(moduleExclusions.testExclusions()).containsOnly("**/*FooTest.java"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java deleted file mode 100644 index 25f52de601e..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/ModuleExclusionsTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.scan.filesystem; - -import org.junit.Test; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; - -import static org.fest.assertions.Assertions.assertThat; - -public class ModuleExclusionsTest { - @Test - public void ignore_inclusion_of_world() { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*"); - settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*"); - assertThat(new ModuleExclusions(settings).sourceInclusions()).isEmpty(); - assertThat(new ModuleExclusions(settings).testInclusions()).isEmpty(); - } - - @Test - public void load_inclusions() { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Foo.java"); - settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*FooTest.java"); - ModuleExclusions moduleExclusions = new ModuleExclusions(settings); - - assertThat(moduleExclusions.sourceInclusions()).containsOnly("**/*Foo.java"); - assertThat(moduleExclusions.testInclusions()).containsOnly("**/*FooTest.java"); - } - - @Test - public void load_exclusions() { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Foo.java"); - settings.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "**/*FooTest.java"); - ModuleExclusions moduleExclusions = new ModuleExclusions(settings); - - assertThat(moduleExclusions.sourceInclusions()).isEmpty(); - assertThat(moduleExclusions.sourceExclusions()).containsOnly("**/*Foo.java"); - assertThat(moduleExclusions.testInclusions()).isEmpty(); - assertThat(moduleExclusions.testExclusions()).containsOnly("**/*FooTest.java"); - } -} -- cgit v1.2.3