Merge branch '5.3.x'

This commit is contained in:
Sam Brannen 2022-03-01 19:10:54 +01:00
commit 14ae522172
1 changed files with 44 additions and 30 deletions

View File

@ -36,6 +36,27 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/ */
class RollbackRuleTests { class RollbackRuleTests {
@Test
void constructorArgumentMustBeThrowableClassWithNonThrowableType() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class));
}
@Test
void constructorArgumentMustBeThrowableClassWithNullThrowableType() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null));
}
@Test
void constructorArgumentMustBeStringWithNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null));
}
@Test
void notFound() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class);
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1);
}
@Test @Test
void foundImmediatelyWithString() { void foundImmediatelyWithString() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName());
@ -49,21 +70,15 @@ class RollbackRuleTests {
} }
@Test @Test
void notFound() { void foundInSuperclassHierarchy() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(java.io.IOException.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class);
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1); // Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException
}
@Test
void ancestry() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName());
// Exception -> Runtime -> NestedRuntime -> MyRuntimeException
assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3); assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3);
} }
@Test @Test
void alwaysTrueForThrowable() { void alwaysFoundForThrowable() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class.getName()); RollbackRuleAttribute rr = new RollbackRuleAttribute(Throwable.class);
assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0); assertThat(rr.getDepth(new MyRuntimeException(""))).isGreaterThan(0);
assertThat(rr.getDepth(new IOException())).isGreaterThan(0); assertThat(rr.getDepth(new IOException())).isGreaterThan(0);
assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0); assertThat(rr.getDepth(new FatalBeanException(null, null))).isGreaterThan(0);
@ -71,33 +86,32 @@ class RollbackRuleTests {
} }
@Test @Test
void ctorArgMustBeAThrowableClassWithNonThrowableType() { void foundNestedExceptionInEnclosingException() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class));
}
@Test
void ctorArgMustBeAThrowableClassWithNullThrowableType() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class<?>) null));
}
@Test
void ctorArgExceptionStringNameVersionWithNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null));
}
@Test
void foundEnclosedExceptionWithEnclosingException() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class);
assertThat(rr.getDepth(new EnclosingException.EnclosedException())).isEqualTo(0); assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0);
} }
@Test
void foundWhenNameOfExceptionThrownStartsWithTheNameOfTheRegisteredExceptionType() {
RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class);
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 {
}
} }