Browse Source

Polish RollbackRuleTests

See gh-28098
pull/27953/head
Sam Brannen 4 years ago
parent
commit
67b91b2390
  1. 68
      spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleTests.java

68
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 { class RollbackRuleTests {
@Test @Test
void foundImmediatelyWithString() { void constructorArgumentMustBeThrowableClassWithNonThrowableType() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class));
assertThat(rr.getDepth(new Exception())).isEqualTo(0);
} }
@Test @Test
void foundImmediatelyWithClass() { void constructorArgumentMustBeThrowableClassWithNullThrowableType() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null));
assertThat(rr.getDepth(new Exception())).isEqualTo(0); }
@Test
void constructorArgumentMustBeStringWithNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null));
} }
@Test @Test
void notFound() { void notFound() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(java.io.IOException.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class);
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1); assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1);
} }
@Test @Test
void ancestry() { void foundImmediatelyWithString() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName());
// Exception -> Runtime -> NestedRuntime -> MyRuntimeException assertThat(rr.getDepth(new Exception())).isEqualTo(0);
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3);
} }
@Test @Test
void alwaysTrueForThrowable() { void foundImmediatelyWithClass() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0); assertThat(rr.getDepth(new Exception())).isEqualTo(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 @Test
void ctorArgMustBeAThrowableClassWithNonThrowableType() { void foundInSuperclassHierarchy() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class)); RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
// Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3);
} }
@Test @Test
void ctorArgMustBeAThrowableClassWithNullThrowableType() { void alwaysFoundForThrowable() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null)); 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 @Test
void ctorArgExceptionStringNameVersionWithNull() { void foundNestedExceptionInEnclosingException() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null)); RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class);
assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0);
} }
@Test @Test
void foundEnclosedExceptionWithEnclosingException() { void foundWhenNameOfExceptionThrownStartsWithTheNameOfTheRegisteredExceptionType() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class);
assertThat(rr.getDepth(new EnclosingException.EnclosedException())).isEqualTo(0); assertThat(rr.getDepth(new MyException2())).isEqualTo(0);
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
static class EnclosingException extends RuntimeException { static class EnclosingException extends RuntimeException {
@SuppressWarnings("serial") @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 {
}
} }

Loading…
Cancel
Save