]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3885 Configure Findbugs Inclusions
authorDavid Gageot <david@gageot.net>
Tue, 6 Nov 2012 12:32:16 +0000 (13:32 +0100)
committerDavid Gageot <david@gageot.net>
Tue, 6 Nov 2012 12:32:16 +0000 (13:32 +0100)
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java

index 5d19d7de7946c497c904765c244b000348e41857..24ff073ddf9cc5d62dd083f8f152bba31fe48c99 100644 (file)
@@ -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
index bc1e11f879386501776eb1197a09f9070fb8becd..b261f2f203acc2a6eab27d4c8b66ff0189214a8d 100644 (file)
@@ -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;
   }
 }
index 27f3545dfd444f29f0c376f28b5dfa6abcd6bb4d..9cb7eb742c0a26553446ee31daec84feacfe5991 100644 (file)
@@ -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");