KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > M2NGraphTest


1 package org.apache.ojb.broker;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
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 /* Copyright 2002-2005 The Apache Software Foundation
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */

26
27 /**
28  * Realize object tree via m:n relation.
29  *
30  * IMPORTANT NOTE: The global runtime metadata changes made by this test case
31  * are NOT recommended in multithreaded environments, because they are global
32  * and each thread will be affected.
33  *
34  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
35  * @version $Id: M2NGraphTest.java,v 1.2.2.3 2005/12/21 22:31:23 tomdz Exp $
36  */

37 public class M2NGraphTest extends PBTestCase
38 {
39     public static void main(String JavaDoc[] args)
40     {
41         String JavaDoc[] arr = {M2NGraphTest.class.getName()};
42         junit.textui.TestRunner.main(arr);
43     }
44
45     public void tearDown() throws Exception JavaDoc
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     /**
53      * All stuff is done by OJB
54      */

55     public void testAddNewChild_1() throws Exception JavaDoc
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 JavaDoc 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         //===============================================
69
broker.beginTransaction();
70         nodeA.addChildNode(nodeB);
71         nodeA.addChildNode(nodeB2);
72         nodeB.addChildNode(nodeC);
73         broker.store(nodeA);
74         broker.commitTransaction();
75         //===============================================
76

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         // get the stored stuff
84
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 JavaDoc i = retrievednodeA.getChildren().iterator(); i.hasNext();)
95         {
96             Node b = (Node) i.next();
97             //this next test fails because the child is null
98
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         // get the stored stuff vice versa
107
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         //===============================================
119
broker.beginTransaction();
120         broker.delete(nodeB);
121         broker.commitTransaction();
122         //===============================================
123

124         retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
125         retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
126         Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC);
127
128         // complete hierachy will be deleted
129
assertNull(retrievednodeA);
130         assertNull(retrievednodeB);
131         assertNull(retrievednodeC);
132     }
133
134     /**
135      * Autoretrieve is true, update/delete is done by hand
136      */

137     public void testAddNewChild_2() throws Exception JavaDoc
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 JavaDoc 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         //===============================================
151
broker.beginTransaction();
152         nodeA.addChildNode(nodeB);
153         nodeA.addChildNode(nodeB2);
154         nodeB.addChildNode(nodeC);
155
156         //store nodeA first
157
broker.store(nodeA);
158         //then store the nodeB
159
broker.store(nodeB);
160         broker.store(nodeB2);
161         broker.store(nodeC);
162         //make A the parent of B because they are not linked in the code automatically
163
broker.serviceBrokerHelper().link(nodeA, true);
164         // broker.serviceBrokerHelper().link(nodeB, true);
165
// this will not work, because nodeB already linked by nodeA
166
// so better specify the field to be link
167
broker.serviceBrokerHelper().link(nodeB, "children", true);
168         broker.commitTransaction();
169         //===============================================
170

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         // get the stored stuff
178
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 JavaDoc i = retrievednodeA.getChildren().iterator(); i.hasNext();)
192         {
193             Node b = (Node) i.next();
194             //this next test fails because the child is null
195
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         // get the stored stuff vice versa
204
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         // cascade delete is not set and we only want to delete node B
216
//===============================================
217
broker.beginTransaction();
218         broker.serviceBrokerHelper().unlink(nodeB);
219         broker.delete(nodeB);
220         broker.commitTransaction();
221         //===============================================
222

223         retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
224         retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
225         Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC);
226
227         // only nodeB should be deleted
228
assertNotNull(retrievednodeA);
229         assertNull(retrievednodeB);
230         assertNotNull(retrievednodeC);
231     }
232
233     /**
234      * Do all work manually
235      */

236     public void testAddNewChild_3() throws Exception JavaDoc
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 JavaDoc 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         //===============================================
248
broker.beginTransaction();
249         nodeA.addChildNode(nodeB);
250         nodeB.addChildNode(nodeC);
251         //store nodeA first
252
broker.store(nodeA);
253         //then store the nodeB
254
broker.store(nodeB);
255         broker.store(nodeC);
256         //make A the parent of B because they are not linked in the code automatically
257
broker.serviceBrokerHelper().link(nodeA, "children", true);
258         broker.serviceBrokerHelper().link(nodeB, "children", true);
259         broker.commitTransaction();
260         //===============================================
261

262         Identity oidA = new Identity(nodeA, broker);
263         Identity oidB = new Identity(nodeB, broker);
264
265         broker.clearCache();
266
267         // get the stored stuff
268
Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
269         assertNotNull("Verifying the nodeB was retrieved", retrievednodeB);
270         assertEquals(nodeB.getName(), retrievednodeB.getName());
271         //===============================================
272
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         //===============================================
277
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 JavaDoc i = retrievednodeA.getChildren().iterator(); i.hasNext();)
284         {
285             Node b = (Node) i.next();
286             //this next test fails because the child is null
287
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         //===============================================
292
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         // get the stored stuff vice versa
298
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 JavaDoc 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     //=========================================
321
// test classes
322
//=========================================
323
public static class Node
324     {
325         private Integer JavaDoc id;
326         private String JavaDoc name;
327         private List JavaDoc children;
328         private List JavaDoc parents;
329
330         public Node()
331         {
332         }
333
334         public Node(String JavaDoc name)
335         {
336             this.name = name;
337         }
338
339         public Integer JavaDoc getId()
340         {
341             return id;
342         }
343
344         public void setId(Integer JavaDoc id)
345         {
346             this.id = id;
347         }
348
349         public String JavaDoc getName()
350         {
351             return name;
352         }
353
354         public void setName(String JavaDoc name)
355         {
356             this.name = name;
357         }
358
359         public void addChildNode(Node child)
360         {
361             // Cannot add same child twice
362
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 // public void addParentNode(Node parent)
373
// {
374
// if(! getParents().contains(parent))
375
// {
376
// getParents().add(parent);
377
// if(!parent.getChildren().contains(this))
378
// {
379
// parent.getChildren().add(this);
380
// }
381
// }
382
// }
383

384         public void removeChild(Node child)
385         {
386             getChildren().remove(child);
387             child.getParents().remove(this);
388         }
389
390         public List JavaDoc getParents()
391         {
392             if (parents == null)
393             {
394                 parents = new ArrayList JavaDoc();
395             }
396             return parents;
397         }
398
399         public void setParents(List JavaDoc parents)
400         {
401             this.parents = parents;
402         }
403
404         public List JavaDoc getChildren()
405         {
406             if (children == null)
407             {
408                 children = new ArrayList JavaDoc();
409             }
410             return children;
411         }
412
413         public void setChildren(List JavaDoc children)
414         {
415             this.children = children;
416         }
417
418         public String JavaDoc 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 JavaDoc 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