2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
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.
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.
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
19 package testsuite.clusterj;
21 import com.mysql.clusterj.Query;
23 import com.mysql.clusterj.query.QueryDomainType;
24 import com.mysql.clusterj.query.QueryBuilder;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
31 import testsuite.clusterj.model.Employee;
33 public class AutoCommitTest extends AbstractClusterJModelTest {
35 protected static final int NUMBER_TO_INSERT = 4;
38 public void localSetUp() {
39 createSessionFactory();
40 session = sessionFactory.getSession();
41 createEmployeeInstances(NUMBER_TO_INSERT);
42 tx = session.currentTransaction();
44 session.deletePersistentAll(Employee.class);
46 addTearDownClasses(Employee.class);
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()");
79 protected void nontransactionalDeletePersistent(int which) {
80 session.deletePersistent(employees.get(which));
83 protected void nontransactionalDeletePersistentAll() {
84 session.deletePersistentAll(Employee.class);
87 protected void nontransactionalDeletePersistentAll(int low, int high) {
88 session.deletePersistentAll(employees.subList(low, high));
91 protected void nontransactionalMakePersistent(int which) {
92 session.makePersistent(employees.get(which));
95 protected void nontransactionalMakePersistentAll(int low, int high) {
96 List<Employee> emps = employees.subList(low, high);
97 session.makePersistentAll(emps);
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) {
111 Set<Integer> actualList = new HashSet<Integer>();
112 for (Employee e: result) {
113 actualList.add(e.getId());
115 errorIfNotEqual("Mismatch in query result", expectedList, actualList);
118 protected void nontransactionalUpdate(int which, int newAge) {
119 Employee e = session.find(Employee.class, which);
121 session.updatePersistent(e);
122 e = session.find(Employee.class, which);
123 errorIfNotEqual("Mismatch in updateAll result", newAge, e.getAge());
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);
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());
140 private void assertTransactionNotActive(String where) {
142 error("After " + where + " the transaction was active." );
146 private void nontransactionalFind(int which) {
147 Employee e = session.find(Employee.class, which);