2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package testsuite.clusterj;
20 import java.util.ArrayList;
21 import java.util.List;
23 import testsuite.clusterj.model.BinaryPK;
24 import testsuite.clusterj.model.DynamicPK;
26 public class DynamicBinaryPKTest extends AbstractClusterJTest {
28 protected int NUMBER_OF_INSTANCES = 15;
29 protected List<DynamicPK> instances = new ArrayList<DynamicPK>();
32 public void localSetUp() {
33 createSessionFactory();
34 session = sessionFactory.getSession();
35 tx = session.currentTransaction();
39 run(DynamicBinaryPK.class);
40 run(DynamicVarbinaryPK.class);
41 run(DynamicLongvarbinaryPK.class);
45 public void run(Class<? extends DynamicPK> cls) {
54 public static class DynamicBinaryPK extends DynamicPK {
56 public String table() {
61 public static class DynamicVarbinaryPK extends DynamicPK {
63 public String table() {
68 public static class DynamicLongvarbinaryPK extends DynamicPK {
70 public String table() {
71 return "longvarbinarypk";
75 protected void deleteAll(Class<? extends DynamicPK> cls) {
78 session.deletePersistentAll(cls);
80 } catch (Throwable t) {
81 // ignore errors while deleting
85 /** Insert all instances.
87 protected void insert() {
88 session.makePersistentAll(instances);
91 /** Find all instances.
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);
102 /** Blind update every fourth instance.
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) {
109 DynamicPK instance = createInstance(cls, i);
110 instance.setName(getValue(NUMBER_OF_INSTANCES - i));
111 session.updatePersistent(instance);
112 verify(where, instance, i, true);
115 // verify the updated instances
116 where = "update after " + cls.getName();
117 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
119 byte[] key = getPK(i);
120 DynamicPK instance = session.find(cls, key);
121 verify(where, instance, i, true);
126 /** Blind delete every fifth instance.
128 protected void delete(Class<? extends DynamicPK> cls) {
129 // delete the instances
130 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
132 DynamicPK instance = createInstance(cls, i);
133 session.deletePersistent(instance);
136 // verify they have been deleted
137 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
139 byte[] key = getPK(i);
140 DynamicPK instance = session.find(cls, key);
141 errorIfNotEqual("Failed to delete instance: " + i, null, instance);
146 /** The strategy for instances is for the "instance number" to create
147 * the keys by creating a byte[] with the encoded number.
149 protected void createInstances(Class<? extends DynamicPK> cls) {
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);
158 /** Create an instance of DynamicPK.
159 * @param index the index to use to generate data
160 * @return the instance
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));
170 protected String toString(DynamicPK instance) {
171 StringBuffer result = new StringBuffer(instance.getClass().getName());
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();
182 protected byte[] getPK(int index) {
183 byte[] result = new byte[255];
184 result[1] = (byte)(index/256);
185 result[2] = (byte)(index%256);
189 protected String getValue(int index) {
190 return "Value " + index;
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());
197 errorIfNotEqual(where + " Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getName());
199 errorIfNotEqual(where + " Value failed", getValue(index), instance.getName());
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]));
210 return builder.toString();