@Test public void testCommitOccurs() { EntityManager em = createContainerManagedEntityManager(); doInstantiateAndSave(em); setComplete(); endTransaction(); // Should rollback assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person")); // Now clean up the database deleteFromTables("person"); }
@Test public void testInstantiateAndSave() { EntityManager em = createContainerManagedEntityManager(); doInstantiateAndSave(em); }
@Test public void testRollbackOccurs() { EntityManager em = createContainerManagedEntityManager(); doInstantiateAndSave(em); endTransaction(); // Should rollback assertEquals("Tx must have been rolled back", 0, countRowsInTable(em, "person")); }
@Test public void testContainerEntityManagerProxyRejectsJoinTransactionWithoutTransaction() { endTransaction(); try { createContainerManagedEntityManager().joinTransaction(); fail("Should have thrown a TransactionRequiredException"); } catch (TransactionRequiredException ex) { // expected } }
@Test public void testEntityManagerProxyRejectsProgrammaticTxManagement() { try { createContainerManagedEntityManager().getTransaction(); fail("Should have thrown an IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Test public void testExceptionTranslationWithDialectFoundOnIntroducedEntityManagerInfo() throws Exception { doTestExceptionTranslationWithDialectFound(((EntityManagerFactoryInfo) entityManagerFactory).getJpaDialect()); }
protected void doInstantiateAndSave(EntityManager em) { assertEquals("Should be no people from previous transactions", 0, countRowsInTable(em, "person")); Person p = new Person(); p.setFirstName("Tony"); p.setLastName("Blair"); em.persist(p); em.flush(); assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person")); }
@Test public void testContainerEntityManagerProxyAllowsJoinTransactionInTransaction() { createContainerManagedEntityManager().joinTransaction(); }
@Test public void testExceptionTranslationWithDialectFoundOnEntityManagerFactoryBean() throws Exception { assertNotNull("Dialect must have been set", entityManagerFactoryBean.getJpaDialect()); doTestExceptionTranslationWithDialectFound(entityManagerFactoryBean); }
@Test public void testReuseInNewTransaction() { EntityManager em = createContainerManagedEntityManager(); doInstantiateAndSave(em); endTransaction(); //assertFalse(em.getTransaction().isActive()); startNewTransaction(); // Call any method: should cause automatic tx invocation assertFalse(em.contains(new Person())); //assertTrue(em.getTransaction().isActive()); doInstantiateAndSave(em); setComplete(); endTransaction(); // Should rollback assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person")); // Now clean up the database deleteFromTables("person"); }
@Test @SuppressWarnings("unchecked") public void testEntityManagerProxyIsProxy() { EntityManager em = createContainerManagedEntityManager(); assertTrue(Proxy.isProxyClass(em.getClass())); Query q = em.createQuery("select p from Person as p"); List<Person> people = q.getResultList(); assertTrue(people.isEmpty()); assertTrue("Should be open to start with", em.isOpen()); try { em.close(); fail("Close should not work on container managed EM"); } catch (IllegalStateException ex) { // OK } assertTrue(em.isOpen()); }