MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LoadTest.java
1 /*
2  Copyright 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 import com.mysql.clusterj.ClusterJUserException;
24 import com.mysql.clusterj.DynamicObject;
25 
26 import testsuite.clusterj.model.Employee;
27 
28 public class LoadTest extends AbstractClusterJModelTest {
29 
30  private static final String tablename = "t_basic";
31 
32  private static final int NUMBER_TO_INSERT = 3;
33 
34  List<DynamicEmployee> loaded = new ArrayList<DynamicEmployee>();
35 
36  @Override
37  public void localSetUp() {
38  createSessionFactory();
39  session = sessionFactory.getSession();
40  createEmployeeInstances(NUMBER_TO_INSERT);
41  tx = session.currentTransaction();
42  tx.begin();
43  session.deletePersistentAll(DynamicEmployee.class);
44  tx.commit();
45  }
46 
47  public void test() {
48  foundIllegalType();
49  foundNull();
50  create();
51  findFound();
52  findFoundAutocommit();
53  load();
54  loadAutocommit();
55  loadNoFlush();
56  loadNotFound();
57  loadFindNoFlush();
58  failOnError();
59  }
60 
61  private void create() {
62  // create instances to find
63  tx = session.currentTransaction();
64  int count = 0;
65  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
66  session.makePersistent(employees.get(i));
67  ++count;
68  }
69  }
70 
71  private void load() {
72  DynamicEmployee e;
73  loaded.clear();
74  tx.begin();
75  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
76  e = session.newInstance(DynamicEmployee.class, i);
77  errorIfNotEqual("load after create new employee id mismatch", i, e.getId());
78  errorIfNotEqual("load before load found mismatch", null, session.found(e));
79  session.load(e);
80  errorIfNotEqual("load after load newInstance employee id mismatch", i, e.getId());
81  errorIfNotEqual("load after load found mismatch", null, session.found(e));
82  loaded.add(e);
83  }
84  session.flush();
85  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
86  e = loaded.get(i);
87  errorIfNotEqual("load after flush found mismatch", true, session.found(e));
88  // see if it is the right Employee
89  errorIfNotEqual("load after flush employee id mismatch", i, e.getId());
90  // make sure all fields were fetched
91  consistencyCheckDynamicEmployee(e);
92  }
93  tx.commit();
94  }
95 
96  private void loadFindNoFlush() {
97  DynamicEmployee e;
98  loaded.clear();
99  tx.begin();
100  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
101  e = session.newInstance(DynamicEmployee.class, i);
102  errorIfNotEqual("loadFindNoFlush after newInstance employee id mismatch", i, e.getId());
103  errorIfNotEqual("loadFindNoFlush after newInstance found mismatch", null, session.found(e));
104  session.load(e);
105  errorIfNotEqual("loadFindNoFlush after load employee id mismatch", i, e.getId());
106  errorIfNotEqual("loadFindNoFlush after load found mismatch", null, session.found(e));
107  loaded.add(e);
108  }
109  session.find(Employee.class, 0); // causes flush
110  for (int i = 0; i < NUMBER_TO_INSERT; ++i) {
111  e = loaded.get(i);
112  errorIfNotEqual("loadFindNoFlush after find found mismatch", true, session.found(e));
113  // see if it is the right Employee
114  errorIfNotEqual("loadFindNoFlush after find employee id mismatch", i, e.getId());
115  // make sure all fields were fetched
116  consistencyCheckDynamicEmployee(e);
117  }
118  tx.commit();
119  }
120 
121  private void consistencyCheckDynamicEmployee(DynamicEmployee e) {
122  int id = e.getId();
123  String name = e.getName();
124  errorIfNotEqual("consistencyCheckDynamicEmployee name mismatch", "Employee number " + id, name);
125  errorIfNotEqual("consistencyCheckDynamicEmployee age mismatch", id, e.getAge());
126  errorIfNotEqual("consistencyCheckDynamicEmployee magic mismatch", id, e.getMagic());
127  }
128 
129  private void loadNoFlush() {
130  DynamicEmployee e;
131  loaded.clear();
132  tx.begin();
133  e = session.newInstance(DynamicEmployee.class, 0);
134  errorIfNotEqual("loadNoFlush after newInstance employee id mismatch", 0, e.getId());
135  session.load(e);
136  errorIfNotEqual("loadNoFlush after load employee id mismatch", 0, e.getId());
137  errorIfNotEqual("loadNoFlush after load employee name mismatch",
138  null, e.getName());
139  tx.commit();
140  }
141 
145  private void loadAutocommit() {
146  DynamicEmployee e = session.newInstance(DynamicEmployee.class, 0);
147  try {
148  session.load(e);
149  error("loadAutocommit expected exception not thrown: a transaction must be in progress for load.");
150  } catch (ClusterJUserException ex) {
151  // good catch
152  }
153  }
154 
158  private void loadNotFound() {
159  tx.begin();
160  // Employee 1000 does not exist
161  DynamicEmployee e = session.newInstance(DynamicEmployee.class, 10000);
162  session.load(e);
163  errorIfNotEqual("loadNotFound dynamic after load found mismatch", null, session.found(e));
164  session.flush();
165  errorIfNotEqual("loadNotFound dynamic after flush found mismatch", false, session.found(e));
166  tx.commit();
167  }
168 
172  private void findFound() {
173  tx.begin();
174  // Employee 1000 does not exist
175  DynamicEmployee e = session.find(DynamicEmployee.class, 0);
176  errorIfNotEqual("findFound dynamic existing found mismatch", true, session.found(e));
177  Employee emp = session.find(Employee.class, 0);
178  errorIfNotEqual("findFound existing found mismatch", true, session.found(emp));
179  tx.commit();
180  }
181 
185  private void findFoundAutocommit() {
186  // Employee 0 exists
187  DynamicEmployee e = session.find(DynamicEmployee.class, 0);
188  errorIfNotEqual("findFoundAutocommit dynamic existing found mismatch", true, session.found(e));
189  Employee emp = session.find(Employee.class, 0);
190  errorIfNotEqual("findFoundAutocommit existing found mismatch", true, session.found(emp));
191  }
192 
193  private void foundIllegalType() {
194  try {
195  // should throw ClusterJUserException
196  session.found(Integer.valueOf(0));
197  error("foundIllegalType expected exception not thrown: ClusterJUserException");
198  } catch(ClusterJUserException ex) {
199  // good catch
200  }
201  }
202 
203  private void foundNull() {
204  errorIfNotEqual("foundNull found mismatch", null, session.found(null));
205  }
206 
207  public static class DynamicEmployee extends DynamicObject {
208 
209  public DynamicEmployee() {}
210 
211  @Override
212  public String table() {
213  return tablename;
214  }
215  public int getId() {
216  return (Integer)get(0);
217  }
218  public String getName() {
219  return (String)get(1);
220  }
221  public int getAge() {
222  return (Integer)get(2);
223  }
224  public int getMagic() {
225  return (Integer)get(3);
226  }
227  }
228 
229 
230 }