From b3e5f86277e73c91990a85d23779509225085d63 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 3 Mar 2022 16:20:13 +0100 Subject: [PATCH] Polish rollback rule support --- .../interceptor/RollbackRuleAttribute.java | 78 +++++----- .../interceptor/MyRuntimeException.java | 8 +- .../RollbackRuleAttributeTests.java | 139 ++++++++++++------ .../RuleBasedTransactionAttributeTests.java | 32 ++-- .../TransactionAttributeEditorTests.java | 74 ++++------ 5 files changed, 188 insertions(+), 143 deletions(-) diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/RollbackRuleAttribute.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/RollbackRuleAttribute.java index 07a59cc1131b..4c3d4ec53c23 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/RollbackRuleAttribute.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/RollbackRuleAttribute.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,13 @@ import org.springframework.util.Assert; /** - * Rule determining whether or not a given exception (and any subclasses) - * should cause a rollback. + * Rule determining whether or not a given exception should cause a rollback. * *

Multiple such rules can be applied to determine whether a transaction * should commit or rollback after an exception has been thrown. * * @author Rod Johnson + * @author Sam Brannen * @since 09.04.2003 * @see NoRollbackRuleAttribute */ @@ -36,7 +36,7 @@ public class RollbackRuleAttribute implements Serializable{ /** - * The {@link RollbackRuleAttribute rollback rule} for + * The {@linkplain RollbackRuleAttribute rollback rule} for * {@link RuntimeException RuntimeExceptions}. */ public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS = @@ -48,30 +48,31 @@ public class RollbackRuleAttribute implements Serializable{ * This way does multiple string comparisons, but how often do we decide * whether to roll back a transaction following an exception? */ - private final String exceptionName; + private final String exceptionPattern; /** - * Create a new instance of the {@code RollbackRuleAttribute} class. + * Create a new instance of the {@code RollbackRuleAttribute} class + * for the given {@code exceptionType}. *

This is the preferred way to construct a rollback rule that matches - * the supplied {@link Exception} class, its subclasses, and its nested classes. - * @param clazz throwable class; must be {@link Throwable} or a subclass + * the supplied exception type, its subclasses, and its nested classes. + * @param exceptionType exception type; must be {@link Throwable} or a subclass * of {@code Throwable} - * @throws IllegalArgumentException if the supplied {@code clazz} is + * @throws IllegalArgumentException if the supplied {@code exceptionType} is * not a {@code Throwable} type or is {@code null} */ - public RollbackRuleAttribute(Class clazz) { - Assert.notNull(clazz, "'clazz' cannot be null"); - if (!Throwable.class.isAssignableFrom(clazz)) { + public RollbackRuleAttribute(Class exceptionType) { + Assert.notNull(exceptionType, "'exceptionType' cannot be null"); + if (!Throwable.class.isAssignableFrom(exceptionType)) { throw new IllegalArgumentException( - "Cannot construct rollback rule from [" + clazz.getName() + "]: it's not a Throwable"); + "Cannot construct rollback rule from [" + exceptionType.getName() + "]: it's not a Throwable"); } - this.exceptionName = clazz.getName(); + this.exceptionPattern = exceptionType.getName(); } /** * Create a new instance of the {@code RollbackRuleAttribute} class - * for the given {@code exceptionName}. + * for the given {@code exceptionPattern}. *

This can be a substring, with no wildcard support at present. A value * of "ServletException" would match * {@code javax.servlet.ServletException} and subclasses, for example. @@ -79,40 +80,49 @@ public RollbackRuleAttribute(Class clazz) { * whether to include package information (which is not mandatory). For * example, "Exception" will match nearly anything, and will probably hide * other rules. "java.lang.Exception" would be correct if "Exception" was - * meant to define a rule for all checked exceptions. With more unusual + * meant to define a rule for all checked exceptions. With more unique * exception names such as "BaseBusinessException" there's no need to use a * fully package-qualified name. - * @param exceptionName the exception name pattern; can also be a fully + * @param exceptionPattern the exception name pattern; can also be a fully * package-qualified class name - * @throws IllegalArgumentException if the supplied - * {@code exceptionName} is {@code null} or empty + * @throws IllegalArgumentException if the supplied {@code exceptionPattern} + * is {@code null} or empty */ - public RollbackRuleAttribute(String exceptionName) { - Assert.hasText(exceptionName, "'exceptionName' cannot be null or empty"); - this.exceptionName = exceptionName; + public RollbackRuleAttribute(String exceptionPattern) { + Assert.hasText(exceptionPattern, "'exceptionPattern' cannot be null or empty"); + this.exceptionPattern = exceptionPattern; } /** - * Return the pattern for the exception name. + * Get the configured exception name pattern that this rule uses for matching. + * @see #getDepth(Throwable) */ public String getExceptionName() { - return this.exceptionName; + return this.exceptionPattern; } /** - * Return the depth of the superclass matching. - *

{@code 0} means {@code ex} matches exactly. Returns - * {@code -1} if there is no match. Otherwise, returns depth with the - * lowest depth winning. + * Return the depth of the superclass matching, with the following semantics. + *

+ *

When comparing roll back rules that match against a given exception, a rule + * with a lower matching depth wins. For example, a direct match ({@code depth == 0}) + * wins over a match in the superclass hierarchy ({@code depth > 0}). */ - public int getDepth(Throwable ex) { - return getDepth(ex.getClass(), 0); + public int getDepth(Throwable exception) { + return getDepth(exception.getClass(), 0); } private int getDepth(Class exceptionClass, int depth) { - if (exceptionClass.getName().contains(this.exceptionName)) { + if (exceptionClass.getName().contains(this.exceptionPattern)) { // Found it! return depth; } @@ -133,17 +143,17 @@ public boolean equals(@Nullable Object other) { return false; } RollbackRuleAttribute rhs = (RollbackRuleAttribute) other; - return this.exceptionName.equals(rhs.exceptionName); + return this.exceptionPattern.equals(rhs.exceptionPattern); } @Override public int hashCode() { - return this.exceptionName.hashCode(); + return this.exceptionPattern.hashCode(); } @Override public String toString() { - return "RollbackRuleAttribute with pattern [" + this.exceptionName + "]"; + return "RollbackRuleAttribute with pattern [" + this.exceptionPattern + "]"; } } diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/MyRuntimeException.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/MyRuntimeException.java index 80affe6bc24f..5b512e1eb8e8 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/MyRuntimeException.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/MyRuntimeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,13 @@ */ @SuppressWarnings("serial") class MyRuntimeException extends NestedRuntimeException { + + public MyRuntimeException() { + super(""); + } + public MyRuntimeException(String msg) { super(msg); } + } diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleAttributeTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleAttributeTests.java index fd05ff675531..f210659af3b6 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleAttributeTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RollbackRuleAttributeTests.java @@ -18,6 +18,7 @@ import java.io.IOException; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.FatalBeanException; @@ -36,65 +37,105 @@ */ class RollbackRuleAttributeTests { - @Test - void constructorArgumentMustBeThrowableClassWithNonThrowableType() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class)); - } + @Nested + class ExceptionPatternTests { - @Test - void constructorArgumentMustBeThrowableClassWithNullThrowableType() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class) null)); - } + @Test + void constructorPreconditions() { + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null)); + } - @Test - void constructorArgumentMustBeStringWithNull() { - assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((String) null)); - } + @Test + void notFound() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class.getName()); + assertThat(rr.getDepth(new MyRuntimeException())).isEqualTo(-1); + } - @Test - void notFound() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class); - assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(-1); - } + @Test + void alwaysFoundForThrowable() { + 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); + } - @Test - void foundImmediatelyWithString() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); - assertThat(rr.getDepth(new Exception())).isEqualTo(0); - } + @Test + void foundImmediatelyWhenDirectMatch() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); + assertThat(rr.getDepth(new Exception())).isEqualTo(0); + } - @Test - void foundImmediatelyWithClass() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); - assertThat(rr.getDepth(new Exception())).isEqualTo(0); - } + @Test + void foundImmediatelyWhenExceptionThrownIsNestedTypeOfRegisteredException() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class.getName()); + assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0); + } - @Test - void foundInSuperclassHierarchy() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); - // Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException - assertThat(rr.getDepth(new MyRuntimeException(""))).isEqualTo(3); - } + @Test + void foundImmediatelyWhenNameOfExceptionThrownStartsWithNameOfRegisteredException() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class.getName()); + assertThat(rr.getDepth(new MyException2())).isEqualTo(0); + } - @Test - 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 foundInSuperclassHierarchy() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class.getName()); + // Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException + assertThat(rr.getDepth(new MyRuntimeException())).isEqualTo(3); + } - @Test - void foundNestedExceptionInEnclosingException() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); - assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0); } - @Test - void foundWhenNameOfExceptionThrownStartsWithTheNameOfTheRegisteredExceptionType() { - RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class); - assertThat(rr.getDepth(new MyException2())).isEqualTo(0); + @Nested + class ExceptionTypeTests { + + @Test + void constructorPreconditions() { + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute(Object.class)); + assertThatIllegalArgumentException().isThrownBy(() -> new RollbackRuleAttribute((Class) null)); + } + + @Test + void notFound() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(IOException.class); + assertThat(rr.getDepth(new MyRuntimeException())).isEqualTo(-1); + } + + @Test + 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 foundImmediatelyWhenDirectMatch() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); + assertThat(rr.getDepth(new Exception())).isEqualTo(0); + } + + @Test + void foundImmediatelyWhenExceptionThrownIsNestedTypeOfRegisteredException() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(EnclosingException.class); + assertThat(rr.getDepth(new EnclosingException.NestedException())).isEqualTo(0); + } + + @Test + void foundImmediatelyWhenNameOfExceptionThrownStartsWithNameOfRegisteredException() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(MyException.class); + assertThat(rr.getDepth(new MyException2())).isEqualTo(0); + } + + @Test + void foundInSuperclassHierarchy() { + RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); + // Exception -> RuntimeException -> NestedRuntimeException -> MyRuntimeException + assertThat(rr.getDepth(new MyRuntimeException())).isEqualTo(3); + } + } diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RuleBasedTransactionAttributeTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RuleBasedTransactionAttributeTests.java index 8aa9815e7c94..3f4cffe2b824 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/RuleBasedTransactionAttributeTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/RuleBasedTransactionAttributeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,13 +35,13 @@ * @author Chris Beams * @since 09.04.2003 */ -public class RuleBasedTransactionAttributeTests { +class RuleBasedTransactionAttributeTests { @Test - public void testDefaultRule() { + void defaultRule() { RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(); assertThat(rta.rollbackOn(new RuntimeException())).isTrue(); - assertThat(rta.rollbackOn(new MyRuntimeException(""))).isTrue(); + assertThat(rta.rollbackOn(new MyRuntimeException())).isTrue(); assertThat(rta.rollbackOn(new Exception())).isFalse(); assertThat(rta.rollbackOn(new IOException())).isFalse(); } @@ -50,20 +50,20 @@ public void testDefaultRule() { * Test one checked exception that should roll back. */ @Test - public void testRuleForRollbackOnChecked() { + void ruleForRollbackOnChecked() { List list = new ArrayList<>(); list.add(new RollbackRuleAttribute(IOException.class.getName())); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); assertThat(rta.rollbackOn(new RuntimeException())).isTrue(); - assertThat(rta.rollbackOn(new MyRuntimeException(""))).isTrue(); + assertThat(rta.rollbackOn(new MyRuntimeException())).isTrue(); assertThat(rta.rollbackOn(new Exception())).isFalse(); // Check that default behaviour is overridden assertThat(rta.rollbackOn(new IOException())).isTrue(); } @Test - public void testRuleForCommitOnUnchecked() { + void ruleForCommitOnUnchecked() { List list = new ArrayList<>(); list.add(new NoRollbackRuleAttribute(MyRuntimeException.class.getName())); list.add(new RollbackRuleAttribute(IOException.class.getName())); @@ -71,14 +71,14 @@ public void testRuleForCommitOnUnchecked() { assertThat(rta.rollbackOn(new RuntimeException())).isTrue(); // Check default behaviour is overridden - assertThat(rta.rollbackOn(new MyRuntimeException(""))).isFalse(); + assertThat(rta.rollbackOn(new MyRuntimeException())).isFalse(); assertThat(rta.rollbackOn(new Exception())).isFalse(); // Check that default behaviour is overridden assertThat(rta.rollbackOn(new IOException())).isTrue(); } @Test - public void testRuleForSelectiveRollbackOnCheckedWithString() { + void ruleForSelectiveRollbackOnCheckedWithString() { List l = new ArrayList<>(); l.add(new RollbackRuleAttribute(java.rmi.RemoteException.class.getName())); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); @@ -86,7 +86,7 @@ public void testRuleForSelectiveRollbackOnCheckedWithString() { } @Test - public void testRuleForSelectiveRollbackOnCheckedWithClass() { + void ruleForSelectiveRollbackOnCheckedWithClass() { List l = Collections.singletonList(new RollbackRuleAttribute(RemoteException.class)); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); doTestRuleForSelectiveRollbackOnChecked(rta); @@ -105,7 +105,7 @@ private void doTestRuleForSelectiveRollbackOnChecked(RuleBasedTransactionAttribu * when Exception prompts a rollback. */ @Test - public void testRuleForCommitOnSubclassOfChecked() { + void ruleForCommitOnSubclassOfChecked() { List list = new ArrayList<>(); // Note that it's important to ensure that we have this as // a FQN: otherwise it will match everything! @@ -120,20 +120,20 @@ public void testRuleForCommitOnSubclassOfChecked() { } @Test - public void testRollbackNever() { + void rollbackNever() { List list = new ArrayList<>(); list.add(new NoRollbackRuleAttribute("Throwable")); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); assertThat(rta.rollbackOn(new Throwable())).isFalse(); assertThat(rta.rollbackOn(new RuntimeException())).isFalse(); - assertThat(rta.rollbackOn(new MyRuntimeException(""))).isFalse(); + assertThat(rta.rollbackOn(new MyRuntimeException())).isFalse(); assertThat(rta.rollbackOn(new Exception())).isFalse(); assertThat(rta.rollbackOn(new IOException())).isFalse(); } @Test - public void testToStringMatchesEditor() { + void toStringMatchesEditor() { List list = new ArrayList<>(); list.add(new NoRollbackRuleAttribute("Throwable")); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); @@ -144,7 +144,7 @@ public void testToStringMatchesEditor() { assertThat(rta.rollbackOn(new Throwable())).isFalse(); assertThat(rta.rollbackOn(new RuntimeException())).isFalse(); - assertThat(rta.rollbackOn(new MyRuntimeException(""))).isFalse(); + assertThat(rta.rollbackOn(new MyRuntimeException())).isFalse(); assertThat(rta.rollbackOn(new Exception())).isFalse(); assertThat(rta.rollbackOn(new IOException())).isFalse(); } @@ -153,7 +153,7 @@ public void testToStringMatchesEditor() { * See this forum post. */ @Test - public void testConflictingRulesToDetermineExactContract() { + void conflictingRulesToDetermineExactContract() { List list = new ArrayList<>(); list.add(new NoRollbackRuleAttribute(MyBusinessWarningException.class)); list.add(new RollbackRuleAttribute(MyBusinessException.class)); diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionAttributeEditorTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionAttributeEditorTests.java index 4614f2648a3c..3bfd82bb679b 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionAttributeEditorTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionAttributeEditorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.transaction.interceptor; - import java.io.IOException; import org.junit.jupiter.api.Test; @@ -27,72 +26,65 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** - * Tests to check conversion from String to TransactionAttribute. + * Tests to check conversion from String to TransactionAttribute using + * a {@link TransactionAttributeEditor}. * * @author Rod Johnson * @author Juergen Hoeller * @author Chris Beams * @since 26.04.2003 */ -public class TransactionAttributeEditorTests { +class TransactionAttributeEditorTests { + + private final TransactionAttributeEditor pe = new TransactionAttributeEditor(); + @Test - public void testNull() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void nullText() { pe.setAsText(null); - TransactionAttribute ta = (TransactionAttribute) pe.getValue(); - assertThat(ta == null).isTrue(); + assertThat(pe.getValue()).isNull(); } @Test - public void testEmptyString() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void emptyString() { pe.setAsText(""); - TransactionAttribute ta = (TransactionAttribute) pe.getValue(); - assertThat(ta == null).isTrue(); + assertThat(pe.getValue()).isNull(); } @Test - public void testValidPropagationCodeOnly() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void validPropagationCodeOnly() { pe.setAsText("PROPAGATION_REQUIRED"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); - assertThat(ta != null).isTrue(); - assertThat(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED).isTrue(); - assertThat(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT).isTrue(); - boolean condition = !ta.isReadOnly(); - assertThat(condition).isTrue(); + assertThat(ta).isNotNull(); + assertThat(ta.getPropagationBehavior()).isEqualTo(TransactionDefinition.PROPAGATION_REQUIRED); + assertThat(ta.getIsolationLevel()).isEqualTo(TransactionDefinition.ISOLATION_DEFAULT); + assertThat(ta.isReadOnly()).isFalse(); } @Test - public void testInvalidPropagationCodeOnly() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void invalidPropagationCodeOnly() { // should have failed with bogus propagation code - assertThatIllegalArgumentException().isThrownBy(() -> - pe.setAsText("XXPROPAGATION_REQUIRED")); + assertThatIllegalArgumentException().isThrownBy(() -> pe.setAsText("XXPROPAGATION_REQUIRED")); } @Test - public void testValidPropagationCodeAndIsolationCode() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void validPropagationCodeAndIsolationCode() { pe.setAsText("PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); - assertThat(ta != null).isTrue(); - assertThat(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED).isTrue(); - assertThat(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED).isTrue(); + assertThat(ta).isNotNull(); + assertThat(ta.getPropagationBehavior()).isEqualTo(TransactionDefinition.PROPAGATION_REQUIRED); + assertThat(ta.getIsolationLevel()).isEqualTo(TransactionDefinition.ISOLATION_READ_UNCOMMITTED); } @Test - public void testValidPropagationAndIsolationCodesAndInvalidRollbackRule() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void validPropagationAndIsolationCodesAndInvalidRollbackRule() { // should fail with bogus rollback rule - assertThatIllegalArgumentException().isThrownBy(() -> - pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX")); + assertThatIllegalArgumentException() + .isThrownBy(() -> pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX")); } @Test - public void testValidPropagationCodeAndIsolationCodeAndRollbackRules1() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void validPropagationCodeAndIsolationCodeAndRollbackRules1() { pe.setAsText("PROPAGATION_MANDATORY,ISOLATION_REPEATABLE_READ,timeout_10,-IOException,+MyRuntimeException"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertThat(ta).isNotNull(); @@ -104,13 +96,11 @@ public void testValidPropagationCodeAndIsolationCodeAndRollbackRules1() { assertThat(ta.rollbackOn(new Exception())).isFalse(); // Check for our bizarre customized rollback rules assertThat(ta.rollbackOn(new IOException())).isTrue(); - boolean condition = !ta.rollbackOn(new MyRuntimeException("")); - assertThat(condition).isTrue(); + assertThat(ta.rollbackOn(new MyRuntimeException())).isFalse(); } @Test - public void testValidPropagationCodeAndIsolationCodeAndRollbackRules2() { - TransactionAttributeEditor pe = new TransactionAttributeEditor(); + void validPropagationCodeAndIsolationCodeAndRollbackRules2() { pe.setAsText("+IOException,readOnly,ISOLATION_READ_COMMITTED,-MyRuntimeException,PROPAGATION_SUPPORTS"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertThat(ta).isNotNull(); @@ -122,18 +112,17 @@ public void testValidPropagationCodeAndIsolationCodeAndRollbackRules2() { assertThat(ta.rollbackOn(new Exception())).isFalse(); // Check for our bizarre customized rollback rules assertThat(ta.rollbackOn(new IOException())).isFalse(); - assertThat(ta.rollbackOn(new MyRuntimeException(""))).isTrue(); + assertThat(ta.rollbackOn(new MyRuntimeException())).isTrue(); } @Test - public void testDefaultTransactionAttributeToString() { + void defaultTransactionAttributeToString() { DefaultTransactionAttribute source = new DefaultTransactionAttribute(); source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); source.setTimeout(10); source.setReadOnly(true); - TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(source.toString()); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertThat(source).isEqualTo(ta); @@ -151,7 +140,7 @@ public void testDefaultTransactionAttributeToString() { } @Test - public void testRuleBasedTransactionAttributeToString() { + void ruleBasedTransactionAttributeToString() { RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute(); source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); @@ -160,7 +149,6 @@ public void testRuleBasedTransactionAttributeToString() { source.getRollbackRules().add(new RollbackRuleAttribute("IllegalArgumentException")); source.getRollbackRules().add(new NoRollbackRuleAttribute("IllegalStateException")); - TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(source.toString()); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertThat(source).isEqualTo(ta);