2 Copyright (c) 2010, 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.HashOnlyLongIntStringPK;
25 public class HashOnlyLongIntStringPKTest extends AbstractClusterJTest {
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>();
33 public void localSetUp() {
34 createSessionFactory();
35 session = sessionFactory.getSession();
36 tx = session.currentTransaction();
39 session.deletePersistentAll(HashOnlyLongIntStringPK.class);
41 } catch (Throwable t) {
42 // ignore errors while deleting
45 addTearDownClasses(HashOnlyLongIntStringPK.class);
56 /** Insert all instances.
58 protected void insert() {
59 session.makePersistentAll(instances);
62 /** Find all instances.
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);
72 /** Blind update every fourth instance.
74 protected void update() {
75 // update the instances
76 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
78 HashOnlyLongIntStringPK instance = createInstance(i);
79 instance.setStringvalue(getValue(NUMBER_OF_INSTANCES - i));
80 session.updatePersistent(instance);
81 verify(instance, i, true);
84 // verify the updated instances
85 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
87 Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
88 HashOnlyLongIntStringPK instance = session.find(HashOnlyLongIntStringPK.class, key);
89 verify(instance, i, true);
94 /** Blind delete every fifth instance.
96 protected void delete() {
97 // delete the instances
98 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
100 HashOnlyLongIntStringPK instance = createInstance(i);
101 session.deletePersistent(instance);
104 // verify they have been deleted
105 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
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);
114 /** The strategy for instances is for the "instance number" to create
115 * the three keys, such that the value of the instance is:
116 * pk1 * PK_MODULUS^2 + pk2 * PK_MODULUS + pk3
119 protected void createInstances() {
120 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
121 HashOnlyLongIntStringPK instance = createInstance(i);
122 // System.out.println(toString(instance));
123 instances.add(instance);
127 /** Create an instance of HashOnlyLongIntStringPK.
128 * @param index the index to use to generate data
129 * @return the instance
131 protected HashOnlyLongIntStringPK createInstance(int index) {
132 HashOnlyLongIntStringPK instance = session.newInstance(HashOnlyLongIntStringPK.class);
133 instance.setLongpk(getPK1(index));
134 instance.setIntpk(getPK2(index));
135 instance.setStringpk(getPK3(index));
136 instance.setStringvalue(getValue(index));
140 protected String toString(HashOnlyLongIntStringPK instance) {
141 StringBuffer result = new StringBuffer();
142 result.append("HashOnlyLongIntStringPK[");
143 result.append(instance.getLongpk());
145 result.append(instance.getIntpk());
146 result.append(",\"");
147 result.append(instance.getStringpk());
148 result.append("\"]: ");
149 result.append(instance.getStringvalue());
151 return result.toString();
154 protected long getPK1(int index) {
155 return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
158 protected int getPK2(int index) {
159 return ((index / PK_MODULUS) % PK_MODULUS);
162 protected String getPK3(int index) {
163 return "" + (index % PK_MODULUS);
166 protected String getValue(int index) {
167 return "Value " + index;
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());
175 errorIfNotEqual("Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getStringvalue());
177 errorIfNotEqual("Value failed", getValue(index), instance.getStringvalue());