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 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;
30 import testsuite.clusterj.model.Dn2id;
32 public class QueryUniqueKeyTest extends AbstractClusterJModelTest {
35 public void localSetUp() {
36 createSessionFactory();
37 session = sessionFactory.getSession();
38 tx = session.currentTransaction();
39 createDn2idInstances(10);
42 session.deletePersistentAll(Dn2id.class);
44 } catch (Throwable t) {
45 // ignore errors while deleting
48 session.makePersistentAll(dn2ids);
50 addTearDownClasses(Dn2id.class);
53 /** Test all queries using the same setup.
54 * Fail if any errors during the tests.
56 public void testUniqueKey() {
57 uniqueKeyBetweenQuery();
58 uniqueKeyEqualQuery();
59 uniqueKeyGreaterEqualQuery();
60 uniqueKeyGreaterThanQuery();
61 uniqueKeyLessEqualQuery();
62 uniqueKeyLessThanQuery();
65 public void uniqueKeyEqualQuery() {
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);
74 PredicateOperand param = dobj.param("eid");
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
81 // create a query instance
82 Query query = session.createQuery(dobj);
84 // set the parameter value
85 query.setParameter("eid", (long)8);
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());
96 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyEqualQuery query: ",
101 public void uniqueKeyGreaterThanQuery() {
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);
110 PredicateOperand param = dobj.param("eid");
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
117 // create a query instance
118 Query query = session.createQuery(dobj);
120 // set the parameter values
121 query.setParameter("eid", (long)6);
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());
135 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterThanQuery query: ",
140 public void uniqueKeyGreaterEqualQuery() {
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);
149 PredicateOperand param = dobj.param("eid");
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
156 // create a query instance
157 Query query = session.createQuery(dobj);
159 // set the parameter values
160 query.setParameter("eid", (long)7);
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());
174 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterEqualQuery query: ",
179 public void uniqueKeyLessThanQuery() {
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);
188 PredicateOperand param = dobj.param("eid");
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
195 // create a query instance
196 Query query = session.createQuery(dobj);
198 // set the parameter values
199 query.setParameter("eid", (long)3);
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());
213 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessThanQuery query: ",
218 public void uniqueKeyLessEqualQuery() {
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);
227 PredicateOperand param = dobj.param("eid");
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
234 // create a query instance
235 Query query = session.createQuery(dobj);
237 // set the parameter values
238 query.setParameter("eid", (long)2);
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());
252 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessEqualQuery query: ",
257 public void uniqueKeyBetweenQuery() {
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);
266 PredicateOperand lower = dobj.param("lower");
268 PredicateOperand upper = dobj.param("upper");
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
275 // create a query instance
276 Query query = session.createQuery(dobj);
278 // set the parameter values
279 query.setParameter("lower", (long)5);
280 query.setParameter("upper", (long)7);
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());
294 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyBetweenQuery query: ",