From de2aeeae880b9fd5bd1dfb6600bc23d6530f68d4 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Wed, 25 Nov 2009 16:13:36 -0400 Subject: Test Product hierarchy cascading. --- .../candlepin/model/test/ProductTest.java | 35 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java index 2341f5b..f1799aa 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java @@ -1,6 +1,6 @@ package org.fedoraproject.candlepin.model.test; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.util.List; @@ -71,8 +71,6 @@ public class ProductTest extends ModelTestFixture { parent.addChildProduct(child1); parent.addChildProduct(child2); - em.persist(child1); - em.persist(child2); em.persist(parent); em.getTransaction().commit(); @@ -92,8 +90,6 @@ public class ProductTest extends ModelTestFixture { Product child1 = new Product("child-product1", "Child Product 1"); Product parent2 = new Product("parent-product2", "Parent Product 2"); - List objects = em.createQuery("from Product p").getResultList(); - parent1.addChildProduct(child1); parent2.addChildProduct(child1); // should cause the failure @@ -102,5 +98,34 @@ public class ProductTest extends ModelTestFixture { em.persist(parent2); em.getTransaction().commit(); } + + @Test + public void testCascading() { + em.getTransaction().begin(); + + Product parent1 = new Product("parent-product1", "Parent Product 1"); + Product child1 = new Product("child-product1", "Child Product 1"); + parent1.addChildProduct(child1); + em.persist(parent1); + em.getTransaction().commit(); + + EntityManager em2 = EntityManagerUtil.createEntityManager(); + Product result = (Product)em2.createQuery( + "select p from Product as p where name = :name") + .setParameter("name", child1.getName()) + .getSingleResult(); + assertNotNull(result); + + em.getTransaction().begin(); + em.remove(parent1); + em.getTransaction().commit(); + + em2 = EntityManagerUtil.createEntityManager(); + List results = em2.createQuery( + "select p from Product as p where name = :name") + .setParameter("name", child1.getName()) + .getResultList(); + assertEquals(0, results.size()); + } } -- cgit