FindBugsValidator implements Validator { @Override public void validate(final Environment env) throws ValidationException { if (env.outdir().exists()) { if (!env.exclude("findbugs", "")) { this.check(this.findbugs(env)); } } else { Logger.info( this, "No classes at %s, no FindBugs validation", env.outdir() ); } }  @Override void validate(final Environment env); @Override String name();  }
@Test public void passesCorrectFilesWithNoExceptions() throws Exception { final Environment env = new Environment.Mock() .withFile("src/main/java/Main.java", "class Main { int x = 0; }") .withDefaultClasspath(); new FindBugsValidator().validate(env); }
@Disabled @Test public void reportsIncorrectlyAddedThrows() throws Exception { Assertions.assertThrows( ValidationException.class, () -> { final byte[] bytecode = new BytecodeMocker() .withSource( Joiner.on("\n").join( "package test;", "final class Main {", "public void foo() throws InterruptedException {", "System.out.println(\"test\");", "}", "}" ) ).mock(); final Environment env = new Environment.Mock() .withFile("target/classes/Main.class", bytecode) .withDefaultClasspath(); new FindBugsValidator().validate(env); } ); }
@Test public void ignoresCorrectlyAddedThrows() throws Exception { final byte[] bytecode = new BytecodeMocker() .withSource( Joiner.on("\n").join( "package test;", "final class Main {", "public void foo() throws InterruptedException {", "Thread.sleep(1);", "}", "}" ) ) .mock(); final Environment env = new Environment.Mock() .withFile("target/classes/Main.class", bytecode) .withDefaultClasspath(); new FindBugsValidator().validate(env); }
@Test public void throwsExceptionOnViolation() throws Exception { Assertions.assertThrows( ValidationException.class, () -> { final byte[] bytecode = new BytecodeMocker() .withSource( "class Foo { public Foo clone() { return this; } }" ).mock(); final Environment env = new Environment.Mock() .withFile("target/classes/Foo.class", bytecode) .withDefaultClasspath(); new FindBugsValidator().validate(env); } ); }
@Test public void excludesIncorrectClassFormCheck() throws Exception { final byte[] bytecode = new BytecodeMocker() .withSource("class Foo { public Foo clone() { return this; } }") .mock(); final Environment env = new Environment.Mock() .withFile("target/classes/Foo.class", bytecode) .withExcludes("Foo") .withDefaultClasspath(); new FindBugsValidator().validate(env); }
@Test public void excludesSeveralIncorrectClassFromCheck() throws Exception { final byte[] bytecode = new BytecodeMocker() .withSource("class Foo { public Foo clone() { return this; } }") .mock(); final byte[] another = new BytecodeMocker() .withSource("class Bar { public Bar clone() { return this; } }") .mock(); final Environment env = new Environment.Mock() .withFile("target/classes/Foo.class", bytecode) .withFile("target/classes/Bar.class", another) .withExcludes("Foo,Bar") .withDefaultClasspath(); new FindBugsValidator().validate(env); }