Search in sources :

Example 6 with CallCountingTransactionManager

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

the class TxNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() {
    ITestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertThat(ptm.begun).as("Should not have any started transactions").isEqualTo(0);
    testBean.getName();
    assertThat(ptm.lastDefinition.isReadOnly()).isTrue();
    assertThat(ptm.lastDefinition.getTimeout()).isEqualTo(5);
    assertThat(ptm.begun).as("Should have 1 started transaction").isEqualTo(1);
    assertThat(ptm.commits).as("Should have 1 committed transaction").isEqualTo(1);
    // try with non-transaction
    testBean.haveBirthday();
    assertThat(ptm.begun).as("Should not have started another transaction").isEqualTo(1);
    // try with exceptional
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> testBean.exceptional(new IllegalArgumentException("foo")));
    assertThat(ptm.begun).as("Should have another started transaction").isEqualTo(2);
    assertThat(ptm.rollbacks).as("Should have 1 rolled back transaction").isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 7 with CallCountingTransactionManager

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

the class AnnotationTransactionNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() throws Exception {
    TransactionalTestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertThat(ptm.begun).as("Should not have any started transactions").isEqualTo(0);
    testBean.findAllFoos();
    assertThat(ptm.begun).as("Should have 1 started transaction").isEqualTo(1);
    assertThat(ptm.commits).as("Should have 1 committed transaction").isEqualTo(1);
    // try with non-transaction
    testBean.doSomething();
    assertThat(ptm.begun).as("Should not have started another transaction").isEqualTo(1);
    // try with exceptional
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> testBean.exceptional(new IllegalArgumentException("foo"))).satisfies(ex -> {
        assertThat(ptm.begun).as("Should have another started transaction").isEqualTo(2);
        assertThat(ptm.rollbacks).as("Should have 1 rolled back transaction").isEqualTo(1);
    });
}
Also used : CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 8 with CallCountingTransactionManager

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

the class TransactionManagerConfiguration method transactionManager2.

@Bean
@NoSynch
public PlatformTransactionManager transactionManager2() {
    CallCountingTransactionManager tm = new CallCountingTransactionManager();
    tm.setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER);
    return tm;
}
Also used : CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Bean(org.springframework.context.annotation.Bean)

Example 9 with CallCountingTransactionManager

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

the class EnableTransactionManagementTests method spr11915TransactionManagerAsManualSingleton.

@Test
public void spr11915TransactionManagerAsManualSingleton() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr11915Config.class);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class);
    bean.saveQualifiedFoo();
    assertThat(txManager.begun).isEqualTo(1);
    assertThat(txManager.commits).isEqualTo(1);
    assertThat(txManager.rollbacks).isEqualTo(0);
    bean.saveQualifiedFooWithAttributeAlias();
    assertThat(txManager.begun).isEqualTo(2);
    assertThat(txManager.commits).isEqualTo(2);
    assertThat(txManager.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 10 with CallCountingTransactionManager

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

the class EnableTransactionManagementTests method txManagerIsResolvedCorrectlyWithTxMgmtConfigurerAndPrimaryPresent.

@Test
public void txManagerIsResolvedCorrectlyWithTxMgmtConfigurerAndPrimaryPresent() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, PrimaryTxManagerAndTxMgmtConfigurerConfig.class);
    assertThat(ctx.getBeansOfType(TransactionManager.class)).hasSize(2);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager primary = ctx.getBean("primary", CallCountingTransactionManager.class);
    CallCountingTransactionManager annotationDriven = ctx.getBean("annotationDrivenTransactionManager", CallCountingTransactionManager.class);
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(primary.begun).isEqualTo(0);
    assertThat(primary.commits).isEqualTo(0);
    assertThat(primary.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)

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