diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestContainerSpecCompatibilityOptions.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestContainerSpecCompatibilityOptions.java index 73109aa4b..92d7cf2c7 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestContainerSpecCompatibilityOptions.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestContainerSpecCompatibilityOptions.java @@ -81,7 +81,7 @@ public class TestContainerSpecCompatibilityOptions assertEquals("JPA", spec.getName().toUpperCase()); assertEquals(spec.getVersion(), 1); - emf1.close(); + closeEMF(emf1); } @@ -136,7 +136,7 @@ public class TestContainerSpecCompatibilityOptions em.close(); } } finally { - oemf.close(); + closeEMF(oemf); } } @@ -205,7 +205,7 @@ public class TestContainerSpecCompatibilityOptions fail("OneToMany mapping failed with exception message: " + e.getMessage()); } finally { em.close(); - oemf.close(); + closeEMF(oemf); } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestQuerySQLCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestQuerySQLCache.java index 30691e538..b002dd3e1 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestQuerySQLCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestQuerySQLCache.java @@ -60,16 +60,19 @@ public class TestQuerySQLCache extends SingleEMFTestCase { props.put("openjpa.jdbc.QuerySQLCache", "org.apache.openjpa.persistence.compatible.TestQuerySQLCache.BadCacheMap"); + OpenJPAEntityManagerFactorySPI emf1 = null; try { - OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. + emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. cast(Persistence.createEntityManagerFactory("test", props)); // // EMF creation must throw an exception because the cache implementation class will not be found // - assertFalse(false); + fail("EMF creation must throw an exception because the cache implementation class will not be found"); } catch (Exception e) { assertTrue(true); + } finally { + closeEMF(emf1); } } @@ -108,117 +111,124 @@ public class TestQuerySQLCache extends SingleEMFTestCase { + TblParent.class.getName() + ")"); props.put("openjpa.jdbc.QuerySQLCache", "true"); - OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence. + OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence. cast(Persistence.createEntityManagerFactory("test", props)); - EntityManagerImpl em = (EntityManagerImpl)emf.createEntityManager(); - - em.getTransaction().begin(); - - for (int i = 1; i < 3; i++) { - TblParent p = new TblParent(); - p.setParentId(i); - TblChild c = new TblChild(); - c.setChildId(i); - c.setTblParent(p); - p.addTblChild(c); - em.persist(p); - em.persist(c); - - TblGrandChild gc = new TblGrandChild(); - gc.setGrandChildId(i); - gc.setTblChild(c); - c.addTblGrandChild(gc); - - em.persist(p); - em.persist(c); - em.persist(gc); - } - em.flush(); - em.getTransaction().commit(); - em.clear(); - - for (int i = 1; i < 3; i++) { - TblParent p = em.find(TblParent.class, i); - int pid = p.getParentId(); - assertEquals(pid, i); - Collection children = p.getTblChildren(); - boolean hasChild = false; - for (TblChild c : children) { - hasChild = true; - Collection gchildren = c.getTblGrandChildren(); - int cid = c.getChildId(); - assertEquals(cid, i); - boolean hasGrandChild = false; - for (TblGrandChild gc : gchildren) { - hasGrandChild = true; - int gcId = gc.getGrandChildId(); - assertEquals(gcId, i); - } - assertTrue(hasGrandChild); + try { + EntityManagerImpl em = (EntityManagerImpl)emf1.createEntityManager(); + + em.getTransaction().begin(); + + for (int i = 1; i < 3; i++) { + TblParent p = new TblParent(); + p.setParentId(i); + TblChild c = new TblChild(); + c.setChildId(i); + c.setTblParent(p); + p.addTblChild(c); + em.persist(p); + em.persist(c); + + TblGrandChild gc = new TblGrandChild(); + gc.setGrandChildId(i); + gc.setTblChild(c); + c.addTblGrandChild(gc); + + em.persist(p); + em.persist(c); + em.persist(gc); } - assertTrue(hasChild); + em.flush(); + em.getTransaction().commit(); + em.clear(); + + for (int i = 1; i < 3; i++) { + TblParent p = em.find(TblParent.class, i); + int pid = p.getParentId(); + assertEquals(pid, i); + Collection children = p.getTblChildren(); + boolean hasChild = false; + for (TblChild c : children) { + hasChild = true; + Collection gchildren = c.getTblGrandChildren(); + int cid = c.getChildId(); + assertEquals(cid, i); + boolean hasGrandChild = false; + for (TblGrandChild gc : gchildren) { + hasGrandChild = true; + int gcId = gc.getGrandChildId(); + assertEquals(gcId, i); + } + assertTrue(hasGrandChild); + } + assertTrue(hasChild); + em.close(); + } + } finally { + closeEMF(emf1); } - em.close(); - emf.close(); } private void runMultiEMCaching(Map props) { EntityManagerFactory emfac = Persistence.createEntityManagerFactory("test", props); - EntityManager em = emfac.createEntityManager(); + try { + EntityManager em = emfac.createEntityManager(); - // - // Create some entities - // - em.getTransaction().begin(); - for (int i = 0; i < nPeople; i++) { - Person p = new Person(); - p.setId(i); - em.persist(p); - } - em.flush(); - em.getTransaction().commit(); - em.close(); - - Thread[] newThreads = new Thread[nThreads]; - FindPeople[] customer = new FindPeople[nThreads]; - for (int i=0; i < nThreads; i++) { - customer[i] = new FindPeople(emfac, 0, nPeople, nIterations, i); - newThreads[i] = new Thread(customer[i]); - newThreads[i].start(); - } - - // - // Wait for the worker threads to complete - // - for (int i = 0; i < nThreads; i++) { - try { - newThreads[i].join(); + // + // Create some entities + // + em.getTransaction().begin(); + for (int i = 0; i < nPeople; i++) { + Person p = new Person(); + p.setId(i); + em.persist(p); } - catch (InterruptedException e) { - this.fail("Caught Interrupted Exception: " + e); + em.flush(); + em.getTransaction().commit(); + em.close(); + + Thread[] newThreads = new Thread[nThreads]; + FindPeople[] customer = new FindPeople[nThreads]; + for (int i=0; i < nThreads; i++) { + customer[i] = new FindPeople(emfac, 0, nPeople, nIterations, i); + newThreads[i] = new Thread(customer[i]); + newThreads[i].start(); } - } - // - // Run through the state of all runnables to assert if any of them failed. - // - for (int i = 0; i < nThreads; i++) { - assertFalse(customer[i].hadFailures()); - } + // + // Wait for the worker threads to complete + // + for (int i = 0; i < nThreads; i++) { + try { + newThreads[i].join(); + } + catch (InterruptedException e) { + this.fail("Caught Interrupted Exception: " + e); + } + } - // - // Clean up the entities used in this test - // - em = emfac.createEntityManager(); - em.getTransaction().begin(); - for (int i = 0; i < nPeople; i++) { - Person p = em.find(Person.class, i); - em.remove(p); + // + // Run through the state of all runnables to assert if any of them failed. + // + for (int i = 0; i < nThreads; i++) { + assertFalse(customer[i].hadFailures()); + } + + // + // Clean up the entities used in this test + // + em = emfac.createEntityManager(); + em.getTransaction().begin(); + for (int i = 0; i < nPeople; i++) { + Person p = em.find(Person.class, i); + em.remove(p); + } + em.flush(); + em.getTransaction().commit(); + em.close(); + } finally { + closeEMF(emfac); } - em.flush(); - em.getTransaction().commit(); - em.close(); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java index a2a0089ba..a783e9e08 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java @@ -54,18 +54,20 @@ extends AbstractCachedEMFTestCase { "org/apache/openjpa/persistence/compat/" + "persistence_1_0.xml"); - Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); - assertTrue(compat.getFlushBeforeDetach()); - assertTrue(compat.getCopyOnDetach()); - assertTrue(compat.getIgnoreDetachedStateFieldForProxySerialization()); - assertTrue(compat.getPrivatePersistentProperties()); - String vMode = emf.getConfiguration().getValidationMode(); - assertEquals("NONE", vMode); - Specification spec = emf.getConfiguration().getSpecificationInstance(); - assertEquals("JPA", spec.getName().toUpperCase()); - assertEquals(spec.getVersion(), 1); - - emf.close(); + try { + Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); + assertTrue(compat.getFlushBeforeDetach()); + assertTrue(compat.getCopyOnDetach()); + assertTrue(compat.getIgnoreDetachedStateFieldForProxySerialization()); + assertTrue(compat.getPrivatePersistentProperties()); + String vMode = emf.getConfiguration().getValidationMode(); + assertEquals("NONE", vMode); + Specification spec = emf.getConfiguration().getSpecificationInstance(); + assertEquals("JPA", spec.getName().toUpperCase()); + assertEquals(spec.getVersion(), 1); + } finally { + closeEMF(emf); + } } /* @@ -79,18 +81,20 @@ extends AbstractCachedEMFTestCase { "org/apache/openjpa/persistence/compat/" + "persistence_2_0.xml"); - Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); - assertFalse(compat.getFlushBeforeDetach()); - assertFalse(compat.getCopyOnDetach()); - assertFalse(compat.getIgnoreDetachedStateFieldForProxySerialization()); - assertFalse(compat.getPrivatePersistentProperties()); - String vMode = emf.getConfiguration().getValidationMode(); - assertEquals("AUTO", vMode); - Specification spec = emf.getConfiguration().getSpecificationInstance(); - assertEquals("JPA", spec.getName().toUpperCase()); - assertEquals(spec.getVersion(), 2); - - emf.close(); + try { + Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); + assertFalse(compat.getFlushBeforeDetach()); + assertFalse(compat.getCopyOnDetach()); + assertFalse(compat.getIgnoreDetachedStateFieldForProxySerialization()); + assertFalse(compat.getPrivatePersistentProperties()); + String vMode = emf.getConfiguration().getValidationMode(); + assertEquals("AUTO", vMode); + Specification spec = emf.getConfiguration().getSpecificationInstance(); + assertEquals("JPA", spec.getName().toUpperCase()); + assertEquals(spec.getVersion(), 2); + } finally { + closeEMF(emf); + } } /* @@ -125,7 +129,7 @@ extends AbstractCachedEMFTestCase { em.close(); } } finally { - emf.close(); + closeEMF(emf); } } @@ -194,7 +198,7 @@ extends AbstractCachedEMFTestCase { fail("OneToMany mapping failed with exception message: " + e.getMessage()); } finally { em.close(); - emf.close(); + closeEMF(emf); } }