public LocalContainerEntityManagerFactoryBean parseValidPersistenceUnit() throws Exception { LocalContainerEntityManagerFactoryBean emfb = createEntityManagerFactoryBean( "org/springframework/orm/jpa/domain/persistence.xml", null, "Person"); return emfb; }
@Test public void testValidPersistenceUnit() throws Exception { parseValidPersistenceUnit(); }
@Test public void testApplicationManagedEntityManagerWithoutTransaction() throws Exception { Object testEntity = new Object(); EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(mockEm); LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); EntityManager em = emf.createEntityManager(); assertFalse(em.contains(testEntity)); cefb.destroy(); verify(mockEmf).close(); }
@Test public void testInvalidPersistenceUnitName() throws Exception { try { createEntityManagerFactoryBean("org/springframework/orm/jpa/domain/persistence.xml", null, "call me Bob"); fail("Should not create factory with this name"); } catch (IllegalArgumentException ex) { // Ok } }
@Test public void testExceptionTranslationWithNoDialect() throws Exception { LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); cefb.getObject(); assertNull("No dialect set", cefb.getJpaDialect()); RuntimeException in1 = new RuntimeException("in1"); PersistenceException in2 = new PersistenceException(); assertNull("No translation here", cefb.translateExceptionIfPossible(in1)); DataAccessException dex = cefb.translateExceptionIfPossible(in2); assertNotNull(dex); assertSame(in2, dex.getCause()); }
@Test public void testEntityManagerFactoryIsProxied() throws Exception { LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); assertTrue(emf.equals(emf)); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setSerializationId("emf-bf"); bf.registerSingleton("emf", cefb); cefb.setBeanFactory(bf); cefb.setBeanName("emf"); assertNotNull(SerializationTestUtils.serializeAndDeserialize(emf)); }
@Test public void testApplicationManagedEntityManagerWithTransaction() throws Exception { Object testEntity = new Object(); EntityTransaction mockTx = mock(EntityTransaction.class); // This one's for the tx (shared) EntityManager sharedEm = mock(EntityManager.class); given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction()); // This is the application-specific one EntityManager mockEm = mock(EntityManager.class); given(mockEm.getTransaction()).willReturn(mockTx); given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm); LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); JpaTransactionManager jpatm = new JpaTransactionManager(); jpatm.setEntityManagerFactory(cefb.getObject()); TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute()); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); EntityManager em = emf.createEntityManager(); em.joinTransaction(); assertFalse(em.contains(testEntity)); jpatm.commit(txStatus); cefb.destroy(); verify(mockTx).begin(); verify(mockTx).commit(); verify(mockEm).contains(testEntity); verify(mockEmf).close(); }
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
@Test public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception { Object testEntity = new Object(); // This one's for the tx (shared) EntityManager sharedEm = mock(EntityManager.class); given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction()); // This is the application-specific one EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm); LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo()); pui.setTransactionType(PersistenceUnitTransactionType.JTA); JpaTransactionManager jpatm = new JpaTransactionManager(); jpatm.setEntityManagerFactory(cefb.getObject()); TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute()); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); EntityManager em = emf.createEntityManager(); em.joinTransaction(); assertFalse(em.contains(testEntity)); jpatm.commit(txStatus); cefb.destroy(); verify(mockEm).joinTransaction(); verify(mockEm).contains(testEntity); verify(mockEmf).close(); }