Search in sources :

Example 11 with CallCountingTransactionManager

use of org.springframework.transaction.testfixture.CallCountingTransactionManager in project spring-framework by spring-projects.

the class EnableTransactionManagementTests method txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent.

@Test
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, MultiTxManagerConfig.class);
    assertThat(ctx.getBeansOfType(TransactionManager.class)).hasSize(2);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);
    CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(txManager.begun).isEqualTo(0);
    assertThat(txManager.commits).isEqualTo(0);
    assertThat(txManager.rollbacks).isEqualTo(0);
    assertThat(txManager2.begun).isEqualTo(1);
    assertThat(txManager2.commits).isEqualTo(1);
    assertThat(txManager2.rollbacks).isEqualTo(0);
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 12 with CallCountingTransactionManager

use of org.springframework.transaction.testfixture.CallCountingTransactionManager in project spring-framework by spring-projects.

the class EnableTransactionManagementTests method txManagerIsResolvedOnInvocationOfTransactionalMethod.

@Test
public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, TxManagerConfig.class);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(txManager.begun).isEqualTo(1);
    assertThat(txManager.commits).isEqualTo(1);
    assertThat(txManager.rollbacks).isEqualTo(0);
    assertThat(txManager.lastDefinition.isReadOnly()).isTrue();
    assertThat(txManager.lastDefinition.getTimeout()).isEqualTo(5);
    assertThat(((TransactionAttribute) txManager.lastDefinition).getLabels()).contains("LABEL");
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 13 with CallCountingTransactionManager

use of org.springframework.transaction.testfixture.CallCountingTransactionManager in project spring-framework by spring-projects.

the class EnableTransactionManagementTests method txManagerIsResolvedCorrectlyWithSingleTxManagerBeanAndTxMgmtConfigurer.

@Test
public void txManagerIsResolvedCorrectlyWithSingleTxManagerBeanAndTxMgmtConfigurer() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, SingleTxManagerBeanAndTxMgmtConfigurerConfig.class);
    assertThat(ctx.getBeansOfType(TransactionManager.class)).hasSize(1);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    SingleTxManagerBeanAndTxMgmtConfigurerConfig config = ctx.getBean(SingleTxManagerBeanAndTxMgmtConfigurerConfig.class);
    CallCountingTransactionManager annotationDriven = config.annotationDriven;
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(txManager.begun).isEqualTo(0);
    assertThat(txManager.commits).isEqualTo(0);
    assertThat(txManager.rollbacks).isEqualTo(0);
    assertThat(annotationDriven.begun).isEqualTo(1);
    assertThat(annotationDriven.commits).isEqualTo(1);
    assertThat(annotationDriven.rollbacks).isEqualTo(0);
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 14 with CallCountingTransactionManager

use of org.springframework.transaction.testfixture.CallCountingTransactionManager in project spring-framework by spring-projects.

the class ScheduledAndTransactionalAnnotationIntegrationTests method succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface.

@Test
void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class);
    ctx.refresh();
    // allow @Scheduled method to be called several times
    Thread.sleep(100);
    MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    assertThat(AopUtils.isJdkDynamicProxy(repository)).isTrue();
    assertThat(repository.getInvocationCount()).isGreaterThan(0);
    assertThat(txManager.commits).isGreaterThan(0);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 15 with CallCountingTransactionManager

use of org.springframework.transaction.testfixture.CallCountingTransactionManager in project spring-framework by spring-projects.

the class ScheduledAndTransactionalAnnotationIntegrationTests method succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface.

@Test
void succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface() throws InterruptedException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(Config.class, SubclassProxyTxConfig.class, RepoConfigA.class);
    ctx.refresh();
    // allow @Scheduled method to be called several times
    Thread.sleep(100);
    MyRepository repository = ctx.getBean(MyRepository.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    assertThat(AopUtils.isCglibProxy(repository)).isTrue();
    assertThat(repository.getInvocationCount()).isGreaterThan(0);
    assertThat(txManager.commits).isGreaterThan(0);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

CallCountingTransactionManager (org.springframework.transaction.testfixture.CallCountingTransactionManager)27 Test (org.junit.jupiter.api.Test)24 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 ServletException (jakarta.servlet.ServletException)2 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)1 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)1 Bean (org.springframework.context.annotation.Bean)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 AfterTransaction (org.springframework.test.context.transaction.AfterTransaction)1 NoTransactionException (org.springframework.transaction.NoTransactionException)1 TransactionAttribute (org.springframework.transaction.interceptor.TransactionAttribute)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1