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());
}
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();
}
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();
}
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());
}
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();
}
Aggregations