From 0f5c5291aef2e1bbf836ca638a7d01be1459560b Mon Sep 17 00:00:00 2001 From: Donald Woods Date: Mon, 18 Oct 2010 17:03:57 +0000 Subject: [PATCH] some testcase cleanup of closing EMs and EMFs git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1023905 13f79535-47bb-0310-9956-ffa450edef68 --- .../validation/TestConstraints.java | 217 ++++-------------- .../validation/TestMappedSuper.java | 14 +- .../validation/TestValidationGroups.java | 31 ++- 3 files changed, 61 insertions(+), 201 deletions(-) diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java index 6f6e80e00..01103e94e 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java @@ -29,7 +29,7 @@ import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAPersistence; -import org.apache.openjpa.persistence.test.PersistenceTestCase; +import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; /** * Tests the new Bean Validation constraint support in the JPA 2.0 spec by @@ -75,13 +75,13 @@ import org.apache.openjpa.persistence.test.PersistenceTestCase; * * @version $Rev$ $Date$ */ -public class TestConstraints extends PersistenceTestCase { +public class TestConstraints extends AbstractPersistenceTestCase { private static OpenJPAEntityManagerFactorySPI emf = null; @Override - public void setUp() { - + public void setUp() throws Exception { + super.setUp(); emf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.createEntityManagerFactory( "ConstraintPU", @@ -89,10 +89,9 @@ public class TestConstraints extends PersistenceTestCase { } @Override - public void tearDown() { - if (emf != null) { - cleanup(emf); - } + public void tearDown() throws Exception { + closeEMF(emf); + super.tearDown(); } /** @@ -126,11 +125,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testNullUpdateConstraint() Part 1 of 2 failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } // Part 2 - Verify that invalid properties are caught on an update @@ -151,11 +146,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testNullUpdateConstraint() Part 2 of 2 passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -194,10 +185,8 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testNullDeleteIgnored() Part 1 of 2 failed"); fail("Caught unexpected exception = " + e); } finally { - if (em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - cleanup(emf); + closeEM(em); + closeEMF(emf); } // Part 2 - Verify delete using default group does not cause Validation @@ -227,10 +216,8 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testNullDeleteIgnored() Part 2 of 2 failed"); fail("Caught unexpected exception = " + e); } finally { - if (em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -267,10 +254,8 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testNullConstraintIgnored() failed"); fail("Caught unexpected exception = " + e); } finally { - if (em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -302,11 +287,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testNullConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -338,11 +319,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testNotNullConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -373,11 +350,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testNullNotNullConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -409,11 +382,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testAssertTrueConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -445,11 +414,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testAssertFalseConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -480,11 +445,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testAssertTrueFalseConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -516,11 +477,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDecimalMinConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -552,11 +509,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDecimalMaxConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -587,11 +540,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testDecimalMinMaxConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -623,11 +572,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testMinConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -659,11 +604,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testMaxConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -694,11 +635,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testMinMaxConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -730,11 +667,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDigitsTwoConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -766,11 +699,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDigitsFiveConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -801,11 +730,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testDigitsConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -837,11 +762,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testSizeStringConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -873,11 +794,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testSizeMapConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -908,11 +825,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testSizeConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -944,11 +857,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDatesFutureConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -980,11 +889,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testDatesPastConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1015,11 +920,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testDatesConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1051,11 +952,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testPatternAlphaConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1087,11 +984,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("testPatternNumericConstraint() passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1122,11 +1015,7 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testPatternConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1170,11 +1059,8 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testValidFailuresConstraint() Part 1 of 2 failed"); fail("Caught unexpected exception = " + e); } finally { - if (em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - cleanup(emf); - em = null; + closeEM(em); + closeEMF(emf); } } catch (Exception e) { // unexpected @@ -1218,11 +1104,7 @@ public class TestConstraints extends PersistenceTestCase { 5, cves.size()); getLog().trace("testValidFailuresConstraint() Part 2 of 2 passed"); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } @@ -1263,24 +1145,11 @@ public class TestConstraints extends PersistenceTestCase { getLog().trace("testValidPassConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { - if ((em != null) && em.isOpen()) { - if (em.getTransaction().isActive()) - em.getTransaction().rollback(); - em.close(); - } + closeEM(em); } } - /** - * Helper method to remove entities and close the emf an any open em's. - * @param emf - */ - private void cleanup(OpenJPAEntityManagerFactorySPI emf) { - clear(emf); - closeEMF(emf); - } - /** * Internal convenience method for getting the OpenJPA logger * diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestMappedSuper.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestMappedSuper.java index 10303bf04..dcc532854 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestMappedSuper.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestMappedSuper.java @@ -27,11 +27,10 @@ import javax.validation.ConstraintViolationException; import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAPersistence; -import org.apache.openjpa.persistence.test.AllowFailure; -import org.apache.openjpa.persistence.test.PersistenceTestCase; +import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; -public class TestMappedSuper extends PersistenceTestCase { +public class TestMappedSuper extends AbstractPersistenceTestCase { /* * Verify constraints defined via XML on a mapped superclass are validated. @@ -62,13 +61,8 @@ public class TestMappedSuper extends PersistenceTestCase { fail("Should have caught a ConstraintViolationException, but instead caught Exception=" + e); } finally { - if (em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - if (emf != null) { - clear(emf); - closeEMF(emf); - } + closeEM(em); + closeEMF(emf); } } diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java index a584313c8..db7aec196 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java @@ -30,7 +30,7 @@ import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAPersistence; -import org.apache.openjpa.persistence.test.PersistenceTestCase; +import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; /** * Tests the Bean Validation groups support as defined in the JPA 2.0 spec @@ -54,7 +54,7 @@ import org.apache.openjpa.persistence.test.PersistenceTestCase; * * @version $Rev$ $Date$ */ -public class TestValidationGroups extends PersistenceTestCase { +public class TestValidationGroups extends AbstractPersistenceTestCase { /** * 1a) verify validation occurs using the default validation groups @@ -247,7 +247,8 @@ public class TestValidationGroups extends PersistenceTestCase { } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -302,7 +303,8 @@ public class TestValidationGroups extends PersistenceTestCase { } } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -381,7 +383,8 @@ public class TestValidationGroups extends PersistenceTestCase { } } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -504,7 +507,8 @@ public class TestValidationGroups extends PersistenceTestCase { } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -556,7 +560,8 @@ public class TestValidationGroups extends PersistenceTestCase { } } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -613,7 +618,8 @@ public class TestValidationGroups extends PersistenceTestCase { } } finally { - cleanup(emf); + closeEM(em); + closeEMF(emf); } } @@ -640,15 +646,6 @@ public class TestValidationGroups extends PersistenceTestCase { } } } - - /** - * Remove entities and close the emf an any open em's. - * @param emf - */ - private void cleanup(OpenJPAEntityManagerFactorySPI emf) { - clear(emf); - closeEMF(emf); - } /** * Internal convenience method for getting the OpenJPA logger