Search in sources :

Example 6 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithDefaultObjectAndExpectedTypeConversionViaBeanFactory.

@Test
public void testLookupWithDefaultObjectAndExpectedTypeConversionViaBeanFactory() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
    jof.setJndiName("myFoo");
    jof.setExpectedType(Integer.class);
    jof.setDefaultObject("5");
    jof.setBeanFactory(new DefaultListableBeanFactory());
    jof.afterPropertiesSet();
    assertThat(jof.getObject()).isEqualTo(5);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) Test(org.junit.jupiter.api.Test)

Example 7 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithExpectedTypeAndNoMatch.

@Test
public void testLookupWithExpectedTypeAndNoMatch() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", new Object()));
    jof.setJndiName("foo");
    jof.setExpectedType(String.class);
    assertThatExceptionOfType(NamingException.class).isThrownBy(jof::afterPropertiesSet).withMessageContaining("java.lang.String");
}
Also used : ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) Test(org.junit.jupiter.api.Test)

Example 8 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithProxyInterface.

@Test
public void testLookupWithProxyInterface() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    TestBean tb = new TestBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
    jof.setJndiName("foo");
    jof.setProxyInterface(ITestBean.class);
    jof.afterPropertiesSet();
    boolean condition = jof.getObject() instanceof ITestBean;
    assertThat(condition).isTrue();
    ITestBean proxy = (ITestBean) jof.getObject();
    assertThat(tb.getAge()).isEqualTo(0);
    proxy.setAge(99);
    assertThat(tb.getAge()).isEqualTo(99);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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 9 with ExpectedLookupTemplate

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

the class JndiObjectFactoryBeanTests method testLookupWithExpectedTypeAndMatch.

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

Example 10 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testSinglePersistenceUnitFromJndi.

@Test
public void testSinglePersistenceUnitFromJndi() {
    Map<String, String> persistenceUnits = new HashMap<>();
    persistenceUnits.put("Person", "pu1");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pu1", mockEmf);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceUnits(persistenceUnits);
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));
    DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
    DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson) bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
    assertThat(bean.emf).isSameAs(mockEmf);
    assertThat(bean2.emf).isSameAs(mockEmf);
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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