1 package org.apache.ojb.broker; 2 3 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 12 import junit.framework.TestCase; 13 import org.apache.ojb.broker.query.Criteria; 14 import org.apache.ojb.broker.query.Query; 15 import org.apache.ojb.broker.query.QueryByCriteria; 16 import org.apache.ojb.broker.query.QueryFactory; 17 import org.apache.ojb.broker.query.UserAlias; 18 19 public class PathTest extends TestCase 20 { 21 22 private static Class CLASS = PathTest.class; 23 private int COUNT = 10; 24 private int id_filter = 10000; 25 private PersistenceBroker broker = null; 26 27 public static void main(String [] args) 28 { 29 String [] arr = {CLASS.getName()}; 30 junit.textui.TestRunner.main(arr); 31 } 32 33 37 public void setUp() 38 { 39 try 40 { 41 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 42 } 43 catch (PBFactoryException e) 44 { 45 } 46 } 47 48 52 public void tearDown() 53 { 54 broker.close(); 55 } 56 57 public PathTest(String name) 58 { 59 super(name); 60 } 61 62 public void testDeleteData() throws Exception 63 { 64 broker.beginTransaction(); 65 Criteria crit = new Criteria(); 66 67 Query query = QueryFactory.newQuery(D.class, crit); 68 Collection Ds = broker.getCollectionByQuery(query); 69 for (Iterator iterator = Ds.iterator(); iterator.hasNext();) 70 { 71 broker.delete(iterator.next()); 72 } 73 query = QueryFactory.newQuery(C.class, crit); 74 Collection Cs = broker.getCollectionByQuery(query); 75 for (Iterator iterator = Cs.iterator(); iterator.hasNext();) 76 { 77 broker.delete(iterator.next()); 78 } 79 query = QueryFactory.newQuery(B.class, crit); 80 Collection Bs = broker.getCollectionByQuery(query); 81 for (Iterator iterator = Bs.iterator(); iterator.hasNext();) 82 { 83 broker.delete(iterator.next()); 84 } 85 query = QueryFactory.newQuery(A.class, crit); 86 Collection As = broker.getCollectionByQuery(query); 87 for (Iterator iterator = As.iterator(); iterator.hasNext();) 88 { 89 broker.delete(iterator.next()); 90 } 91 broker.commitTransaction(); 92 } 93 94 100 private static int NUM_A = 3; 101 private static int NUM_B_PER_A = 4; 102 private static int NUM_C_PER_B = 2; 103 private static int NUM_C1_PER_B = 3; 104 private static int NUM_D_PER_C = 2; 105 106 private static int A_OFFSET = 10000; 107 private static int B_OFFSET = 1000; 108 private static int C_OFFSET = 100; 109 private static int D_OFFSET = 10; 110 111 public void testCreateData() throws Exception 112 { 113 broker.beginTransaction(); 114 115 for (int ia = 0; ia < NUM_A; ia++) 116 { 117 A a = new A(A_OFFSET + A_OFFSET * ia); 118 broker.store(a); 119 System.out.println("A - " + a.getAAttrib()); 120 for (int ib = 0; ib < NUM_B_PER_A; ib++) 121 { 122 B b = new B(A_OFFSET + B_OFFSET * ib); 123 b.setA(a); 124 broker.store(b); 125 System.out.println("\tB - " + b.getBAttrib()); 126 for (int ic = 0; ic < NUM_C_PER_B; ic++) 127 { 128 C c = new C(A_OFFSET + B_OFFSET * ib + C_OFFSET * ic); 129 c.setB(b); 130 for (int id = 0; id < NUM_D_PER_C; id++) 131 { 132 D d = new D(A_OFFSET + B_OFFSET * ib + C_OFFSET * ic + D_OFFSET * id); 133 c.setD(d); 134 broker.store(d); 135 broker.store(c); 136 System.out.println("\t\tC - " + c.getCAttrib()); 137 System.out.println("\t\t\tD - " + d.getDAttrib()); 138 } 139 } 140 for (int ic = 0; ic < NUM_C1_PER_B; ic++) 141 { 142 C1 c1 = new C1(A_OFFSET + B_OFFSET * ib + C_OFFSET * ic); 143 c1.setB(b); 144 for (int id = 0; id < NUM_D_PER_C; id++) 145 { 146 D d = new D(A_OFFSET + B_OFFSET * ib + C_OFFSET * ic + D_OFFSET * id); 147 c1.setD(d); 148 c1.setC1Attrib(c1.getCAttrib() + 1); 149 broker.store(d); 150 broker.store(c1); 151 System.out.println("\t\tC1 - " + c1.getC1Attrib()); 152 System.out.println("\t\t\tD - " + d.getDAttrib()); 153 } 154 } 155 } 156 } 157 158 broker.commitTransaction(); 159 160 broker.clearCache(); 161 162 Criteria crit = new Criteria(); 163 164 Query query = QueryFactory.newQuery(A.class, crit); 165 Collection As = broker.getCollectionByQuery(query); 166 assertEquals(NUM_A, As.size()); 167 168 query = QueryFactory.newQuery(B.class, crit); 169 Collection Bs = broker.getCollectionByQuery(query); 170 int numB = NUM_A * NUM_B_PER_A; 171 assertEquals(numB, Bs.size()); 172 173 query = QueryFactory.newQuery(C.class, crit); 174 Collection Cs = broker.getCollectionByQuery(query); 175 int numC = numB * (NUM_C_PER_B + NUM_C1_PER_B); 176 assertEquals(numC, Cs.size()); 177 178 query = QueryFactory.newQuery(D.class, crit); 179 Collection Ds = broker.getCollectionByQuery(query); 180 int numD = numC * NUM_D_PER_C; 181 assertEquals(numD, Ds.size()); 182 183 } 184 185 233 237 public void testPathClassOnSegment1() throws Exception 238 { 239 try 240 { 241 Criteria crit1 = new Criteria(); 243 crit1.addEqualTo("cSet.c1Attrib", new Integer ("10001")); 244 crit1.addPathClass("cSet", C1.class); 245 246 Query query = new QueryByCriteria(B.class, crit1); 247 248 Collection allBs = broker.getCollectionByQuery(query); 249 250 java.util.Iterator itr = allBs.iterator(); 251 252 assertEquals(1 * NUM_A, allBs.size()); 253 System.out.println("testPathClassOnSegment1() iteration size:" + allBs.size()); 254 while (itr.hasNext()) 255 { 256 B b = (B) itr.next(); 257 System.out.println("Found B: " + b.getId() + " " + b.getBAttrib()); 258 } 259 } 260 catch (Throwable t) 261 { 262 t.printStackTrace(System.out); 263 fail("testPathClassOnSegment1: " + t.getMessage()); 264 } 265 } 266 267 271 public void testPathClassOnSegment2() throws Exception 272 { 273 try 274 { 275 Criteria crit1 = new Criteria(); 277 crit1.addEqualTo("bSet.cSet.c1Attrib", new Integer ("10001")); 278 crit1.addPathClass("bSet.cSet", C1.class); 279 280 Query query = new QueryByCriteria(A.class, crit1); 281 282 Collection allAs = broker.getCollectionByQuery(query); 283 284 java.util.Iterator itr = allAs.iterator(); 285 286 assertEquals(allAs.size(), 1 * NUM_A); 287 System.out.println("testPathClassOnSegment2() iteration size:" + allAs.size()); 288 while (itr.hasNext()) 289 { 290 A a = (A) itr.next(); 291 System.out.println("Found A: " + a.getId() + " " + a.getAAttrib()); 292 } 293 } 294 catch (Throwable t) 295 { 296 t.printStackTrace(System.out); 297 fail("testPathClassOnSegment2: " + t.getMessage()); 298 } 299 } 300 301 305 public void testSingleAlias() throws Exception 306 { 307 try 308 { 309 Criteria crit1 = new Criteria(); 311 crit1.setAlias("cToD1", "cSet"); crit1.addEqualTo("cSet.d.dAttrib", new Integer ("10010")); 314 315 Query query = new QueryByCriteria(B.class, crit1); 316 317 Collection allBs = broker.getCollectionByQuery(query); 318 319 java.util.Iterator itr = allBs.iterator(); 320 321 assertEquals(2 * NUM_A, allBs.size()); 322 System.out.println("testSingleAlias() iteration size:" + allBs.size()); 323 while (itr.hasNext()) 324 { 325 B b = (B) itr.next(); 326 System.out.println("Found B: " + b.getId() + " " + b.getBAttrib()); 327 } 328 } 329 catch (Throwable t) 330 { 331 t.printStackTrace(System.out); 332 fail("testSingleAlias: " + t.getMessage()); 333 } 334 } 335 336 340 public void testTwoAliasesTwoSegments() throws Exception 341 { 342 try 343 { 344 Criteria crit1 = new Criteria(); 346 crit1.setAlias("cToD1", "cSet.d"); 347 crit1.addEqualTo("cSet.d.dAttrib", new Integer ("10010")); 348 349 Criteria crit2 = new Criteria(); 351 crit2.setAlias("cToD2", "cSet.d"); 352 crit2.addEqualTo("cSet.d.dAttrib", new Integer ("10110")); 353 354 crit1.addAndCriteria(crit2); 355 356 Query query = new QueryByCriteria(B.class, crit1); 357 358 Collection allBs = broker.getCollectionByQuery(query); 359 360 java.util.Iterator itr = allBs.iterator(); 361 362 assertEquals(4 * NUM_A, allBs.size()); 363 System.out.println("testTwoAliasesTwoSegments() iteration size:" + allBs.size()); 364 while (itr.hasNext()) 365 { 366 B b = (B) itr.next(); 367 System.out.println("Found B: " + b.getId() + " " + b.getBAttrib()); 368 } 369 } 370 catch (Throwable t) 371 { 372 t.printStackTrace(System.out); 373 fail("testTwoAliasesTwoSegments: " + t.getMessage()); 374 } 375 } 376 377 381 public void testTwoAliasesThreeSegments() throws Exception 382 { 383 try 384 { 385 Criteria crit1 = new Criteria(); 387 crit1.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10010")); 388 crit1.setAlias("bToCToD1", "cSet.d"); 389 390 Criteria crit2 = new Criteria(); 392 crit2.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10110")); 393 crit2.setAlias("bToCToD2", "cSet.d"); 394 395 crit1.addAndCriteria(crit2); 396 397 boolean isDistinct = true; 398 Query query = new QueryByCriteria(A.class, crit1, true); 399 400 Collection allAs = broker.getCollectionByQuery(query); 401 402 java.util.Iterator itr = allAs.iterator(); 403 404 assertEquals(1 * NUM_A, allAs.size()); 405 System.out.println("testTwoAliasesThreeSegments() iteration size:" + allAs.size()); 406 while (itr.hasNext()) 407 { 408 A a = (A) itr.next(); 409 System.out.println("Found A: " + a.getId() + " " + a.getAAttrib()); 410 } 411 } 412 catch (Throwable t) 413 { 414 t.printStackTrace(System.out); 415 fail("testTwoAliasesThreeSegments: " + t.getMessage()); 416 } 417 } 418 419 422 public void testPathClassPerCriteria() throws Exception 423 { 424 try 425 { 426 Criteria crit1 = new Criteria(); 428 crit1.addEqualTo("cSet.cAttrib", new Integer ("10200")); 429 crit1.addPathClass("cSet", C.class); 430 crit1.setAlias("alias1"); 431 432 Criteria crit2 = new Criteria(); 434 crit2.addEqualTo("cSet.c1Attrib", new Integer ("10001")); 435 crit2.addPathClass("cSet", C1.class); 436 crit2.setAlias("alias2"); 437 438 crit1.addAndCriteria(crit2); 439 440 Query query = new QueryByCriteria(B.class, crit1); 441 442 Collection allBs = broker.getCollectionByQuery(query); 443 444 java.util.Iterator itr = allBs.iterator(); 445 446 assertEquals(1 * NUM_A, allBs.size()); 447 System.out.println("testPathClassPerCriteria() iteration size:" + allBs.size()); 448 while (itr.hasNext()) 449 { 450 B b = (B) itr.next(); 451 System.out.println("Found B: " + b.getId() + " " + b.getBAttrib()); 452 } 453 } 454 catch (Throwable t) 455 { 456 t.printStackTrace(System.out); 457 fail("testPathClassPerCriteria: " + t.getMessage()); 458 } 459 } 460 461 465 public void testPathClassPerQuery() throws Exception 466 { 467 try 468 { 469 Criteria crit1 = new Criteria(); 471 crit1.addEqualTo("cSet.c1Attrib", new Integer ("12001")); 472 crit1.setAlias("alias1"); 474 475 Criteria crit2 = new Criteria(); 477 crit2.addEqualTo("cSet.c1Attrib", new Integer ("12101")); 478 crit2.setAlias("alias2"); 480 481 crit1.addAndCriteria(crit2); 482 483 QueryByCriteria query = new QueryByCriteria(B.class, crit1); 484 query.addPathClass("cSet", C1.class); 485 486 Collection allBs = broker.getCollectionByQuery(query); 487 488 java.util.Iterator itr = allBs.iterator(); 489 490 assertEquals(1 * NUM_A, allBs.size()); 491 System.out.println("testPathClassPerQuery() iteration size:" + allBs.size()); 492 while (itr.hasNext()) 493 { 494 B b = (B) itr.next(); 495 System.out.println("Found B: " + b.getId() + " " + b.getBAttrib()); 496 } 497 } 498 catch (Throwable t) 499 { 500 t.printStackTrace(System.out); 501 fail("testPathClassPerQuery: " + t.getMessage()); 502 } 503 } 504 505 509 public void testThreeSegmentsAliasOnSegment2And3() throws Exception 510 { 511 try 512 { 513 Criteria crit1 = new Criteria(); 515 crit1.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10010")); 516 crit1.setAlias("bToCToD1", "cSet.d"); 517 518 Criteria crit2 = new Criteria(); 520 crit2.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10110")); 521 crit2.setAlias("bToCToD2", "cSet.d"); 522 523 crit1.addAndCriteria(crit2); 524 525 boolean isDistinct = true; 526 Query query = new QueryByCriteria(A.class, crit1, isDistinct); 527 528 Collection allAs = broker.getCollectionByQuery(query); 529 530 java.util.Iterator itr = allAs.iterator(); 531 532 assertEquals(1 * NUM_A, allAs.size()); 533 System.out.println("testThreeSegmentsAliasOnSegment2And3() iteration size:" + allAs.size()); 534 while (itr.hasNext()) 535 { 536 A a = (A) itr.next(); 537 System.out.println("Found A: " + a.getId() + " " + a.getAAttrib()); 538 } 539 } 540 catch (Throwable t) 541 { 542 t.printStackTrace(System.out); 543 fail("testThreeSegmentsAliasOnSegment2And3: " + t.getMessage()); 544 } 545 } 546 547 550 public void testThreeSegmentsAliasOnSegment2And3UserAlias() throws Exception 551 { 552 try 553 { 554 UserAlias userAlias1 = new UserAlias("alias1"); 555 userAlias1.add("bSet.cSet"); 556 userAlias1.add("bSet.cSet.d"); 557 558 Criteria crit1 = new Criteria(); 560 crit1.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10010")); 561 crit1.setAlias(userAlias1); 562 563 UserAlias userAlias2 = new UserAlias("alias2"); 565 userAlias2.add("bSet.cSet"); 566 userAlias2.add("bSet.cSet.d"); 567 568 Criteria crit2 = new Criteria(); 569 crit2.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10110")); 570 crit2.setAlias(userAlias2); 571 572 crit1.addAndCriteria(crit2); 573 574 boolean isDistinct = true; 575 Query query = new QueryByCriteria(A.class, crit1, isDistinct); 576 577 Collection allAs = broker.getCollectionByQuery(query); 578 579 java.util.Iterator itr = allAs.iterator(); 580 581 assertEquals(1 * NUM_A, allAs.size()); 582 System.out.println("testThreeSegmentsAliasOnSegment2And3UserAlias() iteration size:" + allAs.size()); 583 while (itr.hasNext()) 584 { 585 A a = (A) itr.next(); 586 System.out.println("Found A: " + a.getId() + " " + a.getAAttrib()); 587 } 588 } 589 catch (Throwable t) 590 { 591 t.printStackTrace(System.out); 592 fail("testThreeSegmentsAliasOnSegment2And3UserAlias: " + t.getMessage()); 593 } 594 } 595 596 public void testSubQueryExists() 597 { 598 Criteria subCrit = new Criteria(); 600 subCrit.addEqualTo(Criteria.PARENT_QUERY_PREFIX + "bSet.cSet.d.dAttrib", new Integer ("10011")); 601 QueryByCriteria subQuery = new QueryByCriteria(A.class, subCrit); 603 604 Criteria crit = new Criteria(); 606 crit.addEqualTo("bSet.cSet.d.dAttrib", new Integer ("10010")); 607 crit.addNotExists(subQuery); 609 QueryByCriteria query = new QueryByCriteria(A.class, crit,true); 610 611 Collection result = broker.getCollectionByQuery(query); 612 assertEquals(1 * NUM_A, result.size()); 613 614 Iterator itr = result.iterator(); 615 while (itr.hasNext()) 616 { 617 A a = (A) itr.next(); 618 System.out.println("Found A: " + a.getId() + " " + a.getAAttrib()); 619 } 620 } 621 622 624 public static class A 625 { 626 private long id; 627 private int aAttrib; 628 private Collection bSet; 629 630 public A() 631 { 632 } 633 634 public A(int aAttrib) 635 { 636 this.aAttrib = aAttrib; 637 this.bSet = new ArrayList (); 638 } 639 640 public int getaAttrib() 642 { 643 return aAttrib; 644 } 645 public Collection getbSet() 647 { 648 return bSet; 649 } 650 public void setbSet(Collection bSet) 652 { 653 this.bSet = bSet; 654 } 655 public void setaAttrib(int aAttrib) 657 { 658 this.aAttrib = aAttrib; 659 } 660 661 664 public int getAAttrib() 665 { 666 return aAttrib; 667 } 668 669 672 public Collection getBSet() 673 { 674 return bSet; 675 } 676 677 680 public long getId() 681 { 682 return id; 683 } 684 685 688 public void setAAttrib(int i) 689 { 690 aAttrib = i; 691 } 692 693 696 public void setBSet(Collection collection) 697 { 698 bSet = collection; 699 } 700 701 704 public void setId(long l) 705 { 706 id = l; 707 } 708 709 } 710 711 public static class B 712 { 713 private long id; 714 private long aId; 715 private int bAttrib; 716 private A a; 717 private Collection cSet; 718 719 public B() 720 { 721 } 722 723 public B(int bAttrib) 724 { 725 this.bAttrib = bAttrib; 726 this.cSet = new ArrayList (); 727 } 728 729 732 public int getBAttrib() 733 { 734 return bAttrib; 735 } 736 737 740 public Collection getCSet() 741 { 742 return cSet; 743 } 744 745 748 public long getId() 749 { 750 return id; 751 } 752 753 756 public void setBAttrib(int i) 757 { 758 bAttrib = i; 759 } 760 761 764 public void setCSet(Collection collection) 765 { 766 cSet = collection; 767 } 768 769 772 public void setId(long l) 773 { 774 id = l; 775 } 776 777 780 public A getA() 781 { 782 return a; 783 } 784 785 788 public void setA(A a) 789 { 790 this.a = a; 791 a.getBSet().add(this); 792 } 793 794 } 795 796 public static class C 797 { 798 private long id; 799 private long bId; 800 private B b; 801 private long dId; 802 private D d; 803 private int cAttrib; 804 805 public C() 806 { 807 } 808 809 public C(int cAttrib) 810 { 811 this.cAttrib = cAttrib; 812 } 813 814 817 public int getCAttrib() 818 { 819 return cAttrib; 820 } 821 822 825 public D getD() 826 { 827 return d; 828 } 829 830 833 public long getId() 834 { 835 return id; 836 } 837 838 841 public void setCAttrib(int i) 842 { 843 cAttrib = i; 844 } 845 846 849 public void setD(D d) 850 { 851 this.d = d; 852 } 853 854 857 public void setId(long l) 858 { 859 id = l; 860 } 861 862 865 public B getB() 866 { 867 return b; 868 } 869 870 873 public void setB(B b) 874 { 875 this.b = b; 876 b.getCSet().add(this); 877 } 878 879 } 880 881 public static class C1 extends C 882 { 883 884 private int c1Attrib; 885 886 889 890 public C1() 891 { 892 } 893 894 public C1(int cAttrib) 895 { 896 super(cAttrib); 897 } 898 899 902 public int getC1Attrib() 903 { 904 return c1Attrib; 905 } 906 907 910 public void setC1Attrib(int i) 911 { 912 c1Attrib = i; 913 } 914 915 } 916 917 public static class D 918 { 919 private long id; 920 private int dAttrib; 921 922 public D() 923 { 924 } 925 926 public D(int dAttrib) 927 { 928 this.dAttrib = dAttrib; 929 } 930 931 934 public int getDAttrib() 935 { 936 return dAttrib; 937 } 938 939 942 public long getId() 943 { 944 return id; 945 } 946 947 950 public void setDAttrib(int i) 951 { 952 dAttrib = i; 953 } 954 955 958 public void setId(long l) 959 { 960 id = l; 961 } 962 963 } 964 965 } 966 967 | Popular Tags |