diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java index 8445de81bb5..47f73dee274 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java @@ -37,67 +37,81 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException class RollbackRuleTests { @Test - void foundImmediatelyWithString() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); - assertThat(rr.getDepth(new Exception())).isEqualTo(0); + void constructorArgumentMustBeThrowableClassWithNonThrowableType() { + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class)); } @Test - void foundImmediatelyWithClass() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); - assertThat(rr.getDepth(new Exception())).isEqualTo(0); + void constructorArgumentMustBeThrowableClassWithNullThrowableType() { + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class) null)); + } + + @Test + void constructorArgumentMustBeStringWithNull() { + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null)); } @Test void notFound() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(java.io.IOException.class.getName()); + RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class); assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1); } @Test - void ancestry() { + void foundImmediatelyWithString() { RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); - // Exception -> Runtime -> NestedRuntime -> MyRuntimeException - assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3); + assertThat(rr.getDepth(new Exception())).isEqualTo(0); } @Test - void alwaysTrueForThrowable() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class.getName()); - assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0); - assertThat(rr.getDepth(new IOException())).isGreaterThan(0); - assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0); - assertThat(rr.getDepth(new RuntimeException())).isGreaterThan(0); + void foundImmediatelyWithClass() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); + assertThat(rr.getDepth(new Exception())).isEqualTo(0); } @Test - void ctorArgMustBeAThrowableClassWithNonThrowableType() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class)); + void foundInSuperclassHierarchy() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); + // Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException + assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3); } @Test - void ctorArgMustBeAThrowableClassWithNullThrowableType() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class) null)); + void alwaysFoundForThrowable() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class); + assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0); + assertThat(rr.getDepth(new IOException())).isGreaterThan(0); + assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0); + assertThat(rr.getDepth(new RuntimeException())).isGreaterThan(0); } @Test - void ctorArgExceptionStringNameVersionWithNull() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null)); + void foundNestedExceptionInEnclosingException() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); + assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0); } @Test - void foundEnclosedExceptionWithEnclosingException() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); - assertThat(rr.getDepth(new EnclosingException.EnclosedException())).isEqualTo(0); + void foundWhenNameOfExceptionThrownStartsWithTheNameOfTheRegisteredExceptionType() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class); + assertThat(rr.getDepth(new MyException2())).isEqualTo(0); } + @SuppressWarnings("serial") static class EnclosingException extends RuntimeException { @SuppressWarnings("serial") - static class EnclosedException extends RuntimeException { - + static class NestedException extends RuntimeException { } } + static class MyException extends RuntimeException { + } + + // Name intentionally starts with MyException (including package) but does + // NOT extend MyException. + static class MyException2 extends RuntimeException { + } + }