Search in sources :

Example 1 with CallCountingTransactionManager

use of cn.taketoday.testfixture.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

the class EnableTransactionManagementIntegrationTests method explicitTxManager.

@Test
void explicitTxManager() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ExplicitTxManagerConfig.class);
    FooRepository fooRepository = ctx.getBean(FooRepository.class);
    fooRepository.findAll();
    CallCountingTransactionManager txManager1 = ctx.getBean("txManager1", CallCountingTransactionManager.class);
    assertThat(txManager1.begun).isEqualTo(1);
    assertThat(txManager1.commits).isEqualTo(1);
    assertThat(txManager1.rollbacks).isEqualTo(0);
    CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
    assertThat(txManager2.begun).isEqualTo(0);
    assertThat(txManager2.commits).isEqualTo(0);
    assertThat(txManager2.rollbacks).isEqualTo(0);
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 2 with CallCountingTransactionManager

use of cn.taketoday.testfixture.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

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(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 3 with CallCountingTransactionManager

use of cn.taketoday.testfixture.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

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(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 4 with CallCountingTransactionManager

use of cn.taketoday.testfixture.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

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(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) NoTransactionException(cn.taketoday.transaction.NoTransactionException) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test)

Example 5 with CallCountingTransactionManager

use of cn.taketoday.testfixture.transaction.CallCountingTransactionManager in project today-framework by TAKETODAY.

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(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) NoTransactionException(cn.taketoday.transaction.NoTransactionException) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test)

Aggregations

CallCountingTransactionManager (cn.taketoday.testfixture.transaction.CallCountingTransactionManager)16 Test (org.junit.jupiter.api.Test)16 BeanFactory (cn.taketoday.beans.factory.BeanFactory)8 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)8 ServletException (jakarta.servlet.ServletException)4 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 NoTransactionException (cn.taketoday.transaction.NoTransactionException)2 IOException (java.io.IOException)2