1 package org.apache.ojb.broker; 2 3 import org.apache.ojb.broker.metadata.FieldDescriptor; 4 import org.apache.ojb.junit.PBTestCase; 5 6 import java.util.Vector ; 7 8 11 public class TreeTest extends PBTestCase 12 { 13 public static void main(String [] args) 14 { 15 String [] arr = {TreeTest.class.getName()}; 16 junit.textui.TestRunner.main(arr); 17 } 18 19 public TreeTest(String name) 20 21 { 22 super(name); 23 } 24 25 30 public TreeGroup createTreeGroup() throws Exception 31 { 32 TreeGroup result = new TreeGroup(); 33 34 FieldDescriptor idFld = broker.getClassDescriptor(Tree.class).getFieldDescriptorByName("id"); 35 Integer idVal = (Integer ) broker.serviceSequenceManager().getUniqueValue(idFld); 36 37 result.setId(idVal.intValue()); 38 result.setData("" + result.getId()); 39 result.setChilds(new Vector ()); 40 result.setMembers(new Vector ()); 41 return result; 42 } 43 44 50 public Tree createTreeNodeWithParent(Tree parent) throws Exception 51 { 52 53 Tree result = new Tree(); 54 try 55 { 56 FieldDescriptor idFld = broker.getClassDescriptor(Tree.class).getFieldDescriptorByName("id"); 57 Integer idVal = (Integer ) broker.serviceSequenceManager().getUniqueValue(idFld); 58 59 result.setId(idVal.intValue()); 60 } 61 catch (PersistenceBrokerException e) 62 63 { 64 } 65 result.setData(parent.getId() + "-" + result.getId()); 66 result.setParentId(parent.getId()); 67 result.setChilds(new Vector ()); 68 69 return result; 70 } 71 72 74 public void testCreate() 75 { 76 try 77 { 78 79 Tree root = new Tree(); 80 try 81 { 82 FieldDescriptor idFld = broker.getClassDescriptor(Tree.class).getFieldDescriptorByName("id"); 83 Integer idVal = (Integer ) broker.serviceSequenceManager().getUniqueValue(idFld); 84 85 root.setId(idVal.intValue()); 86 } 87 catch (PersistenceBrokerException e) 88 89 { 90 } 91 root.setData("a brand new root: " + root.getId()); 92 93 root.addChild(createTreeNodeWithParent(root)); 94 root.addChild(createTreeNodeWithParent(root)); 95 root.addChild(createTreeNodeWithParent(root)); 96 root.addChild(createTreeNodeWithParent(root)); 97 root.addChild(createTreeNodeWithParent(root)); 98 99 Tree child = root.getChild(0); 100 child.addChild(createTreeNodeWithParent(child)); 101 child.addChild(createTreeNodeWithParent(child)); 102 child.addChild(createTreeNodeWithParent(child)); 103 104 child = child.getChild(1); 105 child.addChild(createTreeNodeWithParent(child)); 106 child.addChild(createTreeNodeWithParent(child)); 107 108 broker.beginTransaction(); 111 broker.store(root); 112 broker.commitTransaction(); 113 114 Identity oid = new Identity(root, broker); 115 116 broker.clearCache(); 117 118 Tree retrieved = (Tree) broker.getObjectByIdentity(oid); 119 120 123 assertEquals( 124 "tree should have same size after retrival", 125 root.size(), 126 retrieved.size()); 127 128 } 129 catch (Throwable t) 130 { 131 fail(t.getMessage()); 132 } 133 } 134 135 138 public void testTreeGroup() throws Exception 139 { 140 TreeGroup root = createTreeGroup(); 141 root.setData("The Tree Group root: " + root.getId()); 142 TreeGroup green = createTreeGroup(); 143 green.setData("the GREEN group " + green.getId()); 144 TreeGroup red = createTreeGroup(); 145 red.setData("the RED group " + red.getId()); 146 147 TreeGroup child; 148 for (int i = 0; i < 3; i++) 149 { 150 child = createTreeGroup(); 151 root.addChild(child); 152 green.addMember(child); 153 child = createTreeGroup(); 154 root.addChild(child); 155 red.addMember(child); 156 } 157 158 for (int i = 0; i < 6; i++) 159 { 160 child = root.getChild(i); 161 child.addChild(createTreeGroup()); 162 } 163 164 broker.beginTransaction(); 171 broker.store(root); 172 broker.store(green); 173 broker.store(red); 174 broker.commitTransaction(); 175 176 Identity oid = new Identity(root, broker); 177 178 broker.clearCache(); 179 180 TreeGroup root_r = (TreeGroup) broker.getObjectByIdentity(oid); 181 assertNotNull(root_r); 184 assertEquals( 185 "tree should have same size after retrival", 186 root.size(), 187 root_r.size()); 188 189 oid = new Identity(green, broker); 190 TreeGroup green_r = (TreeGroup) broker.getObjectByIdentity(oid); 191 assertEquals( 194 "tree should have same size after retrival", 195 green.size(), 196 green_r.size()); 197 198 oid = new Identity(red, broker); 199 TreeGroup red_r = (TreeGroup) broker.getObjectByIdentity(oid); 200 assertEquals( 203 "tree should have same size after retrival", 204 red.size(), 205 red_r.size()); 206 207 } 208 209 210 211 212 216 223 public static class Tree implements java.io.Serializable 224 { 225 private int id; 226 private String data; 227 private int parentId; 228 private Vector childs; 229 230 233 public Tree() 234 { 235 super(); 236 } 237 238 241 public Tree(int id, String data, int parentid) 242 { 243 this.id = id; 244 this.data = data; 245 this.parentId = parentid; 246 } 247 248 public void addChild(Tree newChild) 249 { 250 if (childs == null) childs = new Vector (); 251 252 childs.add(newChild); 253 254 } 255 256 public Tree getChild(int index) 257 { 258 return (Tree) childs.get(index); 259 260 } 261 262 267 public java.util.Vector getChilds() 268 { 269 return childs; 270 } 271 272 277 public java.lang.String getData() 278 { 279 return data; 280 } 281 282 287 public int getId() 288 { 289 return id; 290 } 291 292 297 public int getParentId() 298 { 299 return parentId; 300 } 301 302 307 public void setChilds(java.util.Vector newChilds) 308 { 309 childs = newChilds; 310 } 311 312 317 public void setData(java.lang.String newData) 318 { 319 data = newData; 320 } 321 322 327 public void setId(int newId) 328 { 329 id = newId; 330 } 331 332 337 public void setParentId(int newParentId) 338 { 339 parentId = newParentId; 340 } 341 342 347 public int size() 348 { 349 int result = 1; 350 for (int i = 0; i < childs.size(); i++) 351 { 352 result += ((Tree) childs.get(i)).size(); 353 } 354 return result; 355 } 356 357 362 public String toString() 363 { 364 365 return data + ((childs == null) ? "" : childs.toString()); 366 } 367 } 368 369 376 public static class TreeGroup implements java.io.Serializable 377 { 378 private int id; 379 private String data; 380 381 private int parentId; 382 private Vector children; 383 private TreeGroup myParent; 384 385 private int groupId; 386 private Vector groupMembers; 387 private TreeGroup myGroup; 388 389 390 393 public TreeGroup() 394 { 395 super(); 396 } 397 398 401 public TreeGroup(int id, String data, int parentid, int groupid) 402 { 403 this.id = id; 404 this.data = data; 405 this.parentId = parentid; 406 this.groupId = groupid; 407 } 408 409 public void addChild(TreeGroup newChild) 410 { 411 if (children == null) children = new Vector (); 412 413 children.add(newChild); 414 newChild.setParentId(this.getId()); 415 416 } 417 418 public void addMember(TreeGroup newMember) 419 { 420 if (groupMembers == null) groupMembers = new Vector (); 421 422 groupMembers.add(newMember); 423 newMember.setGroupId(this.getId()); 424 425 } 426 427 public TreeGroup getChild(int index) 428 { 429 return (TreeGroup) children.get(index); 430 431 } 432 433 438 public Vector getChilds() 439 { 440 return children; 441 } 442 443 448 public String getData() 449 { 450 return data; 451 } 452 453 458 public int getGroupId() 459 { 460 return groupId; 461 } 462 463 468 public int getId() 469 { 470 return id; 471 } 472 473 public TreeGroup getMember(int index) 474 { 475 return (TreeGroup) groupMembers.get(index); 476 477 } 478 479 484 public Vector getMembers() 485 { 486 return groupMembers; 487 } 488 489 494 public int getParentId() 495 { 496 return parentId; 497 } 498 499 504 public void setChilds(java.util.Vector newChilds) 505 { 506 children = newChilds; 507 } 508 509 513 public void setData(String data) 514 { 515 this.data = data; 516 } 517 518 522 public void setGroupId(int groupId) 523 { 524 this.groupId = groupId; 525 } 526 527 531 public void setId(int id) 532 { 533 this.id = id; 534 } 535 536 541 public void setMembers(java.util.Vector newMembers) 542 { 543 groupMembers = newMembers; 544 } 545 546 550 public void setParentId(int parentId) 551 { 552 this.parentId = parentId; 553 } 554 555 560 public int size() 561 { 562 int result = 1; 563 if(children == null) return result; 564 for (int i = 0; i < children.size(); i++) 565 { 566 result += ((TreeGroup) children.get(i)).size(); 567 } 568 return result; 569 } 570 571 576 public String toString() 577 { 578 return data 579 + ((children == null || (children.size() == 0)) ? "" : " children: " + children.toString()) 580 + ((groupMembers == null || (groupMembers.size() == 0)) ? "" : " members: " + groupMembers.toString()); 581 } 582 586 public Vector getChildren() 587 { 588 return children; 589 } 590 591 595 public void setChildren(Vector children) 596 { 597 this.children = children; 598 } 599 600 604 public Vector getGroupMembers() 605 { 606 return groupMembers; 607 } 608 609 613 public void setGroupMembers(Vector groupMembers) 614 { 615 this.groupMembers = groupMembers; 616 } 617 618 622 public TreeGroup getMyGroup() 623 { 624 return myGroup; 625 } 626 627 631 public void setMyGroup(TreeGroup myGroup) 632 { 633 this.myGroup = myGroup; 634 } 635 636 640 public TreeGroup getMyParent() 641 { 642 return myParent; 643 } 644 645 649 public void setMyParent(TreeGroup myParent) 650 { 651 this.myParent = myParent; 652 } 653 654 } 655 656 657 } 658 | Popular Tags |