Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading of Spotbugs properties from external file #234

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public enum GeneralOption implements ConfigurationOption {
PMD_SHOW_VIOLATION_DETAILS("pmd.showViolationDetails", "Show violation details and URL", "false"),

FINDBUGS_ENABLED("findbugs.enabled", "FindBugs enabled", "false"),
FINDBUGS_LOAD_PROPERTIES_FROM("findbugs.loadPropertiesFrom", "FindBugs properties file", ""),
FINDBUGS_INCLUDE_FILTER("findbugs.includeFilter", "FindBugs include filter file", ""),
FINDBUGS_EXCLUDE_FILTER("findbugs.excludeFilter", "FindBugs exclude filter file", ""),

SPOTBUGS_ENABLED("spotbugs.enabled", "SpotBugs enabled", "false"),
SPOTBUGS_LOAD_PROPERTIES_FROM("spotbugs.loadPropertiesFrom", "SpotBugs properties file", ""),
SPOTBUGS_INCLUDE_FILTER("spotbugs.includeFilter", "SpotBugs include filter file", ""),
SPOTBUGS_EXCLUDE_FILTER("spotbugs.excludeFilter", "SpotBugs exclude filter file", ""),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package pl.touk.sputnik.processor.spotbugs;

import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.ClassScreener;
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.FindBugs2;
import edu.umd.cs.findbugs.IClassScreener;
import edu.umd.cs.findbugs.Priorities;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.SystemProperties;
import edu.umd.cs.findbugs.config.UserPreferences;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -36,8 +42,8 @@ public SpotBugsProcessor(@NotNull Configuration configuration) {
@Nullable
@Override
public ReviewResult process(@NotNull Review review) {
FindBugs2 spotBugs = createFindBugs2(review);
try {
loadSystemProperties();
try (FindBugs2 spotBugs = createFindBugs2(review)) {
spotBugs.execute();
} catch (Exception e) {
log.error("SpotBugs processing error", e);
Expand All @@ -46,6 +52,17 @@ public ReviewResult process(@NotNull Review review) {
return collectorBugReporter.getReviewResult();
}

private void loadSystemProperties() {
getPropertiesFileLocation().ifPresent(propertiesFileLocation -> {
try {
SystemProperties.loadPropertiesFromURL(Paths.get(propertiesFileLocation).toUri().toURL());
log.info("Using SpotBugs properties file {}", propertiesFileLocation);
} catch (MalformedURLException e) {
log.error("Invalid location for properties file: {}", propertiesFileLocation);
}
});
}

@NotNull
@Override
public String getName() {
Expand Down Expand Up @@ -102,6 +119,13 @@ private IClassScreener createClassScreener(@NotNull Review review) {
return classScreener;
}

private Optional<String> getPropertiesFileLocation() {
return Stream.of(GeneralOption.SPOTBUGS_LOAD_PROPERTIES_FROM, GeneralOption.FINDBUGS_LOAD_PROPERTIES_FROM)
.map(config::getProperty)
.filter(StringUtils::isNotBlank)
.findFirst();
}

@Nullable
private String getIncludeFilterFilename() {
String includeFilterFilename = config.getProperty(GeneralOption.SPOTBUGS_INCLUDE_FILTER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.umd.cs.findbugs.SystemProperties;
import pl.touk.sputnik.TestEnvironment;
import pl.touk.sputnik.configuration.ConfigurationSetup;
import pl.touk.sputnik.configuration.GeneralOption;
Expand All @@ -24,7 +26,10 @@ class SpotBugsProcessorTest extends TestEnvironment {

@BeforeEach
void setUp() {
config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.BUILD_TOOL.getKey(), GRADLE));
config = new ConfigurationSetup().setUp(ImmutableMap.of(
GeneralOption.BUILD_TOOL.getKey(), GRADLE,
GeneralOption.SPOTBUGS_LOAD_PROPERTIES_FROM.getKey(), "src/test/resources/spotbugs/spotbugs-config.properties"
));
spotBugsProcessor = new SpotBugsProcessor(config);
}

Expand Down Expand Up @@ -54,4 +59,10 @@ void shouldReturnEmptyWhenNoFilesToReview() {
assertThat(reviewResult.getViolations()).isEmpty();
}

@Test
void shouldLoadPropertiesFromExternalLocation() {
ReviewResult reviewResult = spotBugsProcessor.process(nonExistentReview());

assertThat(SystemProperties.getBoolean("findbugs.de.comment")).isTrue();
}
}
2 changes: 2 additions & 0 deletions src/test/resources/spotbugs/spotbugs-config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Don't report empty catch blocks if a source comment is found in the block.
findbugs.de.comment = true