From 5ed20d21b1e301280b525cdc0c602f09e4ccff83 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 1 Jul 2014 17:20:43 +0200 Subject: [PATCH] Add test This commit adds a test that reproduces the behaviour described in SPR-11915 and validates that the fix introduced in f8b6114440 works as expected. Issue: SPR-11915 --- ...ationTransactionNamespaceHandlerTests.java | 4 ++ .../EnableTransactionManagementTests.java | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java index 43f26f0839..311038d750 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java @@ -118,6 +118,10 @@ public class AnnotationTransactionNamespaceHandlerTests extends TestCase { public void saveFoo() { } + @Transactional("qualifiedTransactionManager") + public void saveQualifiedFoo() { + } + @Transactional public void exceptional(Throwable t) throws Throwable { throw t; diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java index 8a35bb7d55..3ad212dabc 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -21,6 +21,8 @@ import java.util.Map; import org.junit.Test; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -33,10 +35,13 @@ import org.springframework.transaction.annotation.AnnotationTransactionNamespace import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import javax.annotation.PostConstruct; + /** * Tests demonstrating use of @EnableTransactionManagement @Configuration classes. * * @author Chris Beams + * @author Stephane Nicoll * @since 3.1 */ public class EnableTransactionManagementTests { @@ -101,6 +106,21 @@ public class EnableTransactionManagementTests { } } + @Test + public void spr11915() { + AnnotationConfigApplicationContext ctx = + new AnnotationConfigApplicationContext(Spr11915Config.class); + + TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); + bean.saveQualifiedFoo(); + + CallCountingTransactionManager txManager = ctx + .getBean("qualifiedTransactionManager", CallCountingTransactionManager.class); + assertThat(txManager.begun, equalTo(1)); + assertThat(txManager.commits, equalTo(1)); + assertThat(txManager.rollbacks, equalTo(0)); + } + @Configuration @EnableTransactionManagement @@ -118,6 +138,25 @@ public class EnableTransactionManagementTests { static class EnableAspectJTxConfig { } + @Configuration + @EnableTransactionManagement + static class Spr11915Config { + + @Autowired + private ConfigurableApplicationContext applicationContext; + + @PostConstruct + public void initializeApp() { + applicationContext.getBeanFactory().registerSingleton( + "qualifiedTransactionManager", new CallCountingTransactionManager()); + } + + @Bean + public TransactionalTestBean testBean() { + return new TransactionalTestBean(); + } + } + @Configuration static class TxManagerConfig {