Search in sources :

Example 11 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testPersistenceContextsFromJndi.

@Test
public void testPersistenceContextsFromJndi() {
    EntityManager mockEm = mock(EntityManager.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    EntityManager mockEm3 = mock(EntityManager.class);
    Map<String, String> persistenceContexts = new HashMap<>();
    persistenceContexts.put("", "pc1");
    persistenceContexts.put("Person", "pc2");
    Map<String, String> extendedPersistenceContexts = new HashMap<>();
    extendedPersistenceContexts.put("", "pc3");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pc1", mockEm);
    jt.addObject("java:comp/env/pc2", mockEm2);
    jt.addObject("java:comp/env/pc3", mockEm3);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceContexts(persistenceContexts);
    bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson) bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
    DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean1.em).isSameAs(mockEm);
    assertThat(bean2.em).isSameAs(mockEm2);
    assertThat(bean3.em).isSameAs(mockEm3);
}
Also used : EntityManager(jakarta.persistence.EntityManager) 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)

Example 12 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testPersistenceUnitsFromJndi.

@Test
public void testPersistenceUnitsFromJndi() {
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);
    EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);
    Map<String, String> persistenceUnits = new HashMap<>();
    persistenceUnits.put("", "pu1");
    persistenceUnits.put("Person", "pu2");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pu1", mockEmf);
    jt.addObject("java:comp/env/pu2", mockEmf2);
    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));
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
    DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson) bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
    DefaultPrivatePersistenceContextField bean3 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPublicPersistenceContextSetter bean4 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean.emf).isSameAs(mockEmf);
    assertThat(bean2.emf).isSameAs(mockEmf2);
    assertThat(bean3.em).isNotNull();
    assertThat(bean4.em).isNotNull();
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) EntityManager(jakarta.persistence.EntityManager) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 13 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testPersistenceUnitsFromJndiWithDefaultUnit.

@Test
public void testPersistenceUnitsFromJndiWithDefaultUnit() {
    EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);
    Map<String, String> persistenceUnits = new HashMap<>();
    persistenceUnits.put("System", "pu1");
    persistenceUnits.put("Person", "pu2");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pu1", mockEmf);
    jt.addObject("java:comp/env/pu2", mockEmf2);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceUnits(persistenceUnits);
    bpp.setDefaultPersistenceUnitName("System");
    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(mockEmf2);
}
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)

Example 14 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testPersistenceContextsFromJndiWithDefaultUnit.

@Test
public void testPersistenceContextsFromJndiWithDefaultUnit() {
    EntityManager mockEm = mock(EntityManager.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    EntityManager mockEm3 = mock(EntityManager.class);
    Map<String, String> persistenceContexts = new HashMap<>();
    persistenceContexts.put("System", "pc1");
    persistenceContexts.put("Person", "pc2");
    Map<String, String> extendedPersistenceContexts = new HashMap<>();
    extendedPersistenceContexts.put("System", "pc3");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pc1", mockEm);
    jt.addObject("java:comp/env/pc2", mockEm2);
    jt.addObject("java:comp/env/pc3", mockEm3);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceContexts(persistenceContexts);
    bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
    bpp.setDefaultPersistenceUnitName("System");
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson) bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
    DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean1.em).isSameAs(mockEm);
    assertThat(bean2.em).isSameAs(mockEm2);
    assertThat(bean3.em).isSameAs(mockEm3);
}
Also used : EntityManager(jakarta.persistence.EntityManager) 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)

Example 15 with ExpectedLookupTemplate

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

the class PersistenceInjectionTests method testSinglePersistenceContextFromJndi.

@Test
public void testSinglePersistenceContextFromJndi() {
    EntityManager mockEm = mock(EntityManager.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    Map<String, String> persistenceContexts = new HashMap<>();
    persistenceContexts.put("System", "pc1");
    Map<String, String> extendedPersistenceContexts = new HashMap<>();
    extendedPersistenceContexts.put("System", "pc2");
    ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
    jt.addObject("java:comp/env/pc1", mockEm);
    jt.addObject("java:comp/env/pc2", mockEm2);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
    bpp.setPersistenceContexts(persistenceContexts);
    bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
    bpp.setJndiTemplate(jt);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
    bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
    DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField) bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
    DefaultPublicPersistenceContextSetter bean2 = (DefaultPublicPersistenceContextSetter) bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertThat(bean1.em).isSameAs(mockEm);
    assertThat(bean2.em).isSameAs(mockEm2);
}
Also used : EntityManager(jakarta.persistence.EntityManager) 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