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