Skip to content

Commit 67b91b2

Browse files
committed
Polish RollbackRuleTests
See gh-28098
1 parent d67034f commit 67b91b2

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,81 @@
3737
class RollbackRuleTests {
3838

3939
@Test
40-
void foundImmediatelyWithString() {
41-
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName());
42-
assertThat(rr.getDepth(new Exception())).isEqualTo(0);
40+
void constructorArgumentMustBeThrowableClassWithNonThrowableType() {
41+
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class));
4342
}
4443

4544
@Test
46-
void foundImmediatelyWithClass() {
47-
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
48-
assertThat(rr.getDepth(new Exception())).isEqualTo(0);
45+
void constructorArgumentMustBeThrowableClassWithNullThrowableType() {
46+
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null));
47+
}
48+
49+
@Test
50+
void constructorArgumentMustBeStringWithNull() {
51+
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null));
4952
}
5053

5154
@Test
5255
void notFound() {
53-
RollbackRuleAttribute rr = new RollbackRuleAttribute(java.io.IOException.class.getName());
56+
RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class);
5457
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1);
5558
}
5659

5760
@Test
58-
void ancestry() {
61+
void foundImmediatelyWithString() {
5962
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName());
60-
// Exception -> Runtime -> NestedRuntime -> MyRuntimeException
61-
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3);
63+
assertThat(rr.getDepth(new Exception())).isEqualTo(0);
6264
}
6365

6466
@Test
65-
void alwaysTrueForThrowable() {
66-
RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class.getName());
67-
assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0);
68-
assertThat(rr.getDepth(new IOException())).isGreaterThan(0);
69-
assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0);
70-
assertThat(rr.getDepth(new RuntimeException())).isGreaterThan(0);
67+
void foundImmediatelyWithClass() {
68+
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
69+
assertThat(rr.getDepth(new Exception())).isEqualTo(0);
7170
}
7271

7372
@Test
74-
void ctorArgMustBeAThrowableClassWithNonThrowableType() {
75-
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class));
73+
void foundInSuperclassHierarchy() {
74+
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
75+
// Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException
76+
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3);
7677
}
7778

7879
@Test
79-
void ctorArgMustBeAThrowableClassWithNullThrowableType() {
80-
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null));
80+
void alwaysFoundForThrowable() {
81+
RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class);
82+
assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0);
83+
assertThat(rr.getDepth(new IOException())).isGreaterThan(0);
84+
assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0);
85+
assertThat(rr.getDepth(new RuntimeException())).isGreaterThan(0);
8186
}
8287

8388
@Test
84-
void ctorArgExceptionStringNameVersionWithNull() {
85-
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null));
89+
void foundNestedExceptionInEnclosingException() {
90+
RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class);
91+
assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0);
8692
}
8793

8894
@Test
89-
void foundEnclosedExceptionWithEnclosingException() {
90-
RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class);
91-
assertThat(rr.getDepth(new EnclosingException.EnclosedException())).isEqualTo(0);
95+
void foundWhenNameOfExceptionThrownStartsWithTheNameOfTheRegisteredExceptionType() {
96+
RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class);
97+
assertThat(rr.getDepth(new MyException2())).isEqualTo(0);
9298
}
9399

100+
94101
@SuppressWarnings("serial")
95102
static class EnclosingException extends RuntimeException {
96103

97104
@SuppressWarnings("serial")
98-
static class EnclosedException extends RuntimeException {
99-
105+
static class NestedException extends RuntimeException {
100106
}
101107
}
102108

109+
static class MyException extends RuntimeException {
110+
}
111+
112+
// Name intentionally starts with MyException (including package) but does
113+
// NOT extend MyException.
114+
static class MyException2 extends RuntimeException {
115+
}
116+
103117
}

0 commit comments

Comments
 (0)