Skip to content

Commit 25b9258

Browse files
committed
add tests for AssertExceptionMatches
1 parent d0cd3f9 commit 25b9258

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import java.util.function.Supplier;
28+
import java.util.regex.Pattern;
29+
2830
import org.slf4j.event.Level;
2931

3032
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -38,6 +40,7 @@ public void testAssertExceptionContainsNullEx() throws Throwable {
3840
try {
3941
assertExceptionContains("", null);
4042
} catch (AssertionError e) {
43+
// AssertionError not actually thrown
4144
if (!e.toString().contains(E_NULL_THROWABLE)) {
4245
throw e;
4346
}
@@ -49,6 +52,7 @@ public void testAssertExceptionContainsNullString() throws Throwable {
4952
try {
5053
assertExceptionContains("", new BrokenException());
5154
} catch (AssertionError e) {
55+
// AssertionError not actually thrown
5256
if (!e.toString().contains(E_NULL_THROWABLE_STRING)) {
5357
throw e;
5458
}
@@ -76,6 +80,51 @@ public void testAssertExceptionContainsWorking() throws Throwable {
7680
assertExceptionContains("Expected", new Exception("Expected"));
7781
}
7882

83+
@Test
84+
public void testAssertExceptionMatchesNullEx() throws Throwable {
85+
try {
86+
assertExceptionMatches(null, null);
87+
} catch (AssertionError e) {
88+
// AssertionError not actually thrown
89+
if (!e.toString().contains(E_NULL_THROWABLE)) {
90+
throw e;
91+
}
92+
}
93+
}
94+
95+
@Test
96+
public void testAssertExceptionMatchesNullString() throws Throwable {
97+
try {
98+
assertExceptionMatches(null, new BrokenException());
99+
} catch (AssertionError e) {
100+
// AssertionError not actually thrown
101+
if (!e.toString().contains(E_NULL_THROWABLE_STRING)) {
102+
throw e;
103+
}
104+
}
105+
}
106+
107+
@Test
108+
public void testAssertExceptionMatchesWrongText() throws Throwable {
109+
try {
110+
assertExceptionMatches(Pattern.compile(".*Expected.*"), new Exception("(actual)"));
111+
} catch (AssertionError e) {
112+
String s = e.toString();
113+
if (!s.contains(E_UNEXPECTED_EXCEPTION)
114+
|| !s.contains("(actual)") ) {
115+
throw e;
116+
}
117+
if (e.getCause() == null) {
118+
throw new AssertionError("No nested cause in assertion", e);
119+
}
120+
}
121+
}
122+
123+
@Test
124+
public void testAssertExceptionMatchesWorking() throws Throwable {
125+
assertExceptionMatches(Pattern.compile(".*Expected.*"), new Exception("Expected"));
126+
}
127+
79128
private static class BrokenException extends Exception {
80129
public BrokenException() {
81130
}

0 commit comments

Comments
 (0)