|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.transaction.manager; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.context.annotation.Primary; |
| 26 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 27 | +import org.springframework.test.context.transaction.AfterTransaction; |
| 28 | +import org.springframework.transaction.PlatformTransactionManager; |
| 29 | +import org.springframework.transaction.TransactionManager; |
| 30 | +import org.springframework.transaction.annotation.TransactionManagementConfigurer; |
| 31 | +import org.springframework.transaction.annotation.Transactional; |
| 32 | +import org.springframework.transaction.testfixture.CallCountingTransactionManager; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration test that verifies the current behavior for transaction manager |
| 38 | + * lookups when one transaction manager is {@link Primary @Primary} and an |
| 39 | + * additional transaction manager is configured via the |
| 40 | + * {@link TransactionManagementConfigurer} API. |
| 41 | + * |
| 42 | + * @author Sam Brannen |
| 43 | + * @since 5.2.6 |
| 44 | + */ |
| 45 | +@SpringJUnitConfig |
| 46 | +@Transactional |
| 47 | +// TODO Update assertions once https://github.com/spring-projects/spring-framework/issues/24869 is fixed. |
| 48 | +class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + CallCountingTransactionManager primary; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + @Qualifier("annotationDrivenTransactionManager") |
| 55 | + CallCountingTransactionManager annotationDriven; |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + void transactionalTest() { |
| 60 | + assertThat(primary.begun).isEqualTo(1); |
| 61 | + assertThat(primary.inflight).isEqualTo(1); |
| 62 | + assertThat(primary.commits).isEqualTo(0); |
| 63 | + assertThat(primary.rollbacks).isEqualTo(0); |
| 64 | + |
| 65 | + assertThat(annotationDriven.begun).isEqualTo(0); |
| 66 | + assertThat(annotationDriven.inflight).isEqualTo(0); |
| 67 | + assertThat(annotationDriven.commits).isEqualTo(0); |
| 68 | + assertThat(annotationDriven.rollbacks).isEqualTo(0); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterTransaction |
| 72 | + void afterTransaction() { |
| 73 | + assertThat(primary.begun).isEqualTo(1); |
| 74 | + assertThat(primary.inflight).isEqualTo(0); |
| 75 | + assertThat(primary.commits).isEqualTo(0); |
| 76 | + assertThat(primary.rollbacks).isEqualTo(1); |
| 77 | + |
| 78 | + assertThat(annotationDriven.begun).isEqualTo(0); |
| 79 | + assertThat(annotationDriven.inflight).isEqualTo(0); |
| 80 | + assertThat(annotationDriven.commits).isEqualTo(0); |
| 81 | + assertThat(annotationDriven.rollbacks).isEqualTo(0); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Configuration |
| 86 | + static class Config implements TransactionManagementConfigurer { |
| 87 | + |
| 88 | + @Bean |
| 89 | + @Primary |
| 90 | + PlatformTransactionManager primary() { |
| 91 | + return new CallCountingTransactionManager(); |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + @Override |
| 96 | + public TransactionManager annotationDrivenTransactionManager() { |
| 97 | + return new CallCountingTransactionManager(); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments