Author: mbo
Date: Thu May 5 14:44:58 2005
New Revision: 168430
URL: http://svn.apache.org/viewcvs?rev=168430&view=rev
Log:
JDO-32 Some tests, when they fail, do not close pmf. Change method closePMF to
make sure that the pmf is closed and changed cleanup method in
PersistenceManagerTest
Modified:
incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/util/AbstractTest.java
incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/JDO_Test.java
incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/JDO_Test.java
incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
Modified:
incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/util/AbstractTest.java
URL:
http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/util/AbstractTest.java?rev=168430&r1=168429&r2=168430&view=diff
==============================================================================
---
incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/util/AbstractTest.java
(original)
+++
incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/util/AbstractTest.java
Thu May 5 14:44:58 2005
@@ -47,6 +47,7 @@
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
+import javax.jdo.JDOException;
import junit.framework.TestCase;
@@ -297,7 +298,8 @@
*
*/
protected void closePMF() {
- if (pmf != null) {
+ JDOException failure = null;
+ while (pmf != null) {
if (debug) logger.debug("closePMF");
try {
// Execute pmf.close in a privileged block, otherwise the test
@@ -307,11 +309,56 @@
pmf.close();
return null;
}});
- }
- finally {
pmf = null;
}
+ catch (JDOException ex) {
+ // store failure of first call pmf.close
+ if (failure == null)
+ failure = ex;
+ PersistenceManager[] pms = getFailedPersistenceManagers(ex);
+ for (int i = 0; i < pms.length; i++) {
+ closePM(pms[i]);
+ }
+ }
+ }
+
+ // rethrow JDOException thrown by pmf.close
+ if (failure != null)
+ throw failure;
+ }
+
+ /**
+ * This method cleans up the specified
+ * <code>PersistenceManager</code>. If the pm still has an open
+ * transaction, it will be rolled back, before closing the pm.
+ */
+ protected void closePM(PersistenceManager pm)
+ {
+ if ((pm != null) && !pm.isClosed()) {
+ if (pm.currentTransaction().isActive()) {
+ pm.currentTransaction().rollback();
+ }
+ pm.close();
+ }
+ }
+
+ /** */
+ protected PersistenceManager[] getFailedPersistenceManagers(
+ JDOException ex) {
+ Throwable[] nesteds = ex.getNestedExceptions();
+ int numberOfExceptions = nesteds==null ? 0 : nesteds.length;
+ PersistenceManager[] result = new
PersistenceManager[numberOfExceptions];
+ for (int i = 0; i < numberOfExceptions; ++i) {
+ JDOException exc = (JDOException)nesteds[i];
+ Object failedObject = exc.getFailedObject();
+ if (exc.getFailedObject() instanceof PersistenceManager) {
+ result[i] = (PersistenceManager)failedObject;
+ } else {
+ fail("Unexpected failed object of type: " +
+ failedObject.getClass().getName());
+ }
}
+ return result;
}
/**
Modified: incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/JDO_Test.java
URL:
http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/JDO_Test.java?rev=168430&r1=168429&r2=168430&view=diff
==============================================================================
--- incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/JDO_Test.java
(original)
+++ incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/JDO_Test.java Thu
May 5 14:44:58 2005
@@ -26,6 +26,7 @@
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
+import javax.jdo.JDOException;
import junit.framework.TestCase;
@@ -229,10 +230,47 @@
/** Closes the pmf stored in this instance. */
protected void closePMF()
{
- if (pmf != null) {
- pmf.close();
- pmf = null;
+ JDOException failure = null;
+ while (pmf != null) {
+ try {
+ pmf.close();
+ pmf = null;
+ }
+ catch (JDOException ex) {
+ // store failure of first call pmf.close
+ if (failure == null)
+ failure = ex;
+ PersistenceManager[] pms = getFailedPersistenceManagers(
+ "closePMF", ex);
+ for (int i = 0; i < pms.length; i++) {
+ cleanupPM(pms[i]);
+ }
+ }
+ }
+
+ // rethrow JDOException thrown by pmf.close
+ if (failure != null)
+ throw failure;
+ }
+
+ /** */
+ protected PersistenceManager[] getFailedPersistenceManagers(
+ String assertionFailure, JDOException ex) {
+ Throwable[] nesteds = ex.getNestedExceptions();
+ int numberOfExceptions = nesteds==null ? 0 : nesteds.length;
+ PersistenceManager[] result = new
PersistenceManager[numberOfExceptions];
+ for (int i = 0; i < numberOfExceptions; ++i) {
+ JDOException exc = (JDOException)nesteds[i];
+ Object failedObject = exc.getFailedObject();
+ if (exc.getFailedObject() instanceof PersistenceManager) {
+ result[i] = (PersistenceManager)failedObject;
+ } else {
+ fail(assertionFailure,
+ "Unexpected failed object of type: " +
+ failedObject.getClass().getName());
+ }
}
+ return result;
}
/**
Modified:
incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
URL:
http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java?rev=168430&r1=168429&r2=168430&view=diff
==============================================================================
---
incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
(original)
+++
incubator/jdo/trunk/tck11/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
Thu May 5 14:44:58 2005
@@ -36,25 +36,24 @@
/** */
protected void tearDown() {
+ Throwable cleanupFailure = null;
try {
cleanup();
cleanupMylib();
- closePMF();
}
catch (Throwable ex) {
- if (debug) ex.printStackTrace();
- if (testSucceeded) {
- // runTest succeeded, but closePMF throws exception =>
- // failure
- fail("Exception during tearDown: " + ex);
- }
- else {
- // runTest failed and closePMF throws exception =>
- // just print the closePMF exception, otherwise the
- // closePMF exception would swallow the test case failure
- if (debug)
- logger.debug("Exception during tearDown: " + ex);
- }
+ cleanupFailure = ex;
+ // set testSucceeded to false, otherwise a failure during
+ // super.tearDown would swallow this exception
+ testSucceeded = false;
+ }
+
+ // cleanup pmf
+ super.tearDown();
+
+ // fail if there was an exception during cleanup
+ if (cleanupFailure != null) {
+ fail("Exception during cleanupMylib: " + cleanupFailure);
}
}
@@ -66,12 +65,10 @@
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
- Collection c = getAllObjects(pm, PCPoint.class);
+ // Note, remove PCRect instances first because of FK constraints
+ Collection c = getAllObjects(pm, PCRect.class);
pm.deletePersistentAll(c);
- tx.commit();
-
- tx.begin();
- c = getAllObjects(pm, PCRect.class);
+ c = getAllObjects(pm, PCPoint.class);
pm.deletePersistentAll(c);
tx.commit();
}
Modified: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/JDO_Test.java
URL:
http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/JDO_Test.java?rev=168430&r1=168429&r2=168430&view=diff
==============================================================================
--- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/JDO_Test.java
(original)
+++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/JDO_Test.java Thu
May 5 14:44:58 2005
@@ -26,6 +26,7 @@
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
+import javax.jdo.JDOException;
import junit.framework.TestCase;
@@ -229,10 +230,47 @@
/** Closes the pmf stored in this instance. */
protected void closePMF()
{
- if (pmf != null) {
- pmf.close();
- pmf = null;
+ JDOException failure = null;
+ while (pmf != null) {
+ try {
+ pmf.close();
+ pmf = null;
+ }
+ catch (JDOException ex) {
+ // store failure of first call pmf.close
+ if (failure == null)
+ failure = ex;
+ PersistenceManager[] pms = getFailedPersistenceManagers(
+ "closePMF", ex);
+ for (int i = 0; i < pms.length; i++) {
+ cleanupPM(pms[i]);
+ }
+ }
+ }
+
+ // rethrow JDOException thrown by pmf.close
+ if (failure != null)
+ throw failure;
+ }
+
+ /** */
+ protected PersistenceManager[] getFailedPersistenceManagers(
+ String assertionFailure, JDOException ex) {
+ Throwable[] nesteds = ex.getNestedExceptions();
+ int numberOfExceptions = nesteds==null ? 0 : nesteds.length;
+ PersistenceManager[] result = new
PersistenceManager[numberOfExceptions];
+ for (int i = 0; i < numberOfExceptions; ++i) {
+ JDOException exc = (JDOException)nesteds[i];
+ Object failedObject = exc.getFailedObject();
+ if (exc.getFailedObject() instanceof PersistenceManager) {
+ result[i] = (PersistenceManager)failedObject;
+ } else {
+ fail(assertionFailure,
+ "Unexpected failed object of type: " +
+ failedObject.getClass().getName());
+ }
}
+ return result;
}
/**
Modified:
incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
URL:
http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java?rev=168430&r1=168429&r2=168430&view=diff
==============================================================================
---
incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
(original)
+++
incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/api/persistencemanager/PersistenceManagerTest.java
Thu May 5 14:44:58 2005
@@ -36,25 +36,24 @@
/** */
protected void tearDown() {
+ Throwable cleanupFailure = null;
try {
cleanup();
cleanupMylib();
- closePMF();
}
catch (Throwable ex) {
- if (debug) ex.printStackTrace();
- if (testSucceeded) {
- // runTest succeeded, but closePMF throws exception =>
- // failure
- fail("Exception during tearDown: " + ex);
- }
- else {
- // runTest failed and closePMF throws exception =>
- // just print the closePMF exception, otherwise the
- // closePMF exception would swallow the test case failure
- if (debug)
- logger.debug("Exception during tearDown: " + ex);
- }
+ cleanupFailure = ex;
+ // set testSucceeded to false, otherwise a failure during
+ // super.tearDown would swallow this exception
+ testSucceeded = false;
+ }
+
+ // cleanup pmf
+ super.tearDown();
+
+ // fail if there was an exception during cleanup
+ if (cleanupFailure != null) {
+ fail("Exception during cleanupMylib: " + cleanupFailure);
}
}
@@ -66,12 +65,10 @@
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
- Collection c = getAllObjects(pm, PCPoint.class);
+ // Note, remove PCRect instances first because of FK constraints
+ Collection c = getAllObjects(pm, PCRect.class);
pm.deletePersistentAll(c);
- tx.commit();
-
- tx.begin();
- c = getAllObjects(pm, PCRect.class);
+ c = getAllObjects(pm, PCPoint.class);
pm.deletePersistentAll(c);
tx.commit();
}