blob: c4502e1d5635d78154f4b357cef619b0bf843f1c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package org.fedoraproject.candlepin.test;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.fedoraproject.candlepin.model.Consumer;
import org.fedoraproject.candlepin.model.ConsumerType;
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;
/**
* Test fixture for test classes requiring access to the database.
*/
public class DatabaseTestFixture {
protected EntityManager em;
@Before
public void setUp() {
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);
}
List<Consumer> consumers = em.createQuery("from Consumer c").getResultList();
for (Consumer c : consumers) {
em.remove(c);
}
// TODO: Is this right? Or should we pre-populate default defined types, and always
// reference these in the tests instead of creating them everywhere?
List<ConsumerType> consumerTypes = em.createQuery("from ConsumerType c").
getResultList();
for (ConsumerType c : consumerTypes) {
em.remove(c);
}
em.getTransaction().commit();
em.close();
}
/**
* Begin a transaction, persist the given entity, and commit.
*
* @param storeMe Entity to persist.
*/
protected void persistAndCommit(Object storeMe) {
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
em.persist(storeMe);
tx.commit();
}
catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
throw e; // or display error message
}
}
}
|