]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
11441153990ec7994ce4d28482280ac54a74eeed
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright 2010 Sun Microsystems, Inc.
3    All rights reserved. Use is subject to license terms.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
17 */
18
19 package com.mysql.clusterj.jpatest;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityTransaction;
23
24 /**
25  * A base test case for a single EntityManager at any given time.
26  */
27 public abstract class SingleEMTestCase extends SingleEMFTestCase {
28
29     protected EntityManager em;
30
31     protected EntityTransaction tx;
32
33     @Override
34     public void setUp() {
35         super.setUp();
36         em = emf.createEntityManager(); 
37     }
38
39     @Override
40     public void setUp(Object... props) {
41         super.setUp(props);
42         em = emf.createEntityManager(); 
43     }
44
45     @Override
46     public void tearDown() throws Exception {
47         rollback();
48         close();
49         super.tearDown();
50     }
51
52     /** 
53      * Start a new transaction if there isn't currently one active. 
54      * @return true if a transaction was started, false if one already existed
55      */
56     protected boolean begin() {
57         EntityTransaction tx = em.getTransaction();
58         if (tx.isActive())
59             return false;
60         tx.begin();
61         return true;
62     }
63
64     /** 
65      * Commit the current transaction, if it is active. 
66      * @return true if the transaction was committed
67      */
68     protected boolean commit() {
69         EntityTransaction tx = em.getTransaction();
70         if (!tx.isActive())
71             return false;
72
73         tx.commit();
74         return true;
75     }
76
77     /** 
78      * Roll back the current transaction, if it is active. 
79      * @return true if the transaction was rolled back
80      */
81     protected boolean rollback() {
82         if (em != null && em.isOpen()) {
83             EntityTransaction tx = em.getTransaction();
84             if (!tx.isActive()) {
85                 return false;
86             } else {
87                 tx.rollback();
88                 return true;
89             }
90         } else {
91             return false;
92         }
93     }
94
95     /** 
96      * Closes the current EntityManager if it is open. 
97      * @return false if the EntityManager was already closed
98      */
99     protected boolean close() {
100         if (em == null)
101             return false;
102
103         rollback();
104
105         if (!em.isOpen())
106             return false;
107
108         em.close();
109         return !em.isOpen();
110     }
111
112 }