[hibernate-commits] Hibernate SVN: r15686 - core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Dec 12 08:10:10 EST 2008


Author: jcosta at redhat.com
Date: 2008-12-12 08:10:10 -0500 (Fri, 12 Dec 2008)
New Revision: 15686

Modified:
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/TestCase.java
Log:
EJB-406 - Makes a test to fail if there are unfinished transactions, and closes it to not affect other tests

Modified: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/TestCase.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/TestCase.java	2008-12-12 12:48:28 UTC (rev 15685)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/TestCase.java	2008-12-12 13:10:10 UTC (rev 15686)
@@ -12,6 +12,8 @@
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hibernate.cfg.Environment;
 import org.hibernate.ejb.HibernatePersistence;
 
@@ -22,6 +24,7 @@
 public abstract class TestCase extends junit.framework.TestCase {
 	protected EntityManagerFactory factory;
 	protected EntityManager em;
+	private static Log log = LogFactory.getLog( TestCase.class );
 
 	public TestCase() {
 		super();
@@ -44,10 +47,22 @@
 		try {
 			em = getOrCreateEntityManager();
 			super.runTest();
+			if (em.getTransaction().isActive()) {
+				em.getTransaction().rollback();
+				fail("You left an open transaction! Fix your test case. For now, we are closing it for you.");
+			}
+
 		} catch (Throwable t) {
 			if (em.getTransaction().isActive())  
 				em.getTransaction().rollback();
 			throw t;
+		} finally {
+			if (em.isOpen()) {
+				// as we open an EM before the test runs, it will still be open if the test uses a custom EM.
+				// or, the person may have forgotten to close. So, do not raise a "fail", but log the fact.
+				em.close(); 
+				log.warn("The EntityManager is not closed. Closing it.");
+			}
 		}
 	}
 	




More information about the hibernate-commits mailing list