1 package org.apache.ojb.broker; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.ojb.broker.metadata.ClassDescriptor; 10 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 11 import org.apache.ojb.broker.query.Criteria; 12 import org.apache.ojb.broker.query.QueryByCriteria; 13 import org.apache.ojb.junit.JUnitExtensions; 14 15 22 public class MultithreadedReadTest extends JUnitExtensions.MultiThreadedTestCase 23 { 24 static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE; 25 static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK; 26 static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT; 27 28 int loops = 2; 29 int concurrentThreads = 19; 30 int numberOfObjects = 30; 31 32 public MultithreadedReadTest(String s) 33 { 34 super(s); 35 } 36 37 public static void main(String [] args) 38 { 39 String [] arr = {MultithreadedReadTest.class.getName()}; 40 junit.textui.TestRunner.main(arr); 41 } 42 43 protected void setUp() throws Exception 44 { 45 super.setUp(); 46 } 47 48 protected void tearDown() throws Exception 49 { 50 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 51 try 52 { 53 changeReferenceSetting(broker, AccountImpl.class, "buyer", true, NONE, NONE, false); 54 changeReferenceSetting(broker, BuyerImpl.class, "address", true, NONE, NONE, false); 55 changeReferenceSetting(broker, BuyerImpl.class, "invoices", true, NONE, NONE, false); 56 changeReferenceSetting(broker, BuyerImpl.class, "articles", true, NONE, NONE, false); 57 } 58 finally 59 { 60 if(broker != null) 61 { 62 broker.close(); 63 } 64 } 65 super.tearDown(); 66 } 67 68 public void testClosedPB() throws Throwable 69 { 70 String name = "testClosedPB_"+System.currentTimeMillis(); 71 Account account = null; 72 PersistenceBroker broker = null; 73 try 74 { 75 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 76 changeReferenceSetting(broker, AccountImpl.class, "buyer", true, OBJECT, OBJECT, false); 77 changeReferenceSetting(broker, BuyerImpl.class, "address", true, OBJECT, OBJECT, false); 78 changeReferenceSetting(broker, BuyerImpl.class, "invoices", true, OBJECT, OBJECT, false); 79 changeReferenceSetting(broker, BuyerImpl.class, "articles", true, OBJECT, OBJECT, false); 80 broker.beginTransaction(); 81 Integer [] ids = prepareTestRead(broker, name, 5); 82 broker.commitTransaction(); 83 broker.clearCache(); 84 85 Criteria crit = new Criteria(); 86 crit.addIn("id", Arrays.asList(ids)); 87 QueryByCriteria query = new QueryByCriteria(Account.class, crit); 88 Collection result = broker.getCollectionByQuery(query); 89 Iterator iter = result.iterator(); 90 account = (Account) iter.next(); 92 while (iter.hasNext()) 93 { 94 iter.next(); 95 } 96 } 97 finally 98 { 99 if (broker != null) broker.close(); 100 } 101 102 TestCaseRunnable tct [] = new TestCaseRunnable[50]; 103 for (int i = 0; i < concurrentThreads; i++) 104 { 105 tct[i] = new TestHandleMaterialize(account, name); 106 } 107 runTestCaseRunnables(tct); 109 } 110 111 115 public void testObjectMaterializationByDifferentThread() throws Exception 116 { 117 for (int k = 0; k < loops; k++) 118 { 119 String searchCriteria = "testObjectMaterializationByDifferentThread_" + System.currentTimeMillis(); 120 PersistenceBroker broker = null; 121 Collection accounts; 122 try 123 { 124 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 125 changeReferenceSetting(broker, AccountImpl.class, "buyer", true, OBJECT, OBJECT, false); 126 changeReferenceSetting(broker, BuyerImpl.class, "address", true, OBJECT, OBJECT, true); 127 changeReferenceSetting(broker, BuyerImpl.class, "invoices", true, OBJECT, OBJECT, true); 128 changeReferenceSetting(broker, BuyerImpl.class, "articles", true, OBJECT, OBJECT, false); 129 broker.beginTransaction(); 130 prepareTestRead(broker, searchCriteria, concurrentThreads); 131 broker.commitTransaction(); 132 broker.clearCache(); 133 134 Criteria crit = new Criteria(); 135 crit.addEqualTo("name", searchCriteria); 136 QueryByCriteria query = new QueryByCriteria(Account.class, crit); 137 accounts = broker.getCollectionByQuery(query); 138 assertEquals(concurrentThreads, accounts.size()); 139 } 140 finally 141 { 142 if (broker != null) broker.close(); 143 } 144 Iterator iter = accounts.iterator(); 145 TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads]; 146 for (int i = 0; i < concurrentThreads; i++) 147 { 148 tct[i] = new TestHandleMaterialize((Account) iter.next(), searchCriteria); 149 } 150 runTestCaseRunnables(tct); 152 } 153 } 154 155 158 public void testMultithreadedRead() throws Exception 159 { 160 String searchCriteria = "testMultithreadedRead_" + System.currentTimeMillis(); 161 PersistenceBroker broker = null; 162 try 163 { 164 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 165 changeReferenceSetting(broker, AccountImpl.class, "buyer", true, OBJECT, OBJECT, false); 166 changeReferenceSetting(broker, BuyerImpl.class, "address", true, OBJECT, OBJECT, false); 167 changeReferenceSetting(broker, BuyerImpl.class, "invoices", true, OBJECT, OBJECT, false); 168 changeReferenceSetting(broker, BuyerImpl.class, "articles", true, OBJECT, OBJECT, false); 169 broker.beginTransaction(); 170 prepareTestRead(broker, searchCriteria, numberOfObjects); 171 broker.commitTransaction(); 172 broker.clearCache(); 173 } 174 finally 175 { 176 if(broker != null) broker.close(); 177 } 178 179 System.out.println(); 180 System.out.println("Multithreaded read of objects - start"); 181 System.out.println("" + concurrentThreads + " concurrent threads read " 182 + numberOfObjects + " objects per thread, loop " + loops + " times"); 183 for (int k = 0; k < loops; k++) 184 { 185 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 186 broker.clearCache(); 187 broker.close(); 188 189 TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads]; 190 for (int i = 0; i < concurrentThreads; i++) 191 { 192 tct[i] = new TestHandleRead(searchCriteria); 193 } 194 runTestCaseRunnables(tct); 196 } 197 198 System.out.println("Multithreaded read of objects - end"); 199 } 200 201 204 public void testMultithreadedLazyRead() throws Exception 205 { 206 String name = "testMultithreadedLazyRead" + System.currentTimeMillis(); 207 PersistenceBroker broker = null; 208 List identityList = null; 209 try 210 { 211 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 212 changeReferenceSetting(broker, AccountImpl.class, "buyer", true, OBJECT, OBJECT, false); 213 changeReferenceSetting(broker, BuyerImpl.class, "address", true, OBJECT, OBJECT, true); 214 changeReferenceSetting(broker, BuyerImpl.class, "invoices", true, OBJECT, OBJECT, true); 215 changeReferenceSetting(broker, BuyerImpl.class, "articles", true, OBJECT, OBJECT, true); 216 broker.beginTransaction(); 217 identityList = prepareTestLazyRead(broker, name, concurrentThreads); 218 broker.commitTransaction(); 219 broker.clearCache(); 220 } 221 finally 222 { 223 if(broker != null) broker.close(); 224 } 225 226 System.out.println(); 227 System.out.println("Multithreaded lazy read of objects - start"); 228 System.out.println("" + concurrentThreads + " concurrent threads read different object with lazy" + 229 " materialization reference, loop " + loops + " times"); 230 for (int k = 0; k < loops; k++) 231 { 232 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 233 broker.clearCache(); 234 broker.close(); 235 236 TestCaseRunnable tct [] = new TestCaseRunnable[concurrentThreads]; 237 for (int i = 0; i < concurrentThreads; i++) 238 { 239 tct[i] = new TestHandleLazyRead(identityList, name); 240 } 241 runTestCaseRunnables(tct); 243 } 244 System.out.println("Multithreaded lazy read of objects - end"); 245 } 246 247 private Integer [] prepareTestRead(PersistenceBroker broker, String name, int numbers) throws Exception 248 { 249 Integer [] ids = new Integer [numbers]; 250 for (int i = 0; i < numbers; i++) 251 { 252 AddressType type = new AddressTypeImpl(name); 253 Address address = new AddressImpl(name, type); 254 Buyer buyer = new BuyerImpl(name, address); 255 buyer.setArticles(buildArticles(name, numbers)); 256 buyer.setInvoices(buildInvoices(name, numbers)); 257 Account account = new AccountImpl(name, buyer); 258 broker.store(account); 259 ids[i] = account.getId(); 260 } 261 return ids; 262 } 263 264 private List prepareTestLazyRead(PersistenceBroker broker, String searchCriteria, int numbers) throws Exception 265 { 266 List result = new ArrayList (); 267 for (int i = 0; i < numbers; i++) 268 { 269 AddressType type = new AddressTypeImpl(searchCriteria); 270 Address address = new AddressImpl(searchCriteria, type); 271 Buyer buyer = new BuyerImpl(searchCriteria, address); 272 buyer.setArticles(buildArticles(searchCriteria, numbers)); 273 buyer.setInvoices(buildInvoices(searchCriteria, numbers)); 274 Account account = new AccountImpl(searchCriteria, buyer); 275 broker.store(account); 276 Identity oid = broker.serviceIdentity().buildIdentity(account); 277 result.add(oid); 278 } 279 return result; 280 } 281 282 private List buildInvoices(String name, int numbers) 283 { 284 List result = new ArrayList (); 285 for(int i = 0; i < numbers; i++) 286 { 287 String invoiceNumber = "I_" + (long)(Math.random() * Long.MAX_VALUE); 288 Invoice invoice = new InvoiceImpl(name, invoiceNumber); 289 result.add(invoice); 290 } 291 return result; 292 } 293 294 private List buildArticles(String name, int numbers) 295 { 296 List result = new ArrayList (); 297 for(int i = 0; i < numbers; i++) 298 { 299 Article a = new ArticleImpl(name, "a article description"); 300 result.add(a); 301 } 302 return result; 303 } 304 305 void changeReferenceSetting(PersistenceBroker broker, Class clazz, String fieldName, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean proxy) 306 { 307 ClassDescriptor cld = broker.getClassDescriptor(clazz); 308 ObjectReferenceDescriptor descriptor = cld.getCollectionDescriptorByName(fieldName); 309 if(descriptor == null) 310 { 311 descriptor = cld.getObjectReferenceDescriptorByName(fieldName); 312 } 313 if(descriptor == null) 314 { 315 throw new RuntimeException ("Field name " + fieldName + " does not represent a reference in class '" + clazz.getName() + "'"); 316 } 317 descriptor.setLazy(proxy); 318 descriptor.setCascadeRetrieve(autoRetrieve); 319 descriptor.setCascadingStore(autoUpdate); 320 descriptor.setCascadingDelete(autoDelete); 321 } 322 323 324 class TestHandleRead extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 328 { 329 String searchCriteria; 330 331 public TestHandleRead(String searchCriteria) 332 { 333 this.searchCriteria = searchCriteria; 334 } 335 336 public void runTestCase() throws Throwable 337 { 338 readByCollection(); 339 readByIterator(); 340 } 341 342 private void readByCollection() throws Exception 343 { 344 PersistenceBroker broker = null; 345 try 346 { 347 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 348 broker.clearCache(); 349 Criteria crit = new Criteria(); 350 crit.addEqualTo("name", searchCriteria); 351 QueryByCriteria query = new QueryByCriteria(Account.class, crit); 352 Collection accounts = broker.getCollectionByQuery(query); 353 assertEquals("Wrong number of expected objects", numberOfObjects, accounts.size()); 354 for (Iterator iter = accounts.iterator(); iter.hasNext();) 355 { 356 Account account = (Account) iter.next(); 357 assertEquals(searchCriteria, account.getName()); 358 assertNotNull("All accounts have a reference to an Buyer", account.getBuyer()); 359 assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress()); 360 assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType()); 361 assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName()); 362 assertNotNull("All buyers have populated 1:n reference to Invoice", account.getBuyer().getInvoices()); 363 assertNotNull("All buyers have populated 1:n reference to Article", account.getBuyer().getArticles()); 364 } 366 } 367 finally 368 { 369 if (broker != null) broker.close(); 370 } 371 } 372 373 private void readByIterator() throws Exception 374 { 375 PersistenceBroker broker = null; 376 try 377 { 378 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 379 broker.clearCache(); 380 Criteria crit = new Criteria(); 381 crit.addEqualTo("name", searchCriteria); 382 QueryByCriteria query = new QueryByCriteria(Account.class, crit); 383 Iterator iter = broker.getIteratorByQuery(query); 384 for (; iter.hasNext();) 385 { 386 Account account = (Account) iter.next(); 387 assertEquals(searchCriteria, account.getName()); 388 assertNotNull("All accounts have a reference to an Buyer", account.getBuyer()); 389 assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress()); 390 assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType()); 391 assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName()); 392 assertNotNull("All buyers have populated 1:n reference to Invoice", account.getBuyer().getInvoices()); 393 assertNotNull("All buyers have populated 1:n reference to Article", account.getBuyer().getArticles()); 394 } 396 } 397 finally 398 { 399 if (broker != null) broker.close(); 400 } 401 } 402 } 403 404 class TestHandleLazyRead extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 405 { 406 List identityList; 407 String name; 408 409 public TestHandleLazyRead(List identityList, String name) 410 { 411 this.identityList = identityList; 412 this.name = name; 413 } 414 415 public void runTestCase() throws Throwable 416 { 417 PersistenceBroker broker = null; 418 Account account = null; 419 try 420 { 421 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 422 Iterator it = identityList.iterator(); 423 while (it.hasNext()) 424 { 425 Identity oid = (Identity) it.next(); 426 account = (Account) broker.getObjectByIdentity(oid); 427 } 428 } 429 finally 430 { 431 if (broker != null) broker.close(); 432 } 433 assertEquals(name, account.getName()); 434 assertNotNull("All accounts have a reference to an Buyer", account.getBuyer()); 435 assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress()); 436 assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType()); 437 assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName()); 438 assertNotNull("All buyers have populated 1:n reference to Invoice", account.getBuyer().getInvoices()); 439 assertNotNull("All buyers have populated 1:n reference to Article", account.getBuyer().getArticles()); 440 } 441 } 442 443 444 class TestHandleMaterialize extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 445 { 446 Account account; 447 String name; 448 449 public TestHandleMaterialize(Account account, String name) 450 { 451 this.account = account; 452 this.name = name; 453 } 454 455 public void runTestCase() throws Throwable 456 { 457 assertEquals(name, account.getName()); 458 assertNotNull("All accounts have a reference to an Buyer", account.getBuyer()); 459 assertEquals(name, account.getBuyer().getName()); 460 assertNotNull("All buyers have a reference to an Address", account.getBuyer().getAddress()); 461 assertEquals(name, account.getBuyer().getAddress().getName()); 462 assertNotNull("All addresses have a reference to an AdressType", account.getBuyer().getAddress().getType()); 463 assertNotNull("All AddressType have a name", account.getBuyer().getAddress().getType().getName()); 464 assertNotNull("All buyers have populated 1:n reference to Invoice", account.getBuyer().getInvoices()); 465 assertNotNull("All buyers have populated 1:n reference to Article", account.getBuyer().getArticles()); 466 } 467 } 468 469 470 public interface Account extends Base 474 { 475 Buyer getBuyer(); 476 477 void setBuyer(Buyer buyer); 478 } 479 480 public static class AccountImpl extends BaseImpl implements Account 481 { 482 Buyer buyer; 483 484 public AccountImpl(String name, Buyer buyer) 485 { 486 super(name); 487 this.buyer = buyer; 488 } 489 490 public AccountImpl(Buyer buyer) 491 { 492 this.buyer = buyer; 493 } 494 495 public AccountImpl() 496 { 497 498 } 499 500 public Buyer getBuyer() 501 { 502 return buyer; 503 } 504 505 public void setBuyer(Buyer buyer) 506 { 507 this.buyer = buyer; 508 } 509 } 510 511 public interface Buyer extends Base 512 { 513 Address getAddress(); 514 void setAddress(Address address); 515 public List getInvoices(); 516 public void setInvoices(List invoices); 517 public List getArticles(); 518 public void setArticles(List articles); 519 } 520 521 public static class BuyerImpl extends BaseImpl implements Buyer 522 { 523 private Address address; 524 private List invoices; 525 private List articles; 526 527 public BuyerImpl(String name, Address address) 528 { 529 super(name); 530 this.address = address; 531 } 532 533 public BuyerImpl(String name, Address address, List invoices, List articles) 534 { 535 super(name); 536 this.address = address; 537 this.invoices = invoices; 538 this.articles = articles; 539 } 540 541 public BuyerImpl(Address address) 542 { 543 this.address = address; 544 } 545 546 public BuyerImpl() 547 { 548 549 } 550 551 public List getInvoices() 552 { 553 return invoices; 554 } 555 556 public void setInvoices(List invoices) 557 { 558 this.invoices = invoices; 559 } 560 561 public List getArticles() 562 { 563 return articles; 564 } 565 566 public void setArticles(List articles) 567 { 568 this.articles = articles; 569 } 570 571 public Address getAddress() 572 { 573 return address; 574 } 575 576 public void setAddress(Address address) 577 { 578 this.address = address; 579 } 580 } 581 582 public interface Address extends Base 583 { 584 AddressType getType(); 585 586 void setType(AddressType type); 587 } 588 589 public static class AddressImpl extends BaseImpl implements Address 590 { 591 AddressType type; 592 593 public AddressImpl(String name, AddressType type) 594 { 595 super(name); 596 this.type = type; 597 } 598 599 public AddressImpl(AddressType type) 600 { 601 this.type = type; 602 } 603 604 public AddressImpl() 605 { 606 607 } 608 609 public AddressType getType() 610 { 611 return type; 612 } 613 614 public void setType(AddressType type) 615 { 616 this.type = type; 617 } 618 } 619 620 public interface AddressType extends Base 621 { 622 } 623 624 public static class AddressTypeImpl extends BaseImpl implements AddressType 625 { 626 public AddressTypeImpl(String name) 627 { 628 super(name); 629 } 630 631 public AddressTypeImpl() 632 { 633 } 634 } 635 636 public interface Base 637 { 638 Integer getId(); 639 640 void setId(Integer id); 641 642 String getName(); 643 644 void setName(String name); 645 } 646 647 public static class BaseImpl 648 { 649 Integer id; 650 String name; 651 652 public BaseImpl(String name) 653 { 654 this.name = name; 655 } 656 657 public BaseImpl() 658 { 659 } 660 661 public Integer getId() 662 { 663 return id; 664 } 665 666 public void setId(Integer id) 667 { 668 this.id = id; 669 } 670 671 public String getName() 672 { 673 return name; 674 } 675 676 public void setName(String name) 677 { 678 this.name = name; 679 } 680 } 681 682 public static interface Invoice extends Base 683 { 684 public String getInvoiceNumber(); 685 public void setInvoiceNumber(String invoiceNumber); 686 } 687 688 public static class InvoiceImpl extends BaseImpl implements Invoice 689 { 690 private String invoiceNumber; 691 private Integer buyerId; 692 693 public InvoiceImpl() 694 { 695 } 696 697 public InvoiceImpl(String name, String invoiceNumber) 698 { 699 super(name); 700 this.invoiceNumber = invoiceNumber; 701 } 702 703 704 public Integer getBuyerId() 705 { 706 return buyerId; 707 } 708 709 public void setBuyerId(Integer buyerId) 710 { 711 this.buyerId = buyerId; 712 } 713 714 public String getInvoiceNumber() 715 { 716 return invoiceNumber; 717 } 718 719 public void setInvoiceNumber(String invoiceNumber) 720 { 721 this.invoiceNumber = invoiceNumber; 722 } 723 } 724 725 public static interface Article extends Base 726 { 727 public String getDescription(); 728 public void setDescription(String description); 729 } 730 731 public static class ArticleImpl extends BaseImpl implements Article 732 { 733 private String description; 734 private Integer buyerId; 735 736 public ArticleImpl() 737 { 738 } 739 740 public ArticleImpl(String name, String description) 741 { 742 super(name); 743 this.description = description; 744 } 745 746 public Integer getBuyerId() 747 { 748 return buyerId; 749 } 750 751 public void setBuyerId(Integer buyerId) 752 { 753 this.buyerId = buyerId; 754 } 755 756 public String getDescription() 757 { 758 return description; 759 } 760 761 public void setDescription(String description) 762 { 763 this.description = description; 764 } 765 } 766 } 767 | Popular Tags |