2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package testsuite.clusterj;
21 import java.util.ArrayList;
22 import java.util.List;
24 import testsuite.clusterj.model.LongIntStringPK;
26 public class LongIntStringPKTest extends AbstractClusterJTest {
28 protected int PK_MODULUS = 2;
29 protected int NUMBER_OF_INSTANCES = PK_MODULUS * PK_MODULUS * PK_MODULUS;
30 protected long PRETTY_BIG_NUMBER = 1000000000000000L;
31 protected List<LongIntStringPK> instances = new ArrayList<LongIntStringPK>();
34 public void localSetUp() {
35 createSessionFactory();
36 session = sessionFactory.getSession();
37 tx = session.currentTransaction();
40 session.deletePersistentAll(LongIntStringPK.class);
42 } catch (Throwable t) {
43 // ignore errors while deleting
46 addTearDownClasses(LongIntStringPK.class);
57 /** Insert all instances.
59 protected void insert() {
60 session.makePersistentAll(instances);
63 /** Find all instances.
65 protected void find() {
66 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
67 Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
68 LongIntStringPK result = session.find(LongIntStringPK.class, key);
69 verify(result, i, false);
73 /** Blind update every fourth instance.
75 protected void update() {
76 // update the instances
77 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
79 LongIntStringPK instance = createInstance(i);
80 instance.setStringvalue(getValue(NUMBER_OF_INSTANCES - i));
81 session.updatePersistent(instance);
82 verify(instance, i, true);
85 // verify the updated instances
86 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
88 Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
89 LongIntStringPK instance = session.find(LongIntStringPK.class, key);
90 verify(instance, i, true);
95 /** Blind delete every fifth instance.
97 protected void delete() {
98 // delete the instances
99 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
101 LongIntStringPK instance = createInstance(i);
102 session.deletePersistent(instance);
105 // verify they have been deleted
106 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
108 Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
109 LongIntStringPK instance = session.find(LongIntStringPK.class, key);
110 errorIfNotEqual("Failed to delete instance: " + i, null, instance);
115 /** The strategy for instances is for the "instance number" to create
116 * the three keys, such that the value of the instance is:
117 * pk1 * PK_MODULUS^2 + pk2 * PK_MODULUS + pk3
120 protected void createInstances() {
121 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
122 LongIntStringPK instance = createInstance(i);
123 // System.out.println(toString(instance));
124 instances.add(instance);
128 /** Create an instance of LongIntStringPK.
129 * @param index the index to use to generate data
130 * @return the instance
132 protected LongIntStringPK createInstance(int index) {
133 LongIntStringPK instance = session.newInstance(LongIntStringPK.class);
134 instance.setLongpk(getPK1(index));
135 instance.setIntpk(getPK2(index));
136 instance.setStringpk(getPK3(index));
137 instance.setStringvalue(getValue(index));
141 protected String toString(LongIntStringPK instance) {
142 StringBuffer result = new StringBuffer();
143 result.append("LongIntStringPK[");
144 result.append(instance.getLongpk());
146 result.append(instance.getIntpk());
147 result.append(",\"");
148 result.append(instance.getStringpk());
149 result.append("\"]: ");
150 result.append(instance.getStringvalue());
152 return result.toString();
155 protected long getPK1(int index) {
156 return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
159 protected int getPK2(int index) {
160 return ((index / PK_MODULUS) % PK_MODULUS);
163 protected String getPK3(int index) {
164 return "" + (index % PK_MODULUS);
167 protected String getValue(int index) {
168 return "Value " + index;
171 protected void verify(LongIntStringPK instance, int index, boolean updated) {
172 errorIfNotEqual("PK1 failed", getPK1(index), instance.getLongpk());
173 errorIfNotEqual("PK2 failed", getPK2(index), instance.getIntpk());
174 errorIfNotEqual("PK3 failed", getPK3(index), instance.getStringpk());
176 errorIfNotEqual("Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getStringvalue());
178 errorIfNotEqual("Value failed", getValue(index), instance.getStringvalue());