KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
7
8 /**
9  * Testing selfjoins and tree structures
10  */

11 public class TreeTest extends PBTestCase
12 {
13     public static void main(String JavaDoc[] args)
14     {
15         String JavaDoc[] arr = {TreeTest.class.getName()};
16         junit.textui.TestRunner.main(arr);
17     }
18
19     public TreeTest(String JavaDoc name)
20
21     {
22         super(name);
23     }
24
25     /**
26      * Insert the method's description here.
27      * Creation date: (13.02.2001 18:50:29)
28      * @return TestThreadsNLocks.org.apache.ojb.broker.Tree
29      */

30     public TreeGroup createTreeGroup() throws Exception JavaDoc
31     {
32         TreeGroup result = new TreeGroup();
33
34         FieldDescriptor idFld = broker.getClassDescriptor(Tree.class).getFieldDescriptorByName("id");
35         Integer JavaDoc idVal = (Integer JavaDoc) broker.serviceSequenceManager().getUniqueValue(idFld);
36
37         result.setId(idVal.intValue());
38         result.setData("" + result.getId());
39         result.setChilds(new Vector JavaDoc());
40         result.setMembers(new Vector JavaDoc());
41         return result;
42     }
43
44     /**
45      * Insert the method's description here.
46      * Creation date: (13.02.2001 18:50:29)
47      * @return TestThreadsNLocks.org.apache.ojb.broker.Tree
48      * @param parent TestThreadsNLocks.org.apache.ojb.broker.Tree
49      */

50     public Tree createTreeNodeWithParent(Tree parent) throws Exception JavaDoc
51     {
52
53         Tree result = new Tree();
54         try
55         {
56             FieldDescriptor idFld = broker.getClassDescriptor(Tree.class).getFieldDescriptorByName("id");
57             Integer JavaDoc idVal = (Integer JavaDoc) 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 JavaDoc());
68
69         return result;
70     }
71
72     /**
73      */

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 JavaDoc idVal = (Integer JavaDoc) 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             //System.out.println("original tree:");
109
//System.out.println(root);
110
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             //System.out.println("tree after reading from db:");
121
//System.out.println(retrieved);
122

123             assertEquals(
124                 "tree should have same size after retrival",
125                 root.size(),
126                 retrieved.size());
127
128         }
129         catch (Throwable JavaDoc t)
130         {
131             fail(t.getMessage());
132         }
133     }
134
135     /**
136      *
137      */

138     public void testTreeGroup() throws Exception JavaDoc
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         //System.out.println("original TreeGroup:");
165
//System.out.println(root);
166
//System.out.println("GREEN TreeGroup:");
167
//System.out.println(green);
168
//System.out.println("RED TreeGroup:");
169
//System.out.println(red);
170
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         //System.out.println("tree after reading from db:");
182
//System.out.println(root_r);
183
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         //System.out.println("tree after reading from db:");
192
//System.out.println(green_r);
193
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         //System.out.println("tree after reading from db:");
201
//System.out.println(red_r);
202
assertEquals(
203             "tree should have same size after retrival",
204             red.size(),
205             red_r.size());
206
207     }
208
209
210
211     
212     //*****************************************************************
213
// inner classes - test objects
214
//*****************************************************************
215

216     /**
217      * Tree is recursive type: a Tree element contains some data
218      * and a Vector of child Tree elements.
219      * This sample demonstrates what is needed to map such a data
220      * structure on a DB table
221      * @author Thomas Mahler
222      */

223     public static class Tree implements java.io.Serializable JavaDoc
224     {
225         private int id;
226         private String JavaDoc data;
227         private int parentId;
228         private Vector JavaDoc childs;
229
230         /**
231          * Tree constructor comment.
232          */

233         public Tree()
234         {
235             super();
236         }
237
238         /**
239          * Tree constructor comment.
240          */

241         public Tree(int id, String JavaDoc 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 JavaDoc();
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         /**
263          * Insert the method's description here.
264          * Creation date: (13.02.2001 18:33:02)
265          * @return java.util.Vector
266          */

267         public java.util.Vector JavaDoc getChilds()
268         {
269             return childs;
270         }
271
272         /**
273          * Insert the method's description here.
274          * Creation date: (13.02.2001 18:33:02)
275          * @return java.lang.String
276          */

277         public java.lang.String JavaDoc getData()
278         {
279             return data;
280         }
281
282         /**
283          * Insert the method's description here.
284          * Creation date: (13.02.2001 18:33:02)
285          * @return int
286          */

287         public int getId()
288         {
289             return id;
290         }
291
292         /**
293          * Insert the method's description here.
294          * Creation date: (13.02.2001 18:33:02)
295          * @return int
296          */

297         public int getParentId()
298         {
299             return parentId;
300         }
301
302         /**
303          * Insert the method's description here.
304          * Creation date: (13.02.2001 18:33:02)
305          * @param newChilds java.util.Vector
306          */

307         public void setChilds(java.util.Vector JavaDoc newChilds)
308         {
309             childs = newChilds;
310         }
311
312         /**
313          * Insert the method's description here.
314          * Creation date: (13.02.2001 18:33:02)
315          * @param newData java.lang.String
316          */

317         public void setData(java.lang.String JavaDoc newData)
318         {
319             data = newData;
320         }
321
322         /**
323          * Insert the method's description here.
324          * Creation date: (13.02.2001 18:33:02)
325          * @param newId int
326          */

327         public void setId(int newId)
328         {
329             id = newId;
330         }
331
332         /**
333          * Insert the method's description here.
334          * Creation date: (13.02.2001 18:33:02)
335          * @param newParentId int
336          */

337         public void setParentId(int newParentId)
338         {
339             parentId = newParentId;
340         }
341
342         /**
343          * Insert the method's description here.
344          * Creation date: (14.02.2001 19:51:23)
345          * @return int
346          */

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         /**
358          * Insert the method's description here.
359          * Creation date: (13.02.2001 18:33:41)
360          * @return java.lang.String
361          */

362         public String JavaDoc toString()
363         {
364
365             return data + ((childs == null) ? "" : childs.toString());
366         }
367     }
368
369     /**
370      * Tree is recursive type: a Tree element contains some data
371      * and a Vector of child Tree elements.
372      * This sample demonstrates what is needed to map such a data
373      * structure on a DB table
374      * @author Thomas Mahler
375      */

376     public static class TreeGroup implements java.io.Serializable JavaDoc
377     {
378         private int id;
379         private String JavaDoc data;
380
381         private int parentId;
382         private Vector JavaDoc children;
383         private TreeGroup myParent;
384
385         private int groupId;
386         private Vector JavaDoc groupMembers;
387         private TreeGroup myGroup;
388
389
390         /**
391          * Tree constructor comment.
392          */

393         public TreeGroup()
394         {
395             super();
396         }
397
398         /**
399          * Tree constructor comment.
400          */

401         public TreeGroup(int id, String JavaDoc 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 JavaDoc();
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 JavaDoc();
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         /**
434          * Insert the method's description here.
435          * Creation date: (13.02.2001 18:33:02)
436          * @return java.util.Vector
437          */

438         public Vector JavaDoc getChilds()
439         {
440             return children;
441         }
442
443         /**
444          * Insert the method's description here.
445          * Creation date: (13.02.2001 18:33:02)
446          * @return java.lang.String
447          */

448         public String JavaDoc getData()
449         {
450             return data;
451         }
452
453         /**
454          * Insert the method's description here.
455          * Creation date: (13.02.2001 18:33:02)
456          * @return int
457          */

458         public int getGroupId()
459         {
460             return groupId;
461         }
462
463         /**
464          * Insert the method's description here.
465          * Creation date: (13.02.2001 18:33:02)
466          * @return int
467          */

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         /**
480          * Insert the method's description here.
481          * Creation date: (13.02.2001 18:33:02)
482          * @return java.util.Vector
483          */

484         public Vector JavaDoc getMembers()
485         {
486             return groupMembers;
487         }
488
489         /**
490          * Insert the method's description here.
491          * Creation date: (13.02.2001 18:33:02)
492          * @return int
493          */

494         public int getParentId()
495         {
496             return parentId;
497         }
498
499         /**
500          * Insert the method's description here.
501          * Creation date: (13.02.2001 18:33:02)
502          * @param newChilds java.util.Vector
503          */

504         public void setChilds(java.util.Vector JavaDoc newChilds)
505         {
506             children = newChilds;
507         }
508
509         /**
510          * Sets the data.
511          * @param data The data to set
512          */

513         public void setData(String JavaDoc data)
514         {
515             this.data = data;
516         }
517
518         /**
519          * Sets the groupId.
520          * @param groupId The groupId to set
521          */

522         public void setGroupId(int groupId)
523         {
524             this.groupId = groupId;
525         }
526
527         /**
528          * Sets the id.
529          * @param id The id to set
530          */

531         public void setId(int id)
532         {
533             this.id = id;
534         }
535
536         /**
537          * Insert the method's description here.
538          * Creation date: (13.02.2001 18:33:02)
539          * @param newMembers java.util.Vector
540          */

541         public void setMembers(java.util.Vector JavaDoc newMembers)
542         {
543             groupMembers = newMembers;
544         }
545
546         /**
547          * Sets the parentId.
548          * @param parentId The parentId to set
549          */

550         public void setParentId(int parentId)
551         {
552             this.parentId = parentId;
553         }
554
555         /**
556          * Insert the method's description here.
557          * Creation date: (14.02.2001 19:51:23)
558          * @return int
559          */

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         /**
572          * Insert the method's description here.
573          * Creation date: (13.02.2001 18:33:41)
574          * @return java.lang.String
575          */

576         public String JavaDoc 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         /**
583          * Gets the children.
584          * @return Returns a Vector
585          */

586         public Vector JavaDoc getChildren()
587         {
588             return children;
589         }
590
591         /**
592          * Sets the children.
593          * @param children The children to set
594          */

595         public void setChildren(Vector JavaDoc children)
596         {
597             this.children = children;
598         }
599
600         /**
601          * Gets the groupMembers.
602          * @return Returns a Vector
603          */

604         public Vector JavaDoc getGroupMembers()
605         {
606             return groupMembers;
607         }
608
609         /**
610          * Sets the groupMembers.
611          * @param groupMembers The groupMembers to set
612          */

613         public void setGroupMembers(Vector JavaDoc groupMembers)
614         {
615             this.groupMembers = groupMembers;
616         }
617
618         /**
619          * Gets the myGroup.
620          * @return Returns a TreeGroup
621          */

622         public TreeGroup getMyGroup()
623         {
624             return myGroup;
625         }
626
627         /**
628          * Sets the myGroup.
629          * @param myGroup The myGroup to set
630          */

631         public void setMyGroup(TreeGroup myGroup)
632         {
633             this.myGroup = myGroup;
634         }
635
636         /**
637          * Gets the myParent.
638          * @return Returns a TreeGroup
639          */

640         public TreeGroup getMyParent()
641         {
642             return myParent;
643         }
644
645         /**
646          * Sets the myParent.
647          * @param myParent The myParent to set
648          */

649         public void setMyParent(TreeGroup myParent)
650         {
651             this.myParent = myParent;
652         }
653
654     }
655
656
657 }
658
Popular Tags