Search in sources :

Example 1 with UserTransactionAdapter

use of cn.taketoday.transaction.jta.UserTransactionAdapter in project today-framework by TAKETODAY.

the class JndiJtaTransactionManagerTests method doTestJtaTransactionManagerWithDefaultJndiLookups.

private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    if (defaultUt) {
        given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    } else {
        given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    JtaTransactionManager ptm = new JtaTransactionManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    if (defaultUt) {
        jndiTemplate.addObject("java:comp/UserTransaction", ut);
    }
    jndiTemplate.addObject(tmName, tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    if (tmFound) {
        assertThat(ptm.getTransactionManager()).isEqualTo(tm);
    } else {
        assertThat(ptm.getTransactionManager()).isNull();
    }
    if (defaultUt) {
        assertThat(ptm.getUserTransaction()).isEqualTo(ut);
    } else {
        boolean condition = ptm.getUserTransaction() instanceof UserTransactionAdapter;
        assertThat(condition).isTrue();
        UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
        assertThat(uta.getTransactionManager()).isEqualTo(tm);
    }
    TransactionTemplate tt = new TransactionTemplate(ptm);
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).isTrue();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
        }
    });
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).isTrue();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
    if (defaultUt) {
        verify(ut).begin();
        verify(ut).commit();
    } else {
        verify(tm).begin();
        verify(tm).commit();
    }
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) UserTransactionAdapter(cn.taketoday.transaction.jta.UserTransactionAdapter)

Example 2 with UserTransactionAdapter

use of cn.taketoday.transaction.jta.UserTransactionAdapter in project today-infrastructure by TAKETODAY.

the class JndiJtaTransactionManagerTests method doTestJtaTransactionManagerWithDefaultJndiLookups.

private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    if (defaultUt) {
        given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    } else {
        given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    JtaTransactionManager ptm = new JtaTransactionManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    if (defaultUt) {
        jndiTemplate.addObject("java:comp/UserTransaction", ut);
    }
    jndiTemplate.addObject(tmName, tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    if (tmFound) {
        assertThat(ptm.getTransactionManager()).isEqualTo(tm);
    } else {
        assertThat(ptm.getTransactionManager()).isNull();
    }
    if (defaultUt) {
        assertThat(ptm.getUserTransaction()).isEqualTo(ut);
    } else {
        boolean condition = ptm.getUserTransaction() instanceof UserTransactionAdapter;
        assertThat(condition).isTrue();
        UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
        assertThat(uta.getTransactionManager()).isEqualTo(tm);
    }
    TransactionTemplate tt = new TransactionTemplate(ptm);
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).isTrue();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
        }
    });
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).isTrue();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
    if (defaultUt) {
        verify(ut).begin();
        verify(ut).commit();
    } else {
        verify(tm).begin();
        verify(tm).commit();
    }
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) UserTransactionAdapter(cn.taketoday.transaction.jta.UserTransactionAdapter)

Aggregations

ExpectedLookupTemplate (cn.taketoday.testfixture.jndi.ExpectedLookupTemplate)2 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)2 UserTransactionAdapter (cn.taketoday.transaction.jta.UserTransactionAdapter)2 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)2 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)2 TransactionManager (jakarta.transaction.TransactionManager)2 UserTransaction (jakarta.transaction.UserTransaction)2