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;
26 import com.mysql.clusterj.Query;
27 import java.util.HashSet;
28 import java.util.List;
31 public class QueryTableScanTest extends AbstractClusterJModelTest {
34 public void localSetUp() {
35 createSessionFactory();
36 session = sessionFactory.getSession();
37 tx = session.currentTransaction();
38 createEmployeeInstances(10);
41 session.deletePersistentAll(Employee.class);
43 } catch (Throwable t) {
44 // ignore errors while deleting
47 session.makePersistentAll(employees);
49 addTearDownClasses(Employee.class);
52 /** Test all queries using the same setup.
53 * Fail if any errors during the tests.
55 public void testTableScan() {
56 tableScanEqualQuery();
57 tableScanGreaterEqualQuery();
58 tableScanGreaterThanQuery();
59 tableScanLessEqualQuery();
60 tableScanLessThanQuery();
63 public void tableScanEqualQuery() {
66 // QueryBuilder is the sessionFactory for queries
67 QueryBuilder builder = session.getQueryBuilder();
68 // QueryDomainType is the main interface
69 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
72 PredicateOperand param = dobj.param("name");
74 PredicateOperand column = dobj.get("name");
75 // compare the column with the parameter
76 Predicate compare = column.equal(param);
77 // set the where clause into the query
79 // create a query instance
80 Query query = session.createQuery(dobj);
82 // set the parameter value
83 query.setParameter("name", "Employee number 8");
85 List<Employee> results = query.getResultList();
86 // consistency check the results
87 consistencyCheck(results);
88 // verify we got the right instance
89 errorIfNotEqual("Wrong employee id returned from equal query: ",
90 8, results.get(0).getId());
94 public void tableScanGreaterThanQuery() {
97 // QueryBuilder is the sessionFactory for queries
98 QueryBuilder builder = session.getQueryBuilder();
99 // QueryDomainType is the main interface
100 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
103 PredicateOperand param = dobj.param("name");
105 PredicateOperand column = dobj.get("name");
106 // compare the column with the parameter
107 Predicate compare = column.greaterThan(param);
108 // set the where clause into the query
110 // create a query instance
111 Query query = session.createQuery(dobj);
113 // set the parameter values
114 query.setParameter("name", "Employee number 6");
116 List<Employee> results = query.getResultList();
117 // consistency check the results
118 consistencyCheck(results);
119 // verify we got the right instances
120 Set<Integer> expected = new HashSet<Integer>();
124 Set<Integer> actual = new HashSet<Integer>();
125 for (Employee emp: results) {
126 actual.add(emp.getId());
128 errorIfNotEqual("Wrong employee ids returned from greaterThan query: ",
133 public void tableScanGreaterEqualQuery() {
136 // QueryBuilder is the sessionFactory for queries
137 QueryBuilder builder = session.getQueryBuilder();
138 // QueryDomainType is the main interface
139 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
142 PredicateOperand param = dobj.param("name");
144 PredicateOperand column = dobj.get("name");
145 // compare the column with the parameter
146 Predicate compare = column.greaterEqual(param);
147 // set the where clause into the query
149 // create a query instance
150 Query query = session.createQuery(dobj);
152 // set the parameter values
153 query.setParameter("name", "Employee number 7");
155 List<Employee> results = query.getResultList();
156 // consistency check the results
157 consistencyCheck(results);
158 // verify we got the right instances
159 Set<Integer> expected = new HashSet<Integer>();
163 Set<Integer> actual = new HashSet<Integer>();
164 for (Employee emp: results) {
165 actual.add(emp.getId());
167 errorIfNotEqual("Wrong employee ids returned from greaterEqual query: ",
172 public void tableScanLessThanQuery() {
175 // QueryBuilder is the sessionFactory for queries
176 QueryBuilder builder = session.getQueryBuilder();
177 // QueryDomainType is the main interface
178 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
181 PredicateOperand param = dobj.param("name");
183 PredicateOperand column = dobj.get("name");
184 // compare the column with the parameter
185 Predicate compare = column.lessThan(param);
186 // set the where clause into the query
188 // create a query instance
189 Query query = session.createQuery(dobj);
191 // set the parameter values
192 query.setParameter("name", "Employee number 3");
194 List<Employee> results = query.getResultList();
195 // consistency check the results
196 consistencyCheck(results);
197 // verify we got the right instances
198 Set<Integer> expected = new HashSet<Integer>();
202 Set<Integer> actual = new HashSet<Integer>();
203 for (Employee emp: results) {
204 actual.add(emp.getId());
206 errorIfNotEqual("Wrong employee ids returned from lessThan query: ",
211 public void tableScanLessEqualQuery() {
214 // QueryBuilder is the sessionFactory for queries
215 QueryBuilder builder = session.getQueryBuilder();
216 // QueryDomainType is the main interface
217 QueryDomainType dobj = builder.createQueryDefinition(Employee.class);
220 PredicateOperand param = dobj.param("name");
222 PredicateOperand column = dobj.get("name");
223 // compare the column with the parameter
224 Predicate compare = column.lessEqual(param);
225 // set the where clause into the query
227 // create a query instance
228 Query query = session.createQuery(dobj);
230 // set the parameter values
231 query.setParameter("name", "Employee number 2");
233 List<Employee> results = query.getResultList();
234 // consistency check the results
235 consistencyCheck(results);
236 // verify we got the right instances
237 Set<Integer> expected = new HashSet<Integer>();
241 Set<Integer> actual = new HashSet<Integer>();
242 for (Employee emp: results) {
243 actual.add(emp.getId());
245 errorIfNotEqual("Wrong employee ids returned from lessEqual query: ",