From 33f51b183d1fa995abd51f6373df09259a4b1cb0 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sat, 5 Jul 2025 12:15:14 +0200 Subject: [PATCH] Add missing tests for ExceptionTypeFilter --- .../util/ExceptionTypeFilterTests.java | 85 ++++++++++++++++++- 1 file changed, 81 insertions(+), 4 deletions(-) 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(); } }