From a5498ba2ad629616030ffd6b5f3ca88954d7b4d7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 6 Apr 2020 19:11:16 +0200 Subject: [PATCH] Test status quo for @Primary + TransactionManagementConfigurer in TCF This commit introduces an integration test for the status quo in the Spring TestContext Framework (TCF) for multiple transaction managers registered as @Primary and via the TransactionManagementConfigurer API. In Spring Framework 5.3 we will revise the transaction manager lookup in TestContextTransactionUtils so that the transaction manager configured via the TransactionManagementConfigurer API is favored over a @Primary transaction manager. See gh-24869 --- ...gementConfigurerWithPrimaryTxMgrTests.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java 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 new file mode 100644 index 0000000000..05948d8646 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java @@ -0,0 +1,102 @@ +/* + * Copyright 2002-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.transaction.manager; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.context.transaction.AfterTransaction; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionManager; +import org.springframework.transaction.annotation.TransactionManagementConfigurer; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.testfixture.CallCountingTransactionManager; + +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 + * {@link TransactionManagementConfigurer} API. + * + * @author Sam Brannen + * @since 5.2.6 + */ +@SpringJUnitConfig +@Transactional +// TODO Update assertions once https://github.com/spring-projects/spring-framework/issues/24869 is fixed. +class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests { + + @Autowired + CallCountingTransactionManager primary; + + @Autowired + @Qualifier("annotationDrivenTransactionManager") + CallCountingTransactionManager annotationDriven; + + + @Test + void transactionalTest() { + assertThat(primary.begun).isEqualTo(1); + assertThat(primary.inflight).isEqualTo(1); + assertThat(primary.commits).isEqualTo(0); + assertThat(primary.rollbacks).isEqualTo(0); + + assertThat(annotationDriven.begun).isEqualTo(0); + assertThat(annotationDriven.inflight).isEqualTo(0); + assertThat(annotationDriven.commits).isEqualTo(0); + assertThat(annotationDriven.rollbacks).isEqualTo(0); + } + + @AfterTransaction + void afterTransaction() { + assertThat(primary.begun).isEqualTo(1); + assertThat(primary.inflight).isEqualTo(0); + assertThat(primary.commits).isEqualTo(0); + assertThat(primary.rollbacks).isEqualTo(1); + + assertThat(annotationDriven.begun).isEqualTo(0); + assertThat(annotationDriven.inflight).isEqualTo(0); + assertThat(annotationDriven.commits).isEqualTo(0); + assertThat(annotationDriven.rollbacks).isEqualTo(0); + } + + + @Configuration + static class Config implements TransactionManagementConfigurer { + + @Bean + @Primary + PlatformTransactionManager primary() { + return new CallCountingTransactionManager(); + } + + @Bean + @Override + public TransactionManager annotationDrivenTransactionManager() { + return new CallCountingTransactionManager(); + } + + } + +}