Skip to content

Commit

Permalink
findbugs exclude filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
wipu committed Feb 19, 2024
1 parent f6b20c5 commit d69bb1f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class FindbugsDistribution extends TargetBase {
public static FindbugsDistribution _4_7_3 = new FindbugsDistribution(
"4.7.3", "9739f70965c4b89a365419af9c688934");

public static FindbugsDistribution _4_8_3 = new FindbugsDistribution(
"4.8.3", "dae83e21c1c3014ea177c882bb72c1e9");

public FindbugsDistribution(String version, String md5sum) {
super("spotbugs-" + version);
this.version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public class FindbugsReport extends TargetBase {
private final Path antJar;
private final Path antLauncherJar;
private final FindbugsOutputFormat outputFormat;
private final Path excludeFile;

public FindbugsReport(String name,
List<JavaClassesAndSources> classesToAnalyze, List<Path> auxClasses,
FindbugsDistribution findbugs, Path antJar, Path antLauncherJar,
FindbugsOutputFormat outputFormat) {
FindbugsOutputFormat outputFormat, Path excludeFile) {
super(name);
this.classesToAnalyze = classesToAnalyze;
this.auxClasses = auxClasses;
Expand All @@ -42,6 +43,7 @@ public FindbugsReport(String name,
this.findbugs = findbugs;
this.antJar = antJar;
this.antLauncherJar = antLauncherJar;
this.excludeFile = excludeFile;
}

public static FindbugsReportSpex with() {
Expand All @@ -57,10 +59,12 @@ public static class FindbugsReportSpex {
private Path antJar;
private Path antLauncherJar;
private FindbugsOutputFormat outputFormat = FindbugsOutputFormat.HTML;
private Path excludeFile;

public FindbugsReport end() {
return new FindbugsReport(name, classesToAnalyze, auxClasses,
findbugs, antJar, antLauncherJar, outputFormat);
findbugs, antJar, antLauncherJar, outputFormat,
excludeFile);
}

public FindbugsReportSpex name(String name) {
Expand Down Expand Up @@ -129,6 +133,11 @@ public FindbugsReportSpex outputFormat(
return this;
}

public FindbugsReportSpex exclude(Path excludeFile) {
this.excludeFile = excludeFile;
return this;
}

}

@Override
Expand All @@ -148,6 +157,9 @@ protected IngredientsAndParametersDefined ingredientsAndParameters(
}

iUse.ingredients("auxClasses", auxClasses);
if (excludeFile != null) {
iUse.ingredients("excludeFile", excludeFile);
}
iUse.parameter("output-format", outputFormat);
return iUse.nothingElse();
}
Expand All @@ -172,6 +184,11 @@ private String antScript(TargetEvaluationContext ctx) {
.pathWithoutBackslashes(findbugs.homeDirectory(ctx));
String destString = ctx.iwant()
.pathWithoutBackslashes(ctx.cached(this));
String excludeFilePath = null;
if (excludeFile != null) {
excludeFilePath = ctx.iwant()
.pathWithoutBackslashes(ctx.cached(excludeFile));
}

StringBuilder xml = new StringBuilder();
xml.append(
Expand Down Expand Up @@ -203,8 +220,12 @@ private String antScript(TargetEvaluationContext ctx) {
xml.append(" <mkdir dir=\"${findbugs-report}\" />\n");
xml.append(" <findbugs home=\"${findbugs-home}\" output=\""
+ outputFormat + "\" outputfile=\"${findbugs-report}/" + name()
+ "." + outputFormat + "\">\n");
xml.append("\n");
+ "." + outputFormat + "\"\n");
if (excludeFilePath != null) {
xml.append(" excludeFilter=\"").append(excludeFilePath)
.append("\"");
}
xml.append(">\n");

for (JavaClassesAndSources cs : classesToAnalyze) {
xml.append(" <class location=\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.fluentjava.iwant.core.download.TestedIwantDependencies;
import org.fluentjava.iwant.embedded.AsEmbeddedIwantUser;
import org.fluentjava.iwant.entry.Iwant;
import org.fluentjava.iwant.plugin.findbugs.FindbugsReport.FindbugsReportSpex;

public class FindbugsReportTest extends IwantTestCase {

Expand All @@ -38,7 +39,7 @@ private Path antLauncherJar() {
}

private static FindbugsDistribution distroToTest() {
return FindbugsDistribution._4_7_3;
return FindbugsDistribution._4_8_3;
}

private static File cachedFindbugsTarGz() {
Expand Down Expand Up @@ -109,23 +110,30 @@ public void testIngredientsAndContentDescriptor() {
Path emptyClasses = Source.underWsroot("empty-classes");
Path bin = Source.underWsroot("bin.jar");

Target report = FindbugsReport.with().name("fb-empty")
FindbugsReportSpex report = FindbugsReport.with().name("fb-empty")
.using(distroToTest(), antJar(), antLauncherJar())
.classesToAnalyze(
new JavaClassesAndSources(emptyClasses, emptySrc))
.auxClasses(bin).end();
.auxClasses(bin);

assertEquals(
"[spotbugs-4.7.3, " + antJar() + ", " + antLauncherJar()
"[spotbugs-4.8.3, " + antJar() + ", " + antLauncherJar()
+ ", empty-classes, empty-src, bin.jar]",
report.ingredients().toString());
report.end().ingredients().toString());
assertEquals("org.fluentjava.iwant.plugin.findbugs.FindbugsReport\n"
+ "i:findbugs:\n" + " spotbugs-4.7.3\n" + "i:antJar:\n" + " "
+ "i:findbugs:\n" + " spotbugs-4.8.3\n" + "i:antJar:\n" + " "
+ antJar() + "\ni:antLauncherJar:\n" + " " + antLauncherJar()
+ "\ni:classes:\n" + " empty-classes\n" + "i:sources:\n"
+ " empty-src\n" + "i:auxClasses:\n" + " bin.jar\n"
+ "p:output-format:\n" + " html\n" + "",
report.contentDescriptor());
report.end().contentDescriptor());

// optional ingredient:
Path excludeXml = Source.underWsroot("exclude.xml");
report.exclude(excludeXml);
assertTrue(report.end().ingredients().contains(excludeXml));
assertTrue(report.end().contentDescriptor()
.contains("i:excludeFile:\n" + " exclude.xml\n"));
}

public void testExplicitHtmlOutputFormat() {
Expand Down Expand Up @@ -153,7 +161,7 @@ public void testContentDescriptorWithXmlOutputFormat() {
.end();

assertEquals("org.fluentjava.iwant.plugin.findbugs.FindbugsReport\n"
+ "i:findbugs:\n" + " spotbugs-4.7.3\n" + "i:antJar:\n" + " "
+ "i:findbugs:\n" + " spotbugs-4.8.3\n" + "i:antJar:\n" + " "
+ antJar() + "\ni:antLauncherJar:\n" + " " + antLauncherJar()
+ "\ni:classes:\n" + " empty-classes\n" + "i:sources:\n"
+ " empty-src\n" + "i:auxClasses:\n" + "p:output-format:\n"
Expand All @@ -180,7 +188,8 @@ public void testDefaultReportFromEmptyClasses() throws Exception {
assertEquals("", htmlReportContent);
}

public void testReportMentionsIssuesFromTheGivenClass() throws Exception {
public void testReportMentionsIssuesFromTheGivenClassButOnlyIfNotFiltered()
throws Exception {
File srcDir = new File(wsRoot, "src");
srcDirHasFindbugsFodder(srcDir, "testfodder",
"ClassWithFindbugsIssues");
Expand All @@ -192,17 +201,33 @@ public void testReportMentionsIssuesFromTheGivenClass() throws Exception {

distroToTest().path(ctx);

FindbugsReport report = FindbugsReport.with().name("oneclass-report")
String npeWarningSnippet = "<td>Null pointer dereference of ? in "
+ "org.fluentjava.iwant.plugin.findbugs.testfodder.ClassWithFindbugsIssues."
+ "nullReference(Object)</td>";

// all rules in use:

FindbugsReport fullReport = FindbugsReport.with()
.name("oneclass-report")
.using(distroToTest(), antJar(), antLauncherJar())
.classesToAnalyze(new JavaClassesAndSources(classes, src))
.end();
report.path(ctx);
fullReport.path(ctx);
assertTrue(htmlReportContent(fullReport).contains(npeWarningSnippet));

String htmlReportContent = htmlReportContent(report);
assertTrue(htmlReportContent
.contains("<td>Null pointer dereference of ? in "
+ "org.fluentjava.iwant.plugin.findbugs.testfodder.ClassWithFindbugsIssues."
+ "nullReference(Object)</td>"));
// report with excluded NPE rule:

wsRootHasFile("exclude.xml", "<FindBugsFilter><Match><Bug pattern=\""
+ "NP_ALWAYS_NULL\"/></Match></FindBugsFilter>");

FindbugsReport filteredReport = FindbugsReport.with()
.name("filtered-oneclass-report")
.using(distroToTest(), antJar(), antLauncherJar())
.classesToAnalyze(new JavaClassesAndSources(classes, src))
.exclude(Source.underWsroot("exclude.xml")).end();
filteredReport.path(ctx);
assertFalse(
htmlReportContent(filteredReport).contains(npeWarningSnippet));
}

public void testReportDoesNotDetectProblemIfDependencyNotInAuxclasses()
Expand Down

0 comments on commit d69bb1f

Please sign in to comment.