From 613bd3be1db254f713e39dafcf22f52686611cd7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 9 May 2020 20:09:27 +0200 Subject: [PATCH] Honor TxMgmtConfigurer when @Primary tx mgr is present in TCF Prior to this commit, the Spring TestContext Framework (TCF) favored a @Primary transaction manger over one configured via the TransactionManagementConfigurer API, and this contradicts the behavior in production Spring applications. This commit aligns the transaction manger lookup within the TCF so that a transaction manger configured via the TransactionManagementConfigurer API is properly favored over a @Primary transaction manager. Closes gh-24869 --- .../TestContextTransactionUtils.java | 18 +++++++-------- ...gementConfigurerWithPrimaryTxMgrTests.java | 23 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java b/spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java index d00370dcf3..c71e2ec106 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java @@ -145,9 +145,9 @@ public abstract class TestContextTransactionUtils { * {@code name} is non-empty, throwing a {@link BeansException} if the named * transaction manager does not exist. *
  • Attempt to look up the single transaction manager by type. - *
  • Attempt to look up the primary transaction manager by type. *
  • Attempt to look up the transaction manager via a * {@link TransactionManagementConfigurer}, if present. + *
  • Attempt to look up the primary transaction manager by type. *
  • Attempt to look up the transaction manager by type and the * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager * name}. @@ -190,14 +190,6 @@ public abstract class TestContextTransactionUtils { return txMgrs.values().iterator().next(); } - try { - // Look up single bean by type, with support for 'primary' beans - return bf.getBean(PlatformTransactionManager.class); - } - catch (BeansException ex) { - logBeansException(testContext, ex, PlatformTransactionManager.class); - } - // Look up single TransactionManagementConfigurer Map configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class); @@ -210,6 +202,14 @@ public abstract class TestContextTransactionUtils { "is not a PlatformTransactionManager: " + tm); return (PlatformTransactionManager) tm; } + + try { + // Look up single bean by type, with support for 'primary' beans + return bf.getBean(PlatformTransactionManager.class); + } + catch (BeansException ex) { + logBeansException(testContext, ex, PlatformTransactionManager.class); + } } // look up by type and default name diff --git a/spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java b/spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java index 05948d8646..d6e44d4b3e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java @@ -34,9 +34,9 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage import static org.assertj.core.api.Assertions.assertThat; /** - * Integration test that verifies the current behavior for transaction manager - * lookups when one transaction manager is {@link Primary @Primary} and an - * additional transaction manager is configured via the + * Integration test that verifies the behavior for transaction manager lookups + * when one transaction manager is {@link Primary @Primary} and an additional + * transaction manager is configured via the * {@link TransactionManagementConfigurer} API. * * @author Sam Brannen @@ -44,7 +44,6 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringJUnitConfig @Transactional -// TODO Update assertions once https://github.com/spring-projects/spring-framework/issues/24869 is fixed. class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests { @Autowired @@ -57,28 +56,28 @@ class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests { @Test void transactionalTest() { - assertThat(primary.begun).isEqualTo(1); - assertThat(primary.inflight).isEqualTo(1); + assertThat(primary.begun).isEqualTo(0); + assertThat(primary.inflight).isEqualTo(0); assertThat(primary.commits).isEqualTo(0); assertThat(primary.rollbacks).isEqualTo(0); - assertThat(annotationDriven.begun).isEqualTo(0); - assertThat(annotationDriven.inflight).isEqualTo(0); + assertThat(annotationDriven.begun).isEqualTo(1); + assertThat(annotationDriven.inflight).isEqualTo(1); assertThat(annotationDriven.commits).isEqualTo(0); assertThat(annotationDriven.rollbacks).isEqualTo(0); } @AfterTransaction void afterTransaction() { - assertThat(primary.begun).isEqualTo(1); + assertThat(primary.begun).isEqualTo(0); assertThat(primary.inflight).isEqualTo(0); assertThat(primary.commits).isEqualTo(0); - assertThat(primary.rollbacks).isEqualTo(1); + assertThat(primary.rollbacks).isEqualTo(0); - assertThat(annotationDriven.begun).isEqualTo(0); + assertThat(annotationDriven.begun).isEqualTo(1); assertThat(annotationDriven.inflight).isEqualTo(0); assertThat(annotationDriven.commits).isEqualTo(0); - assertThat(annotationDriven.rollbacks).isEqualTo(0); + assertThat(annotationDriven.rollbacks).isEqualTo(1); }