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