1 package org.apache.ojb.odmg; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.commons.lang.SerializationUtils; 10 import org.apache.ojb.junit.ODMGTestCase; 11 import org.apache.ojb.odmg.shared.Article; 12 import org.apache.ojb.odmg.shared.Person; 13 import org.apache.ojb.odmg.shared.PersonImpl; 14 import org.apache.ojb.odmg.shared.ProductGroup; 15 import org.odmg.OQLQuery; 16 import org.odmg.Transaction; 17 18 23 public class OQLTest extends ODMGTestCase 24 { 25 private int COUNT = 10; 26 private int id_filter = 2; 27 28 public static void main(String [] args) 29 { 30 String [] arr = {OQLTest.class.getName()}; 31 junit.textui.TestRunner.main(arr); 32 } 33 34 public OQLTest(String name) 35 { 36 super(name); 37 } 38 39 private void createPersons() 40 throws Exception 41 { 42 Transaction tx = odmg.newTransaction(); 43 tx.begin(); 44 for (int i = 0; i < COUNT; i++) 45 { 46 Person aPerson = new PersonImpl(); 47 aPerson.setFirstname("firstname" + i); 48 aPerson.setLastname("lastname" + i); 49 database.makePersistent(aPerson); 50 } 51 tx.commit(); 52 } 53 54 private void deleteData(Class target) 55 throws Exception 56 { 57 Transaction tx = odmg.newTransaction(); 58 tx.begin(); 59 OQLQuery query = odmg.newOQLQuery(); 60 query.create("select allStuff from " + target.getName()); 61 Collection allTargets = (Collection ) query.execute(); 62 Iterator it = allTargets.iterator(); 63 while (it.hasNext()) 64 { 65 database.deletePersistent(it.next()); 66 } 67 tx.commit(); 68 } 69 70 public void testMultipleLoad() throws Exception 71 { 72 String name = "testMultipleLoad_" + System.currentTimeMillis(); 73 Fish fish = new Fish(); 74 fish.setName(name); 75 fish.setTypeOfWater("normal"); 76 77 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 78 tx.begin(); 79 odmg.getDatabase(null).makePersistent(fish); 80 tx.commit(); 81 82 Fish newFish = (Fish) SerializationUtils.clone(fish); 83 newFish.setTypeOfWater("salty"); 84 85 tx = (TransactionExt) odmg.newTransaction(); 86 tx.begin(); 87 tx.getBroker().clearCache(); 88 89 OQLQuery query = odmg.newOQLQuery(); 90 91 query.create("select object from " + Fish.class.getName() + " where foodId=$1"); 93 query.bind(new Integer (newFish.getFoodId())); 94 List result = (List ) query.execute(); 95 96 assertNotNull(result); 97 assertEquals(1, result.size()); 98 Fish current = (Fish) result.get(0); 99 100 assertEquals(name, current.getName()); 101 assertEquals("normal", current.getTypeOfWater()); 102 103 assertEquals("salty", newFish.getTypeOfWater()); 104 tx.lock(newFish, Transaction.WRITE); 105 tx.markDirty(newFish); 106 tx.commit(); 107 108 tx = (TransactionExt) odmg.newTransaction(); 110 tx.begin(); 111 query = odmg.newOQLQuery(); 112 113 query.create("select object from " + Fish.class.getName() + " where foodId=$1"); 115 query.bind(new Integer (newFish.getFoodId())); 116 result = (List ) query.execute(); 117 tx.commit(); 118 119 assertNotNull(result); 120 assertEquals(1, result.size()); 121 current = (Fish) result.get(0); 122 assertEquals(name, current.getName()); 123 assertEquals("salty", current.getTypeOfWater()); 124 125 tx = (TransactionExt) odmg.newTransaction(); 127 tx.begin(); 128 tx.getBroker().clearCache(); 130 query = odmg.newOQLQuery(); 131 132 query.create("select object from " + Fish.class.getName() + " where foodId=$1"); 134 query.bind(new Integer (newFish.getFoodId())); 135 result = (List ) query.execute(); 136 tx.commit(); 137 138 assertNotNull(result); 139 assertEquals(1, result.size()); 140 current = (Fish) result.get(0); 141 assertEquals(name, current.getName()); 142 assertEquals("salty", current.getTypeOfWater()); 143 } 144 145 151 public void testGetWithLiteral() throws Exception 152 { 153 createPersons(); 154 155 Transaction tx = odmg.newTransaction(); 157 tx.begin(); 158 159 OQLQuery query = odmg.newOQLQuery(); 160 String sql = "select allPersons from " + Person.class.getName() + " where id != 5 and id > $1"; 161 query.create(sql); 162 query.bind(new Integer (id_filter)); 163 164 Collection allPersons = (Collection ) query.execute(); 165 166 java.util.Iterator it = allPersons.iterator(); 168 169 while (it.hasNext()) 170 { 171 174 Object result = it.next(); 175 Person value = (Person) result; 176 if (value.getId() <= id_filter) 177 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over " + id_filter); 178 } 179 tx.commit(); 180 } 181 182 public void testQueryIn() throws Exception 183 { 184 186 Transaction tx = odmg.newTransaction(); 187 tx.begin(); 188 189 OQLQuery query1 = odmg.newOQLQuery(); 190 query1.create("select anArticle from " + Article.class.getName() + " where articleId in(30, 31, 32) order by articleId"); 191 List result1 = (List ) query1.execute(); 192 193 Collection ids = new ArrayList (); 194 ids.add(new Integer (30)); 195 ids.add(new Integer (31)); 196 ids.add(new Integer (32)); 197 OQLQuery query2 = odmg.newOQLQuery(); 198 query2.create("select anArticle from " + Article.class.getName() + " where articleId in($1) order by articleId"); 199 query2.bind(ids); 200 List result2 = (List ) query2.execute(); 201 202 assertEquals(result1.size(), result2.size()); 203 tx.commit(); 204 } 205 206 public void testQueryNull() throws Exception 207 { 208 Transaction tx = odmg.newTransaction(); 209 tx.begin(); 210 211 OQLQuery query1 = odmg.newOQLQuery(); 212 query1.create("select anArticle from " + Article.class.getName() + " where is_undefined(articleName)"); 213 List result1 = (List ) query1.execute(); 214 215 OQLQuery query2 = odmg.newOQLQuery(); 216 query2.create("select anArticle from " + Article.class.getName() + " where articleName = nil"); 217 List result2 = (List ) query2.execute(); 218 219 assertEquals(result1.size(), result2.size()); 220 tx.commit(); 221 } 222 223 public void testQueryNotNull() throws Exception 224 { 225 Transaction tx = odmg.newTransaction(); 226 tx.begin(); 227 228 OQLQuery query1 = odmg.newOQLQuery(); 229 query1.create("select anArticle from " + Article.class.getName() + " where is_defined(articleName)"); 230 List result1 = (List ) query1.execute(); 231 232 OQLQuery query2 = odmg.newOQLQuery(); 233 query2.create("select anArticle from " + Article.class.getName() + " where articleName <> nil"); 234 List result2 = (List ) query2.execute(); 235 236 assertEquals(result1.size(), result2.size()); 237 tx.commit(); 238 } 239 240 public void testQueryBetween() throws Exception 241 { 242 Transaction tx = odmg.newTransaction(); 243 244 tx.begin(); 245 246 OQLQuery query1 = odmg.newOQLQuery(); 247 query1.create("select anArticle from " + Article.class.getName() + " where articleId between 30 and 32 order by articleId desc"); 248 List result1 = (List ) query1.execute(); 249 250 OQLQuery query2 = odmg.newOQLQuery(); 251 query2.create("select anArticle from " + Article.class.getName() + " where articleId between $1 and $2 order by articleId asc"); 252 query2.bind(new Integer (30)); 253 query2.bind(new Integer (32)); 254 List result2 = (List ) query2.execute(); 255 256 tx.commit(); 272 273 tx.begin(); 274 OQLQuery query3 = odmg.newOQLQuery(); 275 query3.create("select Article from " + Article.class.getName() + " where articleId between $1 and $2 order by articleId asc"); 276 query3.bind(new Integer (30)); 277 query3.bind(new Integer (32)); 278 List result3 = (List ) query3.execute(); 279 280 OQLQuery query4 = odmg.newOQLQuery(); 281 query4.create("select Article from " + Article.class.getName() + " where articleId between 30 and 32 order by articleId desc"); 282 List result4 = (List ) query4.execute(); 283 284 tx.commit(); 298 299 assertEquals(result1.size(), result2.size()); 300 assertEquals(result1.size(), result3.size()); 301 assertEquals(result2.size(), result3.size()); 302 assertEquals(result1.size(), result4.size()); 303 assertEquals(result2.size(), result4.size()); 304 assertEquals(result1, result4); 305 assertEquals(result2, result3); 306 } 307 308 public void testInListQuery() throws Exception 309 { 310 Mammal elephant = new Mammal(4, "Minnie", 4); 312 Mammal cat = new Mammal(4, "Winston", 4); 313 Reptile snake = new Reptile(4, "Skuzzlebutt", "green"); 314 315 Transaction tx = odmg.newTransaction(); 316 tx.begin(); 317 OQLQuery query = odmg.newOQLQuery(); 318 query.create("select animals from " + InterfaceAnimal.class.getName() + 319 " where name in list (\"Minnie\", \"Winston\", \"Skuzzlebutt\")"); 320 int before = ((Collection ) query.execute()).size(); 321 tx.commit(); 322 323 324 tx = odmg.newTransaction(); 325 tx.begin(); 326 database.makePersistent(elephant); 327 database.makePersistent(cat); 328 database.makePersistent(snake); 329 tx.commit(); 330 331 tx = odmg.newTransaction(); 332 tx.begin(); 333 List animals = (List ) query.execute(); 334 tx.commit(); 335 assertEquals(before + 3, animals.size()); 336 } 337 338 public void testPrefetchQuery() throws Exception 339 { 340 String oql = "select allProductGroups from " + ProductGroup.class.getName() 341 + " where groupId <= $1 order by groupId prefetch allArticlesInGroup"; 342 OQLQuery query = odmg.newOQLQuery(); 343 query.create(oql); 344 query.bind(new Integer (5)); 345 Transaction tx = odmg.newTransaction(); 346 tx.begin(); 347 Collection results = (Collection ) query.execute(); 348 tx.commit(); 349 assertNotNull(results); 350 assertTrue(results.size() > 0); 351 } 352 353 public void testInterfaceQuery() throws Exception 354 { 355 int age = (int)(Math.random() * Integer.MAX_VALUE); 356 int calories = (int)(Math.random() * Integer.MAX_VALUE); 357 int caloriesOther = (int)(Math.random() * Integer.MAX_VALUE); 358 359 Mammal elephant = new Mammal(age, "Jumbo", 4); 361 Mammal cat = new Mammal(age, "Silvester", 4); 362 Reptile snake = new Reptile(age, "Kaa", "green"); 363 364 Fish tuna = new Fish("tuna", calories, "salt"); 367 Fish trout = new Fish("trout", calories, "fresh water"); 368 369 Salad radiccio = new Salad("Radiccio", calories, "red"); 370 Salad lolloverde = new Salad("Lollo verde", caloriesOther, "green"); 371 372 deleteData(InterfaceAnimal.class); 373 deleteData(InterfaceFood.class); 374 375 Transaction tx = odmg.newTransaction(); 376 tx.begin(); 377 database.makePersistent(elephant); 378 database.makePersistent(cat); 379 database.makePersistent(snake); 380 database.makePersistent(tuna); 381 database.makePersistent(trout); 382 database.makePersistent(radiccio); 383 database.makePersistent(lolloverde); 384 tx.commit(); 385 386 tx = odmg.newTransaction(); 387 tx.begin(); 388 OQLQuery query = odmg.newOQLQuery(); 389 query.create("select animals from " + InterfaceAnimal.class.getName() + 390 " where age=$1"); 391 query.bind(new Integer (age)); 392 List animals = (List ) query.execute(); 393 tx.commit(); 394 Iterator it = animals.iterator(); 395 while (it.hasNext()) 396 { 397 Object obj = it.next(); 398 } 400 assertEquals(3, animals.size()); 401 402 query = odmg.newOQLQuery(); 404 tx.begin(); 405 query.create("select food from " + InterfaceFood.class.getName() + 406 " where calories=$1"); 407 query.bind(new Integer (calories)); 408 List food = (List ) query.execute(); 409 tx.commit(); 410 assertEquals(3, food.size()); 411 } 412 413 416 public void _testFunctions() throws Exception 417 { 418 Transaction tx = odmg.newTransaction(); 419 tx.begin(); 420 421 OQLQuery query = odmg.newOQLQuery(); 422 query.create("select anArticle from " + 423 Article.class.getName() + 424 " where upper(articleName) like \"A%\" "); 425 426 List results = (List ) query.execute(); 427 tx.commit(); 428 assertTrue(results.size() > 0); 429 } 430 431 434 public void testReportQueryGroupBy() throws Exception 435 { 436 Transaction tx = odmg.newTransaction(); 437 tx.begin(); 438 439 OQLQuery query = odmg.newOQLQuery(); 440 query.create("select p.groupName, p.allArticlesInGroup.stock, p.allArticlesInGroup.price" + 441 " from " + ProductGroup.class.getName() + 442 " group by groupName, allArticlesInGroup.stock, allArticlesInGroup.price"); 443 444 448 List results = (List ) query.execute(); 449 tx.commit(); 450 assertTrue(results.size() > 0); 451 } 452 453 public void testRepeatableQuery() throws Exception 454 { 455 deleteData(Person.class); 456 createPersons(); 457 458 Transaction tx = odmg.newTransaction(); 460 tx.begin(); 461 462 OQLQuery query = odmg.newOQLQuery(); 463 String sql = "select allPersons from " + Person.class.getName() + " where id != $1 and id > $2"; 464 query.create(sql); 465 query.bind(new Integer (5)); 466 query.bind(new Integer (id_filter)); 467 468 Collection allPersons = (Collection ) query.execute(); 469 Iterator it = allPersons.iterator(); 471 while (it.hasNext()) 472 { 473 476 Object result = it.next(); 477 Person value = (Person) result; 478 if (value.getId() <= id_filter || value.getId()==5) 479 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over " 480 + id_filter + " and not 5"); 481 } 482 tx.commit(); 483 484 tx.begin(); 486 487 query.bind(new Integer (8)); 488 query.bind(new Integer (id_filter)); 489 allPersons = (Collection ) query.execute(); 490 it = allPersons.iterator(); 492 while (it.hasNext()) 493 { 494 497 Object result = it.next(); 498 Person value = (Person) result; 499 if (value.getId() <= id_filter || value.getId()==8) 500 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over " 501 + id_filter + " and not 8"); 502 } 503 504 query.bind(new Integer (9)); 506 query.bind(new Integer (id_filter)); 507 allPersons = (Collection ) query.execute(); 508 it = allPersons.iterator(); 510 while (it.hasNext()) 511 { 512 515 Object result = it.next(); 516 Person value = (Person) result; 517 if (value.getId() <= id_filter || value.getId()==9) 518 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over " 519 + id_filter + " and not 9"); 520 } 521 tx.commit(); 522 } 523 524 525 532 public void _testSubQuery1() throws Exception 533 { 534 Transaction tx = odmg.newTransaction(); 535 tx.begin(); 536 537 OQLQuery query = odmg.newOQLQuery(); 538 query.create("select anArticle from " + 539 Article.class.getName() + 540 " where " + 541 " price >= (select avg(price) from " + 542 Article.class.getName() + 543 " where articleName like \"A%\") "); 544 545 List results = (List ) query.execute(); 546 tx.commit(); 547 assertTrue(results.size() > 0); 548 } 549 550 551 public interface InterfaceAnimal extends Serializable 555 { 556 int getAge(); 557 String getName(); 558 } 559 560 public interface InterfaceFood extends Serializable 561 { 562 String getName(); 563 int getCalories(); 564 565 } 566 567 public static abstract class AbstractAnimal 568 { 569 int animalId; 570 String name; 571 Integer zooId; 572 573 public int getAnimalId() 574 { 575 return animalId; 576 } 577 578 public void setAnimalId(int animalId) 579 { 580 this.animalId = animalId; 581 } 582 583 public String getName() 584 { 585 return name; 586 } 587 588 public void setName(String name) 589 { 590 this.name = name; 591 } 592 593 public Integer getZooId() 594 { 595 return zooId; 596 } 597 598 public void setZooId(Integer zooId) 599 { 600 this.zooId = zooId; 601 } 602 } 603 604 public static class Mammal extends AbstractAnimal implements InterfaceAnimal, Serializable 605 { 606 private int age; 607 private int numLegs; 608 609 public Mammal() 610 { 611 super(); 612 } 613 614 public Mammal(int age, String name, int numLegs) 615 { 616 this.age = age; 617 this.name = name; 618 this.numLegs = numLegs; 619 } 620 621 public String toString() 622 { 623 return "Mammal: id = " + animalId + "\n name = " + name + 624 "\n age = " + age + 625 "\n Number of legs = " + numLegs + 626 "\n zooId = " + zooId; 627 } 628 629 public int getAge() 630 { 631 return age; 632 } 633 634 public int getNumLegs() 635 { 636 return numLegs; 637 } 638 639 public void setNumLegs(int numLegs) 640 { 641 this.numLegs = numLegs; 642 } 643 } 644 645 646 public static class Reptile extends AbstractAnimal implements InterfaceAnimal, Serializable 647 { 648 private int age; 649 private String color; 650 653 public Reptile() 654 { 655 super(); 656 } 657 658 public Reptile(int age, String name, String color) 659 { 660 this.age = age; 661 this.name = name; 662 this.color = color; 663 } 664 665 public int getAge() 666 { 667 return age; 668 } 669 670 public int getAnimalId() 671 { 672 return animalId; 673 } 674 675 public void setAnimalId(int animalId) 676 { 677 this.animalId = animalId; 678 } 679 680 public String getName() 681 { 682 return name; 683 } 684 685 public void setName(String name) 686 { 687 this.name = name; 688 } 689 690 public String getColor() 691 { 692 return color; 693 } 694 695 public void setColor(String color) 696 { 697 this.color = color; 698 } 699 700 public String toString() 701 { 702 return "Reptile: id = " + animalId + "\n name = " + name + 703 "\n age = " + age + 704 "\n color = " + color + 705 "\n zooId = " + zooId; 706 } 707 } 708 709 710 public static class Fish implements InterfaceFood, Serializable 711 { 712 int foodId; 713 String name; 714 int calories; 715 String typeOfWater; 716 717 720 public Fish() 721 { 722 super(); 723 } 724 725 public Fish(String name, int calories, String typeOfWater) 726 { 727 this.calories = calories; 728 this.name = name; 729 this.typeOfWater = typeOfWater; 730 } 731 732 735 public String getName() 736 { 737 return name; 738 } 739 740 743 public int getCalories() 744 { 745 return calories; 746 } 747 748 749 750 754 public String getTypeOfWater() 755 { 756 return typeOfWater; 757 } 758 759 763 public int getFoodId() 764 { 765 return foodId; 766 } 767 768 public String toString() 769 { 770 return "Fish: id = " + foodId + "\n name = " + name + 771 "\n calories = " + calories + 772 "\n Type of water = " + typeOfWater; 773 } 774 775 776 780 public void setCalories(int calories) 781 { 782 this.calories = calories; 783 } 784 785 789 public void setFoodId(int foodId) 790 { 791 this.foodId = foodId; 792 } 793 794 798 public void setName(String name) 799 { 800 this.name = name; 801 } 802 803 807 public void setTypeOfWater(String typeOfWater) 808 { 809 this.typeOfWater = typeOfWater; 810 } 811 812 } 813 814 815 816 public static class Gourmet implements Serializable 817 { 818 int gourmetId; 819 String name; 820 List favoriteFood = new ArrayList (); 821 824 public Gourmet() 825 { 826 super(); 827 } 828 829 public Gourmet(String name) 830 { 831 this.name = name; 832 } 833 834 public List getFavoriteFood() 835 { 836 return favoriteFood; 837 } 838 839 public void addFavoriteFood(InterfaceFood food) 840 { 841 favoriteFood.add(food); 842 } 843 844 848 public int getGourmetId() 849 { 850 return gourmetId; 851 } 852 853 public String toString() 854 { 855 StringBuffer text = new StringBuffer ("Gourmet: id = " + gourmetId + "\n"); 856 text.append("name = "); 857 text.append(name); 858 text.append("\nFavoriteFood:\n"); 859 for(Iterator it = favoriteFood.iterator(); it.hasNext();) 860 { 861 text.append(it.next().toString()); 862 text.append("\n-------\n"); 863 } 864 return text.toString(); 865 } 866 867 868 872 public String getName() 873 { 874 return name; 875 } 876 877 881 public void setFavoriteFood(List favoriteFood) 882 { 883 this.favoriteFood = favoriteFood; 884 } 885 886 890 public void setGourmetId(int gourmetId) 891 { 892 this.gourmetId = gourmetId; 893 } 894 895 899 public void setName(String name) 900 { 901 this.name = name; 902 } 903 904 } 905 906 907 public static class Salad implements InterfaceFood, Serializable 908 { 909 int foodId; 910 String name; 911 int calories; 912 String color; 913 916 public Salad() 917 { 918 super(); 919 } 920 921 public Salad(String name, int calories, String color) 922 { 923 this.name = name; 924 this.calories = calories; 925 this.color = color; 926 } 927 928 931 public String getName() 932 { 933 return name; 934 } 935 936 939 public int getCalories() 940 { 941 return calories; 942 } 943 944 945 946 950 public String getColor() 951 { 952 return color; 953 } 954 955 959 public int getFoodId() 960 { 961 return foodId; 962 } 963 964 public String toString() 965 { 966 return "Salad: id = " + foodId + "\n name = " + name + 967 "\n calories = " + calories + 968 "\n Color = " + color; 969 } 970 971 975 public void setCalories(int calories) 976 { 977 this.calories = calories; 978 } 979 980 984 public void setColor(String color) 985 { 986 this.color = color; 987 } 988 989 993 public void setFoodId(int foodId) 994 { 995 this.foodId = foodId; 996 } 997 998 1002 public void setName(String name) 1003 { 1004 this.name = name; 1005 } 1006 1007 } 1008 1009 public class Zoo implements Serializable 1010 { 1011 private int zooId; 1012 private String name; 1013 private List animals = new ArrayList (); 1014 1015 1018 public Zoo() 1019 { 1020 super(); 1021 } 1022 1023 public Zoo(String name) 1024 { 1025 this.name = name; 1026 } 1027 1028 public List getAnimals() 1029 { 1030 return animals; 1031 } 1032 1033 public void addAnimal(InterfaceAnimal animal) 1034 { 1035 animals.add(animal); 1036 } 1037 1038 public int getZooId() 1039 { 1040 return zooId; 1041 } 1042 1043 public String toString() 1044 { 1045 StringBuffer text = new StringBuffer ("Zoo: id = " + zooId + "\n"); 1046 text.append("name = "); 1047 text.append(name); 1048 text.append("\nAnimals:\n"); 1049 for(Iterator it = animals.iterator(); it.hasNext();) 1050 { 1051 text.append(it.next().toString()); 1052 text.append("\n-------\n"); 1053 } 1054 return text.toString(); 1055 } 1056 1057 1061 public String getName() 1062 { 1063 return name; 1064 } 1065 1066 1070 public void setAnimals(List animals) 1071 { 1072 this.animals = animals; 1073 } 1074 1075 1079 public void setName(String name) 1080 { 1081 this.name = name; 1082 } 1083 1084 1088 public void setZooId(int zooId) 1089 { 1090 this.zooId = zooId; 1091 } 1092 1093 } 1094 1095 1096} 1097 | Popular Tags |