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.VarbinaryPK;
25 public class VarbinaryPKTest extends AbstractClusterJTest {
27 protected int NUMBER_OF_INSTANCES = 15;
28 protected List<VarbinaryPK> instances = new ArrayList<VarbinaryPK>();
31 public void localSetUp() {
32 createSessionFactory();
33 session = sessionFactory.getSession();
34 tx = session.currentTransaction();
37 session.deletePersistentAll(VarbinaryPK.class);
39 } catch (Throwable t) {
40 // ignore errors while deleting
43 addTearDownClasses(VarbinaryPK.class);
54 /** Insert all instances.
56 protected void insert() {
57 session.makePersistentAll(instances);
60 /** Find all instances.
62 protected void find() {
63 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
64 byte[] key = getPK(i);
65 VarbinaryPK result = session.find(VarbinaryPK.class, key);
66 verify(result, i, false);
70 /** Blind update every fourth instance.
72 protected void update() {
73 // update the instances
74 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
76 VarbinaryPK instance = createInstance(i);
77 instance.setName(getValue(NUMBER_OF_INSTANCES - i));
78 session.updatePersistent(instance);
79 verify(instance, i, true);
82 // verify the updated instances
83 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
85 byte[] key = getPK(i);
86 VarbinaryPK instance = session.find(VarbinaryPK.class, key);
87 verify(instance, i, true);
92 /** Blind delete every fifth instance.
94 protected void delete() {
95 // delete the instances
96 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
98 VarbinaryPK instance = createInstance(i);
99 session.deletePersistent(instance);
102 // verify they have been deleted
103 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
105 byte[] key = getPK(i);
106 VarbinaryPK instance = session.find(VarbinaryPK.class, key);
107 errorIfNotEqual("Failed to delete instance: " + i, null, instance);
112 /** The strategy for instances is for the "instance number" to create
113 * the keys by creating a byte[] with the encoded number.
115 protected void createInstances() {
116 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
117 VarbinaryPK instance = createInstance(i);
118 if (getDebug()) System.out.println(toString(instance));
119 instances.add(instance);
123 /** Create an instance of VarbinaryPK.
124 * @param index the index to use to generate data
125 * @return the instance
127 protected VarbinaryPK createInstance(int index) {
128 VarbinaryPK instance = session.newInstance(VarbinaryPK.class);
129 instance.setId(getPK(index));
130 instance.setNumber(index);
131 instance.setName(getValue(index));
135 protected String toString(VarbinaryPK instance) {
136 StringBuffer result = new StringBuffer();
137 result.append("VarbinaryPK[");
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();
147 protected byte[] getPK(int index) {
148 return new byte[] {0, (byte)(index/256), (byte)(index%256)};
151 protected String getValue(int index) {
152 return "Value " + index;
155 protected void verify(VarbinaryPK instance, int index, boolean updated) {
156 errorIfNotEqual("id failed", toString(getPK(index)), toString(instance.getId()));
157 errorIfNotEqual("number failed", index, instance.getNumber());
159 errorIfNotEqual("Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getName());
161 errorIfNotEqual("Value failed", getValue(index), instance.getName());
166 private String toString(byte[] id) {
167 StringBuilder builder = new StringBuilder();
168 for (int i = 0; i < id.length; ++i) {
169 builder.append(String.valueOf(id[i]));
172 return builder.toString();