1 package org.apache.ojb.broker; 2 3 import org.apache.commons.lang.builder.ToStringBuilder; 4 import org.apache.ojb.junit.PBTestCase; 5 import org.apache.ojb.broker.query.Criteria; 6 import org.apache.ojb.broker.query.QueryByCriteria; 7 import org.apache.ojb.broker.query.QueryFactory; 8 9 import java.io.Serializable ; 10 import java.util.List ; 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 15 19 public class PBListenerTest extends PBTestCase 20 { 21 public static void main(String [] args) 22 { 23 String [] arr = {PBListenerTest.class.getName()}; 24 junit.textui.TestRunner.main(arr); 25 } 26 27 public PBListenerTest(String name) 28 { 29 super(name); 30 } 31 32 36 public void testStoreAndQuery() throws Exception 37 { 38 String name = "testStoreAndQuery_" + System.currentTimeMillis(); 39 40 PBAwareObject pba_1 = new PBAwareObject(name); 41 pba_1.setRefObject(new RefObj(name)); 42 pba_1.addCollObject(new CollObj(name)); 43 pba_1.addCollObject(new CollObj(name)); 44 pba_1.addCollObject(new CollObj(name)); 45 46 PBAwareObject pba_2 = new PBAwareObject(name); 47 pba_2.setRefObject(new RefObj(name)); 48 pba_2.addCollObject(new CollObj(name)); 49 pba_2.addCollObject(new CollObj(name)); 50 pba_2.addCollObject(new CollObj(name)); 51 52 PBAwareObject pba_3 = new PBAwareObject(name); 53 pba_3.setRefObject(new RefObj(name)); 54 pba_3.addCollObject(new CollObj(name)); 55 pba_3.addCollObject(new CollObj(name)); 56 pba_3.addCollObject(new CollObj(name)); 57 58 broker.beginTransaction(); 59 broker.store(pba_1); 60 broker.store(pba_2); 61 broker.store(pba_3); 62 broker.commitTransaction(); 63 64 Identity oid_1 = broker.serviceIdentity().buildIdentity(pba_1); 65 Identity oid_2 = broker.serviceIdentity().buildIdentity(pba_2); 66 67 broker.clearCache(); 68 69 PBAwareObject pba_1_new = (PBAwareObject) broker.getObjectByIdentity(oid_1); 70 PBAwareObject pba_2_new = (PBAwareObject) broker.getObjectByIdentity(oid_2); 71 72 assertNotNull(pba_1_new); 73 assertNotNull(pba_2_new); 74 assertNotNull(pba_1_new.getRefObject()); 75 assertNotNull(pba_2_new.getRefObject()); 76 assertEquals(3, pba_1_new.getCollObjects().size()); 77 assertEquals(3, pba_2_new.getCollObjects().size()); 78 assertTrue(pba_1_new.isAfterLookupRefObjectPopulated()); 79 assertTrue(pba_1_new.isAfterLookupCollObjectsPopulated()); 80 assertTrue(pba_2_new.isAfterLookupRefObjectPopulated()); 81 assertTrue(pba_2_new.isAfterLookupCollObjectsPopulated()); 82 83 broker.clearCache(); 84 85 Criteria criteria = new Criteria(); 86 criteria.addEqualTo( "name", name); 87 QueryByCriteria query = QueryFactory.newQuery(PBAwareObject.class, criteria); 88 Collection result = broker.getCollectionByQuery(query); 89 assertEquals(3, result.size()); 90 for(Iterator iterator = result.iterator(); iterator.hasNext();) 91 { 92 PBAwareObject pba = (PBAwareObject) iterator.next(); 93 assertNotNull(pba); 94 assertNotNull(pba.getRefObject()); 95 assertEquals(3, pba.getCollObjects().size()); 96 assertTrue(pba.isAfterLookupRefObjectPopulated()); 97 assertTrue(pba.isAfterLookupCollObjectsPopulated()); 98 } 99 } 100 101 104 public void testStoreCallbacks() throws Exception 105 { 106 PBAwareObject obj = new PBAwareObject(); 107 assertEquals(false, obj.getCalledBeforeUpdate()); 108 assertEquals(false, obj.getCalledAfterUpdate()); 109 assertEquals(false, obj.getCalledBeforeStore()); 110 assertEquals(false, obj.getCalledAfterStore()); 111 broker.beginTransaction(); 112 broker.store(obj); 113 assertEquals(true, obj.getCalledBeforeStore()); 114 assertEquals(true, obj.getCalledAfterStore()); 115 assertEquals(false, obj.getCalledBeforeUpdate()); 116 assertEquals(false, obj.getCalledAfterUpdate()); 117 broker.commitTransaction(); 118 } 119 120 123 public void testUpdateCallbacks() throws Exception 124 { 125 PBAwareObject obj = new PBAwareObject(); 126 broker.beginTransaction(); 127 broker.store(obj); 128 broker.commitTransaction(); 129 Identity oid = new Identity(obj, broker); 130 131 PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid); 132 lookedUp.setName("testUpdateCallbacks"); 133 assertEquals(false, obj.getCalledBeforeUpdate()); 134 assertEquals(false, obj.getCalledAfterUpdate()); 135 broker.beginTransaction(); 136 broker.store(lookedUp); 137 broker.commitTransaction(); 138 assertEquals(true, lookedUp.getCalledBeforeUpdate()); 139 assertEquals(true, lookedUp.getCalledAfterUpdate()); 140 } 141 142 145 public void testDeleteCallbacks() throws Exception 146 { 147 PBAwareObject obj = new PBAwareObject(); 148 broker.beginTransaction(); 149 broker.store(obj); 150 broker.commitTransaction(); 151 152 assertEquals(false, obj.getCalledBeforeDelete()); 153 assertEquals(false, obj.getCalledAfterDelete()); 154 broker.beginTransaction(); 155 broker.delete(obj); 156 assertEquals(true, obj.getCalledBeforeDelete()); 157 assertEquals(true, obj.getCalledAfterDelete()); 158 broker.commitTransaction(); 159 } 160 161 164 public void testLookupCallback() throws Exception 165 { 166 PBAwareObject obj = new PBAwareObject(); 167 Identity oid = new Identity(obj, broker); 168 broker.beginTransaction(); 169 broker.store(obj); 170 broker.commitTransaction(); 171 assertEquals(false, obj.getCalledAfterLookup()); 172 173 PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid); 174 175 assertEquals(true, lookedUp.getCalledAfterLookup()); 176 } 177 178 public void testLifeCycleListener() 179 { 180 PBAwareObject obj = new PBAwareObject(); 181 obj.setName("testLifeCycleListener"); 182 Identity oid = new Identity(obj, broker); 183 184 PBLifeCycleListenerObject listener = new PBLifeCycleListenerObject(); 186 broker.addListener(listener); 187 188 broker.beginTransaction(); 189 broker.store(obj); 190 assertEquals("insert listener call failed", 6, listener.evaluateTest()); 191 broker.commitTransaction(); 192 193 broker.clearCache(); 194 PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid); 195 assertEquals("lookup listener call failed", 30, listener.evaluateTest()); 196 lookedUp.setName("testLifeCycleListener_updated"); 197 broker.beginTransaction(); 198 broker.store(lookedUp); 199 assertEquals("update listener call failed", 2310, listener.evaluateTest()); 200 broker.commitTransaction(); 201 202 broker.beginTransaction(); 203 broker.delete(obj); 204 assertEquals("delete listener call failed", 510510, listener.evaluateTest()); 205 broker.commitTransaction(); 206 } 207 208 public void testPBStateListener() 209 { 210 PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker(); 212 PBStateListenerObject listener; 213 PBStateListenerObject listener_2 = null; 214 try 215 { 216 listener = new PBStateListenerObject(); 217 pb.addListener(listener); 218 219 listener.afterOpen(new PBStateEvent(pb, PBStateEvent.Type.AFTER_OPEN)); 221 assertEquals("afterOpen listener call failed", 2, listener.evaluateTest()); 222 223 pb.beginTransaction(); 224 assertEquals("beforeBegin/afterBegin listener call failed", 30, listener.evaluateTest()); 225 226 pb.commitTransaction(); 227 assertEquals("beforeCommit/afterCommit listener call failed", 2310, listener.evaluateTest()); 228 229 230 listener_2 = new PBStateListenerObject(); 231 pb.addListener(listener_2); 232 233 listener_2.afterOpen(new PBStateEvent(pb, PBStateEvent.Type.AFTER_OPEN)); 235 assertEquals("afterOpen listener call failed", 2, listener_2.evaluateTest()); 236 237 pb.beginTransaction(); 238 assertEquals("beforeBegin/afterBegin listener call failed", 30, listener_2.evaluateTest()); 239 240 pb.abortTransaction(); 241 assertEquals("beforeRollback/afterRollback listener call failed", 6630, listener_2.evaluateTest()); 242 } 243 finally 244 { 245 pb.close(); 246 if(listener_2 != null) assertEquals("beforeClose listener call failed", 125970, listener_2.evaluateTest()); 247 else fail("Something wrong with test"); 248 } 249 } 250 251 252 253 public static class PBLifeCycleListenerObject implements PBLifeCycleListener 257 { 258 int beforeInsert = 1, afterInsert = 1, beforeUpdate = 1, afterUpdate = 1, 259 beforeDelete = 1, afterDelete = 1, afterLookup = 1; 260 261 public void beforeInsert(PBLifeCycleEvent event) throws PersistenceBrokerException 262 { 263 if(!(event.getTarget() instanceof PBAwareObject)) return; 264 beforeInsert*=2; 265 } 266 267 public void afterInsert(PBLifeCycleEvent event) throws PersistenceBrokerException 268 { 269 if(!(event.getTarget() instanceof PBAwareObject)) return; 270 afterInsert*=3; 271 } 272 273 public void afterLookup(PBLifeCycleEvent event) throws PersistenceBrokerException 274 { 275 if(!(event.getTarget() instanceof PBAwareObject)) return; 276 afterLookup*=5; 277 } 278 279 public void beforeUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException 280 { 281 if(!(event.getTarget() instanceof PBAwareObject)) return; 282 beforeUpdate*=7; 283 } 284 285 public void afterUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException 286 { 287 if(!(event.getTarget() instanceof PBAwareObject)) return; 288 afterUpdate*=11; 289 } 290 291 public void beforeDelete(PBLifeCycleEvent event) throws PersistenceBrokerException 292 { 293 if(!(event.getTarget() instanceof PBAwareObject)) return; 294 beforeDelete*=13; 295 } 296 297 public void afterDelete(PBLifeCycleEvent event) throws PersistenceBrokerException 298 { 299 if(!(event.getTarget() instanceof PBAwareObject)) return; 300 afterDelete*=17; 301 } 302 303 public int evaluateTest() 304 { 305 return beforeInsert * afterInsert * beforeUpdate * afterUpdate * beforeDelete * 306 afterDelete * afterLookup; 307 } 308 } 309 310 311 312 public static class PBStateListenerObject implements PBStateListener 313 { 314 int afterOpen = 1, beforeBegin = 1, afterBegin = 1, beforeCommit = 1, afterCommit = 1, 315 beforeRollback = 1, afterRollback = 1, beforeClose = 1; 316 317 public void afterOpen(PBStateEvent event) 318 { 319 afterOpen*=2; 320 } 321 322 public void beforeBegin(PBStateEvent event) 323 { 324 beforeBegin*=3; 325 } 326 327 public void afterBegin(PBStateEvent event) 328 { 329 afterBegin*=5; 330 } 331 332 public void beforeCommit(PBStateEvent event) 333 { 334 beforeCommit*=7; 335 } 336 337 public void afterCommit(PBStateEvent event) 338 { 339 afterCommit*=11; 340 } 341 342 public void beforeRollback(PBStateEvent event) 343 { 344 beforeRollback*=13; 345 } 346 347 public void afterRollback(PBStateEvent event) 348 { 349 afterRollback*=17; 350 } 351 352 public void beforeClose(PBStateEvent event) 353 { 354 beforeClose*=19; 355 } 356 357 public int evaluateTest() 358 { 359 return afterOpen * beforeBegin * afterBegin * beforeCommit * afterCommit * 360 beforeRollback * afterRollback * beforeClose; 361 } 362 } 363 364 365 366 369 public static class PBAwareObject implements PersistenceBrokerAware, Serializable 370 { 371 private int id; 372 private String name; 373 private RefObj refObject; 374 private List collObjects; 375 376 private boolean calledBeforeInsert = false; 377 private boolean calledAfterInsert = false; 378 private boolean calledBeforeDelete = false; 379 private boolean calledAfterDelete = false; 380 private boolean calledAfterLookup = false; 381 private boolean calledAfterUpdate = false; 382 private boolean calledBeforeUpdate = false; 383 private boolean afterLookupRefObjectPopulated = false; 384 private boolean afterLookupCollObjectsPopulated = false; 385 386 public PBAwareObject() 387 { 388 } 389 390 public PBAwareObject(String name) 391 { 392 this.name = name; 393 } 394 395 public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException 396 { 397 calledBeforeUpdate = true; 398 } 399 400 public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException 401 { 402 calledAfterUpdate = true; 403 } 404 public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException 405 { 406 calledBeforeInsert = true; 408 } 409 public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException 410 { 411 if (calledBeforeInsert) 413 { 414 calledAfterInsert = true; 415 } 416 } 417 public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException 418 { 419 calledBeforeDelete = true; 421 } 422 public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException 423 { 424 if (calledBeforeDelete) 426 { 427 calledAfterDelete = true; 428 } 429 } 430 public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException 431 { 432 calledAfterLookup = true; 434 if(refObject != null) afterLookupRefObjectPopulated = true; 435 if(collObjects != null) afterLookupCollObjectsPopulated = true; 436 } 446 447 448 public boolean getCalledAfterUpdate() 449 { 450 return calledAfterUpdate; 451 } 452 453 public void setCalledAfterUpdate(boolean calledAfterUpdate) 454 { 455 this.calledAfterUpdate = calledAfterUpdate; 456 } 457 458 public boolean getCalledBeforeUpdate() 459 { 460 return calledBeforeUpdate; 461 } 462 463 public void setCalledBeforeUpdate(boolean calledBeforeUpdate) 464 { 465 this.calledBeforeUpdate = calledBeforeUpdate; 466 } 467 468 public boolean getCalledAfterDelete() 469 { 470 return calledAfterDelete; 471 } 472 473 public void setCalledAfterDelete(boolean calledAfterDelete) 474 { 475 this.calledAfterDelete = calledAfterDelete; 476 } 477 478 public boolean getCalledAfterLookup() 479 { 480 return calledAfterLookup; 481 } 482 483 public void setCalledAfterLookup(boolean calledAfterLookup) 484 { 485 this.calledAfterLookup = calledAfterLookup; 486 } 487 488 public boolean getCalledAfterStore() 489 { 490 return calledAfterInsert; 491 } 492 493 public void setCalledAfterStore(boolean calledAfterStore) 494 { 495 this.calledAfterInsert = calledAfterStore; 496 } 497 498 public boolean getCalledBeforeDelete() 499 { 500 return calledBeforeDelete; 501 } 502 503 public void setCalledBeforeDelete(boolean calledBeforeDelete) 504 { 505 this.calledBeforeDelete = calledBeforeDelete; 506 } 507 508 public boolean getCalledBeforeStore() 509 { 510 return calledBeforeInsert; 511 } 512 513 public void setCalledBeforeStore(boolean calledBeforeStore) 514 { 515 this.calledBeforeInsert = calledBeforeStore; 516 } 517 518 public boolean isCalledBeforeInsert() 519 { 520 return calledBeforeInsert; 521 } 522 523 public void setCalledBeforeInsert(boolean calledBeforeInsert) 524 { 525 this.calledBeforeInsert = calledBeforeInsert; 526 } 527 528 public boolean isCalledAfterInsert() 529 { 530 return calledAfterInsert; 531 } 532 533 public void setCalledAfterInsert(boolean calledAfterInsert) 534 { 535 this.calledAfterInsert = calledAfterInsert; 536 } 537 538 public boolean isAfterLookupRefObjectPopulated() 539 { 540 return afterLookupRefObjectPopulated; 541 } 542 543 public void setAfterLookupRefObjectPopulated(boolean afterLookupRefObjectPopulated) 544 { 545 this.afterLookupRefObjectPopulated = afterLookupRefObjectPopulated; 546 } 547 548 public boolean isAfterLookupCollObjectsPopulated() 549 { 550 return afterLookupCollObjectsPopulated; 551 } 552 553 public void setAfterLookupCollObjectsPopulated(boolean afterLookupCollObjectsPopulated) 554 { 555 this.afterLookupCollObjectsPopulated = afterLookupCollObjectsPopulated; 556 } 557 558 public String getName() 559 { 560 return name; 561 } 562 563 public void setName(String name) 564 { 565 this.name = name; 566 } 567 568 public int getId() 569 { 570 return id; 571 } 572 573 public void setId(int id) 574 { 575 this.id = id; 576 } 577 578 public RefObj getRefObject() 579 { 580 return refObject; 581 } 582 583 public void setRefObject(RefObj refObj) 584 { 585 this.refObject = refObj; 586 } 587 588 public List getCollObjects() 589 { 590 return collObjects; 591 } 592 593 public void addCollObject(CollObj obj) 594 { 595 if(collObjects == null) 596 { 597 collObjects = new ArrayList (); 598 } 599 collObjects.add(obj); 600 } 601 602 public void setCollObjects(List collObjects) 603 { 604 this.collObjects = collObjects; 605 } 606 607 public String toString() 608 { 609 return ToStringBuilder.reflectionToString(this); 610 } 611 } 612 613 public static class RefObj implements Serializable 614 { 615 private Integer id; 616 private String name; 617 618 public RefObj() 619 { 620 } 621 622 public RefObj(Integer id, String name) 623 { 624 this.id = id; 625 this.name = name; 626 } 627 628 public RefObj(String name) 629 { 630 this.name = name; 631 } 632 633 public Integer getId() 634 { 635 return id; 636 } 637 638 public void setId(Integer id) 639 { 640 this.id = id; 641 } 642 643 public String getName() 644 { 645 return name; 646 } 647 648 public void setName(String name) 649 { 650 this.name = name; 651 } 652 } 653 654 public static class CollObj extends RefObj 655 { 656 private Integer fkPBAwareObject; 657 658 public CollObj() 659 { 660 } 661 662 public CollObj(Integer id, String name) 663 { 664 super(id, name); 665 } 666 667 public CollObj(String name) 668 { 669 super(name); 670 } 671 672 public Integer getFkPBAwareObject() 673 { 674 return fkPBAwareObject; 675 } 676 677 public void setFkPBAwareObject(Integer fkPBAwareObject) 678 { 679 this.fkPBAwareObject = fkPBAwareObject; 680 } 681 } 682 683 } 684 | Popular Tags |