@ -1,5 +1,5 @@
@@ -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 @@
@@ -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.assertThat;
@@ -27,72 +26,65 @@ import static org.assertj.core.api.Assertions.assertThat;
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 class TransactionAttributeEditorTests {
@@ -104,13 +96,11 @@ public class TransactionAttributeEditorTests {
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 class TransactionAttributeEditorTests {
@@ -122,18 +112,17 @@ public class TransactionAttributeEditorTests {
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 testD efaultTransactionAttributeToString( ) {
void d efaultTransactionAttributeToString( ) {
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 class TransactionAttributeEditorTests {
@@ -151,7 +140,7 @@ public class TransactionAttributeEditorTests {
}
@Test
public void testR uleBasedTransactionAttributeToString( ) {
void r uleBasedTransactionAttributeToString( ) {
RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute ( ) ;
source . setPropagationBehavior ( TransactionDefinition . PROPAGATION_SUPPORTS ) ;
source . setIsolationLevel ( TransactionDefinition . ISOLATION_REPEATABLE_READ ) ;
@ -160,7 +149,6 @@ public class TransactionAttributeEditorTests {
@@ -160,7 +149,6 @@ public class TransactionAttributeEditorTests {
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 ) ;