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);
}
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);
}
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);
}
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();
}
}
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);
}
Aggregations