001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.metadata.FieldDescriptor;
004: import org.apache.ojb.junit.PBTestCase;
005:
006: import java.util.Vector;
007:
008: /**
009: * Testing selfjoins and tree structures
010: */
011: public class TreeTest extends PBTestCase {
012: public static void main(String[] args) {
013: String[] arr = { TreeTest.class.getName() };
014: junit.textui.TestRunner.main(arr);
015: }
016:
017: public TreeTest(String name)
018:
019: {
020: super (name);
021: }
022:
023: /**
024: * Insert the method's description here.
025: * Creation date: (13.02.2001 18:50:29)
026: * @return TestThreadsNLocks.org.apache.ojb.broker.Tree
027: */
028: public TreeGroup createTreeGroup() throws Exception {
029: TreeGroup result = new TreeGroup();
030:
031: FieldDescriptor idFld = broker.getClassDescriptor(Tree.class)
032: .getFieldDescriptorByName("id");
033: Integer idVal = (Integer) broker.serviceSequenceManager()
034: .getUniqueValue(idFld);
035:
036: result.setId(idVal.intValue());
037: result.setData("" + result.getId());
038: result.setChilds(new Vector());
039: result.setMembers(new Vector());
040: return result;
041: }
042:
043: /**
044: * Insert the method's description here.
045: * Creation date: (13.02.2001 18:50:29)
046: * @return TestThreadsNLocks.org.apache.ojb.broker.Tree
047: * @param parent TestThreadsNLocks.org.apache.ojb.broker.Tree
048: */
049: public Tree createTreeNodeWithParent(Tree parent) throws Exception {
050:
051: Tree result = new Tree();
052: try {
053: FieldDescriptor idFld = broker.getClassDescriptor(
054: Tree.class).getFieldDescriptorByName("id");
055: Integer idVal = (Integer) broker.serviceSequenceManager()
056: .getUniqueValue(idFld);
057:
058: result.setId(idVal.intValue());
059: } catch (PersistenceBrokerException e)
060:
061: {
062: }
063: result.setData(parent.getId() + "-" + result.getId());
064: result.setParentId(parent.getId());
065: result.setChilds(new Vector());
066:
067: return result;
068: }
069:
070: /**
071: */
072: public void testCreate() {
073: try {
074:
075: Tree root = new Tree();
076: try {
077: FieldDescriptor idFld = broker.getClassDescriptor(
078: Tree.class).getFieldDescriptorByName("id");
079: Integer idVal = (Integer) broker
080: .serviceSequenceManager().getUniqueValue(idFld);
081:
082: root.setId(idVal.intValue());
083: } catch (PersistenceBrokerException e)
084:
085: {
086: }
087: root.setData("a brand new root: " + root.getId());
088:
089: root.addChild(createTreeNodeWithParent(root));
090: root.addChild(createTreeNodeWithParent(root));
091: root.addChild(createTreeNodeWithParent(root));
092: root.addChild(createTreeNodeWithParent(root));
093: root.addChild(createTreeNodeWithParent(root));
094:
095: Tree child = root.getChild(0);
096: child.addChild(createTreeNodeWithParent(child));
097: child.addChild(createTreeNodeWithParent(child));
098: child.addChild(createTreeNodeWithParent(child));
099:
100: child = child.getChild(1);
101: child.addChild(createTreeNodeWithParent(child));
102: child.addChild(createTreeNodeWithParent(child));
103:
104: //System.out.println("original tree:");
105: //System.out.println(root);
106: broker.beginTransaction();
107: broker.store(root);
108: broker.commitTransaction();
109:
110: Identity oid = new Identity(root, broker);
111:
112: broker.clearCache();
113:
114: Tree retrieved = (Tree) broker.getObjectByIdentity(oid);
115:
116: //System.out.println("tree after reading from db:");
117: //System.out.println(retrieved);
118:
119: assertEquals("tree should have same size after retrival",
120: root.size(), retrieved.size());
121:
122: } catch (Throwable t) {
123: fail(t.getMessage());
124: }
125: }
126:
127: /**
128: *
129: */
130: public void testTreeGroup() throws Exception {
131: TreeGroup root = createTreeGroup();
132: root.setData("The Tree Group root: " + root.getId());
133: TreeGroup green = createTreeGroup();
134: green.setData("the GREEN group " + green.getId());
135: TreeGroup red = createTreeGroup();
136: red.setData("the RED group " + red.getId());
137:
138: TreeGroup child;
139: for (int i = 0; i < 3; i++) {
140: child = createTreeGroup();
141: root.addChild(child);
142: green.addMember(child);
143: child = createTreeGroup();
144: root.addChild(child);
145: red.addMember(child);
146: }
147:
148: for (int i = 0; i < 6; i++) {
149: child = root.getChild(i);
150: child.addChild(createTreeGroup());
151: }
152:
153: //System.out.println("original TreeGroup:");
154: //System.out.println(root);
155: //System.out.println("GREEN TreeGroup:");
156: //System.out.println(green);
157: //System.out.println("RED TreeGroup:");
158: //System.out.println(red);
159: broker.beginTransaction();
160: broker.store(root);
161: broker.store(green);
162: broker.store(red);
163: broker.commitTransaction();
164:
165: Identity oid = new Identity(root, broker);
166:
167: broker.clearCache();
168:
169: TreeGroup root_r = (TreeGroup) broker.getObjectByIdentity(oid);
170: //System.out.println("tree after reading from db:");
171: //System.out.println(root_r);
172: assertNotNull(root_r);
173: assertEquals("tree should have same size after retrival", root
174: .size(), root_r.size());
175:
176: oid = new Identity(green, broker);
177: TreeGroup green_r = (TreeGroup) broker.getObjectByIdentity(oid);
178: //System.out.println("tree after reading from db:");
179: //System.out.println(green_r);
180: assertEquals("tree should have same size after retrival", green
181: .size(), green_r.size());
182:
183: oid = new Identity(red, broker);
184: TreeGroup red_r = (TreeGroup) broker.getObjectByIdentity(oid);
185: //System.out.println("tree after reading from db:");
186: //System.out.println(red_r);
187: assertEquals("tree should have same size after retrival", red
188: .size(), red_r.size());
189:
190: }
191:
192: //*****************************************************************
193: // inner classes - test objects
194: //*****************************************************************
195:
196: /**
197: * Tree is recursive type: a Tree element contains some data
198: * and a Vector of child Tree elements.
199: * This sample demonstrates what is needed to map such a data
200: * structure on a DB table
201: * @author Thomas Mahler
202: */
203: public static class Tree implements java.io.Serializable {
204: private int id;
205: private String data;
206: private int parentId;
207: private Vector childs;
208:
209: /**
210: * Tree constructor comment.
211: */
212: public Tree() {
213: super ();
214: }
215:
216: /**
217: * Tree constructor comment.
218: */
219: public Tree(int id, String data, int parentid) {
220: this .id = id;
221: this .data = data;
222: this .parentId = parentid;
223: }
224:
225: public void addChild(Tree newChild) {
226: if (childs == null)
227: childs = new Vector();
228:
229: childs.add(newChild);
230:
231: }
232:
233: public Tree getChild(int index) {
234: return (Tree) childs.get(index);
235:
236: }
237:
238: /**
239: * Insert the method's description here.
240: * Creation date: (13.02.2001 18:33:02)
241: * @return java.util.Vector
242: */
243: public java.util.Vector getChilds() {
244: return childs;
245: }
246:
247: /**
248: * Insert the method's description here.
249: * Creation date: (13.02.2001 18:33:02)
250: * @return java.lang.String
251: */
252: public java.lang.String getData() {
253: return data;
254: }
255:
256: /**
257: * Insert the method's description here.
258: * Creation date: (13.02.2001 18:33:02)
259: * @return int
260: */
261: public int getId() {
262: return id;
263: }
264:
265: /**
266: * Insert the method's description here.
267: * Creation date: (13.02.2001 18:33:02)
268: * @return int
269: */
270: public int getParentId() {
271: return parentId;
272: }
273:
274: /**
275: * Insert the method's description here.
276: * Creation date: (13.02.2001 18:33:02)
277: * @param newChilds java.util.Vector
278: */
279: public void setChilds(java.util.Vector newChilds) {
280: childs = newChilds;
281: }
282:
283: /**
284: * Insert the method's description here.
285: * Creation date: (13.02.2001 18:33:02)
286: * @param newData java.lang.String
287: */
288: public void setData(java.lang.String newData) {
289: data = newData;
290: }
291:
292: /**
293: * Insert the method's description here.
294: * Creation date: (13.02.2001 18:33:02)
295: * @param newId int
296: */
297: public void setId(int newId) {
298: id = newId;
299: }
300:
301: /**
302: * Insert the method's description here.
303: * Creation date: (13.02.2001 18:33:02)
304: * @param newParentId int
305: */
306: public void setParentId(int newParentId) {
307: parentId = newParentId;
308: }
309:
310: /**
311: * Insert the method's description here.
312: * Creation date: (14.02.2001 19:51:23)
313: * @return int
314: */
315: public int size() {
316: int result = 1;
317: for (int i = 0; i < childs.size(); i++) {
318: result += ((Tree) childs.get(i)).size();
319: }
320: return result;
321: }
322:
323: /**
324: * Insert the method's description here.
325: * Creation date: (13.02.2001 18:33:41)
326: * @return java.lang.String
327: */
328: public String toString() {
329:
330: return data + ((childs == null) ? "" : childs.toString());
331: }
332: }
333:
334: /**
335: * Tree is recursive type: a Tree element contains some data
336: * and a Vector of child Tree elements.
337: * This sample demonstrates what is needed to map such a data
338: * structure on a DB table
339: * @author Thomas Mahler
340: */
341: public static class TreeGroup implements java.io.Serializable {
342: private int id;
343: private String data;
344:
345: private int parentId;
346: private Vector children;
347: private TreeGroup myParent;
348:
349: private int groupId;
350: private Vector groupMembers;
351: private TreeGroup myGroup;
352:
353: /**
354: * Tree constructor comment.
355: */
356: public TreeGroup() {
357: super ();
358: }
359:
360: /**
361: * Tree constructor comment.
362: */
363: public TreeGroup(int id, String data, int parentid, int groupid) {
364: this .id = id;
365: this .data = data;
366: this .parentId = parentid;
367: this .groupId = groupid;
368: }
369:
370: public void addChild(TreeGroup newChild) {
371: if (children == null)
372: children = new Vector();
373:
374: children.add(newChild);
375: newChild.setParentId(this .getId());
376:
377: }
378:
379: public void addMember(TreeGroup newMember) {
380: if (groupMembers == null)
381: groupMembers = new Vector();
382:
383: groupMembers.add(newMember);
384: newMember.setGroupId(this .getId());
385:
386: }
387:
388: public TreeGroup getChild(int index) {
389: return (TreeGroup) children.get(index);
390:
391: }
392:
393: /**
394: * Insert the method's description here.
395: * Creation date: (13.02.2001 18:33:02)
396: * @return java.util.Vector
397: */
398: public Vector getChilds() {
399: return children;
400: }
401:
402: /**
403: * Insert the method's description here.
404: * Creation date: (13.02.2001 18:33:02)
405: * @return java.lang.String
406: */
407: public String getData() {
408: return data;
409: }
410:
411: /**
412: * Insert the method's description here.
413: * Creation date: (13.02.2001 18:33:02)
414: * @return int
415: */
416: public int getGroupId() {
417: return groupId;
418: }
419:
420: /**
421: * Insert the method's description here.
422: * Creation date: (13.02.2001 18:33:02)
423: * @return int
424: */
425: public int getId() {
426: return id;
427: }
428:
429: public TreeGroup getMember(int index) {
430: return (TreeGroup) groupMembers.get(index);
431:
432: }
433:
434: /**
435: * Insert the method's description here.
436: * Creation date: (13.02.2001 18:33:02)
437: * @return java.util.Vector
438: */
439: public Vector getMembers() {
440: return groupMembers;
441: }
442:
443: /**
444: * Insert the method's description here.
445: * Creation date: (13.02.2001 18:33:02)
446: * @return int
447: */
448: public int getParentId() {
449: return parentId;
450: }
451:
452: /**
453: * Insert the method's description here.
454: * Creation date: (13.02.2001 18:33:02)
455: * @param newChilds java.util.Vector
456: */
457: public void setChilds(java.util.Vector newChilds) {
458: children = newChilds;
459: }
460:
461: /**
462: * Sets the data.
463: * @param data The data to set
464: */
465: public void setData(String data) {
466: this .data = data;
467: }
468:
469: /**
470: * Sets the groupId.
471: * @param groupId The groupId to set
472: */
473: public void setGroupId(int groupId) {
474: this .groupId = groupId;
475: }
476:
477: /**
478: * Sets the id.
479: * @param id The id to set
480: */
481: public void setId(int id) {
482: this .id = id;
483: }
484:
485: /**
486: * Insert the method's description here.
487: * Creation date: (13.02.2001 18:33:02)
488: * @param newMembers java.util.Vector
489: */
490: public void setMembers(java.util.Vector newMembers) {
491: groupMembers = newMembers;
492: }
493:
494: /**
495: * Sets the parentId.
496: * @param parentId The parentId to set
497: */
498: public void setParentId(int parentId) {
499: this .parentId = parentId;
500: }
501:
502: /**
503: * Insert the method's description here.
504: * Creation date: (14.02.2001 19:51:23)
505: * @return int
506: */
507: public int size() {
508: int result = 1;
509: if (children == null)
510: return result;
511: for (int i = 0; i < children.size(); i++) {
512: result += ((TreeGroup) children.get(i)).size();
513: }
514: return result;
515: }
516:
517: /**
518: * Insert the method's description here.
519: * Creation date: (13.02.2001 18:33:41)
520: * @return java.lang.String
521: */
522: public String toString() {
523: return data
524: + ((children == null || (children.size() == 0)) ? ""
525: : " children: " + children.toString())
526: + ((groupMembers == null || (groupMembers.size() == 0)) ? ""
527: : " members: " + groupMembers.toString());
528: }
529:
530: /**
531: * Gets the children.
532: * @return Returns a Vector
533: */
534: public Vector getChildren() {
535: return children;
536: }
537:
538: /**
539: * Sets the children.
540: * @param children The children to set
541: */
542: public void setChildren(Vector children) {
543: this .children = children;
544: }
545:
546: /**
547: * Gets the groupMembers.
548: * @return Returns a Vector
549: */
550: public Vector getGroupMembers() {
551: return groupMembers;
552: }
553:
554: /**
555: * Sets the groupMembers.
556: * @param groupMembers The groupMembers to set
557: */
558: public void setGroupMembers(Vector groupMembers) {
559: this .groupMembers = groupMembers;
560: }
561:
562: /**
563: * Gets the myGroup.
564: * @return Returns a TreeGroup
565: */
566: public TreeGroup getMyGroup() {
567: return myGroup;
568: }
569:
570: /**
571: * Sets the myGroup.
572: * @param myGroup The myGroup to set
573: */
574: public void setMyGroup(TreeGroup myGroup) {
575: this .myGroup = myGroup;
576: }
577:
578: /**
579: * Gets the myParent.
580: * @return Returns a TreeGroup
581: */
582: public TreeGroup getMyParent() {
583: return myParent;
584: }
585:
586: /**
587: * Sets the myParent.
588: * @param myParent The myParent to set
589: */
590: public void setMyParent(TreeGroup myParent) {
591: this.myParent = myParent;
592: }
593:
594: }
595:
596: }
|