Search in sources :

Example 1 with MockJtaTransaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndJtaSynchronization.

@Test
public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction tx = new MockJtaTransaction();
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.getTransaction()).willReturn(tx);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNotNull(tx.getSynchronization());
    tx.getSynchronization().beforeCompletion();
    tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

TransactionManager (javax.transaction.TransactionManager)1 UserTransaction (javax.transaction.UserTransaction)1 Test (org.junit.Test)1 MockJtaTransaction (org.springframework.tests.transaction.MockJtaTransaction)1 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1