Search in sources :

Example 16 with CallCountingTransactionManager

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

the class Rollback method testProgrammaticRollback.

@Test
void testProgrammaticRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Object bean = bf.getBean(TXMANAGER_BEAN_NAME);
    boolean condition = bean instanceof CallCountingTransactionManager;
    assertThat(condition).isTrue();
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    Rollback rb = (Rollback) bf.getBean("rollback");
    assertThat(txMan.commits).isEqualTo(0);
    rb.rollbackOnly(false);
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
    assertThat(txMan.rollbacks).isEqualTo(0);
    // Will cause rollback only
    rb.rollbackOnly(true);
    assertThat(txMan.rollbacks).isEqualTo(1);
}
Also used : CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 17 with CallCountingTransactionManager

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

the class Rollback method testRollbackRulesOnMethodCauseRollback.

/**
 * Should not roll back on servlet exception.
 */
@Test
void testRollbackRulesOnMethodCauseRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Rollback rb = (Rollback) bf.getBean("rollback");
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
    assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0);
    assertThat(txMan.commits).isEqualTo(0);
    rb.echoException(null);
    // Fires only on setters
    assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0);
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
    assertThat(txMan.rollbacks).isEqualTo(0);
    Exception ex = new Exception();
    try {
        rb.echoException(ex);
    } catch (Exception actual) {
        assertThat(actual).isEqualTo(ex);
    }
    assertThat(txMan.rollbacks).as("Transaction counts match").isEqualTo(1);
}
Also used : CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NoTransactionException(org.springframework.transaction.NoTransactionException) Test(org.junit.jupiter.api.Test)

Example 18 with CallCountingTransactionManager

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

the class EnableTransactionManagementIntegrationTests method implicitTxManager.

@Test
void implicitTxManager() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImplicitTxManagerConfig.class);
    FooRepository fooRepository = ctx.getBean(FooRepository.class);
    fooRepository.findAll();
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    assertThat(txManager.begun).isEqualTo(1);
    assertThat(txManager.commits).isEqualTo(1);
    assertThat(txManager.rollbacks).isEqualTo(0);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 19 with CallCountingTransactionManager

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

the class EnableTransactionManagementTests method txManagerIsResolvedCorrectlyWhenMultipleManagersArePresentAndOneIsPrimary.

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

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

the class EnableTransactionManagementTests method spr14322FindsOnInterfaceWithCglibProxy.

@Test
public void spr14322FindsOnInterfaceWithCglibProxy() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14322ConfigB.class);
    TransactionalTestInterface bean = ctx.getBean(TransactionalTestInterface.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    bean.saveFoo();
    bean.saveBar();
    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)

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