1 package org.apache.ojb.broker; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import org.apache.commons.lang.builder.ToStringBuilder; 8 import org.apache.ojb.broker.metadata.ClassDescriptor; 9 import org.apache.ojb.broker.metadata.CollectionDescriptor; 10 import org.apache.ojb.junit.PBTestCase; 11 12 26 27 37 public class M2NGraphTest extends PBTestCase 38 { 39 public static void main(String [] args) 40 { 41 String [] arr = {M2NGraphTest.class.getName()}; 42 junit.textui.TestRunner.main(arr); 43 } 44 45 public void tearDown() throws Exception 46 { 47 changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 48 changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 49 super.tearDown(); 50 } 51 52 55 public void testAddNewChild_1() throws Exception 56 { 57 changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_OBJECT, false); 58 changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_OBJECT, false); 59 60 61 String postfix = "_testAddNewChild_1_" + System.currentTimeMillis(); 62 63 Node nodeA = new Node("nodeA" + postfix); 64 Node nodeB = new Node("nodeB" + postfix); 65 Node nodeB2 = new Node("nodeB2" + postfix); 66 Node nodeC = new Node("nodeC" + postfix); 67 68 broker.beginTransaction(); 70 nodeA.addChildNode(nodeB); 71 nodeA.addChildNode(nodeB2); 72 nodeB.addChildNode(nodeC); 73 broker.store(nodeA); 74 broker.commitTransaction(); 75 77 Identity oidA = new Identity(nodeA, broker); 78 Identity oidB = new Identity(nodeB, broker); 79 Identity oidC = new Identity(nodeC, broker); 80 81 broker.clearCache(); 82 83 Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 85 assertNotNull("Verifying the nodeB was retrieved", retrievednodeB); 86 assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)", 87 1, retrievednodeB.getParents().size()); 88 Node retrievednodeA = (Node) retrievednodeB.getParents().get(0); 89 assertEquals("verify the nodeA was stored", nodeA.getName(), 90 retrievednodeA.getName()); 91 assertNotNull(retrievednodeA.getChildren()); 92 assertEquals("verify the retrieved nodeA has 2 childs (the nodeB)", 2, 93 retrievednodeA.getChildren().size()); 94 for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();) 95 { 96 Node b = (Node) i.next(); 97 assertNotNull("Verifying nodeA's children are not null", b); 99 assertTrue("verify, child is the nodeBx", 100 nodeB.getName().equals(b.getName()) || nodeB2.getName().equals(b.getName())); 101 } 102 assertNotNull(retrievednodeB.getChildren()); 103 assertEquals(1, retrievednodeB.getChildren().size()); 104 assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName()); 105 106 retrievednodeA = (Node) broker.getObjectByIdentity(oidA); 108 assertNotNull(retrievednodeA); 109 assertNotNull(retrievednodeA.getChildren()); 110 assertEquals(2, retrievednodeA.getChildren().size()); 111 retrievednodeB = (Node) retrievednodeA.getChildren().get(0); 112 assertNotNull(retrievednodeB); 113 assertNotNull(retrievednodeB.getParents()); 114 assertEquals(1, retrievednodeB.getParents().size()); 115 116 broker.clearCache(); 117 retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 118 broker.beginTransaction(); 120 broker.delete(nodeB); 121 broker.commitTransaction(); 122 124 retrievednodeA = (Node) broker.getObjectByIdentity(oidA); 125 retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 126 Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC); 127 128 assertNull(retrievednodeA); 130 assertNull(retrievednodeB); 131 assertNull(retrievednodeC); 132 } 133 134 137 public void testAddNewChild_2() throws Exception 138 { 139 changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 140 changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 141 142 143 String postfix = "_testAddNewChild_2_" + System.currentTimeMillis(); 144 145 Node nodeA = new Node("nodeA" + postfix); 146 Node nodeB = new Node("nodeB" + postfix); 147 Node nodeB2 = new Node("nodeB2" + postfix); 148 Node nodeC = new Node("nodeC" + postfix); 149 150 broker.beginTransaction(); 152 nodeA.addChildNode(nodeB); 153 nodeA.addChildNode(nodeB2); 154 nodeB.addChildNode(nodeC); 155 156 broker.store(nodeA); 158 broker.store(nodeB); 160 broker.store(nodeB2); 161 broker.store(nodeC); 162 broker.serviceBrokerHelper().link(nodeA, true); 164 broker.serviceBrokerHelper().link(nodeB, "children", true); 168 broker.commitTransaction(); 169 171 Identity oidA = new Identity(nodeA, broker); 172 Identity oidB = new Identity(nodeB, broker); 173 Identity oidC = new Identity(nodeC, broker); 174 175 broker.clearCache(); 176 177 Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 179 180 assertNotNull("Verifying the nodeB was retrieved", retrievednodeB); 181 assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)", 182 1, retrievednodeB.getParents().size()); 183 184 Node retrievednodeA = (Node) retrievednodeB.getParents().get(0); 185 186 assertEquals("verify the nodeA was stored", nodeA.getName(), 187 retrievednodeA.getName()); 188 assertNotNull(retrievednodeA.getChildren()); 189 assertEquals("verify the retrieved nodeA has 2 childs (the nodeB)", 2, 190 retrievednodeA.getChildren().size()); 191 for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();) 192 { 193 Node b = (Node) i.next(); 194 assertNotNull("Verifying nodeA's children are not null", b); 196 assertTrue("verify, child is the nodeBx", 197 nodeB.getName().equals(b.getName()) || nodeB2.getName().equals(b.getName())); 198 } 199 assertNotNull(retrievednodeB.getChildren()); 200 assertEquals(1, retrievednodeB.getChildren().size()); 201 assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName()); 202 203 retrievednodeA = (Node) broker.getObjectByIdentity(oidA); 205 assertNotNull(retrievednodeA); 206 assertNotNull(retrievednodeA.getChildren()); 207 assertEquals(2, retrievednodeA.getChildren().size()); 208 retrievednodeB = (Node) retrievednodeA.getChildren().get(0); 209 assertNotNull(retrievednodeB); 210 assertNotNull(retrievednodeB.getParents()); 211 assertEquals(1, retrievednodeB.getParents().size()); 212 213 broker.clearCache(); 214 retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 215 broker.beginTransaction(); 218 broker.serviceBrokerHelper().unlink(nodeB); 219 broker.delete(nodeB); 220 broker.commitTransaction(); 221 223 retrievednodeA = (Node) broker.getObjectByIdentity(oidA); 224 retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 225 Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC); 226 227 assertNotNull(retrievednodeA); 229 assertNull(retrievednodeB); 230 assertNotNull(retrievednodeC); 231 } 232 233 236 public void testAddNewChild_3() throws Exception 237 { 238 changeRelationMetadata("children", false, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 239 changeRelationMetadata("parents", false, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE, false); 240 241 String postfix = "_testAddNewChild_3_" + System.currentTimeMillis(); 242 243 Node nodeA = new Node("nodeA" + postfix); 244 Node nodeB = new Node("nodeB" + postfix); 245 Node nodeC = new Node("nodeC" + postfix); 246 247 broker.beginTransaction(); 249 nodeA.addChildNode(nodeB); 250 nodeB.addChildNode(nodeC); 251 broker.store(nodeA); 253 broker.store(nodeB); 255 broker.store(nodeC); 256 broker.serviceBrokerHelper().link(nodeA, "children", true); 258 broker.serviceBrokerHelper().link(nodeB, "children", true); 259 broker.commitTransaction(); 260 262 Identity oidA = new Identity(nodeA, broker); 263 Identity oidB = new Identity(nodeB, broker); 264 265 broker.clearCache(); 266 267 Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB); 269 assertNotNull("Verifying the nodeB was retrieved", retrievednodeB); 270 assertEquals(nodeB.getName(), retrievednodeB.getName()); 271 broker.retrieveReference(retrievednodeB, "parents"); 273 assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)", 274 1, retrievednodeB.getParents().size()); 275 Node retrievednodeA = (Node) retrievednodeB.getParents().get(0); 276 broker.retrieveReference(retrievednodeA, "children"); 278 assertEquals("verify the nodeA was stored", nodeA.getName(), 279 retrievednodeA.getName()); 280 assertNotNull(retrievednodeA.getChildren()); 281 assertEquals("verify the retrieved nodeA has 1 child (the nodeB)", 1, 282 retrievednodeA.getChildren().size()); 283 for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();) 284 { 285 Node b = (Node) i.next(); 286 assertNotNull("Verifying nodeA's children are not null", b); 288 assertEquals("verify, using hashcode, that the nodeAs child is the nodeB", 289 retrievednodeB.getName(), b.getName()); 290 } 291 broker.retrieveReference(retrievednodeB, "children"); 293 assertNotNull(retrievednodeB.getChildren()); 294 assertEquals(1, retrievednodeB.getChildren().size()); 295 assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName()); 296 297 retrievednodeA = (Node) broker.getObjectByIdentity(oidA); 299 broker.retrieveReference(retrievednodeA, "children"); 300 assertNotNull(retrievednodeA); 301 assertNotNull(retrievednodeA.getChildren()); 302 assertEquals(1, retrievednodeA.getChildren().size()); 303 retrievednodeB = (Node) retrievednodeA.getChildren().get(0); 304 assertNotNull(retrievednodeB); 305 broker.retrieveReference(retrievednodeB, "parents"); 306 assertNotNull(retrievednodeB.getParents()); 307 assertEquals(1, retrievednodeB.getParents().size()); 308 } 309 310 void changeRelationMetadata(String field, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean proxy) 311 { 312 ClassDescriptor cld = broker.getClassDescriptor(Node.class); 313 CollectionDescriptor cod = cld.getCollectionDescriptorByName(field); 314 cod.setLazy(proxy); 315 cod.setCascadeRetrieve(autoRetrieve); 316 cod.setCascadingStore(autoUpdate); 317 cod.setCascadingDelete(autoDelete); 318 } 319 320 public static class Node 324 { 325 private Integer id; 326 private String name; 327 private List children; 328 private List parents; 329 330 public Node() 331 { 332 } 333 334 public Node(String name) 335 { 336 this.name = name; 337 } 338 339 public Integer getId() 340 { 341 return id; 342 } 343 344 public void setId(Integer id) 345 { 346 this.id = id; 347 } 348 349 public String getName() 350 { 351 return name; 352 } 353 354 public void setName(String name) 355 { 356 this.name = name; 357 } 358 359 public void addChildNode(Node child) 360 { 361 if (!getChildren().contains(child)) 363 { 364 getChildren().add(child); 365 if (!child.getParents().contains(this)) 366 { 367 child.getParents().add(this); 368 } 369 } 370 } 371 372 384 public void removeChild(Node child) 385 { 386 getChildren().remove(child); 387 child.getParents().remove(this); 388 } 389 390 public List getParents() 391 { 392 if (parents == null) 393 { 394 parents = new ArrayList (); 395 } 396 return parents; 397 } 398 399 public void setParents(List parents) 400 { 401 this.parents = parents; 402 } 403 404 public List getChildren() 405 { 406 if (children == null) 407 { 408 children = new ArrayList (); 409 } 410 return children; 411 } 412 413 public void setChildren(List children) 414 { 415 this.children = children; 416 } 417 418 public String toString() 419 { 420 return new ToStringBuilder(this) 421 .append("id", id) 422 .append("name", name) 423 .append("children", children != null ? children.size() : 0) 424 .append("parents", parents != null ? parents.size() : 0) 425 .toString(); 426 } 427 428 public boolean equals(Object ref) 429 { 430 if (ref == null || !(ref instanceof Node)) 431 { 432 return false; 433 } 434 else 435 { 436 return (getId() != null ? getId().equals(((Node) ref).getId()) : false); 437 } 438 } 439 } 440 } 441 | Popular Tags |