Skip to content

Commit 33f51b1

Browse files
committed
Add missing tests for ExceptionTypeFilter
1 parent 0111329 commit 33f51b1

File tree

1 file changed

+81
-4
lines changed

1 file changed

+81
-4
lines changed

spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,99 @@
1616

1717
package org.springframework.util;
1818

19+
import java.io.FileNotFoundException;
20+
import java.io.IOException;
21+
import java.nio.file.FileSystemException;
1922
import java.util.List;
2023

2124
import org.junit.jupiter.api.Test;
2225

2326
import static org.assertj.core.api.Assertions.assertThat;
2427

2528
/**
29+
* Unit tests for {@link ExceptionTypeFilter}.
30+
*
2631
* @author Stephane Nicoll
32+
* @author Sam Brannen
2733
*/
2834
class ExceptionTypeFilterTests {
2935

3036
@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();
35112
}
36113

37114
}

0 commit comments

Comments
 (0)