Search in sources :

Example 16 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class PersistenceContextTransactionTests method setUp.

@Before
public void setUp() throws Exception {
    factory = mock(EntityManagerFactory.class);
    manager = mock(EntityManager.class);
    tx = mock(EntityTransaction.class);
    JpaTransactionManager tm = new JpaTransactionManager(factory);
    tt = new TransactionTemplate(tm);
    given(factory.createEntityManager()).willReturn(manager);
    given(manager.getTransaction()).willReturn(tx);
    given(manager.isOpen()).willReturn(true);
    bean = new EntityManagerHoldingBean();
    @SuppressWarnings("serial") PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {

        @Override
        protected EntityManagerFactory findEntityManagerFactory(String unitName, String requestingBeanName) {
            return factory;
        }
    };
    pabpp.postProcessPropertyValues(null, null, bean, "bean");
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Before(org.junit.Before)

Example 17 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPublicSpecificExtendedPersistenceContextSetter.

@Test
public void testPublicSpecificExtendedPersistenceContextSetter() throws Exception {
    EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    given(mockEmf2.createEntityManager()).willReturn(mockEm2);
    GenericApplicationContext gac = new GenericApplicationContext();
    gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
    gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2);
    gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
    gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class));
    gac.refresh();
    SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean(SpecificPublicPersistenceContextSetter.class.getName());
    assertNotNull(bean.getEntityManager());
    bean.getEntityManager().flush();
    verify(mockEm2).getTransaction();
    verify(mockEm2).flush();
}
Also used : EntityManager(javax.persistence.EntityManager) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 18 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerFactoryTests method testValidUsage.

@Test
public void testValidUsage() {
    Object o = new Object();
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.isOpen()).willReturn(true);
    EntityManagerFactory mockEmf = mock(EntityManagerFactory.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);
    SharedEntityManagerBean proxyFactoryBean = new SharedEntityManagerBean();
    proxyFactoryBean.setEntityManagerFactory(mockEmf);
    proxyFactoryBean.afterPropertiesSet();
    assertTrue(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType()));
    assertTrue(proxyFactoryBean.isSingleton());
    EntityManager proxy = proxyFactoryBean.getObject();
    assertSame(proxy, proxyFactoryBean.getObject());
    assertFalse(proxy.contains(o));
    assertTrue(proxy instanceof EntityManagerProxy);
    EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
    try {
        emProxy.getTargetEntityManager();
        fail("Should have thrown IllegalStateException outside of transaction");
    } catch (IllegalStateException ex) {
    // expected
    }
    TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
    try {
        assertSame(mockEm, emProxy.getTargetEntityManager());
    } finally {
        TransactionSynchronizationManager.unbindResource(mockEmf);
    }
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(mockEm).contains(o);
    verify(mockEm).close();
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) EntityManagerProxy(org.springframework.orm.jpa.EntityManagerProxy) Test(org.junit.Test)

Example 19 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnRemove.

@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnRemove() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.remove(new Object());
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Example 20 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnJoinTransaction.

@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnJoinTransaction() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.joinTransaction();
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)165 Test (org.junit.Test)72 EntityManager (javax.persistence.EntityManager)55 HashMap (java.util.HashMap)21 EJBException (javax.ejb.EJBException)19 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 JMSException (javax.jms.JMSException)15 Map (java.util.Map)14 RemoteException (java.rmi.RemoteException)11 InitialContext (javax.naming.InitialContext)11 EntityTransaction (javax.persistence.EntityTransaction)10 PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)10 NamingException (javax.naming.NamingException)8 PersistenceProvider (javax.persistence.spi.PersistenceProvider)7 BeanContext (org.apache.openejb.BeanContext)7 Item (org.hibernate.jpa.test.Item)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6