1 package org.apache.ojb.broker; 2 3 import org.apache.ojb.broker.metadata.ClassDescriptor; 4 import org.apache.ojb.broker.metadata.CollectionDescriptor; 5 import org.apache.ojb.broker.query.Criteria; 6 import org.apache.ojb.broker.query.Query; 7 import org.apache.ojb.broker.query.QueryByCriteria; 8 import org.apache.ojb.broker.query.QueryFactory; 9 import org.apache.ojb.junit.PBTestCase; 10 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Vector ; 16 17 19 public class MtoNMapping extends PBTestCase 20 { 21 public static void main(String [] args) 22 { 23 String [] arr = {MtoNMapping.class.getName()}; 24 junit.textui.TestRunner.main(arr); 25 } 26 27 public MtoNMapping(String name) 28 { 29 super(name); 30 } 31 32 36 public void testPolymorphMToN() 37 { 38 Gourmet james = new Gourmet("james"); 39 Identity jamesId = new Identity(james, broker); 40 Gourmet doris = new Gourmet("doris"); 41 Identity dorisId = new Identity(doris, broker); 42 43 Fish tuna = new Fish("tuna", 242, "salt"); 44 Fish trout = new Fish("trout", 52, "fresh water"); 45 46 Salad radiccio = new Salad("Radiccio", 7, "red"); 47 Salad lolloverde = new Salad("Lollo verde", 7, "green"); 48 49 james.addFavoriteFood(tuna); 50 james.addFavoriteFood(radiccio); 51 52 doris.addFavoriteFood(tuna); 53 doris.addFavoriteFood(trout); 54 doris.addFavoriteFood(lolloverde); 55 56 broker.beginTransaction(); 57 broker.store(james); 58 broker.store(doris); 59 broker.commitTransaction(); 60 61 broker.clearCache(); 62 63 Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId); 64 List favFood = loadedJames.getFavoriteFood(); 65 assertEquals(2, favFood.size()); 66 67 Gourmet loadedDoris = (Gourmet) broker.getObjectByIdentity(dorisId); 68 favFood = loadedDoris.getFavoriteFood(); 69 assertEquals(3, favFood.size()); 70 } 71 72 public void testPolymorphMToNUpdate() 73 { 74 long timestamp = System.currentTimeMillis(); 75 Gourmet james = new Gourmet("james"); 76 Identity jamesId = new Identity(james, broker); 77 Gourmet doris = new Gourmet("doris"); 78 Identity dorisId = new Identity(doris, broker); 79 80 Fish tuna = new Fish("tuna", 242, "salt"); 81 Fish trout = new Fish("trout", 52, "fresh water"); 82 Fish goldfish = new Fish("goldfish_"+timestamp, 10, "brackish water"); 83 84 Salad radiccio = new Salad("Radiccio", 7, "red"); 85 Salad lolloverde = new Salad("Lollo verde", 7, "green"); 86 87 james.addFavoriteFood(tuna); 88 james.addFavoriteFood(radiccio); 89 90 doris.addFavoriteFood(tuna); 91 doris.addFavoriteFood(trout); 92 doris.addFavoriteFood(lolloverde); 93 94 broker.beginTransaction(); 95 broker.store(james); 96 broker.store(doris); 97 broker.commitTransaction(); 98 99 broker.clearCache(); 100 101 Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId); 102 List favFood = loadedJames.getFavoriteFood(); 103 assertEquals(2, favFood.size()); 104 105 Gourmet loadedDoris = (Gourmet) broker.getObjectByIdentity(dorisId); 106 favFood = loadedDoris.getFavoriteFood(); 107 assertEquals(3, favFood.size()); 108 109 112 loadedDoris.addFavoriteFood(goldfish); 113 broker.beginTransaction(); 115 broker.store(loadedDoris); 116 broker.commitTransaction(); 117 118 broker.clearCache(); 119 loadedDoris = (Gourmet) broker.getObjectByIdentity(dorisId); 121 assertEquals(4, loadedDoris.getFavoriteFood().size()); 122 } 123 124 public void testPolymorphMToNDelete() 125 { 126 long timestamp = System.currentTimeMillis(); 127 Gourmet james = new Gourmet("james_" + timestamp); 128 Identity jamesId = new Identity(james, broker); 129 Gourmet doris = new Gourmet("doris_" + timestamp); 130 Identity dorisId = new Identity(doris, broker); 131 132 Fish tuna = new Fish("tuna_" + timestamp, 242, "salt"); 133 Fish trout = new Fish("trout_" + timestamp, 52, "fresh water"); 134 Fish goldfish = new Fish("goldfish_" + timestamp, 10, "brackish water"); 135 136 Salad radiccio = new Salad("Radiccio_" + timestamp, 7, "red"); 137 Salad lolloverde = new Salad("Lollo verde_" + timestamp, 7, "green"); 138 139 james.addFavoriteFood(tuna); 140 james.addFavoriteFood(radiccio); 141 142 doris.addFavoriteFood(tuna); 143 doris.addFavoriteFood(trout); 144 doris.addFavoriteFood(lolloverde); 145 doris.addFavoriteFood(goldfish); 146 147 broker.beginTransaction(); 148 broker.store(james); 149 broker.store(doris); 150 151 broker.commitTransaction(); 152 153 broker.clearCache(); 154 155 Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId); 156 List favFood = loadedJames.getFavoriteFood(); 157 assertEquals(2, favFood.size()); 158 159 Gourmet loadedDoris = (Gourmet) broker.getObjectByIdentity(dorisId); 160 favFood = loadedDoris.getFavoriteFood(); 161 assertEquals(4, favFood.size()); 162 163 Criteria c = new Criteria(); 164 c.addLike("name", "%"+timestamp); 165 Query q = QueryFactory.newQuery(InterfaceFood.class, c); 166 Collection result = broker.getCollectionByQuery(q); 167 int foodBeforeRemove = result.size(); 168 assertEquals("Wrong number of InterfaceFood objects", 5, foodBeforeRemove); 169 170 171 List foodList = loadedDoris.getFavoriteFood(); 172 foodList.remove(0); 173 loadedDoris.setFavoriteFood(foodList); 174 broker.beginTransaction(); 176 broker.store(loadedDoris); 177 broker.commitTransaction(); 178 179 broker.clearCache(); 180 loadedDoris = (Gourmet) broker.getObjectByIdentity(dorisId); 182 assertEquals(3, loadedDoris.getFavoriteFood().size()); 183 result = broker.getCollectionByQuery(q); 184 assertEquals("n-side object shouldn't be removed", foodBeforeRemove, result.size()); 185 } 186 187 188 public void testMNLoading() throws Exception 189 { 190 broker.clearCache(); 191 192 Person p = new Person(); 193 p.setId(1); 194 Query q = QueryFactory.newQuery(p); 195 p = (Person) broker.getObjectByQuery(q); 196 assertNotNull(p); 197 Collection projects = p.getProjects(); 198 assertNotNull(projects); 199 assertTrue(projects.size() > 0); 200 201 projects.toArray(new Project[0]); 203 Criteria c = null; 204 q = QueryFactory.newQuery(Project.class, c); 205 Collection col = broker.getCollectionByQuery(q); 206 assertNotNull(col); 207 } 208 209 210 public void testMNLoadingPrefetch() throws Exception 211 { 212 Criteria crit; 213 QueryByCriteria qry; 214 Collection col1, col2; 215 216 broker.clearCache(); 217 218 crit = new Criteria(); 219 crit.addLessThan("id",new Integer (6)); 220 qry = QueryFactory.newQuery(Project.class, crit); 221 qry.addOrderByAscending("id"); 222 col1 = broker.getCollectionByQuery(qry); 223 assertNotNull(col1); 224 225 broker.clearCache(); 226 227 crit = new Criteria(); 228 crit.addLessThan("id",new Integer (6)); 229 qry = QueryFactory.newQuery(Project.class, crit); 230 qry.addOrderByAscending("id"); 231 qry.addPrefetchedRelationship("persons"); 232 col2 = broker.getCollectionByQuery(qry); 233 assertNotNull(col2); 234 235 assertEquals("Same size",col1.size(), col2.size()); 236 237 Iterator it1 = col1.iterator(); 238 Iterator it2 = col2.iterator(); 239 240 while (it1.hasNext() && it2.hasNext()) 241 { 242 Project p1 = (Project)it1.next(); 243 Project p2 = (Project)it2.next(); 244 245 assertEquals("Same Title", p1.getTitle(), p2.getTitle()); 246 assertEquals("Same Number of Persons", p1.getPersons().size(), p2.getPersons().size()); 247 assertEquals("Same toString", p1.toString(), p2.toString()); 248 } 249 } 250 251 252 253 public void testMNLoadingUnidirectional() throws Exception 254 { 255 broker.clearCache(); 256 257 PersonUnidirectional p = new PersonUnidirectional(); 258 p.setId(1); 259 Query q = QueryFactory.newQuery(p); 260 p = (PersonUnidirectional) broker.getObjectByQuery(q); 261 Collection projects = p.getProjects(); 262 assertNotNull(projects); 263 assertTrue(projects.size() > 0); 264 projects.toArray(new ProjectUnidirectional[0]); } 266 267 268 public void testLoadingWithAssociationClass() throws Exception 269 { 270 Person p = new Person(); 271 p.setId(1); 272 Query q = QueryFactory.newQuery(p); 273 p = (Person) broker.getObjectByQuery(q); 274 275 Vector roles = (Vector ) p.getRoles(); 276 assertNotNull(roles); 277 279 Criteria c = null; 280 q = QueryFactory.newQuery(Project.class, c); 281 Collection col = broker.getCollectionByQuery(q); 282 assertNotNull(col); 283 284 Iterator iter = col.iterator(); 285 while (iter.hasNext()) 286 { 287 iter.next(); 288 } 290 } 291 292 293 public void testInsertion() throws Exception 294 { 295 Person p = new Person(); 296 p.setId(1); 297 Query q = QueryFactory.newQuery(p); 298 p = (Person) broker.getObjectByQuery(q); 299 assertNotNull("We should found a 'person' for id 1 - check db script", p); 300 Collection projects = p.getProjects(); 301 assertNotNull(projects); 302 projects.toArray(new Project[0]); assertNotNull("Person should have some projects - check db script", projects); 304 int count = projects.size(); 305 306 Project proj = new Project(); 307 proj.setPersons(new ArrayList ()); 308 proj.setTitle("MARS"); 309 proj.setDescription("colonization of planet Mars"); 310 311 p.getProjects().add(proj); 312 proj.getPersons().add(p); 313 assertEquals(count + 1, p.getProjects().size()); 314 315 broker.beginTransaction(); 316 broker.store(p); 317 broker.commitTransaction(); 318 319 broker.clearCache(); 320 321 p = (Person) broker.getObjectByQuery(q); 322 assertEquals(count + 1, p.getProjects().size()); 323 } 324 325 326 public void testInsertAndDelete() throws Exception 327 { 328 Person pers = new Person(); 329 pers.setId(7); 330 Query query = QueryFactory.newQuery(pers); 331 pers = (Person) broker.getObjectByQuery(query); 332 Collection projects = pers.getProjects(); 333 Project[] projectArray = (Project[]) projects.toArray(new Project[0]); 334 Project oldProj, newProj; 335 int count = projects.size(); 336 337 oldProj = projectArray[0]; 338 projects.remove(oldProj); 339 340 newProj = new Project(); 341 newProj.setTitle("Test Project1 for Person 7"); 342 newProj.setDescription("This is a Test Project1 for Person 7"); 343 projects.add(newProj); 344 345 newProj = new Project(); 346 newProj.setTitle("Test Project2 for Person 7"); 347 newProj.setDescription("This is a Test Project2 for Person 7"); 348 projects.add(newProj); 349 350 broker.beginTransaction(); 351 broker.store(pers); 352 broker.commitTransaction(); 353 354 broker.clearCache(); 355 356 pers = (Person) broker.getObjectByQuery(query); 357 assertEquals(count + 1 , pers.getProjects().size()); 358 359 } 360 361 367 private Project createProjectWithAssignedPersons_1(String title) throws Exception 368 { 369 373 Project project = new Project(); 375 project.setTitle(title); 376 377 Person p1 = new Person(); 380 p1.setFirstname(title); 381 broker.beginTransaction(); 382 383 broker.store(p1); 384 385 List projects_1 = new ArrayList (); 386 projects_1.add(project); 387 p1.setProjects(projects_1); 389 Person p2 = new Person(); 390 p2.setFirstname(title); 391 broker.store(p2); 392 393 List projects_2 = new ArrayList (); 394 projects_2.add(project); 395 p2.setProjects(projects_2); 397 ArrayList persons = new ArrayList (); 398 persons.add(p1); 399 persons.add(p2); 400 project.setPersons(persons); 402 broker.store(project); 403 broker.commitTransaction(); 404 405 return project; 406 } 407 408 415 private Project createProjectWithAssignedPersons_2(String title) throws Exception 416 { 417 ClassDescriptor cldProject = broker.getClassDescriptor(Project.class); 418 CollectionDescriptor codPersons =cldProject.getCollectionDescriptorByName("persons"); 419 boolean cascadeStorePersons = codPersons.getCascadeStore(); 420 421 ClassDescriptor cldPerson = broker.getClassDescriptor(Person.class); 422 CollectionDescriptor codProjects =cldPerson.getCollectionDescriptorByName("projects"); 423 boolean cascadeStoreProjects = codProjects.getCascadeStore(); 424 425 codPersons.setCascadeStore(true); 427 codProjects.setCascadeStore(true); 428 429 Project project = new Project(); 431 project.setTitle(title); 432 433 Person p1 = new Person(); 436 p1.setFirstname(title); 437 438 List projects_1 = new ArrayList (); 439 projects_1.add(project); 440 p1.setProjects(projects_1); 442 Person p2 = new Person(); 443 p2.setFirstname(title); 444 445 List projects_2 = new ArrayList (); 446 projects_2.add(project); 447 p2.setProjects(projects_2); 449 ArrayList persons = new ArrayList (); 450 persons.add(p1); 451 persons.add(p2); 452 project.setPersons(persons); 454 broker.beginTransaction(); 455 broker.store(project); 456 broker.commitTransaction(); 457 458 codPersons.setCascadeStore(cascadeStorePersons); 460 codProjects.setCascadeStore(cascadeStoreProjects); 461 462 return project; 463 } 464 465 469 public void testInsertWithIndirectionTable_1() throws Exception 470 { 471 String title = "testInsertWithIndirectionTable_1_" + System.currentTimeMillis(); 472 473 Project project = createProjectWithAssignedPersons_1(title); 474 475 verifyProjectWithAssignedPersons(title, project); 476 } 477 478 483 public void testInsertWithIndirectionTable_2() throws Exception 484 { 485 String title = "testInsertWithIndirectionTable_2_" + System.currentTimeMillis(); 486 487 Project project = createProjectWithAssignedPersons_2(title); 488 489 verifyProjectWithAssignedPersons(title, project); 490 } 491 492 private void verifyProjectWithAssignedPersons(String title, Project project) 493 { 494 499 broker.clearCache(); 500 Criteria crit = new Criteria(); 501 crit.addEqualTo("firstname", title); 502 Query query = new QueryByCriteria(Person.class, crit); 503 Collection result = broker.getCollectionByQuery(query); 504 assertNotNull(result); 505 assertEquals("We expect 2 person instances", 2, result.size()); 506 507 crit = new Criteria(); 508 crit.addEqualTo("id", new Integer (project.getId())); 509 query = new QueryByCriteria(Project.class, crit); 510 result = broker.getCollectionByQuery(query); 511 assertNotNull(result); 512 assertEquals("We expect 1 project instance", 1, result.size()); 513 Project newProject = (Project) result.iterator().next(); 514 assertNotNull(newProject.getRoles()); 515 assertEquals("We expect 2 Role objects", 2, newProject.getRoles().size()); 516 517 crit = new Criteria(); 519 crit.addEqualTo("project_id", new Integer (project.getId())); 520 query = new QueryByCriteria(Role.class, crit); 521 result = broker.getCollectionByQuery(query); 522 assertNotNull(result); 523 assertEquals("We expect 2 role instances", 2, result.size()); 524 } 525 526 530 public void testInsertWithIndirectionTable_3() throws Exception 531 { 532 String title = "testInsertWithIndirectionTable_3_" + System.currentTimeMillis(); 533 534 Project tempProject = createProjectWithAssignedPersons_1(title); 537 538 broker.clearCache(); 540 Criteria critProject = new Criteria(); 541 critProject.addEqualTo("id", new Integer (tempProject.getId())); 542 Query projectQuery = new QueryByCriteria(Project.class, critProject); 543 544 Criteria critPerson = new Criteria(); 545 critPerson.addEqualTo("firstname", title); 546 Query personQuery = new QueryByCriteria(Person.class, critPerson); 547 548 broker.clearCache(); 549 550 Criteria crit = new Criteria(); 553 crit.addEqualTo("project_id", new Integer (tempProject.getId())); 554 Query query = new QueryByCriteria(Role.class, crit); 555 Collection result = broker.getCollectionByQuery(query); 556 assertNotNull(result); 557 assertTrue("test needs existing roles for given id", result.size() > 0); 558 int roleCount = result.size(); 559 560 Project project = (Project) broker.getObjectByQuery(projectQuery); 562 assertNotNull(project); 563 564 Person p1 = new Person(); 566 p1.setFirstname(title); 567 broker.beginTransaction(); 568 broker.store(p1); 569 570 List projects_1 = new ArrayList (); 571 projects_1.add(project); 572 p1.setProjects(projects_1); 573 574 Person p2 = new Person(); 575 p2.setFirstname(title); 576 broker.store(p2); 577 578 List projects_2 = new ArrayList (); 579 projects_2.add(project); 580 p2.setProjects(projects_2); 581 582 project.getPersons().add(p1); 584 project.getPersons().add(p2); 585 586 broker.store(project); 587 broker.commitTransaction(); 588 589 result = broker.getCollectionByQuery(personQuery); 590 assertNotNull(result); 591 assertEquals("We expect 2 new person instances", 2+2, result.size()); 592 593 597 broker.clearCache(); 598 599 result = broker.getCollectionByQuery(personQuery); 600 assertNotNull(result); 601 assertEquals("We expect 2 new person instances", 2+2, result.size()); 602 603 crit = new Criteria(); 604 crit.addEqualTo("id", new Integer (project.getId())); 605 query = new QueryByCriteria(Project.class, crit); 606 result = broker.getCollectionByQuery(query); 607 assertNotNull(result); 608 assertEquals("We expect 1 project instance", 1, result.size()); 609 Project newProject = (Project) result.iterator().next(); 610 assertNotNull(newProject.getRoles()); 611 assertEquals("We expect 2 new Role objects", roleCount + 2, newProject.getRoles().size()); 612 613 crit = new Criteria(); 615 crit.addEqualTo("project_id", new Integer (project.getId())); 616 query = new QueryByCriteria(Role.class, crit); 617 result = broker.getCollectionByQuery(query); 618 assertNotNull(result); 619 assertEquals("We expect 2 role instances", roleCount + 2, result.size()); 620 } 621 622 623 public void testDeletion() throws Exception 624 { 625 Person pers = new Person(); 626 pers.setId(1); 627 Query query = QueryFactory.newQuery(pers); 628 pers = (Person) broker.getObjectByQuery(query); 629 Collection projects = pers.getProjects(); 630 Project[] projectArray = (Project[]) projects.toArray(new Project[0]); assertNotNull(projects); 632 int count = projects.size(); 633 634 Project proj = projectArray[0]; 635 636 Criteria crit = new Criteria(); 637 crit.addEqualTo("person_id", new Integer (pers.getId())); 638 crit.addEqualTo("project_id", new Integer (proj.getId())); 639 Query roleQuery = QueryFactory.newQuery(Role.class, crit); 640 641 Role role = (Role) broker.getObjectByQuery(roleQuery); 642 assertNotNull(role); 643 645 broker.beginTransaction(); 646 broker.delete(proj); 647 broker.commitTransaction(); 648 649 broker.clearCache(); 650 651 pers = (Person) broker.getObjectByQuery(query); 652 assertEquals(count - 1, pers.getProjects().size()); 653 role = (Role) broker.getObjectByQuery(roleQuery); 654 assertNull(role); 655 } 656 657 660 public void testDeleteUnidirectional() throws Exception 661 { 662 PersonUnidirectional p = new PersonUnidirectional(); 663 p.setId(1); 664 Query q = QueryFactory.newQuery(p); 665 p = (PersonUnidirectional) broker.getObjectByQuery(q); 666 Collection projects = p.getProjects(); 667 Collection originalProjects; 668 projects.toArray(new ProjectUnidirectional[0]); originalProjects = new Vector (); 670 originalProjects.addAll(projects); 671 672 assertNotNull(projects); 673 int count = projects.size(); 674 675 ProjectUnidirectional proj = new ProjectUnidirectional(); 676 proj.setTitle("GALVIN"); 677 proj.setDescription("galvins project"); 678 679 p.getProjects().add(proj); 680 broker.beginTransaction(); 681 broker.store(p); 682 broker.commitTransaction(); 683 684 broker.clearCache(); 685 686 p = (PersonUnidirectional) broker.getObjectByQuery(q); 687 688 assertEquals(count + 1, p.getProjects().size()); 689 690 broker.beginTransaction(); 691 692 projects = p.getProjects(); 693 projects.clear(); 694 p.setProjects(projects); 695 696 broker.store(p); 697 broker.commitTransaction(); 698 699 broker.clearCache(); 700 701 p = (PersonUnidirectional) broker.getObjectByQuery(q); 702 assertEquals(0, p.getProjects().size()); 703 704 broker.beginTransaction(); 706 p.setProjects(originalProjects); 707 broker.store(p); 708 broker.delete(proj); 709 broker.commitTransaction(); 710 } 711 } 712 | Popular Tags |