summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevan Goodwin <dgoodwin@redhat.com>2009-11-25 15:56:57 -0400
committerDevan Goodwin <dgoodwin@redhat.com>2009-11-25 15:56:57 -0400
commit5cce0bb1e32e659f0cd2c435ed81147563ce3169 (patch)
tree244a4cf2457fa6e9cba9027430a849b3157c91ea
parente819a57e0f8d055af9afde7e8561339924aa7364 (diff)
downloadcandlepin-5cce0bb1e32e659f0cd2c435ed81147563ce3169.tar.gz
candlepin-5cce0bb1e32e659f0cd2c435ed81147563ce3169.tar.xz
candlepin-5cce0bb1e32e659f0cd2c435ed81147563ce3169.zip
Clean DB objects after every test.
Previous method of specifying what you'd like cleaned up wasn't working out so hot.
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/Product.java28
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java51
-rw-r--r--proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java21
3 files changed, 57 insertions, 43 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java
index 521c6c2..127287c 100644
--- a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java
+++ b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java
@@ -14,9 +14,10 @@
*/
package org.fedoraproject.candlepin.model;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -24,10 +25,8 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
-import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
-import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@@ -55,11 +54,11 @@ public class Product {
@Column(nullable=false, unique=true)
private String name;
- @OneToMany(targetEntity=Product.class)
+ @OneToMany(targetEntity=Product.class, cascade=CascadeType.ALL)
@JoinTable(name="cp_product_hierarchy",
joinColumns=@JoinColumn(name="PARENT_PRODUCT_ID"),
inverseJoinColumns=@JoinColumn(name="CHILD_PRODUCT_ID"))
- private List<Product> childProducts;
+ private Set<Product> childProducts;
/**
* Constructor
@@ -98,14 +97,14 @@ public class Product {
/**
* @return the childProducts
*/
- public List<Product> getChildProducts() {
+ public Set<Product> getChildProducts() {
return childProducts;
}
/**
* @param childProducts the childProducts to set
*/
- public void setChildProducts(List<Product> childProducts) {
+ public void setChildProducts(Set<Product> childProducts) {
this.childProducts = childProducts;
}
@@ -115,21 +114,11 @@ public class Product {
*/
public void addChildProduct(Product p) {
if (this.childProducts == null) {
- this.childProducts = new LinkedList<Product>();
+ this.childProducts = new HashSet<Product>();
}
this.childProducts.add(p);
}
-// /**
-// * Get the list of compatible consumer types
-// * @return list of compatible consumer types
-// */
-// public List<String> getCompatibleConsumerTypes() {
-//
-// return null;
-// }
-
-
/**
* @return Returns the label.
*/
@@ -137,7 +126,6 @@ public class Product {
return label;
}
-
/**
* @param labelIn The label to set.
*/
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java
index 9e4ccdc..09752fe 100644
--- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java
+++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ModelTestFixture.java
@@ -1,9 +1,14 @@
package org.fedoraproject.candlepin.model.test;
+import java.util.List;
+
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
+import org.fedoraproject.candlepin.model.Owner;
+import org.fedoraproject.candlepin.model.Product;
import org.fedoraproject.candlepin.util.EntityManagerUtil;
+import org.junit.After;
import org.junit.Before;
public class ModelTestFixture {
@@ -15,6 +20,39 @@ public class ModelTestFixture {
em = EntityManagerUtil.createEntityManager();
}
+ /*
+ * Cleans out everything in the database. Currently we test against an
+ * in-memory hsqldb, so this should be ok.
+ *
+ * WARNING: Don't flip the persistence unit to point to an actual live
+ * DB or you'll lose everything.
+ *
+ * WARNING: If you're creating objects in tables that have static
+ * pre-populated content, we may not want to wipe those tables here.
+ * This situation hasn't arisen yet.
+ */
+ @After
+ public void cleanDb() {
+ if (!em.getTransaction().isActive()) {
+ em.getTransaction().begin();
+ }
+
+ // TODO: Would rather be doing this, but such a bulk delete does not seem to respect
+ // the cascade to child products and thus fails.
+ // em.createQuery("delete from Product").executeUpdate();
+ List<Product> products = em.createQuery("from Product p").getResultList();
+ for (Product p : products) {
+ em.remove(p);
+ }
+
+ List<Owner> owners = em.createQuery("from Owner o").getResultList();
+ for (Owner o : owners) {
+ em.remove(o);
+ }
+ em.getTransaction().commit();
+ em.close();
+ }
+
/**
* Begin a transaction, persist the given entity, and commit.
*
@@ -22,15 +60,18 @@ public class ModelTestFixture {
*/
protected void persistAndCommit(Object storeMe) {
EntityTransaction tx = null;
- tx = em.getTransaction();
- tx.begin();
- em.persist(storeMe);
try {
+ tx = em.getTransaction();
+ tx.begin();
+ em.persist(storeMe);
tx.commit();
}
- catch (Exception e) {
- tx.rollback();
+ catch (RuntimeException e) {
+ if (tx != null && tx.isActive()) {
+ tx.rollback();
+ }
+ throw e; // or display error message
}
}
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 c139ce5..f960098 100644
--- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java
+++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ProductTest.java
@@ -23,10 +23,6 @@ public class ProductTest extends ModelTestFixture {
List<Product> results = em.createQuery("select p from Product as p")
.getResultList();
assertEquals(1, results.size());
-
- em.getTransaction().begin();
- em.remove(prod);
- em.getTransaction().commit();
}
@Test(expected = PersistenceException.class)
@@ -63,14 +59,7 @@ public class ProductTest extends ModelTestFixture {
Product prod2 = new Product("label1", "name2");
persistAndCommit(prod);
- try {
- persistAndCommit(prod2);
- }
- catch (PersistenceException e) {
- em.remove(prod);
- em.getTransaction().commit();
- throw e;
- }
+ persistAndCommit(prod2);
}
@Test
@@ -93,12 +82,6 @@ public class ProductTest extends ModelTestFixture {
.setParameter("name", parent.getName())
.getSingleResult();
assertEquals(2, result.getChildProducts().size());
-
- em.getTransaction().begin();
- em.remove(parent);
- em.remove(child1);
- em.remove(child2);
- em.getTransaction().commit();
}
@Test
@@ -109,6 +92,8 @@ 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);