Skip to content

Commit

Permalink
Prevent bug with empty options (EnumSet.copyOf needs at least one ele…
Browse files Browse the repository at this point in the history
…ment)
  • Loading branch information
uschindler committed Aug 23, 2015
1 parent f00539a commit 947da61
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public static enum Option {
protected abstract void logWarn(String msg);
protected abstract void logInfo(String msg);

//public Checker(ClassLoader loader, boolean internalRuntimeForbidden, boolean failOnMissingClasses, boolean failOnViolation, boolean defaultFailOnUnresolvableSignatures) {
public Checker(ClassLoader loader, Option... options) {
this(loader, EnumSet.copyOf(Arrays.asList(options)));
this(loader, (options.length == 0) ? EnumSet.noneOf(Option.class) : EnumSet.copyOf(Arrays.asList(options)));
}

public Checker(ClassLoader loader, EnumSet<Option> options) {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/de/thetaphi/forbiddenapis/CheckerSetupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assume.assumeTrue;

import java.util.Collections;
import java.util.EnumSet;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected void logInfo(String msg) {
public void setUp() {
checker = new MyChecker();
assumeTrue("This test only works with a supported JDK (see docs)", checker.isSupportedJDK);
assertEquals(EnumSet.of(INTERNAL_RUNTIME_FORBIDDEN, FAIL_ON_MISSING_CLASSES, FAIL_ON_VIOLATION, FAIL_ON_UNRESOLVABLE_SIGNATURES), checker.options);
}

@Test
Expand Down Expand Up @@ -101,4 +103,14 @@ public void testMethodSignature() throws Exception {
assertEquals(Collections.singletonMap("java/lang/Object\000toString()Ljava/lang/String;", "java.lang.Object#toString() [Foobar]"), checker.forbiddenMethods);
}

@Test
public void testEmptyCtor() throws Exception {
Checker chk = new Checker(ClassLoader.getSystemClassLoader()) {
@Override protected void logError(String msg) { }
@Override protected void logWarn(String msg) { }
@Override protected void logInfo(String msg) { }
};
assertEquals(EnumSet.noneOf(Checker.Option.class), chk.options);
}

}

0 comments on commit 947da61

Please sign in to comment.