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.Employee;
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;
27 import com.mysql.clusterj.Query;
29 import java.util.HashSet;
30 import java.util.List;
33 public class QueryPrimaryKeyTest extends AbstractClusterJModelTest {
36 public void localSetUp() {
37 createSessionFactory();
38 session = sessionFactory.getSession();
39 tx = session.currentTransaction();
40 createEmployeeInstances(10);
43 session.deletePersistentAll(Employee.class);
45 } catch (Throwable t) {
46 // ignore errors while deleting
49 session.makePersistentAll(employees);
51 addTearDownClasses(Employee.class);
54 /** Test all queries using the same setup.
55 * Fail if any errors during the tests.
57 public void testPrimaryKey() {
58 primaryKeyBetweenQuery();
59 primaryKeyEqualQuery();
60 primaryKeyGreaterEqualQuery();
61 primaryKeyGreaterThanQuery();
62 primaryKeyLessEqualQuery();
63 primaryKeyLessThanQuery();
66 public void primaryKeyEqualQuery() {
69 // QueryBuilder is the sessionFactory for queries
70 QueryBuilder builder = session.getQueryBuilder();
71 // QueryDomainType is the main interface
72 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
75 PredicateOperand param = dobj.param("id");
77 PredicateOperand column = dobj.get("id");
78 // compare the column with the parameter
79 Predicate compare = column.equal(param);
80 // set the where clause into the query
82 // create a query instance
83 Query query = session.createQuery(dobj);
85 // set the parameter value
86 query.setParameter("id", 8);
88 List<Employee> results = query.getResultList();
89 // consistency check the results
90 consistencyCheck(results);
91 // verify we got the right instance
92 errorIfNotEqual("Wrong employee id returned from query: ",
93 8, results.get(0).getId());
97 public void primaryKeyGreaterThanQuery() {
100 // QueryBuilder is the sessionFactory for queries
101 QueryBuilder builder = session.getQueryBuilder();
102 // QueryDomainType is the main interface
103 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
106 PredicateOperand param = dobj.param("id");
108 PredicateOperand column = dobj.get("id");
109 // compare the column with the parameter
110 Predicate compare = column.greaterThan(param);
111 // set the where clause into the query
113 // create a query instance
114 Query query = session.createQuery(dobj);
116 // set the parameter values
117 query.setParameter("id", 6);
119 List<Employee> results = query.getResultList();
120 // consistency check the results
121 consistencyCheck(results);
122 // verify we got the right instances
123 Set<Integer> expected = new HashSet<Integer>();
127 Set<Integer> actual = new HashSet<Integer>();
128 for (Employee emp: results) {
129 actual.add(emp.getId());
131 errorIfNotEqual("Wrong employee ids returned from query: ",
136 public void primaryKeyGreaterEqualQuery() {
139 // QueryBuilder is the sessionFactory for queries
140 QueryBuilder builder = session.getQueryBuilder();
141 // QueryDomainType is the main interface
142 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
145 PredicateOperand param = dobj.param("id");
147 PredicateOperand column = dobj.get("id");
148 // compare the column with the parameter
149 Predicate compare = column.greaterEqual(param);
150 // set the where clause into the query
152 // create a query instance
153 Query query = session.createQuery(dobj);
155 // set the parameter values
156 query.setParameter("id", 7);
158 List<Employee> results = query.getResultList();
159 // consistency check the results
160 consistencyCheck(results);
161 // verify we got the right instances
162 Set<Integer> expected = new HashSet<Integer>();
166 Set<Integer> actual = new HashSet<Integer>();
167 for (Employee emp: results) {
168 actual.add(emp.getId());
170 errorIfNotEqual("Wrong employee ids returned from query: ",
175 public void primaryKeyLessThanQuery() {
178 // QueryBuilder is the sessionFactory for queries
179 QueryBuilder builder = session.getQueryBuilder();
180 // QueryDomainType is the main interface
181 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
184 PredicateOperand param = dobj.param("id");
186 PredicateOperand column = dobj.get("id");
187 // compare the column with the parameter
188 Predicate compare = column.lessThan(param);
189 // set the where clause into the query
191 // create a query instance
192 Query query = session.createQuery(dobj);
194 // set the parameter values
195 query.setParameter("id", 3);
197 List<Employee> results = query.getResultList();
198 // consistency check the results
199 consistencyCheck(results);
200 // verify we got the right instances
201 Set<Integer> expected = new HashSet<Integer>();
205 Set<Integer> actual = new HashSet<Integer>();
206 for (Employee emp: results) {
207 actual.add(emp.getId());
209 errorIfNotEqual("Wrong employee ids returned from query: ",
214 public void primaryKeyLessEqualQuery() {
217 // QueryBuilder is the sessionFactory for queries
218 QueryBuilder builder = session.getQueryBuilder();
219 // QueryDomainType is the main interface
220 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
223 PredicateOperand param = dobj.param("id");
225 PredicateOperand column = dobj.get("id");
226 // compare the column with the parameter
227 Predicate compare = column.lessEqual(param);
228 // set the where clause into the query
230 // create a query instance
231 Query query = session.createQuery(dobj);
233 // set the parameter values
234 query.setParameter("id", 2);
236 List<Employee> results = query.getResultList();
237 // consistency check the results
238 consistencyCheck(results);
239 // verify we got the right instances
240 Set<Integer> expected = new HashSet<Integer>();
244 Set<Integer> actual = new HashSet<Integer>();
245 for (Employee emp: results) {
246 actual.add(emp.getId());
248 errorIfNotEqual("Wrong employee ids returned from query: ",
253 public void primaryKeyBetweenQuery() {
256 // QueryBuilder is the sessionFactory for queries
257 QueryBuilder builder = session.getQueryBuilder();
258 // QueryDomainType is the main interface
259 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
262 PredicateOperand lower = dobj.param("lower");
264 PredicateOperand upper = dobj.param("upper");
266 PredicateOperand column = dobj.get("id");
267 // compare the column with the parameter
268 Predicate compare = column.between(lower, upper);
269 // set the where clause into the query
271 // create a query instance
272 Query query = session.createQuery(dobj);
274 // set the parameter values
275 query.setParameter("lower", 5);
276 query.setParameter("upper", 7);
278 List<Employee> results = query.getResultList();
279 // consistency check the results
280 consistencyCheck(results);
281 // verify we got the right instances
282 Set<Integer> expected = new HashSet<Integer>();
286 Set<Integer> actual = new HashSet<Integer>();
287 for (Employee emp: results) {
288 actual.add(emp.getId());
290 errorIfNotEqual("Wrong employee ids returned from query: ",