KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > impl > NodeImplTest


1 package org.sapia.regis.impl;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.sapia.regis.DuplicateNodeException;
8 import org.sapia.regis.Path;
9 import org.sapia.regis.Node;
10 import org.sapia.regis.Query;
11 import org.sapia.regis.RWNode;
12 import org.sapia.regis.impl.NodeImpl;
13
14 import junit.framework.TestCase;
15
16 public class NodeImplTest extends TestCase {
17   
18   public void testGetAbsolutePath() throws Exception JavaDoc{
19     NodeImpl parent = new NodeImpl(null, "");
20     RWNode child1 = (RWNode)parent.createChild("child1");
21     RWNode child2 = (RWNode)child1.createChild("child2");
22     
23     Path path1 = child1.getAbsolutePath();
24     super.assertEquals(1, path1.tokenCount());
25     super.assertEquals("child1", path1.tokens().next());
26     
27     Path path2 = child2.getAbsolutePath();
28     super.assertEquals(2, path2.tokenCount());
29     Iterator JavaDoc tokens = path2.tokens();
30     super.assertEquals("child1", tokens.next());
31     super.assertEquals("child2", tokens.next());
32     
33   }
34   public void testRoot() throws Exception JavaDoc{
35     NodeImpl root = new NodeImpl(null, "");
36     super.assertTrue(root.isRoot());
37   }
38
39   public void testCreateGetChild() throws Exception JavaDoc{
40     NodeImpl parent = new NodeImpl(null, "");
41     Node child = parent.createChild("child");
42     super.assertEquals(parent, child.getParent());
43     try {
44       parent.createChild("child");
45       fail("duplicate node created");
46     } catch (DuplicateNodeException e) {
47       //ok
48
}
49     child = parent.getChild("child");
50     super.assertEquals("child", child.getName());
51     
52   }
53
54   public void testDeleteChild() throws Exception JavaDoc{
55     NodeImpl parent = new NodeImpl(null, "");
56     parent.createChild("child");
57     parent.deleteChild("child");
58     super.assertTrue(parent.getChild("child") == null);
59   }
60
61   public void testGetChildren() throws Exception JavaDoc{
62     NodeImpl parent = new NodeImpl(null, "");
63     parent.createChild("child1");
64     parent.createChild("child2");
65     super.assertEquals(2, parent.getChildren().size());
66   }
67   
68   public void testInclude() throws Exception JavaDoc{
69     NodeImpl parent = new NodeImpl(null, "");
70     Node child1 = parent.createChild("child1");
71     Node child2 = parent.createChild("child2");
72     RWNode child3 = (RWNode)parent.createChild("child3");
73     child3.addInclude(child1);
74     child3.addInclude(child2);
75     super.assertEquals(2, child3.getChildren().size());
76     Collection JavaDoc children = parent.getChildren();
77     super.assertTrue(children.contains(child1));
78     super.assertTrue(children.contains(child2));
79     
80     assertEquals(child1, child3.getChild("child1"));
81     assertEquals(child2, child3.getChild("child2"));
82   }
83   
84   public void testInheritance() throws Exception JavaDoc{
85     NodeImpl parent = new NodeImpl(null, "");
86     parent.setProperty("prop1", "value1");
87     
88     RWNode child = (RWNode)parent.createChild("child1");
89     child.setProperty("prop2", "value2");
90     super.assertTrue(child.getProperty("prop1").isNull());
91     child.setInheritsParent(true);
92     super.assertEquals("value1", child.getProperty("prop1").getValue());
93   }
94   
95   public void testLink() throws Exception JavaDoc{
96     NodeImpl parent = new NodeImpl(null, "");
97     
98     parent.setProperty("var", "${prop1},${prop2}");
99     parent.setProperty("prop1", "value3");
100     RWNode link1 = new NodeImpl(null, "link1");
101     link1.setProperty("prop1", "value1");
102     RWNode link2 = new NodeImpl(null, "link1");
103     link2.setProperty("prop2", "value2");
104     
105     parent.prependLink(link1);
106     parent.appendLink(link2);
107     super.assertEquals("value1", parent.getProperty("prop1").getValue());
108     super.assertEquals("value2", parent.getProperty("prop2").getValue());
109     super.assertEquals("value1,value2", parent.renderProperty("var").getValue());
110   }
111   
112   public void testInheritedLastModifChecksum() throws Exception JavaDoc{
113     NodeImpl parent = new NodeImpl(null, "");
114     NodeImpl child1 = (NodeImpl)parent.createChild("child1");
115     child1.setInheritsParent(true);
116     NodeImpl child2 = (NodeImpl)child1.createChild("child2");
117     child2.setInheritsParent(true);
118     
119     long chk = parent.lastModifChecksum();
120     parent.setVersion(parent.getVersion()+1);
121     super.assertTrue(chk != parent.lastModifChecksum());
122     
123     chk = child1.lastModifChecksum();
124     super.assertEquals(chk, child1.lastModifChecksum());
125     child1.setVersion(child1.getVersion()+1);
126     super.assertTrue(chk != child1.lastModifChecksum());
127     chk = child1.lastModifChecksum();
128     super.assertEquals(chk, child1.lastModifChecksum());
129     parent.setVersion(parent.getVersion()+1);
130     super.assertTrue(chk != child1.lastModifChecksum());
131     
132     chk = child2.lastModifChecksum();
133     super.assertEquals(chk, child2.lastModifChecksum());
134     child2.setVersion(child2.getVersion()+1);
135     super.assertTrue(chk != child2.lastModifChecksum());
136     chk = child2.lastModifChecksum();
137     super.assertEquals(chk, child2.lastModifChecksum());
138     child1.setVersion(child1.getVersion()+1);
139     super.assertTrue(chk != child2.lastModifChecksum());
140     chk = child2.lastModifChecksum();
141     super.assertEquals(chk, child2.lastModifChecksum());
142     parent.setVersion(parent.getVersion()+1);
143     super.assertTrue(chk != child2.lastModifChecksum());
144   }
145   
146   public void testLinkedLastModifChecksum() throws Exception JavaDoc{
147     NodeImpl parent = new NodeImpl(null, "");
148     NodeImpl link1 = new NodeImpl(null, "");
149     NodeImpl link2 = new NodeImpl(null, "");
150     parent.prependLink(link1);
151     parent.appendLink(link2);
152     
153     long chk = parent.lastModifChecksum();
154     link1.setVersion(link1.getVersion()+1);
155     super.assertTrue(chk != parent.lastModifChecksum());
156     
157     chk = parent.lastModifChecksum();
158     link2.setVersion(link2.getVersion()+1);
159     super.assertTrue(chk != parent.lastModifChecksum());
160   }
161   
162   public void testGetProperties(){
163     NodeImpl parent = new NodeImpl(null, "");
164     NodeImpl child1 = new NodeImpl(parent, "child1");
165     NodeImpl child2 = new NodeImpl(child1, "child2");
166     NodeImpl link1 = new NodeImpl(null, "");
167     NodeImpl link2 = new NodeImpl(null, "");
168
169     child1.setInheritsParent(true);
170     child2.setInheritsParent(true);
171     child2.prependLink(link1);
172     child2.appendLink(link2);
173
174     parent.setProperty("parent", "parentProp");
175     child1.setProperty("child1", "${parent}");
176     child1.setProperty("override", "child1");
177     link1.setProperty("link1", "link1Prop");
178     link2.setProperty("link2", "${append}");
179     child2.setProperty("child2", "${link1} ${child1}");
180     child2.setProperty("override", "child2");
181     child2.setProperty("append", "appendProp");
182     child2.setProperty("this", "${child2}");
183     
184     Map JavaDoc props = child2.getProperties();
185     assertEquals("link1Prop parentProp", props.get("child2"));
186     assertEquals("child2", props.get("override"));
187     assertEquals("link1Prop parentProp", props.get("this"));
188     assertEquals("appendProp", props.get("link2"));
189     
190   }
191   
192   public void testGetNodesForQuery() throws Exception JavaDoc{
193     NodeImpl parent = new NodeImpl(null, "");
194     NodeImpl child1 = (NodeImpl)parent.createChild("child1");
195     NodeImpl subChild = (NodeImpl)child1.createChild("child1_1");
196     subChild.setProperty("prop1", "value1");
197     subChild = (NodeImpl)child1.createChild("child1_2");
198     subChild.setProperty("prop1", "value1");
199     subChild.setProperty("prop2", "value2");
200     subChild = (NodeImpl)child1.createChild("child1_3");
201     subChild.setProperty("prop1", "value1");
202     subChild.setProperty("prop2", "value2");
203     subChild.setProperty("prop3", "value3");
204
205     Collection JavaDoc nodes = parent.getNodes(Query.create(""));
206     Node node = (Node)nodes.iterator().next();
207     assertEquals("child1", node.getName());
208     assertEquals(1, nodes.size());
209     
210     nodes = parent.getNodes(Query.create("child1"));
211     assertEquals(3, nodes.size());
212     
213     nodes = parent.getNodes(Query.create("child1").addCrit("prop1", "value1"));
214     assertEquals(3, nodes.size());
215     
216     nodes = parent.getNodes(Query.create("child1")
217         .addCrit("prop1", "value1").addCrit("prop2", "value2"));
218     assertEquals(2, nodes.size());
219     
220     nodes = parent.getNodes(Query.create("child1")
221         .addCrit("prop3", "value3"));
222     assertEquals(1, nodes.size());
223   }
224
225 }
226
Popular Tags