MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LongLongStringPKTest.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 
24 import testsuite.clusterj.model.LongLongStringPK;
25 
27 
28  protected int PK_MODULUS = 2;
29  protected int NUMBER_OF_INSTANCES = PK_MODULUS * PK_MODULUS * PK_MODULUS;
30  protected long PRETTY_BIG_NUMBER = 1000000000000000L;
31  protected List<LongLongStringPK> instances = new ArrayList<LongLongStringPK>();
32 
33  @Override
34  public void localSetUp() {
35  createSessionFactory();
36  session = sessionFactory.getSession();
37  tx = session.currentTransaction();
38  try {
39  tx.begin();
41  tx.commit();
42  } catch (Throwable t) {
43  // ignore errors while deleting
44  }
46  addTearDownClasses(LongLongStringPK.class);
47  }
48 
49  public void test() {
50  insert();
51  find();
52  update();
53  delete();
54  failOnError();
55  }
56 
59  protected void insert() {
60  session.makePersistentAll(instances);
61  }
62 
65  protected void find() {
66  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
67  Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
68  LongLongStringPK result = session.find(LongLongStringPK.class, key);
69  verify(result, i, false);
70  }
71  }
72 
75  protected void update() {
76  // update the instances
77  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
78  if (0 == i % 4) {
79  LongLongStringPK instance = createInstance(i);
80  instance.setStringvalue(getValue(NUMBER_OF_INSTANCES - i));
81  session.updatePersistent(instance);
82  verify(instance, i, true);
83  }
84  }
85  // verify the updated instances
86  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
87  if (0 == i % 4) {
88  Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
89  LongLongStringPK instance = session.find(LongLongStringPK.class, key);
90  verify(instance, i, true);
91  }
92  }
93  }
94 
97  protected void delete() {
98  // delete the instances
99  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
100  if (0 == i % 5) {
101  LongLongStringPK instance = createInstance(i);
102  session.deletePersistent(instance);
103  }
104  }
105  // verify they have been deleted
106  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
107  if (0 == i % 5) {
108  Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
109  LongLongStringPK instance = session.find(LongLongStringPK.class, key);
110  errorIfNotEqual("Failed to delete instance: " + i, null, instance);
111  }
112  }
113  }
114 
120  protected void createInstances() {
121  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
122  LongLongStringPK instance = createInstance(i);
123 // System.out.println(toString(instance));
124  instances.add(instance);
125  }
126  }
127 
133  LongLongStringPK instance = session.newInstance(LongLongStringPK.class);
134  instance.setLongpk1(getPK1(index));
135  instance.setLongpk2(getPK2(index));
136  instance.setStringpk(getPK3(index));
137  instance.setStringvalue(getValue(index));
138  return instance;
139  }
140 
141  protected String toString(LongLongStringPK instance) {
142  StringBuffer result = new StringBuffer();
143  result.append("LongLongStringPK[");
144  result.append(instance.getLongpk1());
145  result.append(",");
146  result.append(instance.getLongpk2());
147  result.append(",\"");
148  result.append(instance.getStringpk());
149  result.append("\"]: ");
150  result.append(instance.getStringvalue());
151  result.append(".");
152  return result.toString();
153  }
154 
155  protected long getPK1(int index) {
156  return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
157  }
158 
159  protected long getPK2(int index) {
160  return PRETTY_BIG_NUMBER * ((index / PK_MODULUS) % PK_MODULUS);
161  }
162 
163  protected String getPK3(int index) {
164  return "" + (index % PK_MODULUS);
165  }
166 
167  protected String getValue(int index) {
168  return "Value " + index;
169  }
170 
171  protected void verify(LongLongStringPK instance, int index, boolean updated) {
172  errorIfNotEqual("PK1 failed", getPK1(index), instance.getLongpk1());
173  errorIfNotEqual("PK2 failed", getPK2(index), instance.getLongpk2());
174  errorIfNotEqual("PK3 failed", getPK3(index), instance.getStringpk());
175  if (updated) {
176  errorIfNotEqual("Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getStringvalue());
177  } else {
178  errorIfNotEqual("Value failed", getValue(index), instance.getStringvalue());
179 
180  }
181  }
182 
183 }