MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryPKTest.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 
25 public class BinaryPKTest extends AbstractClusterJTest {
26 
27  protected int NUMBER_OF_INSTANCES = 15;
28  protected List<BinaryPK> instances = new ArrayList<BinaryPK>();
29 
30  @Override
31  public void localSetUp() {
32  createSessionFactory();
33  session = sessionFactory.getSession();
34  tx = session.currentTransaction();
35  try {
36  tx.begin();
37  session.deletePersistentAll(BinaryPK.class);
38  tx.commit();
39  } catch (Throwable t) {
40  // ignore errors while deleting
41  }
43  addTearDownClasses(BinaryPK.class);
44  }
45 
46  public void test() {
47  insert();
48  find();
49  update();
50  delete();
51  failOnError();
52  }
53 
56  protected void insert() {
57  session.makePersistentAll(instances);
58  }
59 
62  protected void find() {
63  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
64  byte[] key = getStoragePK(i);
65  BinaryPK result = session.find(BinaryPK.class, key);
66  verifyResult("find ", result, i, false);
67  }
68  }
69 
72  protected void update() {
73  // update the instances
74  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
75  if (0 == i % 4) {
76  BinaryPK instance = createInstance(i);
77  instance.setName(getValue(NUMBER_OF_INSTANCES - i));
78  session.updatePersistent(instance);
79  verifyStorage("update ", instance, i, true);
80  }
81  }
82  // verify the updated instances
83  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
84  if (0 == i % 4) {
85  byte[] key = getStoragePK(i);
86  BinaryPK instance = session.find(BinaryPK.class, key);
87  verifyResult("update verify", instance, i, true);
88  }
89  }
90  }
91 
94  protected void delete() {
95  // delete the instances
96  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
97  if (0 == i % 5) {
98  BinaryPK instance = createInstance(i);
99  session.deletePersistent(instance);
100  }
101  }
102  // verify they have been deleted
103  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
104  if (0 == i % 5) {
105  byte[] key = getStoragePK(i);
106  BinaryPK instance = session.find(BinaryPK.class, key);
107  errorIfNotEqual("Failed to delete instance: " + i, null, instance);
108  }
109  }
110  }
111 
115  protected void createInstances() {
116  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
117  BinaryPK instance = createInstance(i);
118  if (getDebug()) System.out.println(toString(instance));
119  instances.add(instance);
120  }
121  }
122 
127  protected BinaryPK createInstance(int index) {
128  BinaryPK instance = session.newInstance(BinaryPK.class);
129  instance.setId(getStoragePK(index));
130  instance.setNumber(index);
131  instance.setName(getValue(index));
132  return instance;
133  }
134 
135  protected String toString(BinaryPK instance) {
136  StringBuffer result = new StringBuffer();
137  result.append("BinaryPK[");
138  result.append(toString(instance.getId()));
139  result.append("]: ");
140  result.append(instance.getNumber());
141  result.append(", \"");
142  result.append(instance.getName());
143  result.append("\".");
144  return result.toString();
145  }
146 
147  protected byte[] getStoragePK(int index) {
148  return new byte[] {0, (byte)(index/256), (byte)(index%256)};
149  }
150 
151  protected byte[] getResultPK(int index) {
152  byte[] result = new byte[255];
153  result[1] = (byte)(index/256);
154  result[2] = (byte)(index%256);
155  return result;
156  }
157 
158  protected String getValue(int index) {
159  return "Value " + index;
160  }
161 
162  protected void verifyStorage(String where, BinaryPK instance, int index, boolean updated) {
163  errorIfNotEqual(where + "id failed", toString(getStoragePK(index)), toString(instance.getId()));
164  errorIfNotEqual(where + "number failed", index, instance.getNumber());
165  if (updated) {
166  errorIfNotEqual(where + "Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getName());
167  } else {
168  errorIfNotEqual(where + "Value failed", getValue(index), instance.getName());
169 
170  }
171  }
172 
173  protected void verifyResult(String where, BinaryPK instance, int index, boolean updated) {
174  errorIfNotEqual(where + "id failed", toString(getResultPK(index)), toString(instance.getId()));
175  errorIfNotEqual("number failed", index, instance.getNumber());
176  if (updated) {
177  errorIfNotEqual(where + "Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getName());
178  } else {
179  errorIfNotEqual(where + "Value failed", getValue(index), instance.getName());
180 
181  }
182  }
183 
184  private String toString(byte[] id) {
185  StringBuilder builder = new StringBuilder();
186  for (int i = 0; i < id.length; ++i) {
187  builder.append(String.valueOf(id[i]));
188  builder.append('-');
189  }
190  return builder.toString();
191  }
192 
193 }