From abf9afb8e5bb3fc70665e315ebd8a18548cdf07c Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 8 Oct 2012 10:50:39 +0200 Subject: [PATCH] SONAR-3758 Add logs and SONAR-1078 Fix default test exclusions --- .../org/sonar/plugins/core/CorePlugin.java | 2 +- .../core/batch/ProjectFileSystemLogger.java | 21 +++++++++++-------- .../java/org/sonar/api/CoreProperties.java | 1 + .../java/org/sonar/api/resources/Project.java | 7 ++++++- .../org/sonar/api/resources/ProjectTest.java | 1 + 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 5476f7eb6f7..074d8d3873e 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -186,7 +186,7 @@ import java.util.List; description = "Exclude tests from code analysis. Applies to every project. Cannot be overriden. Changes will be applied during next code analysis.", multiValues = true, category = CoreProperties.CATEGORY_EXCLUSIONS, - defaultValue = "**/package-info.java"), + defaultValue = CoreProperties.GLOBAL_TEST_EXCLUSIONS_DEFAULT), @Property( key = CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, name = "Exclusions", diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java index cab000d8d85..3364e99b8f6 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java @@ -19,30 +19,34 @@ */ package org.sonar.plugins.core.batch; -import java.util.Arrays; -import java.util.List; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.Initializer; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectFileSystem; -public class ProjectFileSystemLogger extends Initializer { +import java.util.Arrays; +import java.util.List; +public class ProjectFileSystemLogger extends Initializer { private static final Logger LOG = LoggerFactory.getLogger(ProjectFileSystemLogger.class); @Override public void execute(Project project) { - String[] exclusionPatterns = project.getExclusionPatterns(); - if (exclusionPatterns != null && exclusionPatterns.length > 0) { - LOG.info("Excluded sources: {}", Arrays.toString(exclusionPatterns)); - } + logExclusionPatterns("Excluded sources: {}", project.getExclusionPatterns()); + logExclusionPatterns("Excluded tests: {}", project.getTestExclusionPatterns()); + ProjectFileSystem projectFileSystem = project.getFileSystem(); logDirectories("Source directories:", projectFileSystem.getSourceDirs()); logDirectories("Test directories:", projectFileSystem.getTestDirs()); } + private void logExclusionPatterns(String message, String[] exclusionPatterns) { + if (exclusionPatterns != null && exclusionPatterns.length > 0) { + LOG.info(message, Arrays.toString(exclusionPatterns)); + } + } + private void logDirectories(String name, List dirs) { if (!dirs.isEmpty()) { LOG.info(name); @@ -51,5 +55,4 @@ public class ProjectFileSystemLogger extends Initializer { } } } - } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 8bceb171ba2..05e6c192ae8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -116,6 +116,7 @@ public interface CoreProperties { String PROJECT_TEST_EXCLUSIONS_PROPERTY = "sonar.test.exclusions"; String GLOBAL_EXCLUSIONS_PROPERTY = "sonar.global.exclusions"; String GLOBAL_TEST_EXCLUSIONS_PROPERTY = "sonar.global.test.exclusions"; + String GLOBAL_TEST_EXCLUSIONS_DEFAULT = "**/package-info.java"; /** * @deprecated since 2.5. See discussion from http://jira.codehaus.org/browse/SONAR-1873 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 46871a2aca2..a7e8c7bd326 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 @@ -374,9 +374,14 @@ public class Project extends Resource { * @since 3.3 */ public String[] getTestExclusionPatterns() { + String[] globalTestExclusions = configuration.getStringArray(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY); + if (globalTestExclusions.length == 0) { + globalTestExclusions = new String[] {CoreProperties.GLOBAL_TEST_EXCLUSIONS_DEFAULT}; + } + return trimExclusions(ImmutableList. builder() .add(configuration.getStringArray(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY)) - .add(configuration.getStringArray(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY)).build()); + .add(globalTestExclusions).build()); } // http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be 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 ae2d5151be7..81c3f209886 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 @@ -72,6 +72,7 @@ public class ProjectTest { Project project = new Project("key").setConfiguration(conf); assertThat(project.getExclusionPatterns()).isEmpty(); + assertThat(project.getTestExclusionPatterns()).containsOnly("**/package-info.java"); } @Test -- 2.39.5