Skip to content

Commit

Permalink
Issue #40: Add javadocs and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed Apr 11, 2015
1 parent 1020ac4 commit e1b0028
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/de/thetaphi/forbiddenapis/ClassPatternRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

public final class ClassPatternRule {

public final Pattern pattern;
private final Pattern pattern;

/** the message printed if rule is violated */
public final String printout;

/** Create new rule for class glob and given printout */
public ClassPatternRule(String glob, String printout) {
if (glob == null || printout == null) {
throw new NullPointerException();
Expand All @@ -32,11 +35,12 @@ public ClassPatternRule(String glob, String printout) {
this.printout = printout;
}

/** returns true, if the given class (binary name, dotted) matches this rule. */
public boolean matches(String className) {
return pattern.matcher(className).matches();
}

// equals() / hashcode() use the string representation of pattern for comparisons
// equals() / hashCode() use the string representation of pattern for comparisons
// (Pattern unfortunately does not implement equals/hashCode)

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/de/thetaphi/forbiddenapis/AsmUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static de.thetaphi.forbiddenapis.AsmUtils.glob2Pattern;
import static de.thetaphi.forbiddenapis.AsmUtils.isGlob;
import static de.thetaphi.forbiddenapis.AsmUtils.isInternalClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -64,6 +65,14 @@ public void testGlob() {
assertTrue(pat.matcher("java.lang.reflect.Arrays").matches());
}

@Test
public void testCrazyPatterns() {
// those should not cause havoc:
assertEquals("java\\.\\{.*\\}\\.Array", glob2Pattern("java.{**}.Array").pattern());
assertEquals("java\\./.*<>\\.Array\\$1", glob2Pattern("java./**<>.Array$1").pattern());
assertEquals("\\+\\^\\$", glob2Pattern("+^$").pattern());
}

@Test
public void testInternalRuntime() {
assertTrue(isInternalClass("sun.misc.Unsafe"));
Expand Down

0 comments on commit e1b0028

Please sign in to comment.