diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-07-12 19:27:16 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-07-12 20:16:37 +0400 |
commit | 0be3ce80890e26db3d0d94fd9242a230d8ec1747 (patch) | |
tree | 05ca8c459a36e796b718061018f94925e0bc5af9 | |
parent | 13963270280fc022c083d54a6e3105c1da0f1c2f (diff) | |
download | sonarqube-0be3ce80890e26db3d0d94fd9242a230d8ec1747.tar.gz sonarqube-0be3ce80890e26db3d0d94fd9242a230d8ec1747.zip |
SONAR-2477 Allow to use FindBugs filter-files with exclusions
Added parameter "sonar.findbugs.excludesFilters". This parameter will be
automatically configured based on configuration for
findbugs-maven-plugin. However it can reference only local files, even
if findbugs-maven-plugin supports resources and URLs.
9 files changed, 210 insertions, 26 deletions
diff --git a/plugins/sonar-findbugs-plugin/pom.xml b/plugins/sonar-findbugs-plugin/pom.xml index cd26a16d543..7687def4980 100644 --- a/plugins/sonar-findbugs-plugin/pom.xml +++ b/plugins/sonar-findbugs-plugin/pom.xml @@ -69,21 +69,19 @@ </exclusions> </dependency> - <!-- unit tests --> - <dependency> - <groupId>org.codehaus.sonar</groupId> - <artifactId>sonar-testing-harness</artifactId> - <scope>test</scope> - </dependency> - <!-- TODO http://jira.codehaus.org/browse/SONAR-2011 - We need following dependency, otherwise we will receive - java.lang.NoClassDefFoundError: org/apache/maven/project/MavenProject - during call mock(org.sonar.api.resources.Project.class) + We need following dependency, otherwise we will receive compilation error --> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> + <scope>provided</scope> + </dependency> + + <!-- unit tests --> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-testing-harness</artifactId> <scope>test</scope> </dependency> </dependencies> diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java index 4674bf2e7c4..21057ea4663 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java @@ -33,6 +33,8 @@ import org.sonar.plugins.findbugs.xml.FindBugsFilter; import org.sonar.plugins.findbugs.xml.Match; import java.io.*; +import java.util.ArrayList; +import java.util.List; /** * @since 2.4 @@ -98,6 +100,18 @@ public class FindbugsConfiguration implements BatchExtension { return project.getFileSystem().writeToWorkingDirectory(findBugsFilter.toXml(), "findbugs-exclude.xml"); } + public List<File> getExcludesFilters() { + List<File> result = new ArrayList<File>(); + String[] filters = project.getConfiguration().getStringArray(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY); + for (String excludesFilterPath : filters) { + excludesFilterPath = StringUtils.trim(excludesFilterPath); + if (StringUtils.isNotBlank(excludesFilterPath)) { + result.add(project.getFileSystem().resolvePath(excludesFilterPath)); + } + } + return result; + } + public String getEffort() { return StringUtils.lowerCase(project.getConfiguration().getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY, CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE)); diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java index fc1491b9d56..0467f5a3dc6 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java @@ -33,7 +33,12 @@ public final class FindbugsConstants { */ public static final String GENERATE_XML_KEY = "sonar.findbugs.generateXml"; public static final boolean GENERATE_XML_DEFAULT_VALUE = true; // TODO should be false - see SONAR-1880 - + + /** + * @since 2.10 + */ + public static final String EXCLUDES_FILTERS_PROPERTY = "sonar.findbugs.excludesFilters"; + private FindbugsConstants() { } } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java index e1e8fd2a482..c545b63243c 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java @@ -19,19 +19,6 @@ */ package org.sonar.plugins.findbugs; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.lang.reflect.Field; -import java.net.URL; -import java.util.Enumeration; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; - import com.google.common.collect.Lists; import edu.umd.cs.findbugs.*; import edu.umd.cs.findbugs.config.UserPreferences; @@ -46,6 +33,19 @@ import org.sonar.api.utils.Logs; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.net.URL; +import java.util.Enumeration; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + /** * @since 2.4 */ @@ -96,6 +96,14 @@ public class FindbugsExecutor implements BatchExtension { engine.addFilter(configuration.saveIncludeConfigXml().getAbsolutePath(), true); engine.addFilter(configuration.saveExcludeConfigXml().getAbsolutePath(), false); + for (File filterFile : configuration.getExcludesFilters()) { + if (filterFile.isFile()) { + engine.addFilter(filterFile.getAbsolutePath(), false); + } else { + LOG.warn("FindBugs filter-file not found: {}", filterFile); + } + } + engine.setDetectorFactoryCollection(detectorFactory); engine.setAnalysisFeatureSettings(FindBugs.DEFAULT_EFFORT); @@ -167,4 +175,5 @@ public class FindbugsExecutor implements BatchExtension { throw new SonarException(e); } } + } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java new file mode 100644 index 00000000000..26d1e5b7547 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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.plugins.findbugs; + +import org.apache.commons.configuration.Configuration; +import org.sonar.api.batch.Initializer; +import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.batch.maven.MavenPlugin; +import org.sonar.api.batch.maven.MavenUtils; +import org.sonar.api.resources.Project; + +/** + * Configures Sonar FindBugs Plugin according to configuration of findbugs-maven-plugin. + * Supports only "excludeFilterFile". + * + * @since 2.10 + */ +@SupportedEnvironment("maven") +public class FindbugsMavenInitializer extends Initializer { + + private static final String FINDBUGS_GROUP_ID = MavenUtils.GROUP_ID_CODEHAUS_MOJO; + private static final String FINDBUGS_ARTIFACT_ID = "findbugs-maven-plugin"; + + @Override + public void execute(Project project) { + Configuration conf = project.getConfiguration(); + if (!conf.containsKey(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)) { + conf.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, getExcludesFiltersFromPluginConfiguration(project)); + } + } + + private static String getExcludesFiltersFromPluginConfiguration(Project project) { + MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), FINDBUGS_GROUP_ID, FINDBUGS_ARTIFACT_ID); + return mavenPlugin != null ? mavenPlugin.getParameter("excludeFilterFile") : null; + } + +} diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java index c234f6b9059..bb9777ba15b 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java @@ -38,6 +38,11 @@ import java.util.List; name = "Timeout", description = "Specifies the amount of time, in milliseconds, that FindBugs may run before it is assumed to be hung and is terminated. " + "The default is 600,000 milliseconds, which is ten minutes.", + project = true, module = true, global = true), + @Property( + key = FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, + name = "Excludes Filters", + description = "Paths to findbugs filter-files with exclusions (comma-separated).", project = true, module = true, global = true) }) public class FindbugsPlugin extends SonarPlugin { @@ -50,6 +55,7 @@ public class FindbugsPlugin extends SonarPlugin { list.add(FindbugsProfileExporter.class); list.add(FindbugsProfileImporter.class); list.add(SonarWayWithFindbugsProfile.class); + list.add(FindbugsMavenInitializer.class); return list; } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java index ef4b16df8d0..c85fbb38291 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java @@ -24,8 +24,8 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.io.File; - +import org.apache.commons.configuration.BaseConfiguration; +import org.apache.commons.configuration.Configuration; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -34,6 +34,8 @@ import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; import org.sonar.api.test.SimpleProjectFileSystem; +import java.io.File; + public class FindbugsConfigurationTest { @Rule @@ -62,4 +64,14 @@ public class FindbugsConfigurationTest { assertThat(findbugsExcludeFile.exists(), is(true)); } + @Test + public void shouldReturnExcludesFilters() { + Configuration projectConfiguration = new BaseConfiguration(); + when(project.getConfiguration()).thenReturn(projectConfiguration); + FindbugsConfiguration conf = new FindbugsConfiguration(project, RulesProfile.create(), new FindbugsProfileExporter(), null); + + assertThat(conf.getExcludesFilters().size(), is(0)); + projectConfiguration.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, " foo.xml , bar.xml,"); + assertThat(conf.getExcludesFilters().size(), is(2)); + } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java new file mode 100644 index 00000000000..c49dcb36982 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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.plugins.findbugs; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.commons.configuration.Configuration; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.resources.Project; +import org.sonar.api.test.MavenTestUtils; + +public class FindbugsMavenInitializerTest { + + private Project project; + private FindbugsMavenInitializer initializer; + + @Before + public void setUp() { + project = mock(Project.class); + initializer = new FindbugsMavenInitializer(); + } + + @Test + public void doNotSetExcludesFiltersIfAlreadyConfigured() { + Configuration configuration = mock(Configuration.class); + when(configuration.containsKey(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)).thenReturn(true); + when(project.getConfiguration()).thenReturn(configuration); + initializer.execute(project); + verify(configuration, never()).setProperty(eq(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY), anyString()); + } + + @Test + public void shouldGetExcludesFiltersFromPom() { + Project project = MavenTestUtils.loadProjectFromPom(getClass(), "pom.xml"); + initializer.execute(project); + assertThat(project.getConfiguration().getString(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY), is("foo.xml")); + } + +} diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest/pom.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest/pom.xml new file mode 100644 index 00000000000..6cf12342a42 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest/pom.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>fake.group</groupId> + <artifactId>fake.artifactId</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>findbugs-maven-plugin</artifactId> + <configuration> + <excludeFilterFile>foo.xml</excludeFilterFile> + </configuration> + </plugin> + </plugins> + </build> + +</project> |