Support TransactionManagementConfigurer in the TCF

Currently the Spring TestContext Framework looks up a
PlatformTransactionManager bean named "transactionManager". The exact
name of the bean can be overridden via @TransactionConfiguration or
@Transactional; however, the bean will always be looked up 'by name'.

The TransactionManagementConfigurer interface that was introduced in
Spring 3.1 provides a programmatic approach to specifying the
PlatformTransactionManager bean to be used for annotation-driven
transaction management, and that bean is not required to be named
"transactionManager". However, as of Spring 3.1.2, using the
TransactionManagementConfigurer on a @Configuration class has no effect
on how the TestContext framework looks up the transaction manager.
Consequently, if an explicit name or qualifier has not been specified,
the bean must be named "transactionManager" in order for a transactional
integration test to work.

This commit addresses this issue by refactoring the
TransactionalTestExecutionListener so that it looks up and delegates to
a single TransactionManagementConfigurer as part of the algorithm for
determining the transaction manager.

Issue: SPR-9604
This commit is contained in:
Sam Brannen
2012-07-28 01:24:32 +02:00
parent f21fe33e74
commit 2b7a629068
11 changed files with 155 additions and 44 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2002-2012 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
*
* http://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.junit4.spr9604;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.transaction.CallCountingTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration tests that verify the behavior requested in
* <a href="https://jira.springsource.org/browse/SPR-9604">SPR-9604</a>.
*
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class LookUpTxMgrViaTransactionManagementConfigurerTests {
private static final CallCountingTransactionManager txManager1 = new CallCountingTransactionManager();
private static final CallCountingTransactionManager txManager2 = new CallCountingTransactionManager();
@Configuration
static class Config implements TransactionManagementConfigurer {
public PlatformTransactionManager annotationDrivenTransactionManager() {
return txManager1();
}
@Bean
public PlatformTransactionManager txManager1() {
return txManager1;
}
@Bean
public PlatformTransactionManager txManager2() {
return txManager2;
}
}
@BeforeTransaction
public void beforeTransaction() {
txManager1.clear();
txManager2.clear();
}
@Test
public void transactionalTest() {
assertEquals(1, txManager1.begun);
assertEquals(1, txManager1.inflight);
assertEquals(0, txManager1.commits);
assertEquals(0, txManager1.rollbacks);
assertEquals(0, txManager2.begun);
assertEquals(0, txManager2.inflight);
assertEquals(0, txManager2.commits);
assertEquals(0, txManager2.rollbacks);
}
@AfterTransaction
public void afterTransaction() {
assertEquals(1, txManager1.begun);
assertEquals(0, txManager1.inflight);
assertEquals(0, txManager1.commits);
assertEquals(1, txManager1.rollbacks);
assertEquals(0, txManager2.begun);
assertEquals(0, txManager2.inflight);
assertEquals(0, txManager2.commits);
assertEquals(0, txManager2.rollbacks);
}
}

View File

@@ -24,8 +24,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.transaction.CallCountingTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@@ -42,7 +40,6 @@ public class LookUpNonexistentTxMgrTests {
private static final CallCountingTransactionManager txManager = new CallCountingTransactionManager();
@Configuration
static class Config {
@@ -52,26 +49,11 @@ public class LookUpNonexistentTxMgrTests {
}
}
@BeforeTransaction
public void beforeTransaction() {
txManager.clear();
}
@Test
public void lookUpNothing() {
public void nonTransactionalTest() {
assertEquals(0, txManager.begun);
assertEquals(0, txManager.inflight);
assertEquals(0, txManager.commits);
assertEquals(0, txManager.rollbacks);
}
@AfterTransaction
public void afterTransaction() {
assertEquals(0, txManager.begun);
assertEquals(0, txManager.inflight);
assertEquals(0, txManager.commits);
assertEquals(0, txManager.rollbacks);
}
}

View File

@@ -66,7 +66,7 @@ public class LookUpTxMgrByTypeAndDefaultNameTests {
}
@Test
public void lookUpByTypeAndDefaultName() {
public void transactionalTest() {
assertEquals(1, txManager1.begun);
assertEquals(1, txManager1.inflight);
assertEquals(0, txManager1.commits);

View File

@@ -68,7 +68,7 @@ public class LookUpTxMgrByTypeAndNameTests {
}
@Test
public void lookUpByTypeAndName() {
public void transactionalTest() {
assertEquals(1, txManager1.begun);
assertEquals(1, txManager1.inflight);
assertEquals(0, txManager1.commits);

View File

@@ -66,7 +66,7 @@ public class LookUpTxMgrByTypeAndQualifierAtClassLevelTests {
}
@Test
public void lookUpByTypeAndQualifier() {
public void transactionalTest() {
assertEquals(1, txManager1.begun);
assertEquals(1, txManager1.inflight);
assertEquals(0, txManager1.commits);

View File

@@ -66,7 +66,7 @@ public class LookUpTxMgrByTypeAndQualifierAtMethodLevelTests {
@Transactional("txManager1")
@Test
public void lookUpByTypeAndQualifier() {
public void transactionalTest() {
assertEquals(1, txManager1.begun);
assertEquals(1, txManager1.inflight);
assertEquals(0, txManager1.commits);

View File

@@ -59,7 +59,7 @@ public class LookUpTxMgrByTypeTests {
}
@Test
public void lookUpByType() {
public void transactionalTest() {
assertEquals(1, txManager.begun);
assertEquals(1, txManager.inflight);
assertEquals(0, txManager.commits);