KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > RelationTest


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge;
11
12 import org.mmbase.tests.*;
13 /**
14  * Test cases to test the creation of relations and the retrieval of them
15  * @author Kees Jongenburger
16  * @version $Id: RelationTest.java,v 1.7 2006/07/20 17:22:36 michiel Exp $
17  */

18 public class RelationTest extends BridgeTest {
19
20     public RelationTest(String JavaDoc name) {
21         super(name);
22     }
23
24     public void testCreateRelations() {
25         //first create 3 nodes
26
Cloud cloud = getCloud();
27
28         Node aaFirstNode = cloud.getNodeManager("aa").createNode();
29         aaFirstNode.setStringValue("stringfield", "aaFirstNode");
30         aaFirstNode.commit();
31
32         Node bbNode = cloud.getNodeManager("bb").createNode();
33         bbNode.setStringValue("stringfield", "bbNode");
34         bbNode.commit();
35
36         Node aaSecondNode = cloud.getNodeManager("aa").createNode();
37         aaSecondNode.setStringValue("stringfield", "aaSecondNode");
38         aaSecondNode.commit();
39
40         RelationManager relatedRelationManager = cloud.getRelationManager("related");
41
42         //create a relation from aaFirstNode to bbNode
43
Relation related1 = aaFirstNode.createRelation(bbNode, relatedRelationManager);
44         related1.commit();
45
46         //create a relation from bbNode to aaSecondNode
47
Relation related2 = bbNode.createRelation(aaSecondNode, relatedRelationManager);
48         related2.commit();
49
50         //we now have 3 node
51
//in the BridgeTest application the folowing is defined
52
//<relation from="aa" to="bb" type="related" />
53
//<relation from="bb" to="aa" type="related" />
54

55         //and we have created the folowing structure
56
//aaFirstNode -> related1 -> bbNode -> related2 -> aaSecondNode
57

58         //check back if we can get the nodes
59
{
60             //ask for the relation in the "right" direction
61
NodeList aaRelatedList = aaFirstNode.getRelatedNodes("bb", "related", "destination");
62             assertTrue("related list has size " + aaRelatedList.size() + " but should be 1", aaRelatedList.size() == 1);
63             if (aaRelatedList.size() > 0) {
64                 Node theOtherNode = aaRelatedList.getNode(0);
65                 assertTrue("expected the bb node but got " + theOtherNode, theOtherNode.getNumber() == bbNode.getNumber());
66             }
67         }
68
69         {
70             //folow the relation in wrong direction
71
NodeList aaRelatedList = aaFirstNode.getRelatedNodes("bb", "related", "source");
72             assertTrue("related list has size " + aaRelatedList.size() + " but should be 0", aaRelatedList.size() == 0);
73             RelationList aaRelationList = aaFirstNode.getRelations("related", cloud.getNodeManager("bb"), "source");
74             assertTrue("relation list has size " + aaRelationList.size() + " but should be 0", aaRelationList.size() == 0);
75         }
76
77         {
78             //try to get the first aa node from the bb node
79
NodeList bbRelatedListSource = bbNode.getRelatedNodes("aa", "related", "source");
80             assertTrue("relation count should be 1 but is " + bbRelatedListSource.size() + " mmbase relations dont' work", bbRelatedListSource.size() == 1);
81             if (bbRelatedListSource.size() > 0) {
82                 Node sourceNode = bbRelatedListSource.getNode(0);
83                 assertTrue("expected the first aa node but got " + sourceNode, sourceNode.getNumber() == aaFirstNode.getNumber());
84             }
85             RelationList bbRelationListSource = bbNode.getRelations("related", cloud.getNodeManager("aa"), "source");
86             assertTrue("relation list has size " + bbRelationListSource.size() + " but should be 1", bbRelationListSource.size() == 1);
87
88         }
89
90         {
91             //try to get the second aa node from the bb node
92
NodeList bbRelatedListDestination = bbNode.getRelatedNodes("aa", "related", "destination");
93             assertTrue("relation count should be 1 but is " + bbRelatedListDestination.size() + " mmbase relations dont' work", bbRelatedListDestination.size() == 1);
94             if (bbRelatedListDestination.size() > 0) {
95                 Node sourceNode = bbRelatedListDestination.getNode(0);
96                 assertTrue("expected the first aa node but got " + sourceNode, sourceNode.getNumber() == aaSecondNode.getNumber());
97             }
98             RelationList bbRelationListDestination = bbNode.getRelations("related", cloud.getNodeManager("aa"), "destination");
99             assertTrue("relation count should be 1 but is " + bbRelationListDestination.size() + " mmbase relations dont' work", bbRelationListDestination.size() == 1);
100         }
101
102         {
103             //try to get both the aa nodes using "both"
104
NodeList bbRelatedListBoth = bbNode.getRelatedNodes("aa", "related", "both");
105             assertTrue("relation count should be 2 but is " + bbRelatedListBoth.size() + " mmbase relatednodes(BOTH) does not work", bbRelatedListBoth.size() == 2);
106             RelationList bbRelationListBoth = bbNode.getRelations("related", cloud.getNodeManager("aa"), "both");
107             assertTrue("relation count should be 2 but is " + bbRelationListBoth.size() + " mmbase relatednodes(BOTH) does not work", bbRelationListBoth.size() == 2);
108
109         }
110
111         {
112             //try to get both the aa node from bb using "null"
113
NodeList bbRelatedListNull = bbNode.getRelatedNodes("aa", "related", null);
114             assertTrue("relation count should be 2 but is " + bbRelatedListNull.size() + " mmbase relatednodes() does not work", bbRelatedListNull.size() == 2);
115             NodeList bbRelationListNull = bbNode.getRelations("related", cloud.getNodeManager("aa"), null);
116             assertTrue("relation count should be 2 but is " + bbRelationListNull.size() + " mmbase relatednodes() does not work", bbRelationListNull.size() == 2);
117
118         }
119         {
120             try {
121                 aaFirstNode.delete();
122                 fail("Should raise a BridgeException");
123             } catch (BridgeException e) {};
124
125             try {
126                 aaSecondNode.delete();
127                 fail("Should raise a BridgeException");
128             } catch (BridgeException e) {};
129             aaSecondNode.delete(true);
130             aaFirstNode.delete(true);
131             bbNode.delete();
132         }
133     }
134 }
135
Popular Tags