Search in sources :

Example 1 with ExpectedLookupTemplate

use of cn.taketoday.testfixture.jndi.ExpectedLookupTemplate in project today-infrastructure by TAKETODAY.

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);
    StandardBeanFactory bf = new StandardBeanFactory();
    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) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with ExpectedLookupTemplate

use of cn.taketoday.testfixture.jndi.ExpectedLookupTemplate in project today-infrastructure by TAKETODAY.

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);
    StandardBeanFactory bf = new StandardBeanFactory();
    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) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 3 with ExpectedLookupTemplate

use of cn.taketoday.testfixture.jndi.ExpectedLookupTemplate in project today-infrastructure by TAKETODAY.

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);
    StandardBeanFactory bf = new StandardBeanFactory();
    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) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 4 with ExpectedLookupTemplate

use of cn.taketoday.testfixture.jndi.ExpectedLookupTemplate in project today-infrastructure by TAKETODAY.

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);
    StandardBeanFactory bf = new StandardBeanFactory();
    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) ExpectedLookupTemplate(cn.taketoday.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 5 with ExpectedLookupTemplate

use of cn.taketoday.testfixture.jndi.ExpectedLookupTemplate in project today-infrastructure by TAKETODAY.

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(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) Test(org.junit.jupiter.api.Test)

Aggregations

ExpectedLookupTemplate (cn.taketoday.testfixture.jndi.ExpectedLookupTemplate)56 Test (org.junit.jupiter.api.Test)54 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)16 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)14 HashMap (java.util.HashMap)12 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)10 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)8 EntityManager (jakarta.persistence.EntityManager)8 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)6 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)6 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)6 UserTransaction (jakarta.transaction.UserTransaction)6 TransactionManager (jakarta.transaction.TransactionManager)4 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)2 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)2 SimpleJndiBeanFactory (cn.taketoday.jndi.support.SimpleJndiBeanFactory)2 UserTransactionAdapter (cn.taketoday.transaction.jta.UserTransactionAdapter)2 NamingException (javax.naming.NamingException)2