diff --git a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/classfile/impl/ClassPathBuilderTest.java b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/classfile/impl/ClassPathBuilderTest.java new file mode 100644 index 00000000000..f697a962cc3 --- /dev/null +++ b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/classfile/impl/ClassPathBuilderTest.java @@ -0,0 +1,36 @@ +package edu.umd.cs.findbugs.classfile.impl; + +import edu.umd.cs.findbugs.AppVersion; +import edu.umd.cs.findbugs.BugCollection; +import edu.umd.cs.findbugs.test.SpotBugsRule; +import org.junit.Rule; +import org.junit.Test; + +import java.nio.file.Paths; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class ClassPathBuilderTest { + + @Rule + public SpotBugsRule analyzer = new SpotBugsRule(); + + @Test + public void nestedTraversalDisabled() { + BugCollection results = analyzer.performAnalysis((engine) -> { + engine.setScanNestedArchives(false); + engine.setNoClassOk(true); + }, Paths.get("../spotbugsTestCases/archives/nestedArchive.jar")); + AppVersion appInformation = results.getCurrentAppVersion(); + assertThat(appInformation.getNumClasses(), equalTo(0)); + } + + @Test + public void nestedTraversalEnabled() { + BugCollection results = analyzer.performAnalysis((engine) -> engine.setScanNestedArchives(true), + Paths.get("../spotbugsTestCases/archives/nestedArchive.jar")); + AppVersion appInformation = results.getCurrentAppVersion(); + assertThat(appInformation.getNumClasses(), equalTo(5)); + } +} diff --git a/spotbugsTestCases/archives/nestedArchive.jar b/spotbugsTestCases/archives/nestedArchive.jar new file mode 100644 index 00000000000..49e36070aec Binary files /dev/null and b/spotbugsTestCases/archives/nestedArchive.jar differ diff --git a/test-harness-core/src/main/java/edu/umd/cs/findbugs/test/AnalysisRunner.java b/test-harness-core/src/main/java/edu/umd/cs/findbugs/test/AnalysisRunner.java index b29bc022dbb..6ee5f714254 100644 --- a/test-harness-core/src/main/java/edu/umd/cs/findbugs/test/AnalysisRunner.java +++ b/test-harness-core/src/main/java/edu/umd/cs/findbugs/test/AnalysisRunner.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Consumer; import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; @@ -40,7 +41,7 @@ /** *

* This class runs analysis with SpotBugs. The target class files and - * auxClasspathEntries should be specified before you invoke {@link #run(Path...)} + * auxClasspathEntries should be specified before you invoke {@link #run(Consumer, Path...)} * method. *

* @@ -83,7 +84,7 @@ public AnalysisRunner addAuxClasspathEntry(Path path) { } @Nonnull - public BugCollectionBugReporter run(Path... files) { + public BugCollectionBugReporter run(Consumer engineCustomization, Path... files) { DetectorFactoryCollection.resetInstance(new DetectorFactoryCollection()); try (FindBugs2 engine = new FindBugs2(); Project project = createProject(files)) { @@ -102,6 +103,7 @@ public BugCollectionBugReporter run(Path... files) { preferences.enableAllDetectors(true); engine.setUserPreferences(preferences); + engineCustomization.accept(engine); try { engine.execute(); } catch (final IOException | InterruptedException e) { diff --git a/test-harness/src/main/java/edu/umd/cs/findbugs/test/SpotBugsRule.java b/test-harness/src/main/java/edu/umd/cs/findbugs/test/SpotBugsRule.java index e597e27a05b..f789fec517b 100644 --- a/test-harness/src/main/java/edu/umd/cs/findbugs/test/SpotBugsRule.java +++ b/test-harness/src/main/java/edu/umd/cs/findbugs/test/SpotBugsRule.java @@ -1,10 +1,12 @@ package edu.umd.cs.findbugs.test; import java.nio.file.Path; +import java.util.function.Consumer; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import edu.umd.cs.findbugs.IFindBugsEngine; import org.junit.rules.ExternalResource; import edu.umd.cs.findbugs.BugCollection; @@ -76,16 +78,32 @@ public SpotBugsRule addAuxClasspathEntry(Path path) { *

* Run SpotBugs under given condition, and return its result. *

+ * @param engineCustomization + * A customization of the engine to apply before running engine#execute * @param paths * Paths of target class files * @return a {@link BugCollection} which contains all detected bugs. */ // TODO let users specify SlashedClassName, then find its file path automatically @Nonnull - public BugCollection performAnalysis(Path... paths) { + public BugCollection performAnalysis(Consumer engineCustomization, Path... paths) { if (runner == null) { throw new IllegalStateException("Please call this performAnalysis() method in test method"); } - return runner.run(paths).getBugCollection(); + return runner.run(engineCustomization, paths).getBugCollection(); + } + + /** + *

+ * Run SpotBugs under given condition, and return its result. + *

+ * @param paths + * Paths of target class files + * @return a {@link BugCollection} which contains all detected bugs. + */ + // TODO let users specify SlashedClassName, then find its file path automatically + @Nonnull + public BugCollection performAnalysis(Path... paths) { + return performAnalysis(e -> {}, paths); } }