KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > api > NodeSPITest


1 package org.jboss.cache.api;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheSPI;
5 import org.jboss.cache.Fqn;
6 import org.jboss.cache.NodeSPI;
7 import org.jboss.cache.factories.DefaultCacheFactory;
8
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 /**
13  * Tests NodeSPI specific APIs.
14  */

15 public class NodeSPITest extends TestCase
16 {
17    private CacheSPI cache;
18    private NodeSPI root;
19
20    protected void setUp()
21    {
22       cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache();
23       root = cache.getRoot();
24    }
25
26    protected void tearDown()
27    {
28       if (cache != null) cache.stop();
29       root = null;
30       cache = null;
31    }
32
33    public void testDeepOperations() throws Exception JavaDoc
34    {
35       Fqn A = Fqn.fromString("/a");
36       Fqn B = Fqn.fromString("/b");
37       Fqn A_B = Fqn.fromString("/a/b");
38
39       NodeSPI nodeA, nodeB;
40
41       cache.put(A, "k", "v");
42       cache.put(A_B, "k", "v");
43
44       nodeA = cache.getRoot().getChildDirect(A);// should work
45
nodeB = cache.getRoot().getChildDirect(A_B);// should work
46

47       assertEquals(A_B, nodeB.getFqn());
48
49       nodeB = nodeA.getChildDirect(B);// should work
50
assertEquals(A_B, nodeB.getFqn());
51       cache.getRoot().removeChildDirect(A_B);// should work
52

53       nodeA.removeChildDirect(B);// should work
54
cache.getRoot().removeChildDirect(A);// should work
55

56       try
57       {
58          cache.getRoot().addChildDirect(A_B);// should fail
59
fail("Should have failed");
60       }
61       catch (UnsupportedOperationException JavaDoc e)
62       {
63          // expected
64
}
65       nodeA = cache.getRoot().addChildDirect(A);// should work
66
nodeA.addChildDirect(B);// should work
67
}
68
69    public void testDataImmutabilityAndDefensiveCopy()
70    {
71       // put some stuff in the root node
72
root.put("k", "v");
73       Map JavaDoc dataDirect = root.getDataDirect();
74       Set JavaDoc keysDirect = root.getKeysDirect();
75
76       try
77       {
78          dataDirect.remove("k");
79          fail("getDataDirect() should return an unmodifiable collection object");
80       }
81       catch (UnsupportedOperationException JavaDoc uoe)
82       {
83          // good; should be immutable
84
}
85
86       try
87       {
88          keysDirect.clear();
89          fail("getKeysDirect() should return an unmodifiable collection object");
90       }
91       catch (UnsupportedOperationException JavaDoc uoe)
92       {
93          // good; should be immutable
94
}
95
96       // now test defensive copy
97
root.put("k2", "v2");
98
99       assertTrue("root.put() should have succeeded", root.getDataDirect().containsKey("k2"));
100       assertTrue("getDataDirect() should have made a defensive copy of the data collection object", !dataDirect.containsKey("k2"));
101       assertTrue("getKeysDirect() should have made a defensive copy of the data collection object", !keysDirect.contains("k2"));
102    }
103
104    public void testChildrenImmutabilityAndDefensiveCopy()
105    {
106       // put some stuff in the root node
107
Object JavaDoc childName = "childName";
108       Object JavaDoc newChild = "newChild";
109       root.addChild(new Fqn(childName));
110       Map JavaDoc childrenMapDirect = root.getChildrenMapDirect();
111       Set JavaDoc childrenDirect = root.getChildrenDirect();
112       Set JavaDoc childrenNamesDirect = root.getChildrenNamesDirect();
113
114       try
115       {
116          childrenMapDirect.clear();
117          fail("getChildrenMapDirect() should return an unmodifiable collection object");
118       }
119       catch (UnsupportedOperationException JavaDoc uoe)
120       {
121          // good; should be immutable
122
}
123
124       try
125       {
126          childrenDirect.clear();
127          fail("getChildrenDirect() should return an unmodifiable collection object");
128       }
129       catch (UnsupportedOperationException JavaDoc uoe)
130       {
131          // good; should be immutable
132
}
133
134       try
135       {
136          childrenNamesDirect.clear();
137          fail("getChildrenNamesDirect() should return an unmodifiable collection object");
138       }
139       catch (UnsupportedOperationException JavaDoc uoe)
140       {
141          // good; should be immutable
142
}
143
144       // now test defensive copy
145
root.addChild(new Fqn(newChild));
146
147       assertTrue("root.addChild() should have succeeded", root.getChildrenNamesDirect().contains(newChild));
148       assertTrue("getChildrenMapDirect() should have made a defensive copy of the data collection object", !childrenMapDirect.containsKey(newChild));
149       assertTrue("getChildrenDirect() should have made a defensive copy of the data collection object", !childrenDirect.contains(newChild));
150       assertTrue("getChildrenNamesDirect() should have made a defensive copy of the data collection object", !childrenNamesDirect.contains(newChild));
151
152    }
153
154    public void testNullCollections()
155    {
156       // nothing in root, make sure we see no nulls.
157
assertNotNull("Should not be null", root.getDataDirect());
158       assertTrue("Should be empty", root.getDataDirect().isEmpty());
159
160       assertNotNull("Should not be null", root.getKeysDirect());
161       assertTrue("Should be empty", root.getKeysDirect().isEmpty());
162
163       assertNotNull("Should not be null", root.getChildrenMapDirect());
164       assertTrue("Should be empty", root.getChildrenMapDirect().isEmpty());
165
166       assertNotNull("Should not be null", root.getChildrenDirect());
167       assertTrue("Should be empty", root.getChildrenDirect().isEmpty());
168
169       assertNotNull("Should not be null", root.getChildrenNamesDirect());
170       assertTrue("Should be empty", root.getChildrenNamesDirect().isEmpty());
171    }
172
173
174 }
175
Popular Tags