Search in sources :

Example 1 with CallCountingTransactionManager

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

the class Rollback method testRollbackRulesOnMethodPreventRollback.

@Test
void testRollbackRulesOnMethodPreventRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Rollback rb = (Rollback) bf.getBean("rollback");
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    assertThat(txMan.commits).isEqualTo(0);
    // Should NOT roll back on ServletException
    try {
        rb.echoException(new ServletException());
    } catch (ServletException ex) {
    }
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
}
Also used : ServletException(jakarta.servlet.ServletException) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with CallCountingTransactionManager

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

the class Rollback method testTransactionAttributeOnMethod.

@Test
void testTransactionAttributeOnMethod() throws Exception {
    BeanFactory bf = getBeanFactory();
    ITestBean test = (ITestBean) bf.getBean("test");
    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);
    assertThat(test.getAge()).as("Initial value was correct").isEqualTo(4);
    int newAge = 5;
    test.setAge(newAge);
    assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(1);
    assertThat(test.getAge()).as("New value set correctly").isEqualTo(newAge);
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 3 with CallCountingTransactionManager

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

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

Example 4 with CallCountingTransactionManager

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

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        GenericBeanDefinition bd1 = new GenericBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        GenericBeanDefinition bd2 = new GenericBeanDefinition();
        bd2.setBeanClass(DerivedTestBean.class);
        bd2.setScope("tx");
        context.registerBeanDefinition("txScopedObject2", bd2);
        context.refresh();
        CallCountingTransactionManager tm = new CallCountingTransactionManager();
        TransactionTemplate tt = new TransactionTemplate(tm);
        Set<DerivedTestBean> finallyDestroy = new HashSet<>();
        tt.execute(status -> {
            TestBean bean1 = context.getBean(TestBean.class);
            assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2.wasDestroyed()).isTrue();
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
            assertThat(bean2a).isNotSameAs(bean2);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2a.wasDestroyed()).isFalse();
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
            assertThat(bean2b).isNotSameAs(bean2);
            assertThat(bean2b).isNotSameAs(bean2a);
            Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
            tt2.execute(status2 -> {
                DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
                immediatelyDestroy.add(bean2c);
                assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2c);
                assertThat(bean2c).isNotSameAs(bean2);
                assertThat(bean2c).isNotSameAs(bean2a);
                assertThat(bean2c).isNotSameAs(bean2b);
                return null;
            });
            assertThat(immediatelyDestroy.iterator().next().wasDestroyed()).isTrue();
            assertThat(bean2b.wasDestroyed()).isFalse();
            return null;
        });
        assertThat(finallyDestroy.iterator().next().wasDestroyed()).isTrue();
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with CallCountingTransactionManager

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

the class AnnotationTransactionAttributeSourceTests method serializable.

@Test
public void serializable() throws Exception {
    TestBean1 tb = new TestBean1();
    CallCountingTransactionManager ptm = new CallCountingTransactionManager();
    AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
    TransactionInterceptor ti = new TransactionInterceptor((TransactionManager) ptm, tas);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean1.class);
    proxyFactory.addAdvice(ti);
    proxyFactory.setTarget(tb);
    ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
    proxy.getAge();
    assertThat(ptm.commits).isEqualTo(1);
    ITestBean1 serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    serializedProxy.getAge();
    Advised advised = (Advised) serializedProxy;
    TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
    CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager();
    assertThat(serializedPtm.commits).isEqualTo(2);
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) 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