]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
02491d6dec27c5f6c78943bea661be641f40af2c
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright 2010 Sun Microsystems, Inc.
3    All rights reserved. Use is subject to license terms.
4
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.
8
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.
13
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
17 */
18
19 package testsuite.clusterj;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import testsuite.clusterj.model.LongIntStringPK;
25
26 public class LongIntStringPKTest extends AbstractClusterJTest {
27
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>();
32
33     @Override
34     public void localSetUp() {
35         createSessionFactory();
36         session = sessionFactory.getSession();
37         tx = session.currentTransaction();
38         try {
39             tx.begin();
40             session.deletePersistentAll(LongIntStringPK.class);
41             tx.commit();
42         } catch (Throwable t) {
43             // ignore errors while deleting
44         }
45         createInstances();
46         addTearDownClasses(LongIntStringPK.class);
47     }
48
49     public void test() {
50         insert();
51         find();
52         update();
53         delete();
54         failOnError();
55     }
56
57     /** Insert all instances.
58      */
59     protected void insert() {
60         session.makePersistentAll(instances);
61     }
62
63     /** Find all instances.
64      */
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);
70         }
71     }
72
73     /** Blind update every fourth instance.
74      */
75     protected void update() {
76         // update the instances
77         for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
78             if (0 == i % 4) {
79                 LongIntStringPK instance = createInstance(i);
80                 instance.setStringvalue(getValue(NUMBER_OF_INSTANCES - i));
81                 session.updatePersistent(instance);
82                 verify(instance, i, true);
83             }
84         }
85         // verify the updated instances
86         for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
87             if (0 == i % 4) {
88                 Object[] key = new Object[]{getPK1(i), getPK2(i), getPK3(i)};
89                 LongIntStringPK instance = session.find(LongIntStringPK.class, key);
90                 verify(instance, i, true);
91             }
92         }
93     }
94
95     /** Blind delete every fifth instance.
96      */
97     protected void delete() {
98         // delete the instances
99         for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
100             if (0 == i % 5) {
101                 LongIntStringPK instance = createInstance(i);
102                 session.deletePersistent(instance);
103             }
104         }
105         // verify they have been deleted
106         for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
107             if (0 == i % 5) {
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);
111             }
112         }
113     }
114
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
118      * 
119      */
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);
125         }
126     }
127
128     /** Create an instance of LongIntStringPK.
129      * @param index the index to use to generate data
130      * @return the instance
131      */
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));
138         return instance;
139     }
140
141     protected String toString(LongIntStringPK instance) {
142         StringBuffer result = new StringBuffer();
143         result.append("LongIntStringPK[");
144         result.append(instance.getLongpk());
145         result.append(",");
146         result.append(instance.getIntpk());
147         result.append(",\"");
148         result.append(instance.getStringpk());
149         result.append("\"]: ");
150         result.append(instance.getStringvalue());
151         result.append(".");
152         return result.toString();
153     }
154
155     protected long getPK1(int index) {
156         return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
157     }
158
159     protected int getPK2(int index) {
160         return ((index / PK_MODULUS) % PK_MODULUS);
161     }
162
163     protected String getPK3(int index) {
164         return "" + (index % PK_MODULUS);
165     }
166
167     protected String getValue(int index) {
168         return "Value " + index;
169     }
170
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());
175         if (updated) {
176             errorIfNotEqual("Value failed", getValue(NUMBER_OF_INSTANCES - index), instance.getStringvalue());
177         } else {
178             errorIfNotEqual("Value failed", getValue(index), instance.getStringvalue());
179
180         }
181     }
182
183 }