Search in sources :

Example 26 with ExpectedLookupTemplate

use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.

the class JndiObjectFactoryBeanTests method testLookupWithArbitraryNameAndResourceRefFalse.

@Test
public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    Object o = new Object();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o));
    jof.setJndiName("foo");
    jof.setResourceRef(false);
    jof.afterPropertiesSet();
    assertThat(jof.getObject() == o).isTrue();
}
Also used : ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) Test(org.junit.jupiter.api.Test)

Example 27 with ExpectedLookupTemplate

use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.

the class JndiJtaTransactionManagerTests method jtaTransactionManagerWithCustomJndiLookups.

@Test
public void jtaTransactionManagerWithCustomJndiLookups() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    TransactionManager tm = mock(TransactionManager.class);
    JtaTransactionManager ptm = new JtaTransactionManager();
    ptm.setUserTransactionName("jndi-ut");
    ptm.setTransactionManagerName("jndi-tm");
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    jndiTemplate.addObject("jndi-ut", ut);
    jndiTemplate.addObject("jndi-tm", tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    assertThat(ptm.getUserTransaction()).isEqualTo(ut);
    assertThat(ptm.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();
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 28 with ExpectedLookupTemplate

use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.

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(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) UserTransactionAdapter(org.springframework.transaction.jta.UserTransactionAdapter)

Aggregations

ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)28 Test (org.junit.jupiter.api.Test)27 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 HashMap (java.util.HashMap)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 TestBean (org.springframework.beans.testfixture.beans.TestBean)5 EntityManager (jakarta.persistence.EntityManager)4 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)4 UserTransaction (jakarta.transaction.UserTransaction)3 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 TransactionManager (jakarta.transaction.TransactionManager)2 NamingException (javax.naming.NamingException)1 INestedTestBean (org.springframework.beans.testfixture.beans.INestedTestBean)1 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)1 SimpleJndiBeanFactory (org.springframework.jndi.support.SimpleJndiBeanFactory)1 UserTransactionAdapter (org.springframework.transaction.jta.UserTransactionAdapter)1