make sure tests close EMs and EMFs

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1022228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2010-10-13 18:35:46 +00:00
parent 71ae858f91
commit 1a3d16a013
5 changed files with 27 additions and 12 deletions

View File

@ -24,19 +24,15 @@ import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Persistence; import javax.persistence.Persistence;
import junit.framework.TestCase;
import org.apache.openjpa.jdbc.meta.MappingRepository; import org.apache.openjpa.jdbc.meta.MappingRepository;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
/** /**
* Testcase that verifies the names for Foreign Key columns is as expected. * Testcase that verifies the names for Foreign Key columns is as expected.
*/ */
public class TestFKColumnNames extends TestCase { public class TestFKColumnNames extends AbstractPersistenceTestCase {
OpenJPAEntityManagerFactorySPI emf =
(OpenJPAEntityManagerFactorySPI) Persistence
.createEntityManagerFactory("test");
/** /**
* <P> * <P>
@ -49,6 +45,9 @@ public class TestFKColumnNames extends TestCase {
* </P> * </P>
*/ */
public void testSQLKeywords() { public void testSQLKeywords() {
OpenJPAEntityManagerFactorySPI emf =
(OpenJPAEntityManagerFactorySPI) Persistence
.createEntityManagerFactory("test");
MappingRepository repos = MappingRepository repos =
(MappingRepository) emf.getConfiguration() (MappingRepository) emf.getConfiguration()
.getMetaDataRepositoryInstance(); .getMetaDataRepositoryInstance();
@ -58,6 +57,7 @@ public class TestFKColumnNames extends TestCase {
assertEquals("FROM_ID", repos.getMapping(Inner2.class, null, true) assertEquals("FROM_ID", repos.getMapping(Inner2.class, null, true)
.getFieldMapping("from").getColumns()[0].getName()); .getFieldMapping("from").getColumns()[0].getName());
closeEMF(emf);
} }
@Entity @Entity

View File

@ -27,28 +27,32 @@ import org.apache.openjpa.persistence.test.PersistenceTestCase;
public class TestMixedAccess extends AbstractCachedEMFTestCase { public class TestMixedAccess extends AbstractCachedEMFTestCase {
public void testMixedAccessEntityError() { public void testMixedAccessEntityError() {
EntityManagerFactory emf = null;
try { try {
EntityManagerFactory emf = emf = createEMF(UnenhancedMixedAccess.class, "openjpa.RuntimeUnenhancedClasses", "supported");
createEMF(UnenhancedMixedAccess.class, "openjpa.RuntimeUnenhancedClasses", "supported");
emf.createEntityManager().close(); emf.createEntityManager().close();
} catch (RuntimeException e) { } catch (RuntimeException e) {
String msg = e.getMessage(); String msg = e.getMessage();
if (!(msg.contains("UnenhancedMixedAccess.id") && if (!(msg.contains("UnenhancedMixedAccess.id") &&
msg.contains("UnenhancedMixedAccess.getStringField"))) msg.contains("UnenhancedMixedAccess.getStringField")))
throw e; throw e;
} finally {
closeEMF(emf);
} }
} }
public void testInappropriateTransientError() { public void testInappropriateTransientError() {
EntityManagerFactory emf = null;
try { try {
EntityManagerFactory emf = createEMF( emf = createEMF(UnenhancedInappropriateTransient.class, "openjpa.RuntimeUnenhancedClasses", "supported");
UnenhancedInappropriateTransient.class, "openjpa.RuntimeUnenhancedClasses", "supported");
emf.createEntityManager().close(); emf.createEntityManager().close();
} catch (RuntimeException e) { } catch (RuntimeException e) {
String msg = e.getMessage(); String msg = e.getMessage();
if (!(msg.contains("UnenhancedInappropriateTransient.id") && if (!(msg.contains("UnenhancedInappropriateTransient.id") &&
msg.contains("UnenhancedInappropriateTransient.prePersist"))) msg.contains("UnenhancedInappropriateTransient.prePersist")))
throw e; throw e;
} } finally {
closeEMF(emf);
}
} }
} }

View File

@ -63,6 +63,7 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
em.merge(pc); em.merge(pc);
em.getTransaction().commit(); em.getTransaction().commit();
assertVersionEquals(new Number[]{2,2, 2.0f}, em.getVersion(pc)); assertVersionEquals(new Number[]{2,2, 2.0f}, em.getVersion(pc));
em.close();
} }
public void testConcurrentOptimisticUpdateFailsForSingleTable() { public void testConcurrentOptimisticUpdateFailsForSingleTable() {
@ -84,6 +85,7 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
pc1.setName("Updated in em1"); pc1.setName("Updated in em1");
pc2.setName("Updated in em2"); pc2.setName("Updated in em2");
em1.getTransaction().commit(); em1.getTransaction().commit();
em1.close();
try { try {
em2.getTransaction().commit(); em2.getTransaction().commit();
@ -111,7 +113,9 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
assertVersionEquals(em1.getVersion(pc1), em2.getVersion(pc2)); assertVersionEquals(em1.getVersion(pc1), em2.getVersion(pc2));
em1.getTransaction().commit(); em1.getTransaction().commit();
em1.close();
em2.getTransaction().commit(); em2.getTransaction().commit();
em2.close();
} }
public void testVersionOnPersistAndUpdateForMultiTable() { public void testVersionOnPersistAndUpdateForMultiTable() {
@ -128,6 +132,7 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
em.merge(pc); em.merge(pc);
em.getTransaction().commit(); em.getTransaction().commit();
assertVersionEquals(new Number[]{2,2,2,2}, em.getVersion(pc)); assertVersionEquals(new Number[]{2,2,2,2}, em.getVersion(pc));
em.close();
} }
public void testConcurrentOptimisticUpdateFailsForMultiTable() { public void testConcurrentOptimisticUpdateFailsForMultiTable() {
@ -150,6 +155,7 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
pc1.setName("Updated in em1"); pc1.setName("Updated in em1");
pc2.setName("Updated in em2"); pc2.setName("Updated in em2");
em1.getTransaction().commit(); em1.getTransaction().commit();
em1.close();
try { try {
em2.getTransaction().commit(); em2.getTransaction().commit();
@ -178,7 +184,9 @@ public class TestMultiColumnVersion extends SingleEMFTestCase {
assertVersionEquals(em1.getVersion(pc1), em2.getVersion(pc2)); assertVersionEquals(em1.getVersion(pc1), em2.getVersion(pc2));
em1.getTransaction().commit(); em1.getTransaction().commit();
em1.close();
em2.getTransaction().commit(); em2.getTransaction().commit();
em2.close();
} }
static void assertVersionEquals(Object expected, Object actual) { static void assertVersionEquals(Object expected, Object actual) {

View File

@ -346,4 +346,5 @@ public class TestNonstandardMappingAnnotations
} }
em.close(); em.close();
}} }
}

View File

@ -47,6 +47,7 @@ public class TestPropertyAccess
assertNotNull(pa1_2); assertNotNull(pa1_2);
assertEquals(10, pa1_2.getId()); assertEquals(10, pa1_2.getId());
assertEquals("foo", pa1_2.getName()); assertEquals("foo", pa1_2.getName());
em.close();
} }
public void testPropertyAccessBasicMutation() { public void testPropertyAccessBasicMutation() {
@ -70,6 +71,7 @@ public class TestPropertyAccess
PropertyAccess1 pa1_3 = em.find(PropertyAccess1.class, 10); PropertyAccess1 pa1_3 = em.find(PropertyAccess1.class, 10);
assertNotSame(pa1_2, pa1_3); assertNotSame(pa1_2, pa1_3);
assertEquals("foobar", pa1_3.getName()); assertEquals("foobar", pa1_3.getName());
em.close();
} }
public void testJPQL() { public void testJPQL() {