This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ZanataJpaTest.java
137 lines (117 loc) · 3.26 KB
/
ZanataJpaTest.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.zanata;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.hibernate.persister.collection.AbstractCollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.zanata.testng.TestMethodListener;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Map;
@Listeners(TestMethodListener.class)
@Test(groups = { "jpa-tests" })
public abstract class ZanataJpaTest
{
private static final String PERSIST_NAME = "zanataTestDatasourcePU";
private static EntityManagerFactory emf;
protected EntityManager em;
Log log = Logging.getLog(ZanataJpaTest.class);
@BeforeMethod
public void setupEM()
{
log.debug("Setting up EM");
em = emf.createEntityManager();
em.getTransaction().begin();
}
@AfterMethod
public void shutdownEM()
{
log.debug("Shutting down EM");
clearHibernateSecondLevelCache();
em.getTransaction().rollback();
if (em.isOpen())
{
em.close();
}
em = null;
}
protected EntityManager getEm()
{
return em;
}
protected EntityManagerFactory getEmf()
{
return emf;
}
protected Session getSession()
{
return (Session) em.getDelegate();
}
@BeforeSuite
public void initializeEMF()
{
log.debug("Initializing EMF");
emf = Persistence.createEntityManagerFactory(PERSIST_NAME, createPropertiesMap());
}
protected Map<?, ?> createPropertiesMap()
{
return null;
}
@AfterSuite
public void shutDownEMF()
{
log.debug("Shutting down EMF");
emf.close();
emf = null;
}
/**
* Commits the changes on the current session and starts a new one. This
* method is useful whenever multi-session tests are needed.
*
* @return The newly started session
*/
protected Session newSession()
{
em.getTransaction().commit();
setupEM();
return getSession();
}
/**
* This method is used to test multiple Entity Managers (or hibernate
* sessions) working together simultaneously. Use
* {@link org.zanata.ZanataJpaTest#getEm()} for all other tests.
*
* @return A new instance of an entity manager.
*/
protected EntityManager newEntityManagerInstance()
{
return emf.createEntityManager();
}
/**
* Clears the Hibernate Second Level cache.
*/
protected void clearHibernateSecondLevelCache()
{
SessionFactory sessionFactory = ((Session)em.getDelegate()).getSessionFactory();
try
{
sessionFactory.getCache().evictEntityRegions();
sessionFactory.getCache().evictCollectionRegions();
}
catch (Exception e)
{
System.out.println(" *** Cache Exception "+ e.getMessage());
}
}
}