use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method twoTransactionsShouldSucceed.
/**
* Check that two transactions are created and committed.
*/
@Test
public void twoTransactionsShouldSucceed() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource();
tas1.register(getNameMethod, txatt);
MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource();
tas2.register(setNameMethod, 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, new TransactionAttributeSource[] { tas1, tas2 });
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
itb.setName("myName");
checkTransactionStatus(false);
verify(ptm, times(2)).commit(status);
}
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 AbstractTransactionAspectTests method cannotCommitTransaction.
/**
* Simulate failure of the underlying transaction infrastructure to commit.
* Check that the target method was invoked, but that the transaction
* infrastructure exception was thrown to the client
*/
@Test
public void cannotCommitTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = setNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
// Method m2 = getNameMethod;
// No attributes for m2
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
TransactionStatus status = mock(TransactionStatus.class);
given(ptm.getTransaction(txatt)).willReturn(status);
UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
willThrow(ex).given(ptm).commit(status);
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
String name = "new name";
try {
itb.setName(name);
fail("Shouldn't have succeeded");
} catch (UnexpectedRollbackException thrown) {
assertThat(thrown == ex).isTrue();
}
// Should have invoked target and changed name
assertThat(itb.getName() == name).isTrue();
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method cannotCreateTransaction.
/**
* Simulate a transaction infrastructure failure.
* Shouldn't invoke target method.
*/
@Test
public void cannotCreateTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
given(ptm.getTransaction(txatt)).willThrow(ex);
TestBean tb = new TestBean() {
@Override
public String getName() {
throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.getName();
fail("Shouldn't have invoked method");
} catch (CannotCreateTransactionException thrown) {
assertThat(thrown == ex).isTrue();
}
}
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");
TransactionManager actual = ti.determineTransactionManager(attribute);
assertThat(actual).isSameAs(txManager);
// Call again, should be cached
TransactionManager actual2 = ti.determineTransactionManager(attribute);
assertThat(actual2).isSameAs(txManager);
verify(beanFactory, times(1)).containsBean("fooTransactionManager");
verify(beanFactory, times(1)).getBean("fooTransactionManager", TransactionManager.class);
}
Aggregations