@ -33,81 +33,83 @@ import static org.assertj.core.api.Assertions.assertThat;
* /
* /
class ExceptionTypeFilterTests {
class ExceptionTypeFilterTests {
ExceptionTypeFilter filter ;
@Test
@Test
void emptyFilter ( ) {
void emptyFilter ( ) {
var filter = new ExceptionTypeFilter ( null , null ) ;
filter = new ExceptionTypeFilter ( null , null ) ;
assertMatches ( filter , Throwable . class ) ;
assertMatches ( new Throwable ( ) ) ;
assertMatches ( filter , Error . class ) ;
assertMatches ( new Error ( ) ) ;
assertMatches ( filter , Exception . class ) ;
assertMatches ( new Exception ( ) ) ;
assertMatches ( filter , RuntimeException . class ) ;
assertMatches ( new RuntimeException ( ) ) ;
}
}
@Test
@Test
void includes ( ) {
void includes ( ) {
var filter = new ExceptionTypeFilter ( List . of ( FileNotFoundException . class , IllegalArgumentException . class ) , null ) ;
filter = new ExceptionTypeFilter ( List . of ( FileNotFoundException . class , IllegalArgumentException . class ) , null ) ;
assertMatches ( filter , FileNotFoundException . class ) ;
assertMatches ( new FileNotFoundException ( ) ) ;
assertMatches ( filter , IllegalArgumentException . class ) ;
assertMatches ( new IllegalArgumentException ( ) ) ;
assertMatches ( filter , NumberFormatException . class ) ;
assertMatches ( new NumberFormatException ( ) ) ;
assertDoesNotMatch ( filter , Throwable . class ) ;
assertDoesNotMatch ( new Throwable ( ) ) ;
assertDoesNotMatch ( filter , FileSystemException . class ) ;
assertDoesNotMatch ( new FileSystemException ( "test" ) ) ;
}
}
@Test
@Test
void includesSubtypeMatching ( ) {
void includesSubtypeMatching ( ) {
var filter = new ExceptionTypeFilter ( List . of ( RuntimeException . class ) , null ) ;
filter = new ExceptionTypeFilter ( List . of ( RuntimeException . class ) , null ) ;
assertMatches ( filter , RuntimeException . class ) ;
assertMatches ( new RuntimeException ( ) ) ;
assertMatches ( filter , IllegalStateException . class ) ;
assertMatches ( new IllegalStateException ( ) ) ;
assertDoesNotMatch ( filter , Exception . class ) ;
assertDoesNotMatch ( new Exception ( ) ) ;
}
}
@Test
@Test
void excludes ( ) {
void excludes ( ) {
var filter = new ExceptionTypeFilter ( null , List . of ( FileNotFoundException . class , IllegalArgumentException . class ) ) ;
filter = new ExceptionTypeFilter ( null , List . of ( FileNotFoundException . class , IllegalArgumentException . class ) ) ;
assertDoesNotMatch ( filter , FileNotFoundException . class ) ;
assertDoesNotMatch ( new FileNotFoundException ( ) ) ;
assertDoesNotMatch ( filter , IllegalArgumentException . class ) ;
assertDoesNotMatch ( new IllegalArgumentException ( ) ) ;
assertMatches ( filter , Throwable . class ) ;
assertMatches ( new Throwable ( ) ) ;
assertMatches ( filter , AssertionError . class ) ;
assertMatches ( new AssertionError ( ) ) ;
assertMatches ( filter , FileSystemException . class ) ;
assertMatches ( new FileSystemException ( "test" ) ) ;
}
}
@Test
@Test
void excludesSubtypeMatching ( ) {
void excludesSubtypeMatching ( ) {
var filter = new ExceptionTypeFilter ( null , List . of ( IllegalArgumentException . class ) ) ;
filter = new ExceptionTypeFilter ( null , List . of ( IllegalArgumentException . class ) ) ;
assertDoesNotMatch ( filter , IllegalArgumentException . class ) ;
assertDoesNotMatch ( new IllegalArgumentException ( ) ) ;
assertDoesNotMatch ( filter , NumberFormatException . class ) ;
assertDoesNotMatch ( new NumberFormatException ( ) ) ;
assertMatches ( filter , Throwable . class ) ;
assertMatches ( new Throwable ( ) ) ;
}
}
@Test
@Test
void includesAndExcludes ( ) {
void includesAndExcludes ( ) {
var filter = new ExceptionTypeFilter ( List . of ( IOException . class ) , List . of ( FileNotFoundException . class ) ) ;
filter = new ExceptionTypeFilter ( List . of ( IOException . class ) , List . of ( FileNotFoundException . class ) ) ;
assertMatches ( filter , IOException . class ) ;
assertMatches ( new IOException ( ) ) ;
assertMatches ( filter , FileSystemException . class ) ;
assertMatches ( new FileSystemException ( "test" ) ) ;
assertDoesNotMatch ( filter , FileNotFoundException . class ) ;
assertDoesNotMatch ( new FileNotFoundException ( ) ) ;
assertDoesNotMatch ( filter , Throwable . class ) ;
assertDoesNotMatch ( new Throwable ( ) ) ;
}
}
private static void assertMatches ( ExceptionTypeFilter filter , Class < ? extends Throwable > candidate ) {
private void assertMatches ( Throwable candidate ) {
assertThat ( filter . match ( candidate ) )
assertThat ( this . filter . match ( candidate ) )
. as ( "filter '" + filter + "' should match " + candidate . getSimpleName ( ) )
. as ( "filter '" + this . filter + "' should match " + candidate . getClass ( ) . getSimpleName ( ) )
. isTrue ( ) ;
. isTrue ( ) ;
}
}
private static void assertDoesNotMatch ( ExceptionTypeFilter filter , Class < ? extends Throwable > candidate ) {
private void assertDoesNotMatch ( Throwable candidate ) {
assertThat ( filter . match ( candidate ) )
assertThat ( this . filter . match ( candidate ) )
. as ( "filter '" + filter + "' should not match " + candidate . getSimpleName ( ) )
. as ( "filter '" + this . filter + "' should not match " + candidate . getClass ( ) . getSimpleName ( ) )
. isFalse ( ) ;
. isFalse ( ) ;
}
}