]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
92d9c4b00a210a768c08c7bf8894eb6cf855f837
[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 testsuite.clusterj;
20
21 import com.mysql.clusterj.Query;
22
23 import com.mysql.clusterj.query.QueryDomainType;
24 import com.mysql.clusterj.query.QueryBuilder;
25
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import testsuite.clusterj.model.Employee;
32
33 public class AutoCommitTest extends AbstractClusterJModelTest {
34
35     protected static final int NUMBER_TO_INSERT = 4;
36
37     @Override
38     public void localSetUp() {
39         createSessionFactory();
40         session = sessionFactory.getSession();
41         createEmployeeInstances(NUMBER_TO_INSERT);
42         tx = session.currentTransaction();
43         tx.begin();
44         session.deletePersistentAll(Employee.class);
45         tx.commit();
46         addTearDownClasses(Employee.class);
47     }
48
49     public void test() {
50         nontransactionalMakePersistent(0);
51         assertTransactionNotActive("nontransactionalMakePersistent(0)");
52         nontransactionalFind(0);
53         assertTransactionNotActive("nontransactionalFind(0)");
54         nontransactionalQuery(0);
55         assertTransactionNotActive("nontransactionalQuery(0)");
56         nontransactionalUpdate(0, 9);
57         assertTransactionNotActive("nontransactionalUpdate(0, 9)");
58         nontransactionalMakePersistentAll(1, 4);
59         assertTransactionNotActive("nontransactionalMakePersistentAll(1, 4)");
60         nontransactionalUpdateAll(1, 3, 9);
61         assertTransactionNotActive("nontransactionalUpdateAll(1, 3, 9)");
62         nontransactionalQuery(0, 1, 2, 3);
63         assertTransactionNotActive("nontransactionalQuery(0, 1, 2, 3");
64         nontransactionalDeletePersistent(2);
65         assertTransactionNotActive("nontransactionalDeletePersistent(2)");
66         nontransactionalQuery(0, 1, 3);
67         assertTransactionNotActive("nontransactionalQuery(0, 1, 3)");
68         nontransactionalDeletePersistentAll(0, 1);
69         assertTransactionNotActive("nontransactionalDeletePersistentAll(0, 1)");
70         nontransactionalQuery(1, 3);
71         assertTransactionNotActive("nontransactionalQuery(1, 3)");
72         nontransactionalDeletePersistentAll();
73         assertTransactionNotActive("nontransactionalDeletePersistentAll()");
74         nontransactionalQuery();
75         assertTransactionNotActive("nontransactionalQuery()");
76         failOnError();
77     }
78
79     protected void nontransactionalDeletePersistent(int which) {
80         session.deletePersistent(employees.get(which));
81     }
82
83     protected void nontransactionalDeletePersistentAll() {
84         session.deletePersistentAll(Employee.class);
85     }
86
87     protected void nontransactionalDeletePersistentAll(int low, int high) {
88         session.deletePersistentAll(employees.subList(low, high));
89     }
90
91     protected void nontransactionalMakePersistent(int which) {
92         session.makePersistent(employees.get(which));
93     }
94
95     protected void nontransactionalMakePersistentAll(int low, int high) {
96         List<Employee> emps = employees.subList(low, high);
97         session.makePersistentAll(emps);
98     }
99
100     protected void nontransactionalQuery(int... expected) {
101         // QueryBuilder is the sessionFactory for queries
102         QueryBuilder builder = session.getQueryBuilder();
103         // QueryDomainType is the main interface
104         QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
105         Query query = session.createQuery(dobj);
106         List<Employee> result = query.getResultList();
107         Set<Integer> expectedList = new HashSet<Integer>();
108         for (int i: expected) {
109             expectedList.add(i);
110         }
111         Set<Integer> actualList = new HashSet<Integer>();
112         for (Employee e: result) {
113             actualList.add(e.getId());
114         }
115         errorIfNotEqual("Mismatch in query result", expectedList, actualList);
116     }
117
118     protected void nontransactionalUpdate(int which, int newAge) {
119         Employee e = session.find(Employee.class, which);
120         e.setAge(newAge);
121         session.updatePersistent(e);
122         e = session.find(Employee.class, which);
123         errorIfNotEqual("Mismatch in updateAll result", newAge, e.getAge());
124     }
125
126     protected void nontransactionalUpdateAll(int low, int high, int newAge) {
127         List<Employee> emps = new ArrayList<Employee>();
128         for (int i = low; i < high; ++i) {
129             Employee e = session.find(Employee.class, i);
130             e.setAge(newAge);
131             emps.add(e);
132         }
133         session.updatePersistentAll(emps);
134         for (int i = low; i < high; ++i) {
135             Employee e = session.find(Employee.class, i);
136             errorIfNotEqual("Mismatch in updateAll result", newAge, e.getAge());
137         }
138     }
139
140     private void assertTransactionNotActive(String where) {
141         if (tx.isActive()) {
142             error("After " + where + " the transaction was active." );
143         }
144     }
145
146     private void nontransactionalFind(int which) {
147         Employee e = session.find(Employee.class, which);
148     }
149 }