]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
57092499c5f168a148c3db0244801462bd75d58b
[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.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;
29 import java.util.Set;
30
31 public class QueryTableScanTest extends AbstractClusterJModelTest {
32
33     @Override
34     public void localSetUp() {
35         createSessionFactory();
36         session = sessionFactory.getSession();
37         tx = session.currentTransaction();
38         createEmployeeInstances(10);
39         try {
40             tx.begin();
41             session.deletePersistentAll(Employee.class);
42             tx.commit();
43         } catch (Throwable t) {
44             // ignore errors while deleting
45         }
46         tx.begin();
47         session.makePersistentAll(employees);
48         tx.commit();
49         addTearDownClasses(Employee.class);
50     }
51
52     /** Test all queries using the same setup.
53      * Fail if any errors during the tests.
54      */
55     public void testTableScan() {
56         tableScanEqualQuery();
57         tableScanGreaterEqualQuery();
58         tableScanGreaterThanQuery();
59         tableScanLessEqualQuery();
60         tableScanLessThanQuery();
61         failOnError();
62     }
63     public void tableScanEqualQuery() {
64
65         tx.begin();
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);
70
71         // parameter name
72         PredicateOperand param = dobj.param("name");
73         // property 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 
78         dobj.where(compare);
79         // create a query instance
80         Query query = session.createQuery(dobj);
81
82         // set the parameter value
83         query.setParameter("name", "Employee number 8");
84         // get the results
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());
91         tx.commit();
92     }
93
94     public void tableScanGreaterThanQuery() {
95
96         tx.begin();
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);
101
102         // parameter name
103         PredicateOperand param = dobj.param("name");
104         // property 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 
109         dobj.where(compare);
110         // create a query instance
111         Query query = session.createQuery(dobj);
112
113         // set the parameter values
114         query.setParameter("name", "Employee number 6");
115         // get the results
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>();
121         expected.add(7);
122         expected.add(8);
123         expected.add(9);
124         Set<Integer> actual = new HashSet<Integer>();
125         for (Employee emp: results) {
126             actual.add(emp.getId());
127         }
128         errorIfNotEqual("Wrong employee ids returned from greaterThan query: ",
129                 expected, actual);
130         tx.commit();
131     }
132
133     public void tableScanGreaterEqualQuery() {
134
135         tx.begin();
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);
140
141         // parameter name
142         PredicateOperand param = dobj.param("name");
143         // property 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 
148         dobj.where(compare);
149         // create a query instance
150         Query query = session.createQuery(dobj);
151
152         // set the parameter values
153         query.setParameter("name", "Employee number 7");
154         // get the results
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>();
160         expected.add(7);
161         expected.add(8);
162         expected.add(9);
163         Set<Integer> actual = new HashSet<Integer>();
164         for (Employee emp: results) {
165             actual.add(emp.getId());
166         }
167         errorIfNotEqual("Wrong employee ids returned from greaterEqual query: ",
168                 expected, actual);
169         tx.commit();
170     }
171
172     public void tableScanLessThanQuery() {
173
174         tx.begin();
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);
179
180         // parameter name
181         PredicateOperand param = dobj.param("name");
182         // property 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 
187         dobj.where(compare);
188         // create a query instance
189         Query query = session.createQuery(dobj);
190
191         // set the parameter values
192         query.setParameter("name", "Employee number 3");
193         // get the results
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>();
199         expected.add(0);
200         expected.add(1);
201         expected.add(2);
202         Set<Integer> actual = new HashSet<Integer>();
203         for (Employee emp: results) {
204             actual.add(emp.getId());
205         }
206         errorIfNotEqual("Wrong employee ids returned from lessThan query: ",
207                 expected, actual);
208         tx.commit();
209     }
210
211     public void tableScanLessEqualQuery() {
212
213         tx.begin();
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);
218
219         // parameter name
220         PredicateOperand param = dobj.param("name");
221         // property 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 
226         dobj.where(compare);
227         // create a query instance
228         Query query = session.createQuery(dobj);
229
230         // set the parameter values
231         query.setParameter("name", "Employee number 2");
232         // get the results
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>();
238         expected.add(0);
239         expected.add(1);
240         expected.add(2);
241         Set<Integer> actual = new HashSet<Integer>();
242         for (Employee emp: results) {
243             actual.add(emp.getId());
244         }
245         errorIfNotEqual("Wrong employee ids returned from lessEqual query: ",
246                 expected, actual);
247         tx.commit();
248     }
249
250 }