aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-11-06 13:32:16 +0100
committerDavid Gageot <david@gageot.net>2012-11-06 13:32:16 +0100
commit5d308f5d402522b2a387e30292187d71a3724ba8 (patch)
treecd1d03a6fa1dec60b54f05ffd218a655084637bc /plugins
parent123ef89aa488f906363d5fba90951be7d430756a (diff)
downloadsonarqube-5d308f5d402522b2a387e30292187d71a3724ba8.tar.gz
sonarqube-5d308f5d402522b2a387e30292187d71a3724ba8.zip
SONAR-3885 Configure Findbugs Inclusions
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java13
-rw-r--r--plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java54
-rw-r--r--plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java17
3 files changed, 46 insertions, 38 deletions
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 5d19d7de794..24ff073ddf9 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
@@ -35,7 +35,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -88,20 +87,16 @@ public class FindbugsConfiguration implements BatchExtension {
@VisibleForTesting
File saveIncludeConfigXml() throws IOException {
- StringWriter xml = new StringWriter();
+ String xml = exporter.exportProfileAndInclusions(profile, project.getInclusionPatterns());
- exporter.exportProfile(profile, xml);
-
- return project.getFileSystem().writeToWorkingDirectory(xml.toString(), "findbugs-include.xml");
+ return project.getFileSystem().writeToWorkingDirectory(xml, "findbugs-include.xml");
}
@VisibleForTesting
File saveExcludeConfigXml() throws IOException {
- StringWriter xml = new StringWriter();
-
- exporter.exportExclusions(project.getExclusionPatterns(), xml);
+ String xml = exporter.exportExclusions(project.getExclusionPatterns());
- return project.getFileSystem().writeToWorkingDirectory(xml.toString(), "findbugs-exclude.xml");
+ return project.getFileSystem().writeToWorkingDirectory(xml, "findbugs-exclude.xml");
}
@VisibleForTesting
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java
index bc1e11f8793..b261f2f203a 100644
--- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java
+++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java
@@ -44,37 +44,45 @@ public class FindbugsProfileExporter extends ProfileExporter {
@Override
public void exportProfile(RulesProfile profile, Writer writer) {
- List<ActiveRule> activeRulesByRepository = profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY);
- FindBugsFilter filter = buildFindbugsFilter(activeRulesByRepository);
+ try {
+ String xml = exportProfileAndInclusions(profile, null);
+ writer.write(xml);
+ } catch (IOException e) {
+ throw new SonarException("Fail to export Findbugs profile: " + profile.getName(), e);
+ }
+ }
- writeXml(filter, writer);
+ public String exportProfileAndInclusions(RulesProfile profile, String[] inclusions) {
+ FindBugsFilter root = new FindBugsFilter();
+ addRules(root, profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY));
+ addPatterns(root, inclusions);
+ return toXml(root);
}
- public void exportExclusions(String[] exclusions, Writer writer) {
- FindBugsFilter filter = new FindBugsFilter();
- if (exclusions != null) {
- for (String exclusion : exclusions) {
- ClassFilter classFilter = new ClassFilter(FindbugsAntConverter.antToJavaRegexpConvertor(exclusion));
- filter.addMatch(new Match(classFilter));
- }
- }
+ public String exportExclusions(String[] exclusions) {
+ FindBugsFilter root = new FindBugsFilter();
+ addPatterns(root, exclusions);
+ return toXml(root);
+ }
- writeXml(filter, writer);
+ private static String toXml(FindBugsFilter root) {
+ return new StringBuilder()
+ .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+ .append("<!-- Generated by Sonar -->\n")
+ .append(root.toXml())
+ .toString();
}
- private static void writeXml(FindBugsFilter filter, Writer writer) {
- try {
- writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- writer.write("<!-- Generated by Sonar -->\n");
- writer.write(filter.toXml());
- } catch (IOException e) {
- throw new SonarException("Fail to export Findbugs configuration", e);
+ private static void addPatterns(FindBugsFilter root, String[] patterns) {
+ if (patterns != null) {
+ for (String pattern : patterns) {
+ ClassFilter classFilter = new ClassFilter(FindbugsAntConverter.antToJavaRegexpConvertor(pattern));
+ root.addMatch(new Match(classFilter));
+ }
}
}
- protected static FindBugsFilter buildFindbugsFilter(List<ActiveRule> activeRules) {
- FindBugsFilter root = new FindBugsFilter();
-
+ protected static void addRules(FindBugsFilter root, List<ActiveRule> activeRules) {
for (ActiveRule activeRule : activeRules) {
if (FindbugsConstants.REPOSITORY_KEY.equals(activeRule.getRepositoryKey())) {
Match child = new Match();
@@ -82,7 +90,5 @@ public class FindbugsProfileExporter extends ProfileExporter {
root.addMatch(child);
}
}
-
- return root;
}
}
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java
index 27f3545dfd4..9cb7eb742c0 100644
--- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java
+++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java
@@ -64,15 +64,19 @@ public class FindbugsProfileExporterTest extends FindbugsTests {
@Test
public void shouldBuildOnlyOneModuleWhenNoActiveRules() {
- FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Collections.<ActiveRule> emptyList());
- assertThat(filter.getMatchs()).hasSize(0);
+ FindBugsFilter filter = new FindBugsFilter();
+ FindbugsProfileExporter.addRules(filter, Collections.<ActiveRule> emptyList());
+
+ assertThat(filter.getMatchs()).isEmpty();
}
@Test
public void shouldBuildTwoModulesEvenIfSameTwoRulesActivated() {
ActiveRule activeRule1 = anActiveRule(DLS_DEAD_LOCAL_STORE);
ActiveRule activeRule2 = anActiveRule(SS_SHOULD_BE_STATIC);
- FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
+
+ FindBugsFilter filter = new FindBugsFilter();
+ FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule1, activeRule2));
List<Match> matches = filter.getMatchs();
assertThat(matches).hasSize(2);
@@ -86,14 +90,17 @@ public class FindbugsProfileExporterTest extends FindbugsTests {
ActiveRule activeRule1 = anActiveRuleFromAnotherPlugin();
ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin();
- FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
+ FindBugsFilter filter = new FindBugsFilter();
+ FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule1, activeRule2));
assertThat(filter.getMatchs()).hasSize(0);
}
@Test
public void shouldBuildModuleWithProperties() {
ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE);
- FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule));
+
+ FindBugsFilter filter = new FindBugsFilter();
+ FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule));
assertThat(filter.getMatchs()).hasSize(1);
assertThat(filter.getMatchs().get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");