Search in sources :

Example 1 with ExpectedLookupTemplate

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

the class CommonAnnotationBeanPostProcessorTests method testResourceInjectionFromJndi.

@Test
public void testResourceInjectionFromJndi() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    SimpleJndiBeanFactory resourceFactory = new SimpleJndiBeanFactory();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    TestBean tb = new TestBean();
    jndiTemplate.addObject("java:comp/env/testBean", tb);
    TestBean tb2 = new TestBean();
    jndiTemplate.addObject("java:comp/env/testBean2", tb2);
    resourceFactory.setJndiTemplate(jndiTemplate);
    bpp.setResourceFactory(resourceFactory);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class));
    ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb2);
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) SimpleJndiBeanFactory(org.springframework.jndi.support.SimpleJndiBeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with ExpectedLookupTemplate

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

the class JndiJtaTransactionManagerTests method jtaTransactionManagerWithNotCacheUserTransaction.

@Test
public void jtaTransactionManagerWithNotCacheUserTransaction() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    UserTransaction ut2 = mock(UserTransaction.class);
    given(ut2.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = new JtaTransactionManager();
    ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut));
    ptm.setCacheUserTransaction(false);
    ptm.afterPropertiesSet();
    assertThat(ptm.getUserTransaction()).isEqualTo(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertThat(ptm.getTransactionSynchronization()).isEqualTo(JtaTransactionManager.SYNCHRONIZATION_ALWAYS);
    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();
        }
    });
    ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut2));
    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();
    verify(ut2).begin();
    verify(ut2).commit();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) 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 3 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithSchemeNameAndResourceRefFalse.

@Test
public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    Object o = new Object();
    jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
    jof.setJndiName("java: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 4 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch.

@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    TestBean tb = new TestBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
    jof.setJndiName("foo");
    jof.setExpectedType(DerivedTestBean.class);
    jof.setProxyInterface(ITestBean.class);
    assertThatExceptionOfType(NamingException.class).isThrownBy(jof::afterPropertiesSet).withMessageContaining("org.springframework.beans.testfixture.beans.DerivedTestBean");
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) Test(org.junit.jupiter.api.Test)

Example 5 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithDefaultObjectAndExpectedType.

@Test
public void testLookupWithDefaultObjectAndExpectedType() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
    jof.setJndiName("myFoo");
    jof.setExpectedType(String.class);
    jof.setDefaultObject("myString");
    jof.afterPropertiesSet();
    assertThat(jof.getObject()).isEqualTo("myString");
}
Also used : ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) Test(org.junit.jupiter.api.Test)

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