make sure tests cleanup EMs and EMFs

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1006038 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2010-10-08 21:46:25 +00:00
parent a0fad7795b
commit 044164b4ad
3 changed files with 142 additions and 128 deletions

View File

@ -81,7 +81,7 @@ public class TestContainerSpecCompatibilityOptions
assertEquals("JPA", spec.getName().toUpperCase()); assertEquals("JPA", spec.getName().toUpperCase());
assertEquals(spec.getVersion(), 1); assertEquals(spec.getVersion(), 1);
emf1.close(); closeEMF(emf1);
} }
@ -136,7 +136,7 @@ public class TestContainerSpecCompatibilityOptions
em.close(); em.close();
} }
} finally { } finally {
oemf.close(); closeEMF(oemf);
} }
} }
@ -205,7 +205,7 @@ public class TestContainerSpecCompatibilityOptions
fail("OneToMany mapping failed with exception message: " + e.getMessage()); fail("OneToMany mapping failed with exception message: " + e.getMessage());
} finally { } finally {
em.close(); em.close();
oemf.close(); closeEMF(oemf);
} }
} }

View File

@ -60,16 +60,19 @@ public class TestQuerySQLCache extends SingleEMFTestCase {
props.put("openjpa.jdbc.QuerySQLCache", props.put("openjpa.jdbc.QuerySQLCache",
"org.apache.openjpa.persistence.compatible.TestQuerySQLCache.BadCacheMap"); "org.apache.openjpa.persistence.compatible.TestQuerySQLCache.BadCacheMap");
OpenJPAEntityManagerFactorySPI emf1 = null;
try { try {
OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
cast(Persistence.createEntityManagerFactory("test", props)); cast(Persistence.createEntityManagerFactory("test", props));
// //
// EMF creation must throw an exception because the cache implementation class will not be found // 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) { catch (Exception e) {
assertTrue(true); assertTrue(true);
} finally {
closeEMF(emf1);
} }
} }
@ -108,117 +111,124 @@ public class TestQuerySQLCache extends SingleEMFTestCase {
+ TblParent.class.getName() + ")"); + TblParent.class.getName() + ")");
props.put("openjpa.jdbc.QuerySQLCache", "true"); props.put("openjpa.jdbc.QuerySQLCache", "true");
OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence. OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.
cast(Persistence.createEntityManagerFactory("test", props)); cast(Persistence.createEntityManagerFactory("test", props));
EntityManagerImpl em = (EntityManagerImpl)emf.createEntityManager(); try {
EntityManagerImpl em = (EntityManagerImpl)emf1.createEntityManager();
em.getTransaction().begin();
em.getTransaction().begin();
for (int i = 1; i < 3; i++) {
TblParent p = new TblParent(); for (int i = 1; i < 3; i++) {
p.setParentId(i); TblParent p = new TblParent();
TblChild c = new TblChild(); p.setParentId(i);
c.setChildId(i); TblChild c = new TblChild();
c.setTblParent(p); c.setChildId(i);
p.addTblChild(c); c.setTblParent(p);
em.persist(p); p.addTblChild(c);
em.persist(c); em.persist(p);
em.persist(c);
TblGrandChild gc = new TblGrandChild();
gc.setGrandChildId(i); TblGrandChild gc = new TblGrandChild();
gc.setTblChild(c); gc.setGrandChildId(i);
c.addTblGrandChild(gc); gc.setTblChild(c);
c.addTblGrandChild(gc);
em.persist(p);
em.persist(c); em.persist(p);
em.persist(gc); 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<TblChild> children = p.getTblChildren();
boolean hasChild = false;
for (TblChild c : children) {
hasChild = true;
Collection<TblGrandChild> 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.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<TblChild> children = p.getTblChildren();
boolean hasChild = false;
for (TblChild c : children) {
hasChild = true;
Collection<TblGrandChild> 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) { private void runMultiEMCaching(Map props) {
EntityManagerFactory emfac = Persistence.createEntityManagerFactory("test", props); EntityManagerFactory emfac = Persistence.createEntityManagerFactory("test", props);
EntityManager em = emfac.createEntityManager(); try {
EntityManager em = emfac.createEntityManager();
// //
// Create some entities // Create some entities
// //
em.getTransaction().begin(); em.getTransaction().begin();
for (int i = 0; i < nPeople; i++) { for (int i = 0; i < nPeople; i++) {
Person p = new Person(); Person p = new Person();
p.setId(i); p.setId(i);
em.persist(p); 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();
} }
catch (InterruptedException e) { em.flush();
this.fail("Caught Interrupted Exception: " + e); 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. // Wait for the worker threads to complete
// //
for (int i = 0; i < nThreads; i++) { for (int i = 0; i < nThreads; i++) {
assertFalse(customer[i].hadFailures()); try {
} newThreads[i].join();
}
catch (InterruptedException e) {
this.fail("Caught Interrupted Exception: " + e);
}
}
// //
// Clean up the entities used in this test // Run through the state of all runnables to assert if any of them failed.
// //
em = emfac.createEntityManager(); for (int i = 0; i < nThreads; i++) {
em.getTransaction().begin(); assertFalse(customer[i].hadFailures());
for (int i = 0; i < nPeople; i++) { }
Person p = em.find(Person.class, i);
em.remove(p); //
// 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();
} }

View File

@ -54,18 +54,20 @@ extends AbstractCachedEMFTestCase {
"org/apache/openjpa/persistence/compat/" + "org/apache/openjpa/persistence/compat/" +
"persistence_1_0.xml"); "persistence_1_0.xml");
Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); try {
assertTrue(compat.getFlushBeforeDetach()); Compatibility compat = emf.getConfiguration().getCompatibilityInstance();
assertTrue(compat.getCopyOnDetach()); assertTrue(compat.getFlushBeforeDetach());
assertTrue(compat.getIgnoreDetachedStateFieldForProxySerialization()); assertTrue(compat.getCopyOnDetach());
assertTrue(compat.getPrivatePersistentProperties()); assertTrue(compat.getIgnoreDetachedStateFieldForProxySerialization());
String vMode = emf.getConfiguration().getValidationMode(); assertTrue(compat.getPrivatePersistentProperties());
assertEquals("NONE", vMode); String vMode = emf.getConfiguration().getValidationMode();
Specification spec = emf.getConfiguration().getSpecificationInstance(); assertEquals("NONE", vMode);
assertEquals("JPA", spec.getName().toUpperCase()); Specification spec = emf.getConfiguration().getSpecificationInstance();
assertEquals(spec.getVersion(), 1); assertEquals("JPA", spec.getName().toUpperCase());
assertEquals(spec.getVersion(), 1);
emf.close(); } finally {
closeEMF(emf);
}
} }
/* /*
@ -79,18 +81,20 @@ extends AbstractCachedEMFTestCase {
"org/apache/openjpa/persistence/compat/" + "org/apache/openjpa/persistence/compat/" +
"persistence_2_0.xml"); "persistence_2_0.xml");
Compatibility compat = emf.getConfiguration().getCompatibilityInstance(); try {
assertFalse(compat.getFlushBeforeDetach()); Compatibility compat = emf.getConfiguration().getCompatibilityInstance();
assertFalse(compat.getCopyOnDetach()); assertFalse(compat.getFlushBeforeDetach());
assertFalse(compat.getIgnoreDetachedStateFieldForProxySerialization()); assertFalse(compat.getCopyOnDetach());
assertFalse(compat.getPrivatePersistentProperties()); assertFalse(compat.getIgnoreDetachedStateFieldForProxySerialization());
String vMode = emf.getConfiguration().getValidationMode(); assertFalse(compat.getPrivatePersistentProperties());
assertEquals("AUTO", vMode); String vMode = emf.getConfiguration().getValidationMode();
Specification spec = emf.getConfiguration().getSpecificationInstance(); assertEquals("AUTO", vMode);
assertEquals("JPA", spec.getName().toUpperCase()); Specification spec = emf.getConfiguration().getSpecificationInstance();
assertEquals(spec.getVersion(), 2); assertEquals("JPA", spec.getName().toUpperCase());
assertEquals(spec.getVersion(), 2);
emf.close(); } finally {
closeEMF(emf);
}
} }
/* /*
@ -125,7 +129,7 @@ extends AbstractCachedEMFTestCase {
em.close(); em.close();
} }
} finally { } finally {
emf.close(); closeEMF(emf);
} }
} }
@ -194,7 +198,7 @@ extends AbstractCachedEMFTestCase {
fail("OneToMany mapping failed with exception message: " + e.getMessage()); fail("OneToMany mapping failed with exception message: " + e.getMessage());
} finally { } finally {
em.close(); em.close();
emf.close(); closeEMF(emf);
} }
} }