]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
e3d173ca2e10a0bcd6ad0568812a587601cd5cd7
[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 testsuite.clusterj.model.Dn2id;
22 import com.mysql.clusterj.query.QueryBuilder;
23 import com.mysql.clusterj.query.QueryDomainType;
24 import com.mysql.clusterj.query.Predicate;
25 import com.mysql.clusterj.query.PredicateOperand;
26 import com.mysql.clusterj.Query;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import testsuite.clusterj.model.Dn2id;
31
32 public class QueryUniqueKeyTest extends AbstractClusterJModelTest {
33
34     @Override
35     public void localSetUp() {
36         createSessionFactory();
37         session = sessionFactory.getSession();
38         tx = session.currentTransaction();
39         createDn2idInstances(10);
40         try {
41             tx.begin();
42             session.deletePersistentAll(Dn2id.class);
43             tx.commit();
44         } catch (Throwable t) {
45             // ignore errors while deleting
46         }
47         tx.begin();
48         session.makePersistentAll(dn2ids);
49         tx.commit();
50         addTearDownClasses(Dn2id.class);
51     }
52
53     /** Test all queries using the same setup.
54      * Fail if any errors during the tests.
55      */
56     public void testUniqueKey() {
57         uniqueKeyBetweenQuery();
58         uniqueKeyEqualQuery();
59         uniqueKeyGreaterEqualQuery();
60         uniqueKeyGreaterThanQuery();
61         uniqueKeyLessEqualQuery();
62         uniqueKeyLessThanQuery();
63         failOnError();
64     }
65     public void uniqueKeyEqualQuery() {
66
67         tx.begin();
68         // QueryBuilder is the sessionFactory for queries
69         QueryBuilder builder = session.getQueryBuilder();
70         // QueryDomainType is the main interface
71         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
72
73         // parameter name
74         PredicateOperand param = dobj.param("eid");
75         // property name
76         PredicateOperand column = dobj.get("eid");
77         // compare the column with the parameter
78         Predicate compare = column.equal(param);
79         // set the where clause into the query 
80         dobj.where(compare);
81         // create a query instance
82         Query query = session.createQuery(dobj);
83
84         // set the parameter value
85         query.setParameter("eid", (long)8);
86         // get the results
87         List<Dn2id> results = query.getResultList();
88         // consistency check the results
89         consistencyCheck(results);
90         Set<Long> expected = new HashSet<Long>();
91         expected.add((long)8);
92         Set<Long> actual = new HashSet<Long>();
93         for (Dn2id dn2id: results) {
94             actual.add(dn2id.getEid());
95         }
96         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyEqualQuery query: ",
97                 expected, actual);
98         tx.commit();
99     }
100
101     public void uniqueKeyGreaterThanQuery() {
102
103         tx.begin();
104         // QueryBuilder is the sessionFactory for queries
105         QueryBuilder builder = session.getQueryBuilder();
106         // QueryDomainType is the main interface
107         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
108
109         // parameter name
110         PredicateOperand param = dobj.param("eid");
111         // property name
112         PredicateOperand column = dobj.get("eid");
113         // compare the column with the parameter
114         Predicate compare = column.greaterThan(param);
115         // set the where clause into the query 
116         dobj.where(compare);
117         // create a query instance
118         Query query = session.createQuery(dobj);
119
120         // set the parameter values
121         query.setParameter("eid", (long)6);
122         // get the results
123         List<Dn2id> results = query.getResultList();
124         // consistency check the results
125         consistencyCheck(results);
126         // verify we got the right instances
127         Set<Long> expected = new HashSet<Long>();
128         expected.add((long)7);
129         expected.add((long)8);
130         expected.add((long)9);
131         Set<Long> actual = new HashSet<Long>();
132         for (Dn2id dn2id: results) {
133             actual.add(dn2id.getEid());
134         }
135         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterThanQuery query: ",
136                 expected, actual);
137         tx.commit();
138     }
139
140     public void uniqueKeyGreaterEqualQuery() {
141
142         tx.begin();
143         // QueryBuilder is the sessionFactory for queries
144         QueryBuilder builder = session.getQueryBuilder();
145         // QueryDomainType is the main interface
146         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
147
148         // parameter name
149         PredicateOperand param = dobj.param("eid");
150         // property name
151         PredicateOperand column = dobj.get("eid");
152         // compare the column with the parameter
153         Predicate compare = column.greaterEqual(param);
154         // set the where clause into the query 
155         dobj.where(compare);
156         // create a query instance
157         Query query = session.createQuery(dobj);
158
159         // set the parameter values
160         query.setParameter("eid", (long)7);
161         // get the results
162         List<Dn2id> results = query.getResultList();
163         // consistency check the results
164         consistencyCheck(results);
165         // verify we got the right instances
166         Set<Long> expected = new HashSet<Long>();
167         expected.add((long)7);
168         expected.add((long)8);
169         expected.add((long)9);
170         Set<Long> actual = new HashSet<Long>();
171         for (Dn2id dn2id: results) {
172             actual.add(dn2id.getEid());
173         }
174         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterEqualQuery query: ",
175                 expected, actual);
176         tx.commit();
177     }
178
179     public void uniqueKeyLessThanQuery() {
180
181         tx.begin();
182         // QueryBuilder is the sessionFactory for queries
183         QueryBuilder builder = session.getQueryBuilder();
184         // QueryDomainType is the main interface
185         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
186
187         // parameter name
188         PredicateOperand param = dobj.param("eid");
189         // property name
190         PredicateOperand column = dobj.get("eid");
191         // compare the column with the parameter
192         Predicate compare = column.lessThan(param);
193         // set the where clause into the query 
194         dobj.where(compare);
195         // create a query instance
196         Query query = session.createQuery(dobj);
197
198         // set the parameter values
199         query.setParameter("eid", (long)3);
200         // get the results
201         List<Dn2id> results = query.getResultList();
202         // consistency check the results
203         consistencyCheck(results);
204         // verify we got the right instances
205         Set<Long> expected = new HashSet<Long>();
206         expected.add((long)0);
207         expected.add((long)1);
208         expected.add((long)2);
209         Set<Long> actual = new HashSet<Long>();
210         for (Dn2id dn2id: results) {
211             actual.add(dn2id.getEid());
212         }
213         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessThanQuery query: ",
214                 expected, actual);
215         tx.commit();
216     }
217
218     public void uniqueKeyLessEqualQuery() {
219
220         tx.begin();
221         // QueryBuilder is the sessionFactory for queries
222         QueryBuilder builder = session.getQueryBuilder();
223         // QueryDomainType is the main interface
224         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
225
226         // parameter name
227         PredicateOperand param = dobj.param("eid");
228         // property name
229         PredicateOperand column = dobj.get("eid");
230         // compare the column with the parameter
231         Predicate compare = column.lessEqual(param);
232         // set the where clause into the query 
233         dobj.where(compare);
234         // create a query instance
235         Query query = session.createQuery(dobj);
236
237         // set the parameter values
238         query.setParameter("eid", (long)2);
239         // get the results
240         List<Dn2id> results = query.getResultList();
241         // consistency check the results
242         consistencyCheck(results);
243         // verify we got the right instances
244         Set<Long> expected = new HashSet<Long>();
245         expected.add((long)0);
246         expected.add((long)1);
247         expected.add((long)2);
248         Set<Long> actual = new HashSet<Long>();
249         for (Dn2id dn2id: results) {
250             actual.add(dn2id.getEid());
251         }
252         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessEqualQuery query: ",
253                 expected, actual);
254         tx.commit();
255     }
256
257     public void uniqueKeyBetweenQuery() {
258
259         tx.begin();
260         // QueryBuilder is the sessionFactory for queries
261         QueryBuilder builder = session.getQueryBuilder();
262         // QueryDomainType is the main interface
263         QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class);
264
265         // parameter name
266         PredicateOperand lower = dobj.param("lower");
267         // parameter name
268         PredicateOperand upper = dobj.param("upper");
269         // property name
270         PredicateOperand column = dobj.get("eid");
271         // compare the column with the parameter
272         Predicate compare = column.between(lower, upper);
273         // set the where clause into the query 
274         dobj.where(compare);
275         // create a query instance
276         Query query = session.createQuery(dobj);
277
278         // set the parameter values
279         query.setParameter("lower", (long)5);
280         query.setParameter("upper", (long)7);
281         // get the results
282         List<Dn2id> results = query.getResultList();
283         // consistency check the results
284         consistencyCheck(results);
285         // verify we got the right instances
286         Set<Long> expected = new HashSet<Long>();
287         expected.add((long)5);
288         expected.add((long)6);
289         expected.add((long)7);
290         Set<Long> actual = new HashSet<Long>();
291         for (Dn2id dn2id: results) {
292             actual.add(dn2id.getEid());
293         }
294         errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyBetweenQuery query: ",
295                 expected, actual);
296         tx.commit();
297     }
298 }