Skip to content

Commit

Permalink
Create Unit-Tests checking that nested archives are traversed
Browse files Browse the repository at this point in the history
This required an extension of the UnitTesting support provided by SpotBugsRule to allow modifying the engine options
from within a test case.
  • Loading branch information
Vogel612 committed Feb 28, 2022
1 parent d83ae6e commit b5b8b57
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
@@ -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));
}
}
Binary file added spotbugsTestCases/archives/nestedArchive.jar
Binary file not shown.
Expand Up @@ -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;

Expand All @@ -40,7 +41,7 @@
/**
* <p>
* 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.
* </p>
*
Expand Down Expand Up @@ -83,7 +84,7 @@ public AnalysisRunner addAuxClasspathEntry(Path path) {
}

@Nonnull
public BugCollectionBugReporter run(Path... files) {
public BugCollectionBugReporter run(Consumer<IFindBugsEngine> engineCustomization, Path... files) {
DetectorFactoryCollection.resetInstance(new DetectorFactoryCollection());

try (FindBugs2 engine = new FindBugs2(); Project project = createProject(files)) {
Expand All @@ -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) {
Expand Down
@@ -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;
Expand Down Expand Up @@ -76,16 +78,32 @@ public SpotBugsRule addAuxClasspathEntry(Path path) {
* <p>
* Run SpotBugs under given condition, and return its result.
* </p>
* @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<IFindBugsEngine> 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();
}

/**
* <p>
* Run SpotBugs under given condition, and return its result.
* </p>
* @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);
}
}

0 comments on commit b5b8b57

Please sign in to comment.