1 11 package com.triactive.jdo.test; 12 13 import java.util.Collection ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.text.DecimalFormat ; 17 import javax.jdo.PersistenceManager; 18 import javax.jdo.Extent; 19 import javax.jdo.Query; 20 import javax.jdo.Transaction; 21 22 import junit.framework.TestCase; 23 24 25 28 public class QueryFullTest extends PersistenceTestCase 29 { 30 31 private static boolean[] booleanValues = { false, true, false }; 33 34 private static byte[] byteValues; 35 36 private static byte[] normalByteValues = { -0x80, 0, 0x7F }; 37 private static byte[] positiveByteValues = { 0, 0x3F, 0x7F }; 38 39 private static char[] charValues = { 'a', 'm', 'z' }; 40 private static short[] shortValues = { -32768, 0, 32767 }; 41 42 private static int[] intValues = { -0x80000000, 0, 0x7FFFFFFF }; 43 private static long[] longValues = { -8000000000000000L, 0L, 0x7FFFFFFFFFFFFFFFL }; 44 45 private static float[] floatDelta = { 1e-6F, 0, 1.0e33f }; 48 private static double[] doubleDelta = { 1e-15F, 0, (Math.pow(2, 971) + 1.0F) }; 49 50 private static float[] floatValues = { FloatWidget.MIN_FLOAT_VALUE, 1, FloatWidget.MAX_FLOAT_VALUE }; 51 private static double[] doubleValues = { FloatWidget.MIN_DOUBLE_VALUE, 1, FloatWidget.MAX_DOUBLE_VALUE}; 52 53 private static String [] fixedLengthStringValues = { 54 "fixedLengthString1--", 55 "fixedLengthString2--", 56 "fixedLengthString3--" 57 }; 58 private static String [] stringValues = { "stringValue1", "stringValue2", "stringValue3" }; 59 private static java.util.Date [] utilDateValues = { new java.util.Date (System.currentTimeMillis()), 60 new java.util.Date (System.currentTimeMillis() + 4000L), 61 new java.util.Date (System.currentTimeMillis() + 8000L) }; 62 private static java.sql.Date [] sqlDateValues = { java.sql.Date.valueOf("2000-01-01"), 63 java.sql.Date.valueOf("2001-06-31"), 64 java.sql.Date.valueOf("2001-12-31") }; 65 private static java.sql.Timestamp [] sqlTimestampValues = { new java.sql.Timestamp (utilDateValues[0].getTime()), 66 new java.sql.Timestamp (utilDateValues[1].getTime()), 67 new java.sql.Timestamp (utilDateValues[2].getTime()) }; 68 69 private static String hugeStringValue; 70 71 static 74 { 75 StringBuffer s = new StringBuffer (); 76 java.util.Random r = new java.util.Random (); 77 int length = 1024 * 1024; 78 79 while (length-- > 0) 80 { 81 char nextChar = (char) (' ' + r.nextInt(94)); 82 s.append(nextChar); 83 } 84 85 hugeStringValue = s.toString(); 86 } 87 88 89 90 91 96 public QueryFullTest(String name) 97 { 98 super(name); 99 } 100 101 102 103 104 105 106 public void testRelationalOperatorsWithConstantsAndExtentCandidate() 107 { 108 TestDataSet dataset = null; 109 PersistenceManager pm = pmf.getPersistenceManager(); 110 Transaction tx = pm.currentTransaction(); 111 112 try 113 { 114 Query q = 115 pm.newQuery(pm.getExtent(com.triactive.jdo.test.Primitive.class, false)); 116 117 int[][] expectedResultsByOperator = { {1, 0, 2, 1, 3} , 118 {1, 1, 1, 2, 2} , 119 {1, 2, 0, 3, 1} }; 120 121 dataset = new TestDataSet(q, expectedResultsByOperator); 122 123 tryRelationalOperatorsWithConstants(dataset, tx); 124 } 125 finally 126 { 127 if (tx.isActive()) 128 tx.rollback(); 129 130 pm.close(); 131 } 132 } 133 134 135 public void testRelationalOperatorsWithConstantsAndNormalCollectionCandidate() 136 { 137 TestDataSet dataset = null; 138 PersistenceManager pm = pmf.getPersistenceManager(); 139 Transaction tx = pm.currentTransaction(); 140 141 try 142 { 143 Query q = pm.newQuery(pm.getExtent(CollectionFieldTester.class, true)); 145 146 int[][] expectedResultsByOperator = { {1, 0, 2, 1, 3} , 147 {1, 1, 1, 2, 2} , 148 {1, 2, 0, 3, 1} }; 149 150 tx.begin(); 151 152 CollectionFieldTester tester; 153 Collection c = (Collection )q.execute(); 154 155 try 156 { 157 assertEquals(1, c.size()); 158 tester = (CollectionFieldTester)c.iterator().next(); 159 } 160 finally 161 { 162 q.closeAll(); 163 } 164 165 q = pm.newQuery(Primitive.class, tester.getPrimitiveCollection()); 167 tx.commit(); 168 169 dataset = new TestDataSet(q, expectedResultsByOperator); 170 171 tryRelationalOperatorsWithConstants(dataset, tx); 172 } 173 finally 174 { 175 if (tx.isActive()) 176 tx.rollback(); 177 178 pm.close(); 179 } 180 } 181 182 183 public void testRelationalOperatorsWithConstantsAndInverseCollectionCandidate() 184 { 185 TestDataSet dataset = null; 186 PersistenceManager pm = pmf.getPersistenceManager(); 187 Transaction tx = pm.currentTransaction(); 188 189 try 190 { 191 Query q = pm.newQuery(pm.getExtent(CollectionFieldTester.class, true)); 193 194 int[][] expectedResultsByOperator = { {1, 0, 2, 1, 3} , 195 {1, 1, 1, 2, 2} , 196 {1, 2, 0, 3, 1} }; 197 198 tx.begin(); 199 200 CollectionFieldTester tester; 201 Collection c = (Collection )q.execute(); 202 203 try 204 { 205 assertEquals(1, c.size()); 206 tester = (CollectionFieldTester)c.iterator().next(); 207 } 208 finally 209 { 210 q.closeAll(); 211 } 212 213 q = pm.newQuery(InversePrimitive.class, tester.getInversePrimitiveCollection()); 215 assertTrue(!tester.getInversePrimitiveCollection().isEmpty()); 216 tx.commit(); 217 218 dataset = new TestDataSet(q, expectedResultsByOperator); 219 220 tryRelationalOperatorsWithConstants(dataset, tx); 221 } 222 finally 223 { 224 if (tx.isActive()) 225 tx.rollback(); 226 227 pm.close(); 228 } 229 } 230 231 232 236 private void tryRelationalOperatorsWithConstants(TestDataSet dataset, Transaction tx) 237 { 238 String [] operators = { "==", "<", ">", "<=", ">=" }; 239 240 for (int i = 0; i < 3; i++) 242 { 243 for (int j = 0; j < operators.length; j++) 245 { 246 Query q = dataset.getQuery(); 247 248 Collection collection = null; 249 ArrayList filterList = genConstantsFiltersList(operators[j], i); 250 251 for (int l = 0; l < filterList.size(); l++) 252 { 253 String filter = (String ) filterList.get(l); 254 q.setFilter(filter); 255 int expectedCollectionSize = dataset.getResults()[i][j]; 256 257 tx.begin(); 258 collection = (Collection )q.execute(); 259 260 try 261 { 262 assertEquals("Wrong result set size: " + filter, expectedCollectionSize, collection.size()); 263 264 Iterator resultsIterator = collection.iterator(); 265 266 while (resultsIterator.hasNext()) 267 { 268 Primitive p = (Primitive)resultsIterator.next(); 269 assertResult(p, operators[j], i); 270 } 271 } 272 finally 273 { 274 q.close(collection); 275 } 276 277 tx.commit(); 278 } 279 } 280 } 281 } 282 283 284 private ArrayList genConstantsFiltersList(String qOperator, int valueArrayIndex) 285 { 286 ArrayList filterStringList = new ArrayList (); 287 DecimalFormat df = new DecimalFormat ("0.0#####E00"); 288 289 filterStringList.add("byteField " + qOperator + " " + byteValues[valueArrayIndex]); 290 filterStringList.add("byteObjField " + qOperator + " " + byteValues[valueArrayIndex]); 291 filterStringList.add("charField " + qOperator + " '" + charValues[valueArrayIndex] + "'"); 292 filterStringList.add("charObjField " + qOperator + " '" + charValues[valueArrayIndex] + "'"); 293 filterStringList.add("shortField " + qOperator + " " + shortValues[valueArrayIndex]); 294 filterStringList.add("shortObjField " + qOperator + " " + shortValues[valueArrayIndex]); 295 filterStringList.add("intField " + qOperator + " " + intValues[valueArrayIndex]); 296 filterStringList.add("intObjField " + qOperator + " " + intValues[valueArrayIndex]); 297 filterStringList.add("longField " + qOperator + " " + longValues[valueArrayIndex]); 298 filterStringList.add("longObjField " + qOperator + " " + longValues[valueArrayIndex]); 299 filterStringList.add("normalString " + qOperator + " \"" + 300 stringValues[valueArrayIndex] + "\""); 301 filterStringList.add("fixedLengthString " + qOperator + " \"" + 302 fixedLengthStringValues[valueArrayIndex] + "\""); 303 304 if (qOperator.equals("<")) 307 { 308 filterStringList.add("floatField " + qOperator + " " + 309 df.format(0.5 * floatValues[valueArrayIndex])); 310 filterStringList.add("floatObjField " + qOperator + " " + 311 df.format(0.5 * floatValues[valueArrayIndex])); 312 filterStringList.add("doubleField " + qOperator + " " + 313 df.format(0.5 * doubleValues[valueArrayIndex])); 314 filterStringList.add("doubleObjField " + qOperator + " " + 315 df.format(0.5 * doubleValues[valueArrayIndex])); 316 } 317 else if (qOperator.equals(">")) 318 { 319 filterStringList.add("floatField " + qOperator + " " + 320 df.format(2 * floatValues[valueArrayIndex])); 321 filterStringList.add("floatObjField " + qOperator + " " + 322 df.format(2 * floatValues[valueArrayIndex])); 323 filterStringList.add("doubleField " + qOperator + " " + 324 df.format(2 * doubleValues[valueArrayIndex])); 325 filterStringList.add("doubleObjField " + qOperator + " " + 326 df.format(2 * doubleValues[valueArrayIndex])); 327 } 328 329 return filterStringList; 330 } 331 332 333 protected void setUp() throws Exception 334 { 335 super.setUp(); 336 337 if (TestObject.allowNegativeByteValues) 338 byteValues = normalByteValues; 339 else 340 byteValues = positiveByteValues; 341 342 clearData(); 343 344 PersistenceManager pm = pmf.getPersistenceManager(); 345 Transaction tx = pm.currentTransaction(); 346 347 try 348 { 349 Primitive p; 350 CollectionFieldTester tester = new CollectionFieldTester(); 351 352 tx.begin(); 353 354 for (int i = 0; i < 3; i++) 355 { 356 p = new Primitive(); 357 p.setBoolean(booleanValues[i]); 358 p.setBooleanObject(new Boolean (booleanValues[i])); 359 p.setByte(byteValues[i]); 360 p.setByteObject(new Byte (byteValues[i])); 361 p.setChar(charValues[i]); 362 p.setCharObject(new Character (charValues[i])); 363 p.setDouble(doubleValues[i]); 364 p.setDoubleObject(new Double (doubleValues[i])); 365 p.setFixedLengthString(fixedLengthStringValues[i]); 366 p.setFloat(floatValues[i]); 367 p.setFloatObject(new Float (floatValues[i])); 368 p.setHugeString(stringValues[i]); 369 p.setInt(intValues[i]); 370 p.setIntObject(new Integer (intValues[i])); 371 p.setLong(longValues[i]); 372 p.setLongObject(new Long (longValues[i])); 373 p.setNormalString(stringValues[i]); 374 p.setShort(shortValues[i]); 375 p.setShortObject(new Short (shortValues[i])); 376 p.setSqlDateField(sqlDateValues[i]); 377 p.setUtilDateField(utilDateValues[i]); 378 p.setSqlTimestamp(sqlTimestampValues[i]); 379 380 tester.getPrimitiveCollection().add(p); 381 } 382 383 pm.makePersistent(tester); 384 385 for (int i = 0; i < 3; i++) 386 { 387 p = new InversePrimitive(); 388 p.setBoolean(booleanValues[i]); 389 p.setBooleanObject(new Boolean (booleanValues[i])); 390 p.setByte(byteValues[i]); 391 p.setByteObject(new Byte (byteValues[i])); 392 p.setChar(charValues[i]); 393 p.setCharObject(new Character (charValues[i])); 394 p.setDouble(doubleValues[i]); 395 p.setDoubleObject(new Double (doubleValues[i])); 396 p.setFixedLengthString(fixedLengthStringValues[i]); 397 p.setFloat(floatValues[i]); 398 p.setFloatObject(new Float (floatValues[i])); 399 p.setHugeString(stringValues[i]); 400 p.setInt(intValues[i]); 401 p.setIntObject(new Integer (intValues[i])); 402 p.setLong(longValues[i]); 403 p.setLongObject(new Long (longValues[i])); 404 p.setNormalString(stringValues[i]); 405 p.setShort(shortValues[i]); 406 p.setShortObject(new Short (shortValues[i])); 407 p.setSqlDateField(sqlDateValues[i]); 408 p.setUtilDateField(utilDateValues[i]); 409 p.setSqlTimestamp(sqlTimestampValues[i]); 410 411 tester.getInversePrimitiveCollection().add(p); 412 } 413 414 415 tx.commit(); 416 } 417 finally 418 { 419 if (tx.isActive()) 420 tx.rollback(); 421 422 pm.close(); 423 } 424 425 } 426 427 428 public void tearDown() throws Exception 429 { 430 super.tearDown(); 431 clearData(); 432 } 433 434 protected void clearData() 435 { 436 Extent ext = null; 437 java.util.Iterator it = null; 438 PersistenceManager pm = pmf.getPersistenceManager(); 439 Transaction tx = pm.currentTransaction(); 440 441 try 442 { 443 tx.begin(); 445 446 ext = pm.getExtent(InversePrimitive.class, false); 447 it = ext.iterator(); 448 449 while (it.hasNext()) 450 { 451 InversePrimitive ip = (InversePrimitive) it.next(); 452 pm.deletePersistent(ip); 453 } 454 455 tx.commit(); 456 457 458 tx.begin(); 460 461 ext = pm.getExtent(CollectionFieldTester.class, false); 462 it = ext.iterator(); 463 464 while (it.hasNext()) 465 { 466 CollectionFieldTester t = (CollectionFieldTester) it.next(); 467 t.getPrimitiveCollection().clear(); 468 pm.deletePersistent(t); 469 } 470 471 tx.commit(); 472 473 tx.begin(); 475 476 ext = pm.getExtent(Primitive.class, false); 477 it = ext.iterator(); 478 479 while (it.hasNext()) 480 { 481 Primitive p = (Primitive) it.next(); 482 pm.deletePersistent(p); 483 } 484 485 tx.commit(); 486 487 tx.begin(); 489 490 ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 491 it = ext.iterator(); 492 493 while (it.hasNext()) 494 { 495 Manager mgr = (Manager) it.next(); 496 mgr.getSubordinates().clear(); 497 mgr.getDepartments().clear(); 498 } 499 500 tx.commit(); 501 502 tx.begin(); 504 505 ext = pm.getExtent(Employee.class, false); 506 it = ext.iterator(); 507 508 while (it.hasNext()) 509 { 510 Employee emp = (Employee) it.next(); 511 pm.deletePersistent(emp); 512 } 513 514 tx.commit(); 515 tx.begin(); 516 517 ext = pm.getExtent(Department.class, false); 519 it = ext.iterator(); 520 521 while (it.hasNext()) 522 { 523 Department d = (Department) it.next(); 524 pm.deletePersistent(d); 525 } 526 527 tx.commit(); 528 529 tx.begin(); 531 532 ext = pm.getExtent(Manager.class, false); 533 it = ext.iterator(); 534 535 while (it.hasNext()) 536 { 537 Manager mgr = (Manager) it.next(); 538 pm.deletePersistent(mgr); 539 } 540 541 tx.commit(); 542 543 tx.begin(); 545 546 ext = pm.getExtent(Person.class, true); 547 it = ext.iterator(); 548 549 while (it.hasNext()) 550 { 551 Person person = (Person) it.next(); 552 pm.deletePersistent(person); 553 } 554 555 tx.commit(); 556 } 557 finally 558 { 559 if (tx.isActive()) 560 tx.rollback(); 561 562 pm.close(); 563 } 564 } 565 566 567 568 572 class TestDataSet 573 { 574 private Query query; 575 private int[][] resultArray; 576 577 TestDataSet(Query q, int[][] results) 578 { 579 query = q; 580 resultArray = results; 581 } 582 583 public Query getQuery() 584 { 585 return query; 586 } 587 588 public int[][] getResults() 589 { 590 return resultArray; 591 } 592 593 } 594 595 596 private void assertResult(Primitive p, String op, int i) 597 { 598 599 if (op.equals("==")) 600 { 601 assertEquals(booleanValues[i], p.getBoolean()); 602 assertEquals(booleanValues[i], p.getBooleanObject().booleanValue()); 603 assertEquals(byteValues[i], p.getByte()); 604 assertEquals(byteValues[i], p.getByteObject().byteValue()); 605 assertEquals(charValues[i], p.getChar()); 606 assertEquals(charValues[i], p.getCharObject().charValue()); 607 assertTrue(FloatWidget.approximates(doubleValues[i], p.getDouble())); 608 assertTrue(FloatWidget.approximates(doubleValues[i], p.getDoubleObject().doubleValue())); 609 assertEquals(fixedLengthStringValues[i], p.getFixedLengthString()); 610 assertTrue(FloatWidget.approximates(floatValues[i], p.getFloat())); 611 assertTrue(FloatWidget.approximates(floatValues[i], p.getFloatObject().floatValue())); 612 assertEquals(stringValues[i], p.getHugeString()); 613 assertEquals(intValues[i], p.getInt()); 614 assertEquals(intValues[i], p.getIntObject().intValue()); 615 assertEquals(longValues[i], p.getLong()); 616 assertEquals(longValues[i], p.getLongObject().longValue()); 617 assertEquals(stringValues[i], p.getNormalString()); 618 assertEquals(shortValues[i], p.getShort()); 619 assertEquals(shortValues[i], p.getShortObject().shortValue()); 620 assertEquals(utilDateValues[i].getTime() / 1000, p.getUtilDateField().getTime() / 1000); 621 assertEquals(sqlDateValues[i], p.getSqlDateField()); 622 assertEquals(sqlTimestampValues[i].getTime() / 1000, p.getSqlTimestamp().getTime() / 1000); 623 } 624 else if (op.equals("<=")) 625 { 626 assertTrue(p.getByte() <= byteValues[i]); 627 assertTrue(p.getByteObject().byteValue() <= byteValues[i]); 628 assertTrue(p.getChar() <= charValues[i]); 629 assertTrue(p.getCharObject().charValue() <= charValues[i]); 630 assertTrue(p.getFixedLengthString().compareTo(fixedLengthStringValues[i]) <= 0); 633 assertTrue(p.getHugeString().compareTo(stringValues[i]) <= 0); 636 assertTrue(p.getInt() <= intValues[i]); 637 assertTrue(p.getIntObject().intValue() <= intValues[i]); 638 assertTrue(p.getLong() <= longValues[i]); 639 assertTrue(p.getLongObject().longValue() <= longValues[i]); 640 assertTrue(p.getNormalString().compareTo(stringValues[i]) <= 0); 641 assertTrue(p.getShort() <= shortValues[i]); 642 assertTrue(p.getShortObject().shortValue() <= shortValues[i]); 643 assertTrue(p.getUtilDateField().getTime() / 1000 <= utilDateValues[i].getTime() / 1000); 644 assertTrue(p.getSqlDateField().compareTo(sqlDateValues[i]) <= 0); 645 assertTrue(p.getSqlTimestamp().getTime() / 1000 <= sqlTimestampValues[i].getTime() / 1000); 646 } 647 else if (op.equals(">=")) 648 { 649 assertTrue(p.getByte() >= byteValues[i]); 650 assertTrue(p.getByteObject().byteValue() >= byteValues[i]); 651 assertTrue(p.getChar() >= charValues[i]); 652 assertTrue(p.getCharObject().charValue() >= charValues[i]); 653 assertTrue(p.getFixedLengthString().compareTo(fixedLengthStringValues[i]) >= 0); 656 assertTrue(p.getHugeString().compareTo(stringValues[i]) >= 0); 659 assertTrue(p.getInt() >= intValues[i]); 660 assertTrue(p.getIntObject().intValue() >= intValues[i]); 661 assertTrue(p.getLong() >= longValues[i]); 662 assertTrue(p.getLongObject().longValue() >= longValues[i]); 663 assertTrue(p.getNormalString().compareTo(stringValues[i]) >= 0); 664 assertTrue(p.getShort() >= shortValues[i]); 665 assertTrue(p.getShortObject().shortValue() >= shortValues[i]); 666 assertTrue(p.getUtilDateField().getTime() / 1000 >= utilDateValues[i].getTime() / 1000); 667 assertTrue(p.getSqlDateField().compareTo(sqlDateValues[i]) >= 0); 668 assertTrue(p.getSqlTimestamp().getTime() / 1000 >= sqlTimestampValues[i].getTime() / 1000); 669 } 670 else if (op.equals("<")) 671 { 672 assertTrue(p.getByte() < byteValues[i]); 673 assertTrue(p.getByteObject().byteValue() < byteValues[i]); 674 assertTrue(p.getChar() < charValues[i]); 675 assertTrue(p.getCharObject().charValue() < charValues[i]); 676 assertTrue(p.getDouble() < doubleValues[i]); 677 assertTrue(p.getDoubleObject().doubleValue() < doubleValues[i]); 678 assertTrue(p.getFixedLengthString().compareTo(fixedLengthStringValues[i]) < 0); 679 assertTrue(p.getFloat() < floatValues[i]); 680 assertTrue(p.getFloatObject().floatValue() < floatValues[i]); 681 assertTrue(p.getHugeString().compareTo(stringValues[i]) < 0); 682 assertTrue(p.getInt() < intValues[i]); 683 assertTrue(p.getIntObject().intValue() < intValues[i]); 684 assertTrue(p.getLong() < longValues[i]); 685 assertTrue(p.getLongObject().longValue() < longValues[i]); 686 assertTrue(p.getNormalString().compareTo(stringValues[i]) < 0); 687 assertTrue(p.getShort() < shortValues[i]); 688 assertTrue(p.getShortObject().shortValue() < shortValues[i]); 689 assertTrue(p.getUtilDateField().getTime() / 1000 < utilDateValues[i].getTime() / 1000); 690 assertTrue(p.getSqlDateField().compareTo(sqlDateValues[i]) < 0); 691 assertTrue(p.getSqlTimestamp().getTime() / 1000 < sqlTimestampValues[i].getTime() / 1000); 692 } 693 else if (op.equals(">")) 694 { 695 assertTrue(p.getByte() > byteValues[i]); 696 assertTrue(p.getByteObject().byteValue() > byteValues[i]); 697 assertTrue(p.getChar() > charValues[i]); 698 assertTrue(p.getCharObject().charValue() > charValues[i]); 699 assertTrue(p.getDouble() > doubleValues[i]); 700 assertTrue(p.getDoubleObject().doubleValue() > doubleValues[i]); 701 assertTrue(p.getFixedLengthString().compareTo(fixedLengthStringValues[i]) > 0); 702 assertTrue(p.getFloat() > floatValues[i]); 703 assertTrue(p.getFloatObject().floatValue() > floatValues[i]); 704 assertTrue(p.getHugeString().compareTo(stringValues[i]) > 0); 705 assertTrue(p.getInt() > intValues[i]); 706 assertTrue(p.getIntObject().intValue() > intValues[i]); 707 assertTrue(p.getLong() > longValues[i]); 708 assertTrue(p.getLongObject().longValue() > longValues[i]); 709 assertTrue(p.getNormalString().compareTo(stringValues[i]) > 0); 710 assertTrue(p.getShort() > shortValues[i]); 711 assertTrue(p.getShortObject().shortValue() > shortValues[i]); 712 assertTrue(p.getUtilDateField().getTime() / 1000 > utilDateValues[i].getTime() / 1000); 713 assertTrue(p.getSqlDateField().compareTo(sqlDateValues[i]) > 0); 714 assertTrue(p.getSqlTimestamp().getTime() / 1000 > sqlTimestampValues[i].getTime() / 1000); 715 } 716 } 717 } 718 | Popular Tags |