View Javadoc
1   package ejava.jpa.examples.query;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import javax.persistence.*;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.junit.*;
12  
13  public class QueryBase {
14      private static Log log = LogFactory.getLog(QueryBase.class);
15      private static final String PERSISTENCE_UNIT = "jpa-query-example-test";
16      private List<EntityManager> ems = new ArrayList<EntityManager>();
17      protected static EntityManagerFactory emf;
18      protected EntityManager em;    
19  
20      @BeforeClass
21      public static void setUpClass() {
22          log.debug("creating entity manager factory");
23          emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
24          EntityManager em1 = emf.createEntityManager();
25          cleanup(em1);
26          populate(em1);
27      }
28      
29      @Before
30      public void setUp() throws Exception {
31          log.debug("creating entity manager");
32          em = createEm();
33          em.getTransaction().begin();
34      }
35  
36      @After
37      public void tearDown() throws Exception {
38      	for (Iterator<EntityManager> itr=ems.iterator(); itr.hasNext();) {
39      		close(itr.next());
40      		itr.remove();
41      	}
42      }
43      
44      @AfterClass
45      public static void tearDownClass() {
46          log.debug("closing entity manager factory");
47          if (emf!=null) { emf.close(); emf=null; }
48      }    
49  
50      protected EntityManager createEm() {
51      	EntityManager emgr = emf.createEntityManager();
52      	ems.add(emgr);
53      	return emgr;
54      }
55  
56      public void close(EntityManager emgr) throws Exception {
57      	if (emgr==null || !emgr.isOpen()) { return; }
58          try {
59              log.debug("tearDown() started, em=" + em);
60              if (!emgr.getTransaction().isActive()) {
61                  emgr.getTransaction().begin();
62                  emgr.getTransaction().commit();            
63              } else if (!emgr.getTransaction().getRollbackOnly()) {
64                  emgr.getTransaction().commit();                        	
65              } else {
66              	emgr.getTransaction().rollback();
67              }
68              emgr.close();
69          }
70          catch (Exception ex) {
71              log.fatal("tearDown failed", ex);
72              throw ex;
73          }
74      }
75      
76      public static void cleanup(EntityManager em) {
77      	em.getTransaction().begin();
78      	new SalesFactory().setEntityManager(em).cleanup();
79      	em.getTransaction().commit();
80      	em.clear();
81      }
82      
83      public static void populate(EntityManager em) {
84      	em.getTransaction().begin();
85      	new SalesFactory().setEntityManager(em).populate();
86      	em.getTransaction().commit();
87      	em.clear();
88      }
89  }