|
16 | 16 |
|
17 | 17 | package org.springframework.util;
|
18 | 18 |
|
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.FileSystemException; |
19 | 22 | import java.util.List;
|
20 | 23 |
|
21 | 24 | import org.junit.jupiter.api.Test;
|
22 | 25 |
|
23 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
24 | 27 |
|
25 | 28 | /**
|
| 29 | + * Unit tests for {@link ExceptionTypeFilter}. |
| 30 | + * |
26 | 31 | * @author Stephane Nicoll
|
| 32 | + * @author Sam Brannen |
27 | 33 | */
|
28 | 34 | class ExceptionTypeFilterTests {
|
29 | 35 |
|
30 | 36 | @Test
|
31 |
| - void subClassMatch() { |
32 |
| - ExceptionTypeFilter filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null); |
33 |
| - assertThat(filter.match(RuntimeException.class)).isTrue(); |
34 |
| - assertThat(filter.match(IllegalStateException.class)).isTrue(); |
| 37 | + void emptyFilter() { |
| 38 | + var filter = new ExceptionTypeFilter(null, null); |
| 39 | + |
| 40 | + assertMatches(filter, Throwable.class); |
| 41 | + assertMatches(filter, Error.class); |
| 42 | + assertMatches(filter, Exception.class); |
| 43 | + assertMatches(filter, RuntimeException.class); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void includes() { |
| 48 | + var filter = new ExceptionTypeFilter(List.of(FileNotFoundException.class, IllegalArgumentException.class), null); |
| 49 | + |
| 50 | + assertMatches(filter, FileNotFoundException.class); |
| 51 | + assertMatches(filter, IllegalArgumentException.class); |
| 52 | + assertMatches(filter, NumberFormatException.class); |
| 53 | + |
| 54 | + assertDoesNotMatch(filter, Throwable.class); |
| 55 | + assertDoesNotMatch(filter, FileSystemException.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void includesSubtypeMatching() { |
| 60 | + var filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null); |
| 61 | + |
| 62 | + assertMatches(filter, RuntimeException.class); |
| 63 | + assertMatches(filter, IllegalStateException.class); |
| 64 | + |
| 65 | + assertDoesNotMatch(filter, Exception.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void excludes() { |
| 70 | + var filter = new ExceptionTypeFilter(null, List.of(FileNotFoundException.class, IllegalArgumentException.class)); |
| 71 | + |
| 72 | + assertDoesNotMatch(filter, FileNotFoundException.class); |
| 73 | + assertDoesNotMatch(filter, IllegalArgumentException.class); |
| 74 | + |
| 75 | + assertMatches(filter, Throwable.class); |
| 76 | + assertMatches(filter, AssertionError.class); |
| 77 | + assertMatches(filter, FileSystemException.class); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void excludesSubtypeMatching() { |
| 82 | + var filter = new ExceptionTypeFilter(null, List.of(IllegalArgumentException.class)); |
| 83 | + |
| 84 | + assertDoesNotMatch(filter, IllegalArgumentException.class); |
| 85 | + assertDoesNotMatch(filter, NumberFormatException.class); |
| 86 | + |
| 87 | + assertMatches(filter, Throwable.class); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void includesAndExcludes() { |
| 92 | + var filter = new ExceptionTypeFilter(List.of(IOException.class), List.of(FileNotFoundException.class)); |
| 93 | + |
| 94 | + assertMatches(filter, IOException.class); |
| 95 | + assertMatches(filter, FileSystemException.class); |
| 96 | + |
| 97 | + assertDoesNotMatch(filter, FileNotFoundException.class); |
| 98 | + assertDoesNotMatch(filter, Throwable.class); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private static void assertMatches(ExceptionTypeFilter filter, Class<? extends Throwable> candidate) { |
| 103 | + assertThat(filter.match(candidate)) |
| 104 | + .as("filter '" + filter + "' should match " + candidate.getSimpleName()) |
| 105 | + .isTrue(); |
| 106 | + } |
| 107 | + |
| 108 | + private static void assertDoesNotMatch(ExceptionTypeFilter filter, Class<? extends Throwable> candidate) { |
| 109 | + assertThat(filter.match(candidate)) |
| 110 | + .as("filter '" + filter + "' should not match " + candidate.getSimpleName()) |
| 111 | + .isFalse(); |
35 | 112 | }
|
36 | 113 |
|
37 | 114 | }
|
0 commit comments