diff --git a/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java b/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java index 222b8d4ab98..363211a20d9 100644 --- a/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java +++ b/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java @@ -16,6 +16,9 @@ package org.springframework.util; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.FileSystemException; import java.util.List; import org.junit.jupiter.api.Test; @@ -23,15 +26,89 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link ExceptionTypeFilter}. + * * @author Stephane Nicoll + * @author Sam Brannen */ class ExceptionTypeFilterTests { @Test - void subClassMatch() { - ExceptionTypeFilter filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null); - assertThat(filter.match(RuntimeException.class)).isTrue(); - assertThat(filter.match(IllegalStateException.class)).isTrue(); + void emptyFilter() { + var filter = new ExceptionTypeFilter(null, null); + + assertMatches(filter, Throwable.class); + assertMatches(filter, Error.class); + assertMatches(filter, Exception.class); + assertMatches(filter, RuntimeException.class); + } + + @Test + void includes() { + var filter = new ExceptionTypeFilter(List.of(FileNotFoundException.class, IllegalArgumentException.class), null); + + assertMatches(filter, FileNotFoundException.class); + assertMatches(filter, IllegalArgumentException.class); + assertMatches(filter, NumberFormatException.class); + + assertDoesNotMatch(filter, Throwable.class); + assertDoesNotMatch(filter, FileSystemException.class); + } + + @Test + void includesSubtypeMatching() { + var filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null); + + assertMatches(filter, RuntimeException.class); + assertMatches(filter, IllegalStateException.class); + + assertDoesNotMatch(filter, Exception.class); + } + + @Test + void excludes() { + var filter = new ExceptionTypeFilter(null, List.of(FileNotFoundException.class, IllegalArgumentException.class)); + + assertDoesNotMatch(filter, FileNotFoundException.class); + assertDoesNotMatch(filter, IllegalArgumentException.class); + + assertMatches(filter, Throwable.class); + assertMatches(filter, AssertionError.class); + assertMatches(filter, FileSystemException.class); + } + + @Test + void excludesSubtypeMatching() { + var filter = new ExceptionTypeFilter(null, List.of(IllegalArgumentException.class)); + + assertDoesNotMatch(filter, IllegalArgumentException.class); + assertDoesNotMatch(filter, NumberFormatException.class); + + assertMatches(filter, Throwable.class); + } + + @Test + void includesAndExcludes() { + var filter = new ExceptionTypeFilter(List.of(IOException.class), List.of(FileNotFoundException.class)); + + assertMatches(filter, IOException.class); + assertMatches(filter, FileSystemException.class); + + assertDoesNotMatch(filter, FileNotFoundException.class); + assertDoesNotMatch(filter, Throwable.class); + } + + + private static void assertMatches(ExceptionTypeFilter filter, Class candidate) { + assertThat(filter.match(candidate)) + .as("filter '" + filter + "' should match " + candidate.getSimpleName()) + .isTrue(); + } + + private static void assertDoesNotMatch(ExceptionTypeFilter filter, Class candidate) { + assertThat(filter.match(candidate)) + .as("filter '" + filter + "' should not match " + candidate.getSimpleName()) + .isFalse(); } }