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, version 2.0, 7 as published by the Free Software Foundation. 8 9 This program is also distributed with certain software (including 10 but not limited to OpenSSL) that is licensed under separate terms, 11 as designated in a particular file or component or in included license 12 documentation. The authors of MySQL hereby grant you an additional 13 permission to link the program and your derivative works with the 14 separately licensed software that they have included with MySQL. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License, version 2.0, for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 */ 25 26 package testsuite.clusterj; 27 28 import testsuite.clusterj.model.Dn2id; 29 import com.mysql.clusterj.query.QueryBuilder; 30 import com.mysql.clusterj.query.QueryDomainType; 31 import com.mysql.clusterj.query.Predicate; 32 import com.mysql.clusterj.query.PredicateOperand; 33 import com.mysql.clusterj.Query; 34 import java.util.HashSet; 35 import java.util.List; 36 import java.util.Set; 37 import testsuite.clusterj.model.Dn2id; 38 39 public class QueryUniqueKeyTest extends AbstractClusterJModelTest { 40 41 @Override localSetUp()42 public void localSetUp() { 43 createSessionFactory(); 44 session = sessionFactory.getSession(); 45 tx = session.currentTransaction(); 46 createDn2idInstances(10); 47 try { 48 tx.begin(); 49 session.deletePersistentAll(Dn2id.class); 50 tx.commit(); 51 } catch (Throwable t) { 52 // ignore errors while deleting 53 } 54 tx.begin(); 55 session.makePersistentAll(dn2ids); 56 tx.commit(); 57 addTearDownClasses(Dn2id.class); 58 } 59 60 /** Test all queries using the same setup. 61 * Fail if any errors during the tests. 62 */ testUniqueKey()63 public void testUniqueKey() { 64 uniqueKeyBetweenQuery(); 65 uniqueKeyEqualQuery(); 66 uniqueKeyGreaterEqualQuery(); 67 uniqueKeyGreaterThanQuery(); 68 uniqueKeyLessEqualQuery(); 69 uniqueKeyLessThanQuery(); 70 failOnError(); 71 } uniqueKeyEqualQuery()72 public void uniqueKeyEqualQuery() { 73 74 tx.begin(); 75 // QueryBuilder is the sessionFactory for queries 76 QueryBuilder builder = session.getQueryBuilder(); 77 // QueryDomainType is the main interface 78 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 79 80 // parameter name 81 PredicateOperand param = dobj.param("eid"); 82 // property name 83 PredicateOperand column = dobj.get("eid"); 84 // compare the column with the parameter 85 Predicate compare = column.equal(param); 86 // set the where clause into the query 87 dobj.where(compare); 88 // create a query instance 89 Query query = session.createQuery(dobj); 90 91 // set the parameter value 92 query.setParameter("eid", (long)8); 93 // get the results 94 List<Dn2id> results = query.getResultList(); 95 // consistency check the results 96 consistencyCheck(results); 97 Set<Long> expected = new HashSet<Long>(); 98 expected.add((long)8); 99 Set<Long> actual = new HashSet<Long>(); 100 for (Dn2id dn2id: results) { 101 actual.add(dn2id.getEid()); 102 } 103 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyEqualQuery query: ", 104 expected, actual); 105 tx.commit(); 106 } 107 uniqueKeyGreaterThanQuery()108 public void uniqueKeyGreaterThanQuery() { 109 110 tx.begin(); 111 // QueryBuilder is the sessionFactory for queries 112 QueryBuilder builder = session.getQueryBuilder(); 113 // QueryDomainType is the main interface 114 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 115 116 // parameter name 117 PredicateOperand param = dobj.param("eid"); 118 // property name 119 PredicateOperand column = dobj.get("eid"); 120 // compare the column with the parameter 121 Predicate compare = column.greaterThan(param); 122 // set the where clause into the query 123 dobj.where(compare); 124 // create a query instance 125 Query query = session.createQuery(dobj); 126 127 // set the parameter values 128 query.setParameter("eid", (long)6); 129 // get the results 130 List<Dn2id> results = query.getResultList(); 131 // consistency check the results 132 consistencyCheck(results); 133 // verify we got the right instances 134 Set<Long> expected = new HashSet<Long>(); 135 expected.add((long)7); 136 expected.add((long)8); 137 expected.add((long)9); 138 Set<Long> actual = new HashSet<Long>(); 139 for (Dn2id dn2id: results) { 140 actual.add(dn2id.getEid()); 141 } 142 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterThanQuery query: ", 143 expected, actual); 144 tx.commit(); 145 } 146 uniqueKeyGreaterEqualQuery()147 public void uniqueKeyGreaterEqualQuery() { 148 149 tx.begin(); 150 // QueryBuilder is the sessionFactory for queries 151 QueryBuilder builder = session.getQueryBuilder(); 152 // QueryDomainType is the main interface 153 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 154 155 // parameter name 156 PredicateOperand param = dobj.param("eid"); 157 // property name 158 PredicateOperand column = dobj.get("eid"); 159 // compare the column with the parameter 160 Predicate compare = column.greaterEqual(param); 161 // set the where clause into the query 162 dobj.where(compare); 163 // create a query instance 164 Query query = session.createQuery(dobj); 165 166 // set the parameter values 167 query.setParameter("eid", (long)7); 168 // get the results 169 List<Dn2id> results = query.getResultList(); 170 // consistency check the results 171 consistencyCheck(results); 172 // verify we got the right instances 173 Set<Long> expected = new HashSet<Long>(); 174 expected.add((long)7); 175 expected.add((long)8); 176 expected.add((long)9); 177 Set<Long> actual = new HashSet<Long>(); 178 for (Dn2id dn2id: results) { 179 actual.add(dn2id.getEid()); 180 } 181 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyGreaterEqualQuery query: ", 182 expected, actual); 183 tx.commit(); 184 } 185 uniqueKeyLessThanQuery()186 public void uniqueKeyLessThanQuery() { 187 188 tx.begin(); 189 // QueryBuilder is the sessionFactory for queries 190 QueryBuilder builder = session.getQueryBuilder(); 191 // QueryDomainType is the main interface 192 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 193 194 // parameter name 195 PredicateOperand param = dobj.param("eid"); 196 // property name 197 PredicateOperand column = dobj.get("eid"); 198 // compare the column with the parameter 199 Predicate compare = column.lessThan(param); 200 // set the where clause into the query 201 dobj.where(compare); 202 // create a query instance 203 Query query = session.createQuery(dobj); 204 205 // set the parameter values 206 query.setParameter("eid", (long)3); 207 // get the results 208 List<Dn2id> results = query.getResultList(); 209 // consistency check the results 210 consistencyCheck(results); 211 // verify we got the right instances 212 Set<Long> expected = new HashSet<Long>(); 213 expected.add((long)0); 214 expected.add((long)1); 215 expected.add((long)2); 216 Set<Long> actual = new HashSet<Long>(); 217 for (Dn2id dn2id: results) { 218 actual.add(dn2id.getEid()); 219 } 220 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessThanQuery query: ", 221 expected, actual); 222 tx.commit(); 223 } 224 uniqueKeyLessEqualQuery()225 public void uniqueKeyLessEqualQuery() { 226 227 tx.begin(); 228 // QueryBuilder is the sessionFactory for queries 229 QueryBuilder builder = session.getQueryBuilder(); 230 // QueryDomainType is the main interface 231 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 232 233 // parameter name 234 PredicateOperand param = dobj.param("eid"); 235 // property name 236 PredicateOperand column = dobj.get("eid"); 237 // compare the column with the parameter 238 Predicate compare = column.lessEqual(param); 239 // set the where clause into the query 240 dobj.where(compare); 241 // create a query instance 242 Query query = session.createQuery(dobj); 243 244 // set the parameter values 245 query.setParameter("eid", (long)2); 246 // get the results 247 List<Dn2id> results = query.getResultList(); 248 // consistency check the results 249 consistencyCheck(results); 250 // verify we got the right instances 251 Set<Long> expected = new HashSet<Long>(); 252 expected.add((long)0); 253 expected.add((long)1); 254 expected.add((long)2); 255 Set<Long> actual = new HashSet<Long>(); 256 for (Dn2id dn2id: results) { 257 actual.add(dn2id.getEid()); 258 } 259 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyLessEqualQuery query: ", 260 expected, actual); 261 tx.commit(); 262 } 263 uniqueKeyBetweenQuery()264 public void uniqueKeyBetweenQuery() { 265 266 tx.begin(); 267 // QueryBuilder is the sessionFactory for queries 268 QueryBuilder builder = session.getQueryBuilder(); 269 // QueryDomainType is the main interface 270 QueryDomainType dobj = builder.createQueryDefinition(Dn2id.class); 271 272 // parameter name 273 PredicateOperand lower = dobj.param("lower"); 274 // parameter name 275 PredicateOperand upper = dobj.param("upper"); 276 // property name 277 PredicateOperand column = dobj.get("eid"); 278 // compare the column with the parameter 279 Predicate compare = column.between(lower, upper); 280 // set the where clause into the query 281 dobj.where(compare); 282 // create a query instance 283 Query query = session.createQuery(dobj); 284 285 // set the parameter values 286 query.setParameter("lower", (long)5); 287 query.setParameter("upper", (long)7); 288 // get the results 289 List<Dn2id> results = query.getResultList(); 290 // consistency check the results 291 consistencyCheck(results); 292 // verify we got the right instances 293 Set<Long> expected = new HashSet<Long>(); 294 expected.add((long)5); 295 expected.add((long)6); 296 expected.add((long)7); 297 Set<Long> actual = new HashSet<Long>(); 298 for (Dn2id dn2id: results) { 299 actual.add(dn2id.getEid()); 300 } 301 errorIfNotEqual("Wrong Dn2id eids returned from uniqueKeyBetweenQuery query: ", 302 expected, actual); 303 tx.commit(); 304 } 305 } 306