MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynamicBinaryPKTest.java
1 /*
2  * Copyright (c) 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 testsuite.clusterj.model.BinaryPK;
24 import testsuite.clusterj.model.DynamicPK;
25 
27 
28  protected int NUMBER_OF_INSTANCES = 15;
29  protected List<DynamicPK> instances = new ArrayList<DynamicPK>();
30 
31  @Override
32  public void localSetUp() {
33  createSessionFactory();
34  session = sessionFactory.getSession();
35  tx = session.currentTransaction();
36  }
37 
38  public void test() {
39  run(DynamicBinaryPK.class);
40  run(DynamicVarbinaryPK.class);
41  run(DynamicLongvarbinaryPK.class);
42  failOnError();
43  }
44 
45  public void run(Class<? extends DynamicPK> cls) {
46  deleteAll(cls);
47  createInstances(cls);
48  insert();
49  find(cls);
50  update(cls);
51  delete(cls);
52  }
53 
54  public static class DynamicBinaryPK extends DynamicPK {
55  @Override
56  public String table() {
57  return "binarypk";
58  }
59  }
60 
61  public static class DynamicVarbinaryPK extends DynamicPK {
62  @Override
63  public String table() {
64  return "varbinarypk";
65  }
66  }
67 
68  public static class DynamicLongvarbinaryPK extends DynamicPK {
69  @Override
70  public String table() {
71  return "longvarbinarypk";
72  }
73  }
74 
75  protected void deleteAll(Class<? extends DynamicPK> cls) {
76  try {
77  tx.begin();
78  session.deletePersistentAll(cls);
79  tx.commit();
80  } catch (Throwable t) {
81  // ignore errors while deleting
82  }
83  }
84 
87  protected void insert() {
88  session.makePersistentAll(instances);
89  }
90 
93  protected void find(Class<? extends DynamicPK> cls) {
94  String where = "find " + cls.getName();
95  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
96  byte[] key = getPK(i);
97  DynamicPK result = session.find(cls, key);
98  verify(where, result, i, false);
99  }
100  }
101 
104  protected void update(Class<? extends DynamicPK> cls) {
105  // update the instances
106  String where = "update before " + cls.getName();
107  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
108  if (0 == i % 4) {
109  DynamicPK instance = createInstance(cls, i);
110  instance.setName(getValue(NUMBER_OF_INSTANCES - i));
111  session.updatePersistent(instance);
112  verify(where, instance, i, true);
113  }
114  }
115  // verify the updated instances
116  where = "update after " + cls.getName();
117  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
118  if (0 == i % 4) {
119  byte[] key = getPK(i);
120  DynamicPK instance = session.find(cls, key);
121  verify(where, instance, i, true);
122  }
123  }
124  }
125 
128  protected void delete(Class<? extends DynamicPK> cls) {
129  // delete the instances
130  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
131  if (0 == i % 5) {
132  DynamicPK instance = createInstance(cls, i);
133  session.deletePersistent(instance);
134  }
135  }
136  // verify they have been deleted
137  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
138  if (0 == i % 5) {
139  byte[] key = getPK(i);
140  DynamicPK instance = session.find(cls, key);
141  errorIfNotEqual("Failed to delete instance: " + i, null, instance);
142  }
143  }
144  }
145 
149  protected void createInstances(Class<? extends DynamicPK> cls) {
150  instances.clear();
151  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
152  DynamicPK instance = createInstance(cls, i);
153  if (getDebug()) System.out.println(toString(instance));
154  instances.add(instance);
155  }
156  }
157 
162  protected DynamicPK createInstance(Class<?extends DynamicPK> cls, int index) {
163  DynamicPK instance = session.newInstance(cls);
164  instance.setId(getPK(index));
165  instance.setNumber(index);
166  instance.setName(getValue(index));
167  return instance;
168  }
169 
170  protected String toString(DynamicPK instance) {
171  StringBuffer result = new StringBuffer(instance.getClass().getName());
172  result.append("[");
173  result.append(toString(instance.getId()));
174  result.append("]: ");
175  result.append(instance.getNumber());
176  result.append(", \"");
177  result.append(instance.getName());
178  result.append("\".");
179  return result.toString();
180  }
181 
182  protected byte[] getPK(int index) {
183  byte[] result = new byte[255];
184  result[1] = (byte)(index/256);
185  result[2] = (byte)(index%256);
186  return result;
187  }
188 
189  protected String getValue(int index) {
190  return "Value " + index;
191  }
192 
193  protected void verify(String where, DynamicPK instance, int index, boolean updated) {
194  errorIfNotEqual(where + "id failed", toString(getPK(index)), toString(instance.getId()));
195  errorIfNotEqual(where + "number failed", index, instance.getNumber());
196  if (updated) {
197  errorIfNotEqual(where + " Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getName());
198  } else {
199  errorIfNotEqual(where + " Value failed", getValue(index), instance.getName());
200 
201  }
202  }
203 
204  private String toString(byte[] id) {
205  StringBuilder builder = new StringBuilder();
206  for (int i = 0; i < id.length; ++i) {
207  builder.append(String.valueOf(id[i]));
208  builder.append('-');
209  }
210  return builder.toString();
211  }
212 
213 }