MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SaveTest.java
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 java.util.ArrayList;
22 import java.util.List;
23 import testsuite.clusterj.model.Employee;
24 
25 public class SaveTest extends AbstractClusterJModelTest {
26 
27  private static final int NUMBER_TO_INSERT = 4;
28 
29  @Override
30  public void localSetUp() {
31  createSessionFactory();
32  session = sessionFactory.getSession();
33  createEmployeeInstances(NUMBER_TO_INSERT);
34  tx = session.currentTransaction();
35  tx.begin();
36  session.deletePersistentAll(Employee.class);
37  tx.commit();
38  tx.begin();
39  List<Employee> insertedEmployees = new ArrayList<Employee>();
40  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
41  if (i%2 == 0) {
42  // only make even employees persistent now
43  insertedEmployees.add(employees.get(i));
44  }
45  }
46  session.makePersistentAll(insertedEmployees);
47  tx.commit();
48  addTearDownClasses(Employee.class);
49  }
50 
51  public void testSave() {
52  tx.begin();
53  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
54  Employee e = session.find(Employee.class, i);
55  if (e != null) {
56  if (i%2 != 0) {
57  error("Employee " + i + " should not exist.");
58  }
59  // if exists, change age
60  e.setAge(NUMBER_TO_INSERT - i);
61  } else {
62  // if not exist, insert with new age
63  if (i%2 == 0) {
64  error("Employee " + i + " should exist.");
65  } else {
66  e = employees.get(i);
67  e.setAge(NUMBER_TO_INSERT - i);
68  }
69  }
70  // send the change to the database
71  session.savePersistent(e);
72  }
73  tx.commit();
74 
75  // now verify that the changes were committed
76  tx.begin();
77  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
78  Employee e = session.find(Employee.class, i);
79  if (e == null) {
80  error("Failed save: employee " + i + " does not exist.");
81  } else {
82  // verify age
83  int expected = NUMBER_TO_INSERT - i;
84  int actual = e.getAge();
85  if (expected != actual) {
86  error("Failed save: for employee " + i
87  + " expected age " + expected
88  + " actual age " + actual);
89  }
90  }
91  }
92  tx.commit();
93  failOnError();
94  }
95 
96  public void testSaveAll() {
97  tx.begin();
98  List<Employee> emps = new ArrayList<Employee>();
99  List<Employee> expectedEmployees = new ArrayList<Employee>();
100  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
101  Employee e = session.find(Employee.class, i);
102  if (e != null) {
103  if (i%2 != 0) {
104  error("Employee " + i + " should not exist.");
105  }
106  // if exists, change age
107  e.setAge(NUMBER_TO_INSERT - i);
108  } else {
109  // if not exist, insert with new age
110  if (i%2 == 0) {
111  error("Employee " + i + " should exist.");
112  } else {
113  e = employees.get(i);
114  e.setAge(NUMBER_TO_INSERT - i);
115  }
116  }
117  emps.add(e);
118  expectedEmployees.add(e);
119  }
120  // send the changes to the database
121  List<Employee> savedEmployees = (List<Employee>)session.savePersistentAll(emps);
122  if (savedEmployees.size() != NUMBER_TO_INSERT) {
123  error("Wrong size for saved employees. Expected: " + NUMBER_TO_INSERT
124  + " actual: " + savedEmployees.size());
125  }
126  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
127  Employee e = expectedEmployees.get(i);
128  // verify saved employees
129  Employee saved = savedEmployees.get(i);
130  if (saved != e) {
131  error ("Failed saveAll: employee " + i + " did not match savedEmployees. "
132  + "Expected: " + e.toString() + " hashcode: " + e.hashCode()
133  + " actual: " + saved.toString() + " hashcode: " + saved.hashCode());
134  }
135  }
136  tx.commit();
137 
138  // now verify that the changes were committed
139  tx.begin();
140  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
141  Employee e = session.find(Employee.class, i);
142  if (e == null) {
143  error("Failed saveAll: employee " + i + " does not exist.");
144  } else {
145  // verify age
146  int expected = NUMBER_TO_INSERT - i;
147  int actual = e.getAge();
148  if (expected != actual) {
149  error("Failed saveAll: for employee " + i
150  + " expected age " + expected
151  + " actual age " + actual);
152  }
153  }
154  }
155  tx.commit();
156  failOnError();
157  }
158 }