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.commons.lang.builder.EqualsBuilder; 11 import org.apache.commons.lang.builder.ToStringBuilder; 12 import org.apache.commons.lang.builder.ToStringStyle; 13 import org.apache.ojb.broker.*; 14 import org.apache.ojb.junit.ODMGTestCase; 15 import org.odmg.OQLQuery; 16 import org.odmg.Transaction; 17 18 34 public class InheritanceMultipleTableTest extends ODMGTestCase 35 { 36 public static void main(String [] args) 37 { 38 junit.textui.TestRunner.main(new String []{InheritanceMultipleTableTest.class.getName()}); 39 } 40 41 public void testQueryUsingReference_1() throws Exception 42 { 43 long timestamp = System.currentTimeMillis(); 44 Long id_2 = new Long (timestamp); 45 String name = "testQueryUsingReference_1" + timestamp; 46 47 Manager m_1 = new Manager(id_2, name + "_manager_1"); 48 m_1.setDepartment("m_1"); 49 Address a_1 = new Address("snob allee"); 50 m_1.setAddress(a_1); 51 52 53 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 54 tx.begin(); 55 database.makePersistent(m_1); 56 tx.commit(); 57 58 tx = (TransactionExt) odmg.newTransaction(); 59 tx.begin(); 60 tx.getBroker().clearCache(); 61 OQLQuery query = odmg.newOQLQuery(); 62 query.create("select objects from " + Manager.class.getName() + " where name like $1 and address.street like $2"); 63 query.bind(name + "%"); 64 query.bind("snob allee"); 65 Collection result = (Collection ) query.execute(); 66 tx.commit(); 67 68 assertEquals(1, result.size()); 69 Manager retManager = (Manager) result.iterator().next(); 70 assertNotNull(retManager); 71 assertEquals(name + "_manager_1", retManager.getName()); 72 assertNotNull(retManager.getAddress()); 73 assertEquals("snob allee", retManager.getAddress().getStreet()); 74 75 tx = (TransactionExt) odmg.newTransaction(); 76 tx.begin(); 77 tx.lock(m_1, Transaction.WRITE); 78 m_1.setName(m_1.getName() + "_updated"); 79 tx.commit(); 80 } 81 82 public void testQueryUsingReference_2() throws Exception 83 { 84 long timestamp = System.currentTimeMillis(); 85 Long id_2 = new Long (timestamp); 86 String name = "testQueryUsingReference_2" + timestamp; 87 88 Manager manager = new Manager(id_2, name + "_manager_1"); 89 manager.setDepartment("manager"); 90 Address addressManager = new Address("snob allee 1"); 91 Address addressManagerOld = new Address("snob allee 2"); 92 Address address3 = new Address("snob allee 3"); 93 Address address4 = new Address("snob allee 4"); 94 manager.setAddress(addressManager); 95 manager.addOldAddress(addressManagerOld); 96 manager.addCarrel(address3); 97 manager.addCarrel(address4); 98 99 100 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 101 tx.begin(); 102 database.makePersistent(manager); 103 tx.commit(); 104 105 tx = (TransactionExt) odmg.newTransaction(); 106 tx.begin(); 107 tx.getBroker().clearCache(); 108 OQLQuery query = odmg.newOQLQuery(); 109 query.create("select objects from " + Manager.class.getName() + " where name like $1 and address.street like $2"); 110 query.bind(name + "%"); 111 query.bind("snob allee 1"); 112 Collection result = (Collection ) query.execute(); 113 tx.commit(); 114 115 assertEquals(1, result.size()); 116 Manager retManager = (Manager) result.iterator().next(); 117 assertNotNull(retManager); 118 assertEquals(name + "_manager_1", retManager.getName()); 119 assertNotNull(retManager.getAddress()); 120 assertEquals("snob allee 1", retManager.getAddress().getStreet()); 121 122 tx = (TransactionExt) odmg.newTransaction(); 123 tx.begin(); 124 tx.lock(manager, Transaction.WRITE); 125 manager.setName(manager.getName() + "_updated"); 126 manager.getAddress().setStreet("updated_street"); 127 tx.commit(); 128 129 tx = (TransactionExt) odmg.newTransaction(); 130 tx.begin(); 131 tx.getBroker().clearCache(); 132 query = odmg.newOQLQuery(); 133 query.create("select objects from " + Manager.class.getName() + " where name like $1 and address.street like $2"); 134 query.bind(name + "%"); 135 query.bind("updated_street"); 136 result = (Collection ) query.execute(); 137 tx.commit(); 138 139 assertEquals(1, result.size()); 140 retManager = (Manager) result.iterator().next(); 141 assertNotNull(retManager); 142 assertEquals(name + "_manager_1_updated", retManager.getName()); 143 assertNotNull(retManager.getAddress()); 144 assertEquals("updated_street", retManager.getAddress().getStreet()); 145 } 146 147 public void testQuery_3() throws Exception 148 { 149 long timestamp = System.currentTimeMillis(); 150 Long id_2 = new Long (timestamp); 151 String name = "testInsert" + timestamp; 152 Manager m_3 = new Manager(id_2, name + "_manager_3"); 153 m_3.setDepartment("none"); 154 155 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 156 tx.begin(); 157 database.makePersistent(m_3); 158 tx.commit(); 159 160 tx.begin(); 161 tx.getBroker().clearCache(); 162 163 OQLQuery query = odmg.newOQLQuery(); 164 query.create("select objects from " + Manager.class.getName() + " where name like $1"); 165 query.bind(name + "%"); 166 List newManagers = new ArrayList ((Collection ) query.execute()); 167 tx.commit(); 168 assertEquals(1, newManagers.size()); 169 170 Manager new_m = (Manager) newManagers.get(0); 171 assertNotNull(new_m.getId()); 172 assertNotNull(new_m.getId_2()); 173 assertEquals(m_3.getName(), new_m.getName()); 174 assertEquals(m_3.getDepartment(), new_m.getDepartment()); 175 176 tx = (TransactionExt) odmg.newTransaction(); 177 tx.begin(); 178 database.deletePersistent(m_3); 179 tx.commit(); 180 } 181 182 public void testQuery_2() throws Exception 183 { 184 long timestamp = System.currentTimeMillis(); 185 Long id_2 = new Long (timestamp); 186 String name = "testInsert" + timestamp; 187 Manager m_1 = new Manager(id_2, name + "_manager_1"); 188 Manager m_2 = new Manager(id_2, name + "_manager_2"); 189 Manager m_3 = new Manager(id_2, name + "_manager_3"); 190 m_3.setDepartment("none"); 191 192 Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null); 193 Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null); 194 195 Employee em = new Employee(id_2, name + "_employee"); 196 197 List executives = new ArrayList (); 198 executives.add(ex_1); 199 executives.add(ex_2); 200 m_3.setExecutives(executives); 201 202 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 203 tx.begin(); 204 database.makePersistent(m_1); 205 database.makePersistent(m_2); 206 database.makePersistent(m_3); 207 database.makePersistent(ex_1); 208 database.makePersistent(ex_2); 209 database.makePersistent(em); 210 tx.commit(); 211 212 tx.begin(); 213 tx.getBroker().clearCache(); 214 tx.commit(); 215 216 OQLQuery query = odmg.newOQLQuery(); 217 query.create("select objects from " + Employee.class.getName() + " where name like $1"); 218 query.bind(name + "%"); 219 List newEmployees = new ArrayList ((Collection ) query.execute()); 220 assertEquals(6, newEmployees.size()); 221 int countEmployee = 0; 222 int countExecutive = 0; 223 int countManager = 0; 224 for(int i = 0; i < newEmployees.size(); i++) 225 { 226 Object o = newEmployees.get(i); 227 if(o instanceof Employee) 228 { 229 ++countEmployee; 230 } 231 if(o instanceof Executive) 232 { 233 ++countExecutive; 234 } 235 if(o instanceof Manager) 236 { 237 ++countManager; 238 } 239 } 240 assertEquals(6, countEmployee); 241 247 assertEquals(5, countExecutive); 248 assertEquals(3, countManager); 249 } 250 251 public void testQuery_1() throws Exception 252 { 253 long timestamp = System.currentTimeMillis(); 254 Long id_2 = new Long (timestamp); 255 String name = "testInsert" + timestamp; 256 Manager m_1 = new Manager(id_2, name + "_manager_1"); 257 Manager m_2 = new Manager(id_2, name + "_manager_2"); 258 Manager m_3 = new Manager(id_2, name + "_manager_3"); 259 m_3.setDepartment("none"); 260 261 Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null); 262 Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null); 263 264 Employee em = new Employee(id_2, name + "_employee"); 265 266 List executives = new ArrayList (); 267 executives.add(ex_1); 268 executives.add(ex_2); 269 m_3.setExecutives(executives); 270 271 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 272 tx.begin(); 273 database.makePersistent(m_1); 274 database.makePersistent(m_2); 275 database.makePersistent(m_3); 276 database.makePersistent(ex_1); 277 database.makePersistent(ex_2); 278 database.makePersistent(em); 279 tx.commit(); 280 281 tx.begin(); 282 tx.getBroker().clearCache(); 283 tx.commit(); 284 285 OQLQuery query = odmg.newOQLQuery(); 286 query.create("select objects from " + Manager.class.getName() + " where name like $1 and department like $2"); 287 query.bind(name + "%"); 288 query.bind("none"); 289 Collection result = (Collection ) query.execute(); 290 assertEquals(1, result.size()); 291 for(Iterator iterator = result.iterator(); iterator.hasNext();) 292 { 293 Object o = iterator.next(); 294 assertTrue(o instanceof Manager); 295 Manager temp = (Manager) o; 296 assertNotNull(temp.getExecutives()); 297 assertEquals(2, temp.getExecutives().size()); 298 } 299 300 query = odmg.newOQLQuery(); 301 query.create("select objects from " + Employee.class.getName() + " where name like $1"); 302 query.bind(name + "%"); 303 result = (Collection ) query.execute(); 304 assertEquals(6, result.size()); 305 for(Iterator iterator = result.iterator(); iterator.hasNext();) 306 { 307 Object o = iterator.next(); 308 assertTrue(o instanceof Employee); 309 } 310 311 query = odmg.newOQLQuery(); 312 query.create("select objects from " + Executive.class.getName() + " where name like $1"); 313 query.bind(name + "%"); 314 result = (Collection ) query.execute(); 315 assertEquals(5, result.size()); 316 for(Iterator iterator = result.iterator(); iterator.hasNext();) 317 { 318 Object o = iterator.next(); 319 assertTrue(o instanceof Executive); 320 } 321 322 query = odmg.newOQLQuery(); 323 query.create("select objects from " + Manager.class.getName() + " where name like $1"); 324 query.bind(name + "%"); 325 result = (Collection ) query.execute(); 326 assertEquals(3, result.size()); 327 for(Iterator iterator = result.iterator(); iterator.hasNext();) 328 { 329 Object o = iterator.next(); 330 assertTrue(o instanceof Manager); 331 } 332 } 333 334 public void testQueryWithSerializedObjects() throws Exception 335 { 336 long timestamp = System.currentTimeMillis(); 337 Long id_2 = new Long (timestamp); 338 String name = "testInsert" + timestamp; 339 Manager m_1 = new Manager(id_2, name + "_manager_1"); 340 Manager m_2 = new Manager(id_2, name + "_manager_2"); 341 Manager m_3 = new Manager(id_2, name + "_manager_3"); 342 m_3.setDepartment("none"); 343 344 Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null); 345 Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null); 346 347 Employee em = new Employee(id_2, name + "_employee"); 348 349 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 350 tx.begin(); 351 database.makePersistent(m_1); 352 database.makePersistent(m_2); 353 database.makePersistent(m_3); 354 database.makePersistent(ex_1); 355 database.makePersistent(ex_2); 356 database.makePersistent(em); 357 tx.commit(); 358 359 tx.begin(); 360 tx.getBroker().clearCache(); 361 tx.commit(); 362 363 OQLQuery query = odmg.newOQLQuery(); 364 query.create("select objects from " + Manager.class.getName() + " where name like $1 and department like $2"); 365 query.bind(name + "%"); 366 query.bind("none"); 367 Collection result = (Collection ) query.execute(); 368 assertEquals(1, result.size()); 369 370 query = odmg.newOQLQuery(); 371 query.create("select objects from " + Employee.class.getName() + " where name like $1"); 372 query.bind(name + "%"); 373 result = (Collection ) query.execute(); 374 assertEquals(6, result.size()); 375 376 query = odmg.newOQLQuery(); 377 query.create("select objects from " + Executive.class.getName() + " where name like $1"); 378 query.bind(name + "%"); 379 result = (Collection ) query.execute(); 380 assertEquals(5, result.size()); 381 382 query = odmg.newOQLQuery(); 383 query.create("select objects from " + Manager.class.getName() + " where name like $1"); 384 query.bind(name + "%"); 385 result = (Collection ) query.execute(); 386 assertEquals(3, result.size()); 387 388 em = (Employee) SerializationUtils.deserialize(SerializationUtils.serialize(em)); 389 m_1 = (Manager) SerializationUtils.deserialize(SerializationUtils.serialize(m_1)); 390 m_2 = (Manager) SerializationUtils.deserialize(SerializationUtils.serialize(m_2)); 391 m_3 = (Manager) SerializationUtils.deserialize(SerializationUtils.serialize(m_3)); 392 ex_1 = (Executive) SerializationUtils.deserialize(SerializationUtils.serialize(ex_1)); 393 ex_2 = (Executive) SerializationUtils.deserialize(SerializationUtils.serialize(ex_2)); 394 395 tx = (TransactionExt) odmg.newTransaction(); 396 tx.begin(); 397 tx.getBroker().clearCache(); 398 tx.lock(em, Transaction.WRITE); 399 tx.lock(m_1, Transaction.WRITE); 400 tx.lock(m_2, Transaction.WRITE); 401 tx.lock(m_3, Transaction.WRITE); 402 tx.lock(ex_1, Transaction.WRITE); 403 tx.lock(ex_2, Transaction.WRITE); 404 405 em.setName(em.getName() + "_updated"); 406 m_1.setName(m_1.getName() + "_updated"); 407 m_2.setName(m_2.getName() + "_updated"); 408 m_3.setName(m_3.getName() + "_updated"); 409 ex_1.setName(ex_1.getName() + "_updated"); 410 ex_2.setName(ex_2.getName() + "_updated"); 411 412 tx.commit(); 413 414 query = odmg.newOQLQuery(); 415 query.create("select objects from " + Manager.class.getName() + " where name like $1 and department like $2"); 416 query.bind(name + "%"); 417 query.bind("none"); 418 result = (Collection ) query.execute(); 419 assertEquals("Expect the same number of objects as before update", 1, result.size()); 420 421 query = odmg.newOQLQuery(); 422 query.create("select objects from " + Employee.class.getName() + " where name like $1"); 423 query.bind(name + "%"); 424 result = (Collection ) query.execute(); 425 assertEquals("Expect the same number of objects as before update", 6, result.size()); 426 427 query = odmg.newOQLQuery(); 428 query.create("select objects from " + Executive.class.getName() + " where name like $1"); 429 query.bind(name + "%"); 430 result = (Collection ) query.execute(); 431 assertEquals("Expect the same number of objects as before update", 5, result.size()); 432 433 query = odmg.newOQLQuery(); 434 query.create("select objects from " + Manager.class.getName() + " where name like $1"); 435 query.bind(name + "%"); 436 result = (Collection ) query.execute(); 437 assertEquals("Expect the same number of objects as before update", 3, result.size()); 438 } 439 440 private void prepareForQueryTests(Long id_2, String name) 441 { 442 Manager m_1 = new Manager(id_2, name + "_manager_1"); 443 Manager m_2 = new Manager(id_2, name + "_manager_2"); 444 Manager m_3 = new Manager(id_2, name + "_manager_3"); 445 m_3.setDepartment("none"); 446 Address a_1 = new Address("snob allee"); 447 m_1.setAddress(a_1); 448 449 Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null); 450 Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null); 451 452 Employee em = new Employee(id_2, name + "_employee"); 453 Address a_2 = new Address("cockroaches valley"); 454 em.setAddress(a_2); 455 456 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 457 tx.begin(); 458 database.makePersistent(m_1); 459 database.makePersistent(m_2); 460 database.makePersistent(m_3); 461 database.makePersistent(ex_1); 462 database.makePersistent(ex_2); 463 database.makePersistent(em); 464 tx.commit(); 465 } 466 467 public void testQuery_InheritedObjects() throws Exception 468 { 469 long timestamp = System.currentTimeMillis(); 470 Long id_2 = new Long (timestamp); 471 String name = "testQuery_InheritedObjects" + timestamp; 472 prepareForQueryTests(id_2, name); 473 474 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 475 tx.begin(); 476 tx.getBroker().clearCache(); 477 478 OQLQuery query = odmg.newOQLQuery(); 479 query.create("select objects from " + Employee.class.getName() + " where name like $1"); 480 query.bind(name + "%"); 481 Collection result = (Collection ) query.execute(); 482 assertEquals(6, result.size()); 483 for (Iterator iterator = result.iterator(); iterator.hasNext();) 484 { 485 Employee obj = (Employee) iterator.next(); 486 assertNotNull(obj.getName()); 487 } 488 489 tx.getBroker().clearCache(); 490 query = odmg.newOQLQuery(); 491 query.create("select objects from " + Executive.class.getName() + " where name like $1"); 492 query.bind(name + "%"); 493 result = (Collection ) query.execute(); 494 assertEquals(5, result.size()); 495 for (Iterator iterator = result.iterator(); iterator.hasNext();) 496 { 497 Executive obj = (Executive) iterator.next(); 498 assertNotNull(obj.getName()); 499 } 500 501 tx.getBroker().clearCache(); 502 query = odmg.newOQLQuery(); 503 query.create("select objects from " + Manager.class.getName() + " where name like $1"); 504 query.bind(name + "%"); 505 result = (Collection ) query.execute(); 506 assertEquals(3, result.size()); 507 for (Iterator iterator = result.iterator(); iterator.hasNext();) 508 { 509 Manager obj = (Manager) iterator.next(); 510 assertNotNull(obj.getName()); 511 } 512 513 tx.commit(); 514 } 515 516 public void testQuery_InheritedField() throws Exception 517 { 518 long timestamp = System.currentTimeMillis(); 519 Long id_2 = new Long (timestamp); 520 String name = "testQuery_InheritedField" + timestamp; 521 prepareForQueryTests(id_2, name); 522 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 523 tx.begin(); 524 tx.getBroker().clearCache(); 525 OQLQuery query = odmg.newOQLQuery(); 526 query.create("select objects from " + Manager.class.getName() + " where name like $1 and department like $2"); 527 query.bind(name + "%"); 528 query.bind("none"); 529 Collection result = (Collection ) query.execute(); 530 tx.commit(); 531 assertEquals(1, result.size()); 532 } 533 534 public void testQuery_Reference() throws Exception 535 { 536 long timestamp = System.currentTimeMillis(); 537 Long id_2 = new Long (timestamp); 538 String name = "testQuery_Reference" + timestamp; 539 prepareForQueryTests(id_2, name); 540 541 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 542 tx.begin(); 543 tx.getBroker().clearCache(); 544 OQLQuery query = odmg.newOQLQuery(); 545 query.create("select objects from " + Employee.class.getName() + " where name like $1 and address.street like $2"); 546 query.bind(name + "%"); 547 query.bind("%valley"); 548 Collection result = (Collection ) query.execute(); 549 tx.commit(); 550 551 assertEquals(1, result.size()); 552 Employee emp = (Employee) result.iterator().next(); 553 assertNotNull(emp.getAddress()); 554 assertEquals("cockroaches valley", emp.getAddress().getStreet()); 555 } 556 557 public void testQuery_InheritedReference_1() throws Exception 558 { 559 long timestamp = System.currentTimeMillis(); 560 Long id_2 = new Long (timestamp); 561 String name = "testQuery_InheritedReference_1" + timestamp; 562 prepareForQueryTests(id_2, name); 563 564 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 565 tx.begin(); 566 tx.getBroker().clearCache(); 567 OQLQuery query = odmg.newOQLQuery(); 568 query.create("select objects from " + Manager.class.getName() + " where name like $1 and address.street like $2"); 569 query.bind(name + "%"); 570 query.bind("snob allee"); 571 Collection result = (Collection ) query.execute(); 572 tx.commit(); 573 574 assertEquals(1, result.size()); 575 Manager retManager = (Manager) result.iterator().next(); 576 assertNotNull(retManager); 577 assertEquals(name + "_manager_1", retManager.getName()); 578 assertNotNull(retManager.getAddress()); 579 assertEquals("snob allee", retManager.getAddress().getStreet()); 580 } 581 582 public void testQuery_InheritedReference_2() throws Exception 583 { 584 long timestamp = System.currentTimeMillis(); 585 Long id_2 = new Long (timestamp); 586 String name = "testQuery_InheritedReference_2" + timestamp; 587 prepareForQueryTests(id_2, name); 588 589 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 590 tx.begin(); 591 tx.getBroker().clearCache(); 592 OQLQuery query = odmg.newOQLQuery(); 593 query.create("select objects from " + Executive.class.getName() + " where name like $1 and address.street like $2"); 594 query.bind(name + "%"); 595 query.bind("snob allee"); 596 Collection result = (Collection ) query.execute(); 597 tx.commit(); 598 599 assertEquals(1, result.size()); 600 Executive retManager = (Executive) result.iterator().next(); 601 assertNotNull(retManager); 602 assertEquals(name + "_manager_1", retManager.getName()); 603 } 604 605 public void testQuery_InheritedReference_3() throws Exception 606 { 607 long timestamp = System.currentTimeMillis(); 608 Long id_2 = new Long (timestamp); 609 String name = "testQuery_InheritedReference_3" + timestamp; 610 prepareForQueryTests(id_2, name); 611 612 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 613 tx.begin(); 614 tx.getBroker().clearCache(); 615 OQLQuery query = odmg.newOQLQuery(); 616 query.create("select objects from " + Employee.class.getName() + " where name like $1 and address.street like $2"); 617 query.bind(name + "%"); 618 query.bind("snob allee"); 619 Collection result = (Collection ) query.execute(); 620 tx.commit(); 621 assertEquals(1, result.size()); 622 Employee emp = (Employee) result.iterator().next(); 623 assertNotNull(emp); 624 assertEquals(name + "_manager_1", emp.getName()); 625 } 626 627 public void testInsertQuery() throws Exception 628 { 629 long timestamp = System.currentTimeMillis(); 630 Long id_2 = new Long (timestamp); 631 String name = "testInsert" + timestamp; 632 633 Employee em1 = new Employee(id_2, name); 634 Executive ex1 = new Executive(id_2, name, "department_1", null); 635 Executive ex2 = new Executive(id_2, name, "department_2", null); 636 ArrayList list = new ArrayList (); 637 list.add(ex1); 638 list.add(ex2); 639 Manager m1 = new Manager(id_2, name); 640 m1.setExecutives(list); 641 ex1.setManager(m1); 642 ex2.setManager(m1); 643 644 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 645 tx.begin(); 646 database.makePersistent(em1); 647 database.makePersistent(m1); 648 tx.commit(); 649 650 tx.begin(); 651 Identity m1_oid = tx.getBroker().serviceIdentity().buildIdentity(m1); 652 Identity ex1_oid = tx.getBroker().serviceIdentity().buildIdentity(ex1); 653 Identity em1_oid = tx.getBroker().serviceIdentity().buildIdentity(em1); 654 655 tx.getBroker().clearCache(); 656 657 Employee newEm1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid); 658 Executive newEx1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid); 659 Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid); 660 661 assertEquals(em1, newEm1); 662 assertEquals(ex1, newEx1); 663 assertEquals(m1, newM1); 664 assertEquals(name, newEx1.getName()); 665 assertEquals(name, newM1.getName()); 666 667 assertEquals(2, newM1.getExecutives().size()); 668 669 OQLQuery queryEmployee = odmg.newOQLQuery(); 670 queryEmployee.create("select objects from " + Employee.class.getName() + " where name like $1"); 671 queryEmployee.bind(name); 672 673 OQLQuery queryExecutive = odmg.newOQLQuery(); 674 queryExecutive.create("select objects from " + Executive.class.getName() + " where name like $1"); 675 queryExecutive.bind(name); 676 677 OQLQuery queryManager = odmg.newOQLQuery(); 678 queryManager.create("select objects from " + Manager.class.getName() + " where name like $1"); 679 queryManager.bind(name); 680 681 Collection result = (Collection ) queryEmployee.execute(); 682 assertEquals(4, result.size()); 683 684 result = (Collection ) queryExecutive.execute(); 685 assertEquals(3, result.size()); 686 687 result = (Collection ) queryManager.execute(); 688 assertEquals(1, result.size()); 689 } 690 691 public void testUpdate() throws Exception 692 { 693 697 long timestamp = System.currentTimeMillis(); 698 Long id_2 = new Long (timestamp); 699 String name = "testUpdate" + timestamp; 700 Employee em1 = new Employee(id_2, "employee_" + name); 701 Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null); 702 Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null); 703 ArrayList list = new ArrayList (); 704 list.add(ex1); 705 list.add(ex2); 706 Manager m1 = new Manager(id_2, "manager_" + name); 707 m1.setExecutives(list); 708 709 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 710 tx.begin(); 711 database.makePersistent(em1); 712 database.makePersistent(m1); 713 tx.commit(); 714 715 tx.begin(); 716 Identity m1_oid = tx.getBroker().serviceIdentity().buildIdentity(m1); 717 Identity ex1_oid = tx.getBroker().serviceIdentity().buildIdentity(ex1); 718 Identity em1_oid = tx.getBroker().serviceIdentity().buildIdentity(em1); 719 720 tx.getBroker().clearCache(); 721 722 Employee newEm1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid); 723 Executive newEx1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid); 724 Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid); 725 tx.commit(); 726 727 assertEquals(2, newM1.getExecutives().size()); 728 729 tx.begin(); 730 tx.lock(newEm1, Transaction.WRITE); 731 tx.lock(newEx1, Transaction.WRITE); 732 tx.lock(newM1, Transaction.WRITE); 733 newEm1.setName("**updated_employee_" + name); 734 newM1.setName("**updated_manager1_" + name); 735 newM1.setDepartment("**new"); 736 ((Executive) newM1.getExecutives().get(0)).setName("**updated_executive1_" + name); 737 ((Executive) newM1.getExecutives().get(1)).setName("**updated_executive2_" + name); 738 tx.commit(); 739 740 tx.begin(); 742 tx.getBroker().clearCache(); 743 em1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid); 744 ex1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid); 745 m1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid); 746 tx.commit(); 747 749 assertEquals(newEm1, em1); 750 assertEquals(newM1, m1); 751 assertEquals(2, m1.getExecutives().size()); 752 } 753 754 public void testDelete() 755 { 756 760 long timestamp = System.currentTimeMillis(); 761 Long id_2 = new Long (timestamp); 762 String name = "testUpdate" + timestamp; 763 Employee em1 = new Employee(id_2, "employee_" + name); 764 Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null); 765 Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null); 766 ArrayList list = new ArrayList (); 767 list.add(ex1); 768 list.add(ex2); 769 Manager m1 = new Manager(id_2, "manager_" + name); 770 m1.setExecutives(list); 771 772 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 773 tx.begin(); 774 database.makePersistent(em1); 775 database.makePersistent(m1); 776 tx.commit(); 777 778 tx.begin(); 779 Identity m1_oid = tx.getBroker().serviceIdentity().buildIdentity(m1); 780 Identity ex1_oid = tx.getBroker().serviceIdentity().buildIdentity(ex1); 781 Identity em1_oid = tx.getBroker().serviceIdentity().buildIdentity(em1); 782 783 tx.getBroker().clearCache(); 784 785 Employee newEm1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid); 786 Executive newEx1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid); 787 Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid); 788 tx.commit(); 789 790 assertNotNull(newEm1); 791 assertNotNull(newEx1); 792 assertNotNull(newM1); 793 assertEquals(2, newM1.getExecutives().size()); 794 795 tx.begin(); 797 database.deletePersistent(newEm1); 798 database.deletePersistent(newEx1); 799 database.deletePersistent(newM1); 800 tx.commit(); 801 803 tx.begin(); 804 newEm1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid); 805 newEx1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid); 806 newM1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid); 807 tx.commit(); 808 809 assertNull(newEm1); 810 assertNull(newEx1); 811 assertNull(newM1); 812 } 813 814 public static class Manager extends Executive 818 { 819 private List executives = new ArrayList (); 820 821 public Manager() 822 { 823 } 824 825 public Manager(Long id_2, String name) 826 { 827 super(id_2, name, null, null); 828 } 829 830 public boolean equals(Object obj) 831 { 832 if (!(obj instanceof Manager)) 833 { 834 return false; 835 } 836 Manager other = (Manager) obj; 837 return new EqualsBuilder().append(getExecutives(), other.getExecutives()) 838 .isEquals() && super.equals(obj); 839 } 840 841 public List getExecutives() 842 { 843 return executives; 844 } 845 846 public void setExecutives(List executives) 847 { 848 this.executives = executives; 849 } 850 } 851 852 public static class Executive extends Employee 853 { 854 private String department; 855 private Manager manager; 856 857 public Executive() 858 { 859 } 860 861 public Executive(Long id_2, String name, String department, Manager manager) 862 { 863 super(id_2, name); 864 this.department = department; 865 this.manager = manager; 866 } 867 868 public String getDepartment() 869 { 870 return department; 871 } 872 873 public void setDepartment(String department) 874 { 875 this.department = department; 876 } 877 878 public Manager getManager() 879 { 880 return manager; 881 } 882 883 public void setManager(Manager manager) 884 { 885 this.manager = manager; 886 } 887 888 public boolean equals(Object obj) 889 { 890 if (!(obj instanceof Executive)) 891 { 892 return false; 893 } 894 Executive ex = (Executive) obj; 895 return new EqualsBuilder() 896 .append((getManager() != null ? getManager().getId(): null), (ex.getManager() != null ? ex.getManager().getId() : null)) 898 .append((getManager() != null ? getManager().getId_2(): null), (ex.getManager() != null ? ex.getManager().getId_2() : null)) 899 .append(getDepartment(), ex.getDepartment()) 900 .isEquals() && super.equals(obj); 901 } 902 } 903 904 public static class Employee implements Serializable 905 { 906 private Integer id; 907 private Long id_2; 908 private String name; 909 private AddressIF address; 910 private List oldAddresses = new ArrayList (); 911 private List carrels = new ArrayList (); 912 913 public Employee() 914 { 915 } 916 917 public Employee(Long id_2, String name) 918 { 919 this.id_2 = id_2; 920 this.name = name; 921 } 922 923 public Integer getId() 924 { 925 return id; 926 } 927 928 public Long getId_2() 929 { 930 return id_2; 931 } 932 933 public void setId_2(Long id_2) 934 { 935 this.id_2 = id_2; 936 } 937 938 public void setId(Integer id) 939 { 940 this.id = id; 941 } 942 943 public void addOldAddress(AddressIF address) 944 { 945 if(oldAddresses == null) 946 { 947 oldAddresses = new ArrayList (); 948 } 949 oldAddresses.add(address); 950 } 951 952 953 public AddressIF getAddress() 954 { 955 return address; 956 } 957 958 public void setAddress(AddressIF address) 959 { 960 this.address = address; 961 } 962 963 public String getName() 964 { 965 return name; 966 } 967 968 public void setName(String name) 969 { 970 this.name = name; 971 } 972 973 public List getOldAddresses() 974 { 975 return oldAddresses; 976 } 977 978 public void setOldAddresses(List oldAddresses) 979 { 980 this.oldAddresses = oldAddresses; 981 } 982 983 public void addCarrel(Address address) 984 { 985 if(carrels == null) 986 { 987 carrels = new ArrayList (); 988 } 989 carrels.add(address); 990 } 991 992 public List getCarrels() 993 { 994 return carrels; 995 } 996 997 public void setCarrels(List carrels) 998 { 999 this.carrels = carrels; 1000 } 1001 1002 public boolean equals(Object obj) 1003 { 1004 if (!(obj instanceof Employee)) 1005 { 1006 return false; 1007 } 1008 Employee em = (Employee) obj; 1009 return new EqualsBuilder() 1010 .append(getId(), em.getId()) 1011 .append(getId_2(), em.getId_2()) 1012 .append(getName(), em.getName()) 1013 .append((getAddress() != null ? getAddress().getId() : null), (em.getAddress() != null ? em.getAddress().getId() : null)) 1015 .append(getOldAddresses(), em.getOldAddresses()) 1016 .append(getCarrels(), em.getCarrels()) 1017 .isEquals(); 1018 } 1019 1020 public String toString() 1021 { 1022 return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Employee.class); 1023 } 1024 } 1025 1026 public static class Address implements AddressIF 1027 { 1028 private Integer id; 1029 private String street; 1030 private List employees = new ArrayList (); 1031 private Integer fkEmployee1; 1032 private Long fkEmployee2; 1033 1034 public Address() 1035 { 1036 } 1037 1038 public Address(String street) 1039 { 1040 this.street = street; 1041 } 1042 1043 public boolean equals(Object obj) 1044 { 1045 if (!(obj instanceof AddressIF)) 1046 { 1047 return false; 1048 } 1049 AddressIF other = (AddressIF) obj; 1050 return new EqualsBuilder() 1051 .append(getId(), other.getId()) 1052 .append(getStreet(), other.getStreet()) 1053 .append(getEmployees(), other.getEmployees()) 1054 .append(getFkEmployee1(), other.getFkEmployee1()) 1055 .append(getFkEmployee2(), other.getFkEmployee2()) 1056 .isEquals(); 1057 } 1058 1059 public Integer getId() 1060 { 1061 return id; 1062 } 1063 1064 public void setId(Integer id) 1065 { 1066 this.id = id; 1067 } 1068 1069 public String getStreet() 1070 { 1071 return street; 1072 } 1073 1074 public void setStreet(String street) 1075 { 1076 this.street = street; 1077 } 1078 1079 public Integer getFkEmployee1() 1080 { 1081 return fkEmployee1; 1082 } 1083 1084 public void setFkEmployee1(Integer fkEmployee1) 1085 { 1086 this.fkEmployee1 = fkEmployee1; 1087 } 1088 1089 public Long getFkEmployee2() 1090 { 1091 return fkEmployee2; 1092 } 1093 1094 public void setFkEmployee2(Long fkEmployee2) 1095 { 1096 this.fkEmployee2 = fkEmployee2; 1097 } 1098 1099 public void addEmployee(Employee emp) 1100 { 1101 if(employees == null) 1102 { 1103 employees = new ArrayList (); 1104 } 1105 employees.add(emp); 1106 } 1107 1108 public List getEmployees() 1109 { 1110 return employees; 1111 } 1112 1113 public void setEmployees(List employees) 1114 { 1115 this.employees = employees; 1116 } 1117 } 1118 1119 public static interface AddressIF 1120 { 1121 public Integer getId(); 1122 public void setId(Integer id); 1123 public String getStreet(); 1124 public void setStreet(String street); 1125 1126 public List getEmployees(); 1127 public void setEmployees(List employees); 1128 public Integer getFkEmployee1(); 1129 public void setFkEmployee1(Integer id); 1130 public Long getFkEmployee2(); 1131 public void setFkEmployee2(Long id); 1132 } 1133} 1134 | Popular Tags |