diff options
6 files changed, 78 insertions, 6 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 8e127a8ccd2..ad24fa026ae 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 @@ -34,7 +34,11 @@ import org.sonar.plugins.findbugs.xml.ClassFilter; import org.sonar.plugins.findbugs.xml.FindBugsFilter; import org.sonar.plugins.findbugs.xml.Match; -import java.io.*; +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; @@ -66,7 +70,7 @@ public class FindbugsConfiguration implements BatchExtension { File classesDir = project.getFileSystem().getBuildOutputDir(); if (classesDir == null || !classesDir.exists()) { throw new SonarException("Findbugs needs sources to be compiled. " - + "Please build project before executing sonar and check the location of compiled classes."); + + "Please build project before executing sonar and check the location of compiled classes."); } edu.umd.cs.findbugs.Project findbugsProject = new edu.umd.cs.findbugs.Project(); @@ -121,6 +125,10 @@ public class FindbugsConfiguration implements BatchExtension { return StringUtils.lowerCase(settings.getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY)); } + public String getConfidenceLevel() { + return StringUtils.lowerCase(settings.getString(CoreProperties.FINDBUGS_CONFIDENCE_LEVEL_PROPERTY)); + } + public long getTimeout() { return settings.getLong(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY); } 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 6e6d73e3e9f..e5676892594 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 @@ -20,7 +20,14 @@ package org.sonar.plugins.findbugs; import com.google.common.collect.Lists; -import edu.umd.cs.findbugs.*; +import edu.umd.cs.findbugs.DetectorFactoryCollection; +import edu.umd.cs.findbugs.FindBugs; +import edu.umd.cs.findbugs.FindBugs2; +import edu.umd.cs.findbugs.Plugin; +import edu.umd.cs.findbugs.PluginException; +import edu.umd.cs.findbugs.Priorities; +import edu.umd.cs.findbugs.Project; +import edu.umd.cs.findbugs.XMLBugReporter; import edu.umd.cs.findbugs.config.UserPreferences; import edu.umd.cs.findbugs.plugins.DuplicatePluginIdException; import org.apache.commons.io.FileUtils; @@ -39,8 +46,10 @@ import java.io.PrintStream; import java.net.URL; import java.util.Collection; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -55,6 +64,16 @@ public class FindbugsExecutor implements BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(FindbugsExecutor.class); + /** Map of priority level names to their numeric values. */ + private static Map<String, Integer> priorityNameToValueMap = new HashMap<String, Integer>(); + static { + priorityNameToValueMap.put("high", (Priorities.HIGH_PRIORITY)); + priorityNameToValueMap.put("medium", (Priorities.NORMAL_PRIORITY)); + priorityNameToValueMap.put("low", (Priorities.LOW_PRIORITY)); + priorityNameToValueMap.put("experimental", (Priorities.EXP_PRIORITY)); + } + private final static Integer DEFAULT_PRIORITY = Priorities.NORMAL_PRIORITY; + private FindbugsConfiguration configuration; public FindbugsExecutor(FindbugsConfiguration configuration) { @@ -87,7 +106,7 @@ public class FindbugsExecutor implements BatchExtension { engine.setProject(project); XMLBugReporter xmlBugReporter = new XMLBugReporter(project); - xmlBugReporter.setPriorityThreshold(Priorities.LOW_PRIORITY); + xmlBugReporter.setPriorityThreshold(determinePriorityThreshold()); xmlBugReporter.setAddMessages(true); File xmlReport = configuration.getTargetXMLReport(); @@ -135,6 +154,14 @@ public class FindbugsExecutor implements BatchExtension { } } + private Integer determinePriorityThreshold() { + Integer integer = priorityNameToValueMap.get(configuration.getConfidenceLevel()); + if (integer == null) { + integer = DEFAULT_PRIORITY; + } + return integer; + } + private static class FindbugsTask implements Callable<Object> { private FindBugs2 engine; 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 c38f73aae77..2c6e87a53b2 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 @@ -56,8 +56,17 @@ import java.util.List; project = true, module = true, global = true, - multiValues = true - )}) + multiValues = true), + @Property( + key = CoreProperties.FINDBUGS_CONFIDENCE_LEVEL_PROPERTY, + defaultValue = CoreProperties.FINDBUGS_CONFIDENCE_LEVEL_DEFAULT_VALUE, + name = "Confidence Level", + description = "Specifies the confidence threshold (previously called \"priority\") for reporting issues. If set to \"low\", confidence is not used to filter bugs. " + + "If set to \"medium\" (the default), low confidence issues are supressed. If set to \"high\", only high confidence bugs are reported. ", + project = true, + module = true, + global = true) +}) public class FindbugsPlugin extends SonarPlugin { public List<Class<? extends Extension>> getExtensions() { 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 28662ceb8f9..a2d2cc3643d 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 @@ -115,4 +115,11 @@ public class FindbugsConfigurationTest { assertThat(conf.getExcludesFilters()).hasSize(2); } + @Test + public void should_return_confidence_level() { + assertThat(conf.getConfidenceLevel()).as("default confidence level").isEqualTo("medium"); + settings.setProperty(CoreProperties.FINDBUGS_EFFORT_PROPERTY, "HIGH"); + assertThat(conf.getEffort()).isEqualTo("high"); + } + } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java index c8bb43d02b1..891ede6a980 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java @@ -49,6 +49,25 @@ public class FindbugsExecutorTest { String report = FileUtils.readFileToString(reportFile); assertThat(report).as("Report should contain bug instance").contains("<BugInstance"); assertThat(report).as("Report should be generated with messages").contains("<Message>"); + assertThat(report).contains("priority=\"1\""); + assertThat(report).doesNotContain("priority=\"3\""); + } + + @Test + public void canGenerateXMLReportWithCustomConfidence() throws Exception { + FindbugsConfiguration conf = mockConf(); + File reportFile = new File("target/test-tmp/customized-findbugs-report.xml"); + when(conf.getTargetXMLReport()).thenReturn(reportFile); + when(conf.getConfidenceLevel()).thenReturn("low"); + + new FindbugsExecutor(conf).execute(); + + assertThat(reportFile.exists()).isTrue(); + String report = FileUtils.readFileToString(reportFile); + assertThat(report).as("Report should contain bug instance").contains("<BugInstance"); + assertThat(report).as("Report should be generated with messages").contains("<Message>"); + assertThat(report).contains("priority=\"1\""); + assertThat(report).contains("priority=\"3\""); assertThat(report).contains("synthetic=\"true\""); } 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 b3b55b5ab03..c6e91e815fc 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 @@ -254,6 +254,8 @@ public interface CoreProperties { String FINDBUGS_PLUGIN = "findbugs"; String FINDBUGS_EFFORT_PROPERTY = "sonar.findbugs.effort"; String FINDBUGS_EFFORT_DEFAULT_VALUE = "Default"; + String FINDBUGS_CONFIDENCE_LEVEL_PROPERTY = "sonar.findbugs.confidenceLevel"; + String FINDBUGS_CONFIDENCE_LEVEL_DEFAULT_VALUE = "medium"; String FINDBUGS_REPORT_PATH = "sonar.findbugs.reportPath"; String FINDBUGS_TIMEOUT_PROPERTY = "sonar.findbugs.timeout"; long FINDBUGS_TIMEOUT_DEFAULT_VALUE = 600000; |