deprecatedRules) {
+ for (BugPattern bugPattern : plugin.getBugPatterns()) {
+ String type = bugPattern.getType();
+
+ String category = bugPattern.getCategory();
+
+ if(category.equals("NOISE") || Arrays.asList("TESTING", "TESTING1", "TESTING2", "TESTING3", "UNKNOWN").contains(type)) {
+ continue;
+ }
+
+ if(category.equals("MT_CORRECTNESS")) {
+ category = "MULTI-THREADING";
+ }
+
+ String htmlDescription = bugPattern.getDetailText();
+ String severity = getSonarSeverity(type, category, htmlDescription);
+ String name = capitalize(category.toLowerCase()).replace("_"," ") + " - " + bugPattern.getShortDescription();
+ boolean deprecated = bugPattern.isDeprecated();
+ String deprecationReplacement = deprecatedRules.get(type);
+
+ RuleStatus ruleStatus = RuleStatus.READY;
+
+ if (deprecationReplacement != null) {
+ htmlDescription = htmlDescription.trim() + "\nDeprecated
\nThis rule is deprecated; use {rule:" + deprecationReplacement + "} instead.
";
+ ruleStatus = RuleStatus.DEPRECATED;
+ } else if (deprecated) {
+ htmlDescription = htmlDescription.trim() + "\nDeprecated
\nThis rule is deprecated
";
+ ruleStatus = RuleStatus.DEPRECATED;
+ }
+
+ List tags = new ArrayList<>();
+
+ //OWASP TOP 10 2013
+ if (htmlDescription.toLowerCase().contains("injection") || htmlDescription.contains("A1-Injection")) {
+ tags.add("owasp-a1");
+ tags.add("injection");
+ }
+ if (htmlDescription.contains("A2-Broken_Authentication_and_Session_Management")) {
+ tags.add("owasp-a2");
+ }
+ if (type.contains("XSS") || htmlDescription.contains("A3-Cross-Site_Scripting")) {
+ tags.add("owasp-a3");
+ }
+ if (htmlDescription.contains("A4-Insecure_Direct_Object_References") || htmlDescription.contains("Path_Traversal")) {
+ tags.add("owasp-a4");
+ }
+ if (htmlDescription.contains("A5-Security_Misconfiguration")) {
+ tags.add("owasp-a5");
+ }
+ if (type.equals("HARD_CODE_PASSWORD") ||
+ CRYPTO_BUGS.contains(type) ||
+ htmlDescription.contains("A6-Sensitive_Data_Exposure")) {
+ tags.add("owasp-a6");
+ tags.add("cryptography");
+ }
+ if (htmlDescription.contains("A7-Missing_Function_Level_Access_Control")) {
+ tags.add("owasp-a7");
+ }
+ if (htmlDescription.toLowerCase().contains("A8-Cross-Site_Request_Forgery")) {
+ tags.add("owasp-a8");
+ }
+ if (htmlDescription.toLowerCase().contains("A9-Using_Components_with_Known_Vulnerabilities")) {
+ tags.add("owasp-a9");
+ }
+ if (htmlDescription.toLowerCase().contains("A10-Unvalidated_Redirects_and_Forwards")) {
+ tags.add("owasp-a10");
+ }
+
+ //Misc tags
+
+ if (htmlDescription.toLowerCase().contains("wasc")) {
+ tags.add("wasc");
+ }
+ if (htmlDescription.toLowerCase().contains("cwe")) {
+ tags.add("cwe");
+ }
+ if (bugPattern.getShortDescription().toLowerCase().contains("android")) {
+ tags.add("android");
+ }
+ if (type.contains("JSP")) {
+ tags.add("jsp");
+ }
+
+ //Category related
+ tags.add(category.toLowerCase().replace("_","-"));
+
+ if(Arrays.asList("PERFORMANCE","CORRECTNESS","MULTI-THREADING").contains(category)) {
+ tags.add("bug");
+ }
+
+ if((includedBugs.isEmpty() || includedBugs.contains(type)) && !excludedBugs.contains(type)) {
+ repository
+ .createRule(type)
+ .setInternalKey(type)
+ .setSeverity(severity)
+ .setName(name)
+ .setHtmlDescription(htmlDescription)
+ .setStatus(ruleStatus)
+ .setTags(tags.toArray(String[]::new));
+ }
+ }
+ }
+
+ public static String capitalize(String s) {
+ return s.substring(0, 1).toUpperCase() + s.substring(1);
+ }
+
+ public String getSonarSeverity(String type, String category, String description) {
+ String priority = getFsbSeverityFromType(type,category);
+ if(priority != null) {
+ return priority;
+ }
+
+ //Findbugs critical base on the type or message
+ if(type.contains("IMPOSSIBLE")) {
+ return Severity.CRITICAL;
+ }
+
+ Pattern willResultInExceptionAtRuntimePattern = Pattern.compile("[\\S\\s]*will result in [\\w]+Exception at runtime[\\S\\s]*");
+ Pattern willAlwaysThrowExceptionPattern = Pattern.compile("[\\S\\s]*will always throw a [\\w]+Exception[\\S\\s]*");
+
+ if(willResultInExceptionAtRuntimePattern.matcher(description).matches() || willAlwaysThrowExceptionPattern.matcher(description).matches()) {
+ return Severity.CRITICAL;
+ }
+
+ //Findbugs general
+ if(Arrays.asList("CORRECTNESS", "PERFORMANCE", "SECURITY","MULTI-THREADING","BAD_PRACTICE").contains(category)) {
+ return Severity.MAJOR;
+ }
+ if(Arrays.asList("STYLE", "MALICIOUS_CODE", "I18N","EXPERIMENTAL").contains(category)) {
+ return Severity.INFO;
+ }
+
+ LOG.warn("Unknown priority for {} ({})", type, category);
+ return Severity.INFO;
+ }
+
+ private String getFsbSeverityFromType(String type, String category) {
+ if (CRITICAL_BUGS.contains(type) || CRITICAL_JSP_BUGS.contains(type) || CRITICAL_SCALA_BUGS.contains(type)) {
+ return Severity.CRITICAL;
+ }
+
+ if (MAJOR_BUGS.contains(type) || CRYPTO_BUGS.contains(type) || MAJOR_JSP_BUGS.contains(type)) {
+ return Severity.MAJOR;
+ }
+
+ if (INFORMATIONAL_PATTERNS.contains(type)) {
+ return Severity.INFO;
+ }
+
+ if(category.equals("SECURITY")) {
+ return Severity.MAJOR;
+ }
+
+ return null;
+ }
+}
diff --git a/src/main/java/org/sonar/plugins/findbugs/xml/FindBugsFilter.java b/src/main/java/org/sonar/plugins/findbugs/xml/FindBugsFilter.java
index ba43cbc8..28844fc7 100644
--- a/src/main/java/org/sonar/plugins/findbugs/xml/FindBugsFilter.java
+++ b/src/main/java/org/sonar/plugins/findbugs/xml/FindBugsFilter.java
@@ -24,13 +24,12 @@
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.io.xml.StaxDriver;
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.StringUtils;
import org.sonar.api.rule.Severity;
import org.sonar.plugins.findbugs.FindbugsLevelUtils;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
diff --git a/src/test/java/org/sonar/plugins/findbugs/FbContribRulesDefinitionTest.java b/src/test/java/org/sonar/plugins/findbugs/FbContribRulesDefinitionTest.java
index 17feeab7..ad7e26e7 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FbContribRulesDefinitionTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FbContribRulesDefinitionTest.java
@@ -23,6 +23,7 @@
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.plugins.findbugs.rules.FbContribRulesDefinition;
+import org.sonar.plugins.findbugs.rules.FindbugsRulesPluginsDefinition;
import org.sonar.plugins.java.Java;
import java.util.List;
@@ -33,7 +34,7 @@ class FbContribRulesDefinitionTest {
@Test
void test() {
- FbContribRulesDefinition definition = new FbContribRulesDefinition();
+ FindbugsRulesPluginsDefinition definition = new FindbugsRulesPluginsDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
RulesDefinition.Repository repository = context.repository(FbContribRulesDefinition.REPOSITORY_KEY);
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindSecurityBugsRulesDefinitionTest.java b/src/test/java/org/sonar/plugins/findbugs/FindSecurityBugsRulesDefinitionTest.java
index 53ff20f1..f17fbaf9 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindSecurityBugsRulesDefinitionTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindSecurityBugsRulesDefinitionTest.java
@@ -23,6 +23,7 @@
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsRulesDefinition;
+import org.sonar.plugins.findbugs.rules.FindbugsRulesPluginsDefinition;
import org.sonar.plugins.java.Java;
import java.util.List;
@@ -33,7 +34,7 @@ class FindSecurityBugsRulesDefinitionTest {
@Test
void testLoadRepositoryFromXml() {
- FindSecurityBugsRulesDefinition definition = new FindSecurityBugsRulesDefinition();
+ FindbugsRulesPluginsDefinition definition = new FindbugsRulesPluginsDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
RulesDefinition.Repository repository = context.repository(FindSecurityBugsRulesDefinition.REPOSITORY_KEY);
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java
index a1457c38..bd235a84 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java
@@ -19,14 +19,13 @@
*/
package org.sonar.plugins.findbugs;
-import com.google.common.collect.ImmutableList;
-
import edu.umd.cs.findbugs.ClassScreener;
import edu.umd.cs.findbugs.Project;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
@@ -130,63 +129,24 @@ void should_return_confidence_level() {
@Test
void should_set_class_files() throws IOException {
File file = new File(temp, "MyClass.class");
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(ImmutableList.of(file));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(file));
try (Project findbugsProject = new Project()) {
conf.initializeFindbugsProject(findbugsProject);
assertThat(findbugsProject.getFileList()).containsOnly(file.getCanonicalPath());
- conf.stop();
}
}
@Test
void should_set_class_path() throws IOException {
File classpath = new File(temp, "classpath");
- when(javaResourceLocator.classpath()).thenReturn(ImmutableList.of(classpath));
+ when(javaResourceLocator.classpath()).thenReturn(Collections.singletonList(classpath));
try (Project findbugsProject = new Project()) {
conf.initializeFindbugsProject(findbugsProject);
assertThat(findbugsProject.getAuxClasspathEntryList()).contains(classpath.getCanonicalPath());
- conf.stop();
}
}
-
- @Test
- void should_copy_lib_in_working_dir() throws IOException {
- String jsr305 = "findbugs/jsr305.jar";
- String annotations = "findbugs/annotations.jar";
-
- // stop at start
- conf.stop();
- assertThat(new File(fs.workDir(), jsr305)).doesNotExist();
- assertThat(new File(fs.workDir(), annotations)).doesNotExist();
-
- conf.copyLibs();
- assertThat(new File(fs.workDir(), jsr305)).isFile();
- assertThat(new File(fs.workDir(), annotations)).isFile();
-
- // copy again
- conf.copyLibs();
- assertThat(new File(fs.workDir(), jsr305)).isFile();
- assertThat(new File(fs.workDir(), annotations)).isFile();
-
- conf.stop();
- assertThat(new File(fs.workDir(), jsr305)).doesNotExist();
- assertThat(new File(fs.workDir(), annotations)).doesNotExist();
-
- }
-
- @Test
- void should_get_fbcontrib() throws IOException {
- conf.copyLibs();
- assertThat(conf.getFbContribJar()).isFile();
- }
-
- @Test
- void should_get_findSecBugs() throws IOException {
- conf.copyLibs();
- assertThat(conf.getFindSecBugsJar()).isFile();
- }
@Test
public void should_get_only_analyze_filter() {
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java
index 07694b9b..da837305 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java
@@ -19,9 +19,14 @@
*/
package org.sonar.plugins.findbugs;
-import com.google.common.collect.Lists;
-
+import edu.umd.cs.findbugs.DetectorFactory;
+import edu.umd.cs.findbugs.DetectorFactoryCollection;
+import edu.umd.cs.findbugs.Plugin;
import edu.umd.cs.findbugs.Project;
+import edu.umd.cs.findbugs.config.UserPreferences;
+import edu.umd.cs.findbugs.detect.DumbMethods;
+import edu.umd.cs.findbugs.detect.FindFinalizeInvocations;
+import edu.umd.cs.findbugs.detect.TrainFieldStoreTypes;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
@@ -31,13 +36,19 @@
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Configuration;
+import org.sonar.api.rule.RuleKey;
import org.sonar.plugins.findbugs.configuration.SimpleConfiguration;
+import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.Map;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@@ -56,6 +67,8 @@ class FindbugsExecutorTest {
FilePredicates predicatesEmpty;
Configuration configEmpty;
+
+ ActiveRules activeRules;
@BeforeEach
public void setUp() {
@@ -68,6 +81,8 @@ public void setUp() {
configEmpty = mock(Configuration.class);
when(configEmpty.getStringArray(any())).thenReturn(new String[0]);
when(configEmpty.get(any())).thenReturn(Optional.of(""));
+
+ activeRules = mock(ActiveRules.class);
}
@Test
@@ -77,7 +92,7 @@ void canGenerateXMLReport() throws Exception {
File reportFile = new File(temporaryFolder, "findbugs-report.xml");
when(conf.getTargetXMLReport()).thenReturn(reportFile);
- new FindbugsExecutor(conf, fsEmpty, configEmpty).execute();
+ new FindbugsExecutor(conf, fsEmpty, configEmpty).execute(activeRules);
assertThat(reportFile).exists();
String report = FileUtils.readFileToString(reportFile, StandardCharsets.UTF_8);
@@ -96,7 +111,7 @@ void canGenerateXMLReportWithCustomConfidence() throws Exception {
when(conf.getTargetXMLReport()).thenReturn(reportFile);
when(conf.getConfidenceLevel()).thenReturn("low");
- new FindbugsExecutor(conf, fsEmpty, configEmpty).execute();
+ new FindbugsExecutor(conf, fsEmpty, configEmpty).execute(activeRules);
assertThat(reportFile).exists();
String report = FileUtils.readFileToString(reportFile, StandardCharsets.UTF_8);
@@ -114,7 +129,7 @@ public void shouldTerminateAfterTimeout() throws Exception {
FindbugsExecutor executor = new FindbugsExecutor(conf, fsEmpty, configEmpty);
assertThrows(IllegalStateException.class, () -> {
- executor.execute();
+ executor.execute(activeRules);
});
}
@@ -127,7 +142,7 @@ public void shoulFailIfNoCompiledClasses() throws Exception {
FindbugsExecutor executor = new FindbugsExecutor(conf, fsEmpty, configEmpty);
assertThrows(IllegalStateException.class, () -> {
- executor.execute();
+ executor.execute(activeRules);
});
}
@@ -142,10 +157,30 @@ private FindbugsConfiguration mockConf() throws Exception {
return null;
}).when(conf).initializeFindbugsProject(any());
when(conf.saveIncludeConfigXml()).thenReturn(new File("test-resources/findbugs-include.xml"));
- when(conf.getExcludesFilters()).thenReturn(Lists.newArrayList(new File("test-resources/findbugs-exclude.xml"), new File("test-resources/fake-file.xml")));
+ when(conf.getExcludesFilters()).thenReturn(Arrays.asList(new File("test-resources/findbugs-exclude.xml"), new File("test-resources/fake-file.xml")));
when(conf.getEffort()).thenReturn("default");
when(conf.getTimeout()).thenReturn(FindbugsConstants.TIMEOUT_DEFAULT_VALUE);
return conf;
}
+ @Test
+ void disableUnnecessaryDetectors() {
+ Map plugins = FindbugsExecutor.loadFindbugsPlugins();
+
+ when(activeRules.find(RuleKey.of(FindbugsRulesDefinition.REPOSITORY_KEY, "DM_INVALID_MIN_MAX"))).thenReturn(mock(ActiveRule.class));
+
+ UserPreferences userPreferences = UserPreferences.createDefaultUserPreferences();
+ FindbugsExecutor.disableUnnecessaryDetectors(userPreferences, activeRules);
+
+ DetectorFactory dumbMethods = DetectorFactoryCollection.instance().getFactoryByClassName(DumbMethods.class.getName());
+ DetectorFactory findFinalize = DetectorFactoryCollection.instance().getFactoryByClassName(FindFinalizeInvocations.class.getName());
+ DetectorFactory trainFieldStoreTypes = DetectorFactoryCollection.instance().getFactoryByClassName(TrainFieldStoreTypes.class.getName());
+
+ // DM_INVALID_MIN_MAX is reported by DumbMethods so the detector should be enabled
+ assertThat(userPreferences.isDetectorEnabled(dumbMethods)).withFailMessage("DumbMethods should be enabled").isTrue();
+ // No active rule reported by FindFinalizeInvocations so it should be disabled
+ assertThat(userPreferences.isDetectorEnabled(findFinalize)).withFailMessage("FindFinalizeInvocations should be enabled").isFalse();
+ // TrainFieldStoreTypes is not a reporting detector so it should always be enabled
+ assertThat(userPreferences.isDetectorEnabled(trainFieldStoreTypes)).withFailMessage("TrainFieldStoreTypes should be enabled").isTrue();
+ }
}
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsPluginTest.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsPluginTest.java
index b56ea42c..d8fc9b65 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsPluginTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsPluginTest.java
@@ -38,6 +38,6 @@ void testGetExtensions() {
FindbugsPlugin plugin = new FindbugsPlugin();
plugin.define(ctx);
- assertEquals(24, ctx.getExtensions().size(), "extension count");
+ assertEquals(14, ctx.getExtensions().size(), "extension count");
}
}
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesDefinitionTest.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesDefinitionTest.java
index 3bb104a8..ec038181 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesDefinitionTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesDefinitionTest.java
@@ -19,28 +19,31 @@
*/
package org.sonar.plugins.findbugs;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
+import org.sonar.plugins.findbugs.rules.FindbugsRulesPluginsDefinition;
import org.sonar.plugins.java.Java;
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
class FindbugsRulesDefinitionTest {
/**
* The SpotBugs rules repository
*/
private RulesDefinition.Repository repository;
+ private RulesDefinition.Context context;
@BeforeEach
public void setupRepository() {
- FindbugsRulesDefinition definition = new FindbugsRulesDefinition();
- RulesDefinition.Context context = new RulesDefinition.Context();
+ FindbugsRulesPluginsDefinition definition = new FindbugsRulesPluginsDefinition();
+
+ context = new RulesDefinition.Context();
definition.define(context);
repository = context.repository(FindbugsRulesDefinition.REPOSITORY_KEY);
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
index 658ff0ed..a55a3079 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.plugins.findbugs;
-import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.ClassAnnotation;
import edu.umd.cs.findbugs.MethodAnnotation;
@@ -43,6 +42,7 @@
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
+import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.issue.NewIssue;
@@ -69,12 +69,14 @@ class FindbugsSensorTest extends FindbugsTests {
private ByteCodeResourceLocator byteCodeResourceLocator;
private MutablePicoContainer pico;
private SensorContext sensorContext;
+ private ActiveRules activeRules;
private FindbugsExecutor executor;
private JavaResourceLocator javaResourceLocator;
@BeforeEach
public void setUp() throws IOException {
sensorContext = mock(SensorContext.class);
+ activeRules = mock(ActiveRules.class);
byteCodeResourceLocator = mock(ByteCodeResourceLocator.class);
executor = mock(FindbugsExecutor.class);
javaResourceLocator = mockJavaResourceLocator();
@@ -117,6 +119,7 @@ public void setUp() throws IOException {
//--
when(sensorContext.newIssue()).thenReturn(newIssue);
+ when(sensorContext.activeRules()).thenReturn(activeRules);
pico.addComponent(executor);
pico.addComponent(javaResourceLocator);
@@ -134,15 +137,15 @@ void should_execute_findbugs() throws Exception {
BugInstance bugInstance = getBugInstance("AM_CREATES_EMPTY_ZIP_FILE_ENTRY", 6, true);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute(false, false)).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
JavaResourceLocator javaResourceLocator = mockJavaResourceLocator();
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindbugsRules());
FindbugsSensor sensor = pico.getComponent(FindbugsSensor.class);
sensor.execute(sensorContext);
- verify(executor).execute(false, false);
+ verify(executor).execute(activeRules);
verify(sensorContext, times(1)).newIssue();
}
@@ -151,17 +154,17 @@ void should_not_add_issue_if_resource_not_found() throws Exception {
BugInstance bugInstance = getBugInstance("AM_CREATES_EMPTY_ZIP_FILE_ENTRY", 13, false);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute(false, false)).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(null);
when(fs.inputFiles(any(FilePredicate.class))).thenReturn(new ArrayList());
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindbugsRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor).execute(false, false);
+ verify(executor).execute(activeRules);
verify(sensorContext, never()).newIssue();
}
@@ -171,16 +174,16 @@ void should_execute_findbugs_even_if_only_fbcontrib() throws Exception {
BugInstance bugInstance = getBugInstance("ISB_INEFFICIENT_STRING_BUFFERING", 49, true);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute(true, false)).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
JavaResourceLocator javaResourceLocator = mockJavaResourceLocator();
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFbContribRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor).execute(true, false);
+ verify(executor).execute(activeRules);
verify(sensorContext, times(1)).newIssue();
}
@@ -189,16 +192,16 @@ void should_execute_findbugs_even_if_only_findsecbug() throws Exception {
BugInstance bugInstance = getBugInstance("PREDICTABLE_RANDOM", 0, true);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute(false, true)).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindSecBugsRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor).execute(false, true);
+ verify(executor).execute(activeRules);
verify(sensorContext, times(1)).newIssue();
}
@@ -207,30 +210,30 @@ void should_execute_findbugs_but_not_find_violation() throws Exception {
BugInstance bugInstance = getBugInstance("THIS_RULE_DOES_NOT_EXIST", 107, true);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute(false, false)).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindbugsRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor).execute(false, false);
+ verify(executor).execute(activeRules);
verify(sensorContext, never()).newIssue();
}
@Test
void should_not_execute_findbugs_if_no_active() throws Exception {
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithNoRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor, never()).execute(false, false);
+ verify(executor, never()).execute(activeRules);
verify(sensorContext, never()).newIssue();
}
@@ -244,14 +247,14 @@ void should_not_execute_findbugs_if_only_jsp_rules_and_no_jsp_file() throws Exce
TreeSet languages = new TreeSet<>(Arrays.asList("java", "xml"));
when(fs.languages()).thenReturn(languages);
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindSecBugsJspRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor, never()).execute(false, false);
+ verify(executor, never()).execute(activeRules);
verify(sensorContext, never()).newIssue();
}
@@ -263,14 +266,14 @@ void should_execute_findbugs_if_only_jsp_rules_and_some_jsp_files() throws Excep
TreeSet languages = new TreeSet<>(Arrays.asList("java", "xml", "jsp"));
when(fs.languages()).thenReturn(languages);
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Lists.newArrayList(new File("file")));
+ when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.singletonList(new File("file")));
pico.addComponent(FakeActiveRules.createWithOnlyFindSecBugsJspRules());
FindbugsSensor analyser = pico.getComponent(FindbugsSensor.class);
analyser.execute(sensorContext);
- verify(executor).execute(false, true);
+ verify(executor).execute(activeRules);
verify(sensorContext, never()).newIssue();
}
@@ -296,16 +299,6 @@ private BugInstance getBugInstance(String name, int line, boolean mockFindSource
return bugInstance;
}
- @Test
- void should_not_execute_if_no_compiled_class_available() throws Exception {
- when(javaResourceLocator.classFilesToAnalyze()).thenReturn(Collections.emptyList());
- pico.addComponent(FakeActiveRules.createWithOnlyFindbugsRules());
- FindbugsSensor sensor = pico.getComponent(FindbugsSensor.class);
- sensor.execute(sensorContext);
-
- verify(executor, never()).execute();
- }
-
@Test
void shouldIgnoreNotActiveViolations() throws Exception {
BugInstance bugInstance = new BugInstance("UNKNOWN", 2);
@@ -314,7 +307,7 @@ void shouldIgnoreNotActiveViolations() throws Exception {
ClassAnnotation classAnnotation = new ClassAnnotation(className, sourceFile);
bugInstance.add(classAnnotation);
Collection collection = Arrays.asList(new ReportedBug(bugInstance));
- when(executor.execute()).thenReturn(collection);
+ when(executor.execute(activeRules)).thenReturn(collection);
pico.addComponent(FakeActiveRules.createWithOnlyFindbugsRules());
FindbugsSensor sensor = pico.getComponent(FindbugsSensor.class);
diff --git a/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java b/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java
index c55c37fa..07e0714d 100644
--- a/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java
+++ b/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java
@@ -25,7 +25,7 @@
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang.CharUtils;
+import org.apache.commons.lang3.CharUtils;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.jupiter.api.Assertions;
diff --git a/src/test/java/org/sonar/plugins/findbugs/configuration/SimpleConfiguration.java b/src/test/java/org/sonar/plugins/findbugs/configuration/SimpleConfiguration.java
index 761f0096..4d7e06da 100644
--- a/src/test/java/org/sonar/plugins/findbugs/configuration/SimpleConfiguration.java
+++ b/src/test/java/org/sonar/plugins/findbugs/configuration/SimpleConfiguration.java
@@ -17,7 +17,7 @@
*/
package org.sonar.plugins.findbugs.configuration;
-import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang3.ArrayUtils;
import org.sonar.api.config.Configuration;
import java.util.ArrayList;
@@ -26,8 +26,6 @@
import java.util.Map;
import java.util.Optional;
-import com.google.common.base.Splitter;
-
public class SimpleConfiguration implements Configuration {
private Map values = new HashMap<>();
@@ -49,7 +47,7 @@ public String[] getStringArray(String key) {
}
List values = new ArrayList<>();
- for (String v : Splitter.on(",").trimResults().split(value)) {
+ for (String v : value.split(",")) {
values.add(v.replace("%2C", ","));
}
return values.toArray(new String[values.size()]);
diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java
index 18154093..4aaec534 100644
--- a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java
+++ b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java
@@ -30,9 +30,9 @@
import java.io.File;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.util.List;
-import com.google.common.io.Files;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.build.SonarScanner;
@@ -139,7 +139,7 @@ void inclusions_exclusions() throws Exception {
orchestrator.executeBuild(sonarScanner);
// Check that class was really excluded from Findbugs analysis:
- String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8);
+ String findbugsXml = Files.readString(projectDir.toPath().resolve(".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8);
// FIXME Even though a source file is excluded, the corresponding .class file is currently analyzed by the plugin
// assertThat(findbugsXml).doesNotContain("Findbugs2.class");
@@ -182,7 +182,7 @@ void only_analyze() throws Exception {
orchestrator.executeBuild(build);
// Check that class was really excluded from Findbugs analysis:
- String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8);
+ String findbugsXml = Files.readString(projectDir.toPath().resolve(".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8);
assertThat(findbugsXml).doesNotContain("Findbugs2.class");
diff --git a/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java b/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java
index 65b74e85..4ef3deaf 100644
--- a/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java
+++ b/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java
@@ -19,15 +19,15 @@
import static org.assertj.core.api.Assertions.assertThat;
+import java.util.List;
+
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.sonar.plugins.findbugs.profiles.FindbugsSecurityScalaProfile;
+import org.sonar.plugins.findbugs.profiles.FindbugsProfile;
import org.sonarqube.ws.Issues.Issue;
import org.sonarqube.ws.client.issues.IssuesService;
-import java.util.List;
-
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.MavenBuild;
@@ -39,7 +39,7 @@ class ScalaIT {
@BeforeEach
public void setupProfile() {
FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Scala Integration Tests", "IT", "java");
- FindbugsTestSuite.setupProfile(PROJECT_KEY, FindbugsSecurityScalaProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME, "scala");
+ FindbugsTestSuite.setupProfile(PROJECT_KEY, FindbugsProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME, "scala");
}
@AfterEach
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsContribProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsContribProfileTest.java
index 5c9877ba..4c45d044 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsContribProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsContribProfileTest.java
@@ -2,11 +2,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FbContribRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
@@ -15,6 +16,9 @@
import static org.assertj.core.api.Assertions.assertThat;
+import java.util.List;
+import java.util.stream.Collectors;
+
class FindbugsContribProfileTest {
@RegisterExtension
@@ -22,16 +26,47 @@ class FindbugsContribProfileTest {
@Test
void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsContribProfile findbugsProfile = new FindbugsContribProfile(importer);
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
- BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsContribProfile.FB_CONTRIB_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsProfile.FB_CONTRIB_PROFILE_NAME);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindbugsRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FbContribRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FbContribRulesDefinition.RULE_COUNT);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
FindbugsProfileTest.assertHasOnlyRulesForLanguage(profile.rules(), Java.KEY);
}
+
+ @Test
+ void coreRulesAreFindBugsProfile() {
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
+ Context context = new Context();
+ findbugsProfile.define(context);
+
+ BuiltInQualityProfile fbContribQualityProfile = context.profile(Java.KEY, FindbugsProfile.FB_CONTRIB_PROFILE_NAME);
+ BuiltInQualityProfile findbugsQualityProfile = context.profile(Java.KEY, FindbugsProfile.FINDBUGS_PROFILE_NAME);
+
+ List findbugsRulesInFbContribProfile = fbContribQualityProfile
+ .rules()
+ .stream()
+ .filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY))
+ .map(BuiltInActiveRule::ruleKey)
+ .sorted()
+ .collect(Collectors.toList());
+
+ List findbugsRules = findbugsQualityProfile
+ .rules()
+ .stream()
+ .filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY))
+ .map(BuiltInActiveRule::ruleKey)
+ .sorted()
+ .collect(Collectors.toList());
+
+ assertThat(findbugsRulesInFbContribProfile)
+ .containsExactlyElementsOf(findbugsRules)
+ .hasSize(FindbugsRulesDefinition.RULE_COUNT);
+ }
}
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsProfileTest.java
index 613cd305..3e1b9b8b 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsProfileTest.java
@@ -21,12 +21,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.Jsp;
import org.sonar.plugins.findbugs.language.scala.Scala;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
@@ -48,15 +48,16 @@ class FindbugsProfileTest {
public LogTester logTester = new JupiterLogTester();
@Test
- void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsProfile findbugsProfile = new FindbugsProfile(importer);
+ void findbugsProfile() {
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsProfile.FINDBUGS_PROFILE_NAME);
assertThat(profile.rules()).hasSize(FindbugsRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindbugsRulesDefinition.RULE_COUNT);
+ assertThat(profile.rules().stream().filter(r -> !r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isZero();
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
FindbugsProfileTest.assertHasOnlyRulesForLanguage(profile.rules(), Java.KEY);
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsScalaProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsScalaProfileTest.java
index c8d5ba8d..73b057f2 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsScalaProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsScalaProfileTest.java
@@ -21,11 +21,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.scala.Scala;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsScalaRulesDefinition;
@@ -38,12 +38,12 @@ class FindbugsScalaProfileTest {
@Test
void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsSecurityScalaProfile findbugsProfile = new FindbugsSecurityScalaProfile(importer);
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
- BuiltInQualityProfile profile = context.profile(Scala.KEY, FindbugsSecurityScalaProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Scala.KEY, FindbugsProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME);
assertThat(profile.rules()).hasSize(FindSecurityBugsScalaRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsScalaRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindSecurityBugsScalaRulesDefinition.RULE_COUNT);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityAuditProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityAuditProfileTest.java
index a2c4decb..2a7aae44 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityAuditProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityAuditProfileTest.java
@@ -21,11 +21,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
@@ -41,13 +41,13 @@ class FindbugsSecurityAuditProfileTest {
@Test
void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsSecurityAuditProfile findbugsProfile = new FindbugsSecurityAuditProfile(importer);
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
// The standard FindBugs include only 9. Fb-Contrib and FindSecurityBugs include other rules
- BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsSecurityAuditProfile.FINDBUGS_SECURITY_AUDIT_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsProfile.FINDBUGS_SECURITY_AUDIT_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
// FSB rules must be added to FsbClassifier.groovy otherwise new rules metadata are not added in rules-findsecbugs.xml
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityJspProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityJspProfileTest.java
index 562e50ba..0c2886e3 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityJspProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityJspProfileTest.java
@@ -26,7 +26,6 @@
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.Jsp;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsJspRulesDefinition;
@@ -42,13 +41,13 @@ class FindbugsSecurityJspProfileTest {
@Test
void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsSecurityJspProfile findbugsProfile = new FindbugsSecurityJspProfile(importer);
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
//There are 6 rules that are JSP specific (the other findbugs rules can also be found in JSP files)
- BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsSecurityJspProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(6);
@@ -64,14 +63,13 @@ void disabledRuleMustNotBeActivated() {
// Mark a rule as removed
org.sonar.api.rules.Rule rule = ruleFinder.findByKey(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY, "XSS_JSP_PRINT");
rule.setStatus(org.sonar.api.rules.Rule.STATUS_REMOVED);
-
- FindbugsProfileImporter importer = new FindbugsProfileImporter(ruleFinder);
- FindbugsSecurityJspProfile findbugsProfile = new FindbugsSecurityJspProfile(importer);
+
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
//There should be 5 rules left since we removed one
- BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsSecurityJspProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(5);
diff --git a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityMinimalProfileTest.java b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityMinimalProfileTest.java
index 69c3afd2..da08debe 100644
--- a/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityMinimalProfileTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/profiles/FindbugsSecurityMinimalProfileTest.java
@@ -21,11 +21,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
@@ -41,12 +41,12 @@ class FindbugsSecurityMinimalProfileTest {
@Test
void shouldCreateProfile() {
- FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithAllRules());
- FindbugsSecurityMinimalProfile findbugsProfile = new FindbugsSecurityMinimalProfile(importer);
+ RuleFinder ruleFinder = FakeRuleFinder.createWithAllRules();
+ FindbugsProfile findbugsProfile = new FindbugsProfile(ruleFinder);
Context context = new Context();
findbugsProfile.define(context);
- BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsSecurityMinimalProfile.FINDBUGS_SECURITY_AUDIT_PROFILE_NAME);
+ BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsProfile.FINDBUGS_SECURITY_MINIMAL_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
// FSB rules must be added to FsbClassifier.groovy otherwise new rules metadata are not added in rules-findsecbugs.xml
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
diff --git a/src/test/java/org/sonar/plugins/findbugs/resource/ByteCodeResourceLocatorTest.java b/src/test/java/org/sonar/plugins/findbugs/resource/ByteCodeResourceLocatorTest.java
index 83153998..797bdf0b 100644
--- a/src/test/java/org/sonar/plugins/findbugs/resource/ByteCodeResourceLocatorTest.java
+++ b/src/test/java/org/sonar/plugins/findbugs/resource/ByteCodeResourceLocatorTest.java
@@ -8,8 +8,7 @@
import org.sonar.api.batch.fs.InputFile;
import java.util.ArrayList;
-
-import com.google.common.collect.ImmutableList;
+import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@@ -103,7 +102,7 @@ void findTemplateFile_jasperFileName() {
@Test
void findRegularSourceFile() throws Exception {
InputFile givenJavaFile = mock(InputFile.class);
- when(fsEmpty.inputFiles(any())).thenReturn(ImmutableList.of(givenJavaFile));
+ when(fsEmpty.inputFiles(any())).thenReturn(Collections.singletonList(givenJavaFile));
ByteCodeResourceLocator locator = new ByteCodeResourceLocator();
assertEquals(givenJavaFile, locator.findSourceFile("com/helloworld/TestJavaClass.java", fsEmpty));
@@ -112,7 +111,7 @@ void findRegularSourceFile() throws Exception {
@Test
void findSourceFileFromScalaClassName() throws Exception {
InputFile givenJavaFile = mock(InputFile.class);
- when(fsEmpty.inputFiles(any())).thenReturn(ImmutableList.of(givenJavaFile));
+ when(fsEmpty.inputFiles(any())).thenReturn(Collections.singletonList(givenJavaFile));
ByteCodeResourceLocator locator = new ByteCodeResourceLocator();
assertEquals(givenJavaFile, locator.findSourceFile("TestOperationalProfileIccidModel$TestOperationalProfileIccid$.class", fsEmpty));
diff --git a/src/test/java/org/sonar/plugins/findbugs/rule/FakeRuleFinder.java b/src/test/java/org/sonar/plugins/findbugs/rule/FakeRuleFinder.java
index ca8aa28c..8b31050c 100644
--- a/src/test/java/org/sonar/plugins/findbugs/rule/FakeRuleFinder.java
+++ b/src/test/java/org/sonar/plugins/findbugs/rule/FakeRuleFinder.java
@@ -19,7 +19,6 @@
*/
package org.sonar.plugins.findbugs.rule;
-import com.google.common.collect.Lists;
import java.util.ArrayList;
import org.mockito.ArgumentMatcher;
import org.mockito.invocation.InvocationOnMock;
@@ -51,39 +50,33 @@ private static RuleFinder create(boolean findbugs, boolean fbContrib, boolean fi
RuleFinder ruleFinder = mock(RuleFinder.class);
RulesDefinition.Context context = new RulesDefinition.Context();
List allRules = new ArrayList<>();
+
+ RulesDefinition rulesDefinition = new FindbugsRulesPluginsDefinition();
+ rulesDefinition.define(context);
+
if (findbugs) {
- RulesDefinition rulesDefinition = new FindbugsRulesDefinition();
- rulesDefinition.define(context);
configRuleFinderForRepo(ruleFinder, context, FindbugsRulesDefinition.REPOSITORY_KEY);
allRules.addAll(convert(context.repository(FindbugsRulesDefinition.REPOSITORY_KEY).rules()));
}
if (fbContrib) {
- RulesDefinition rulesDefinition = new FbContribRulesDefinition();
- rulesDefinition.define(context);
configRuleFinderForRepo(ruleFinder, context, FbContribRulesDefinition.REPOSITORY_KEY);
allRules.addAll(convert(context.repository(FbContribRulesDefinition.REPOSITORY_KEY).rules()));
}
if (findSecBug) {
- RulesDefinition rulesDefinition = new FindSecurityBugsRulesDefinition();
- rulesDefinition.define(context);
configRuleFinderForRepo(ruleFinder, context, FindSecurityBugsRulesDefinition.REPOSITORY_KEY);
allRules.addAll(convert(context.repository(FindSecurityBugsRulesDefinition.REPOSITORY_KEY).rules()));
}
if (findSecBugJsp) {
- RulesDefinition rulesDefinition = new FindSecurityBugsJspRulesDefinition();
- rulesDefinition.define(context);
configRuleFinderForRepo(ruleFinder, context, FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY);
allRules.addAll(convert(context.repository(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY).rules()));
}
if (findSecBugScala) {
- RulesDefinition rulesDefinition = new FindSecurityBugsScalaRulesDefinition();
- rulesDefinition.define(context);
configRuleFinderForRepo(ruleFinder, context, FindSecurityBugsScalaRulesDefinition.REPOSITORY_KEY);
allRules.addAll(convert(context.repository(FindSecurityBugsScalaRulesDefinition.REPOSITORY_KEY).rules()));
}
@@ -145,7 +138,7 @@ public Rule answer(InvocationOnMock invocation) throws Throwable {
}
private static List convert(List rules) {
- List results = Lists.newArrayListWithCapacity(rules.size());
+ List results = new ArrayList<>();
for (RulesDefinition.Rule rule : rules) {
Rule newRule = Rule.create(rule.repository().key(), rule.key(), rule.name()).setDescription(rule.htmlDescription()).setRepositoryKey(rule.repository().key());
results.add(newRule);
diff --git a/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml b/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml
deleted file mode 100644
index 9fc59110..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml
+++ /dev/null
@@ -1,36 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml b/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml
deleted file mode 100644
index 590a855c..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml
+++ /dev/null
@@ -1,40 +0,0 @@
-
-
-
-
-
- Method may fail to clean up stream or resource
- Method com.exedio.csvtools.DBTool.executeUpdate(String) may fail to clean up java.sql.Statement
-
-
- At DBTool.java:[lines 55-338]
-
- In class com.exedio.csvtools.DBTool
-
-
-
- In method com.exedio.csvtools.DBTool.executeUpdate(String)
-
-
-
- In Statement.java
-
- Reference type java.sql.Statement
-
-
- 1 instances of obligation remaining
-
-
- Obligation to clean up resource created at DBTool.java:[line 302] is not discharged
-
-
- Path continues at DBTool.java:[line 303]
-
-
- Path continues at DBTool.java:[line 313]
-
-
- Remaining obligations: {Statement x 1}
-
-
-
diff --git a/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml b/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml
deleted file mode 100644
index fa1766f9..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml b/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml
deleted file mode 100644
index fa48f22b..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/invalidPriority.xml b/src/test/resources/org/sonar/plugins/findbugs/invalidPriority.xml
deleted file mode 100644
index 35b38e5a..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/invalidPriority.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/shouldImportCategories.xml b/src/test/resources/org/sonar/plugins/findbugs/shouldImportCategories.xml
deleted file mode 100644
index 5f4ae48e..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/shouldImportCategories.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/shouldImportCodes.xml b/src/test/resources/org/sonar/plugins/findbugs/shouldImportCodes.xml
deleted file mode 100644
index f079f7da..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/shouldImportCodes.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml b/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml
deleted file mode 100644
index a07b9ca4..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatternsWithMultiplePriorities.xml b/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatternsWithMultiplePriorities.xml
deleted file mode 100644
index ed3cad66..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatternsWithMultiplePriorities.xml
+++ /dev/null
@@ -1,46 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml b/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml
deleted file mode 100644
index 23b9b37a..00000000
--- a/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
- /Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target/classes
- /Users/freddy/.m2/repository/org/apache/maven/reporting/maven-reporting-impl/2.0/maven-reporting-impl-2.0.jar
-
\ No newline at end of file