Search in sources :

Example 16 with CallCountingTransactionManager

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

the class ScheduledAndTransactionalAnnotationIntegrationTests method succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface.

@Test
public 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("repository is not a proxy", AopUtils.isJdkDynamicProxy(repository), is(true));
    assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0));
    assertThat("no transactions were committed", txManager.commits, greaterThan(0));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 17 with CallCountingTransactionManager

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

the class ScheduledAndTransactionalAnnotationIntegrationTests method succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface.

@Test
public 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("repository is not a proxy", AopUtils.isCglibProxy(repository), equalTo(true));
    assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0));
    assertThat("no transactions were committed", txManager.commits, greaterThan(0));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 18 with CallCountingTransactionManager

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

the class EnableTransactionManagementIntegrationTests method implicitTxManager.

@Test
public void implicitTxManager() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ImplicitTxManagerConfig.class);
    ctx.refresh();
    FooRepository fooRepository = ctx.getBean(FooRepository.class);
    fooRepository.findAll();
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    assertThat(txManager.begun, equalTo(1));
    assertThat(txManager.commits, equalTo(1));
    assertThat(txManager.rollbacks, equalTo(0));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 19 with CallCountingTransactionManager

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

the class Rollback method testProgrammaticRollback.

@Test
public void testProgrammaticRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Object bean = bf.getBean(TXMANAGER_BEAN_NAME);
    assertTrue(bean instanceof CallCountingTransactionManager);
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    Rollback rb = (Rollback) bf.getBean("rollback");
    assertEquals(0, txMan.commits);
    rb.rollbackOnly(false);
    assertEquals("Transaction counts match", 1, txMan.commits);
    assertEquals(0, txMan.rollbacks);
    // Will cause rollback only
    rb.rollbackOnly(true);
    assertEquals(1, txMan.rollbacks);
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 20 with CallCountingTransactionManager

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

the class Rollback method testRollbackRulesOnMethodCauseRollback.

/**
	 * Should not roll back on servlet exception.
	 */
@Test
public 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");
    assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
    assertEquals(0, txMan.commits);
    rb.echoException(null);
    // Fires only on setters
    assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
    assertEquals("Transaction counts match", 1, txMan.commits);
    assertEquals(0, txMan.rollbacks);
    Exception ex = new Exception();
    try {
        rb.echoException(ex);
    } catch (Exception actual) {
        assertEquals(ex, actual);
    }
    assertEquals("Transaction counts match", 1, txMan.rollbacks);
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) NoTransactionException(org.springframework.transaction.NoTransactionException) Test(org.junit.Test)

Aggregations

CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)20 Test (org.junit.Test)18 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)7 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)3 ServletException (javax.servlet.ServletException)2 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Ignore (org.junit.Ignore)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 NoTransactionException (org.springframework.transaction.NoTransactionException)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1