From 4f0c54211df547e848b6a7605b2f255563634fda Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Thu, 18 Jan 2007 19:26:39 +0000 Subject: [PATCH] test case for OPENJPA-102 and OPENJPA-104 git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@497547 13f79535-47bb-0310-9956-ffa450edef68 --- ...TestGetReferenceAndImplicitDetachment.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestGetReferenceAndImplicitDetachment.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestGetReferenceAndImplicitDetachment.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestGetReferenceAndImplicitDetachment.java new file mode 100644 index 000000000..bf3b4c85a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestGetReferenceAndImplicitDetachment.java @@ -0,0 +1,120 @@ +package org.apache.openjpa.persistence.detachment; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.persistence.Persistence; +import javax.persistence.RollbackException; + +import org.apache.openjpa.enhance.PersistenceCapable; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory; +import org.apache.openjpa.persistence.OpenJPAPersistence; + +import junit.framework.TestCase; + + +public class TestGetReferenceAndImplicitDetachment + extends TestCase { + + private OpenJPAEntityManagerFactory emf; + + public void setUp() { + String types = DetachmentOneManyParent.class.getName() + ";" + + DetachmentOneManyChild.class.getName(); + Map props = new HashMap(System.getProperties()); + props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")"); + props.put("openjpa.DetachState", "fgs"); + emf = (OpenJPAEntityManagerFactory) Persistence. + createEntityManagerFactory("test", props); + deleteAll(); + } + + public void tearDown() { + if (emf == null) + return; + try { + deleteAll(); + emf.close(); + } catch (Exception e) { + } + } + + private void deleteAll() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.createQuery("delete from DetachmentOneManyChild"). + executeUpdate(); + em.createQuery("delete from DetachmentOneManyParent"). + executeUpdate(); + em.getTransaction().commit(); + em.close(); + } + + public void testNonexistentGetReferenceDetachmentInTxWithCommit() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + DetachmentOneManyParent o = + em.getReference(DetachmentOneManyParent.class, 0); + em.getTransaction().commit(); + em.close(); + + // the close detachment should leave these invalid objects in a + // transient state + assertFalse(((PersistenceCapable) o).pcIsTransactional()); + assertFalse(((PersistenceCapable) o).pcIsPersistent()); + // pcIsDetached() will give a false positive in this configuration + // assertFalse(((PersistenceCapable) o).pcIsDetached()); + } + + public void testNonexistentGetReferenceDetachmentOutsideTx() { + EntityManager em = emf.createEntityManager(); + DetachmentOneManyParent o = + em.getReference(DetachmentOneManyParent.class, 0); + em.close(); + + // the close detachment should leave these invalid objects in a + // transient state + assertFalse(((PersistenceCapable) o).pcIsTransactional()); + assertFalse(((PersistenceCapable) o).pcIsPersistent()); + // pcIsDetached() will give a false positive in this configuration + // assertFalse(((PersistenceCapable) o).pcIsDetached()); + } + + public void testNonexistentGetReferenceDetachmentInTxWithRollback() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + DetachmentOneManyParent o = + em.getReference(DetachmentOneManyParent.class, 0); + em.getTransaction().rollback(); + + // the rollback should cause a detachment + assertFalse(OpenJPAPersistence.cast(em).isTransactional(o)); + assertFalse(OpenJPAPersistence.cast(em).isPersistent(o)); + // pcIsDetached() will give a false positive in this configuration + // assertFalse(OpenJPAPersistence.cast(em).isDetached(o)); + + em.close(); + } + + public void testNonexistentGetReferenceDetachmentInTxWithFailedCommit() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + DetachmentOneManyParent o = + em.getReference(DetachmentOneManyParent.class, 0); + em.getTransaction().setRollbackOnly(); + try { + em.getTransaction().commit(); + } catch (RollbackException re) { + // expected + } + + // the failed commit should cause a detachment + assertFalse(OpenJPAPersistence.cast(em).isTransactional(o)); + assertFalse(OpenJPAPersistence.cast(em).isPersistent(o)); + // pcIsDetached() will give a false positive in this configuration + // assertFalse(OpenJPAPersistence.cast(em).isDetached(o)); + + em.close(); + } +}