use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method transactionShouldSucceed.
/**
* Check that a transaction is created and committed.
*/
@Test
public void transactionShouldSucceed() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(getNameMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
verify(ptm).commit(status);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class BeanFactoryTransactionTests method doTestGetsAreNotTransactional.
private void doTestGetsAreNotTransactional(final ITestBean testBean) {
// Install facade
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
PlatformTransactionManagerFacade.delegate = ptm;
assertTrue("Age should not be " + testBean.getAge(), testBean.getAge() == 666);
// Expect no methods
verifyZeroInteractions(ptm);
// Install facade expecting a call
final TransactionStatus ts = mock(TransactionStatus.class);
ptm = new PlatformTransactionManager() {
private boolean invoked;
@Override
public TransactionStatus getTransaction(TransactionDefinition def) throws TransactionException {
if (invoked) {
throw new IllegalStateException("getTransaction should not get invoked more than once");
}
invoked = true;
if (!(def.getName().contains(DerivedTestBean.class.getName()) && def.getName().contains("setAge"))) {
throw new IllegalStateException("transaction name should contain class and method name: " + def.getName());
}
return ts;
}
@Override
public void commit(TransactionStatus status) throws TransactionException {
assertTrue(status == ts);
}
@Override
public void rollback(TransactionStatus status) throws TransactionException {
throw new IllegalStateException("rollback should not get invoked");
}
};
PlatformTransactionManagerFacade.delegate = ptm;
// TODO same as old age to avoid ordering effect for now
int age = 666;
testBean.setAge(age);
assertTrue(testBean.getAge() == age);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class TransactionInterceptorTests method determineTransactionManagerWithQualifierSeveralTimes.
@Test
public void determineTransactionManagerWithQualifierSeveralTimes() {
BeanFactory beanFactory = mock(BeanFactory.class);
TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
attribute.setQualifier("fooTransactionManager");
PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
assertSame(txManager, actual);
// Call again, should be cached
PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
assertSame(txManager, actual2);
verify(beanFactory, times(1)).containsBean("fooTransactionManager");
verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class TransactionInterceptorTests method determineTransactionManagerWithNoBeanFactoryAndNoTransactionAttribute.
@Test
public void determineTransactionManagerWithNoBeanFactoryAndNoTransactionAttribute() {
PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, null);
assertSame(transactionManager, ti.determineTransactionManager(null));
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class TransactionInterceptorTests method determineTransactionManagerWithBeanNameSeveralTimes.
@Test
public void determineTransactionManagerWithBeanNameSeveralTimes() {
BeanFactory beanFactory = mock(BeanFactory.class);
TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("fooTransactionManager", beanFactory);
PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
assertSame(txManager, actual);
// Call again, should be cached
PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
assertSame(txManager, actual2);
verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Aggregations