1 |
| package org.jboss.cache.api; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheSPI; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.NodeSPI; |
8 |
| |
9 |
| import java.util.Map; |
10 |
| import java.util.Set; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| public class NodeSPITest extends TestCase |
16 |
| { |
17 |
| private CacheSPI cache; |
18 |
| private NodeSPI root; |
19 |
| |
20 |
4
| protected void setUp()
|
21 |
| { |
22 |
4
| cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache();
|
23 |
4
| root = cache.getRoot();
|
24 |
| } |
25 |
| |
26 |
4
| protected void tearDown()
|
27 |
| { |
28 |
4
| if (cache != null) cache.stop();
|
29 |
4
| root = null;
|
30 |
4
| cache = null;
|
31 |
| } |
32 |
| |
33 |
1
| public void testDeepOperations() throws Exception
|
34 |
| { |
35 |
1
| Fqn A = Fqn.fromString("/a");
|
36 |
1
| Fqn B = Fqn.fromString("/b");
|
37 |
1
| Fqn A_B = Fqn.fromString("/a/b");
|
38 |
| |
39 |
1
| NodeSPI nodeA, nodeB;
|
40 |
| |
41 |
1
| cache.put(A, "k", "v");
|
42 |
1
| cache.put(A_B, "k", "v");
|
43 |
| |
44 |
1
| nodeA = cache.getRoot().getChildDirect(A);
|
45 |
1
| nodeB = cache.getRoot().getChildDirect(A_B);
|
46 |
| |
47 |
1
| assertEquals(A_B, nodeB.getFqn());
|
48 |
| |
49 |
1
| nodeB = nodeA.getChildDirect(B);
|
50 |
1
| assertEquals(A_B, nodeB.getFqn());
|
51 |
1
| assertEquals(true, cache.getRoot().removeChildDirect(A_B));
|
52 |
1
| assertEquals(false, cache.getRoot().removeChildDirect(A_B));
|
53 |
| |
54 |
1
| cache.put(A_B, "k", "v");
|
55 |
1
| assertEquals(true, nodeA.removeChildDirect(B));
|
56 |
1
| assertEquals(false, nodeA.removeChildDirect(B));
|
57 |
1
| assertEquals(true, cache.getRoot().removeChildDirect(A.getLastElement()));
|
58 |
1
| assertEquals(false, cache.getRoot().removeChildDirect(A.getLastElement()));
|
59 |
| |
60 |
1
| try
|
61 |
| { |
62 |
1
| cache.getRoot().addChildDirect(A_B);
|
63 |
0
| fail("Should have failed");
|
64 |
| } |
65 |
| catch (UnsupportedOperationException e) |
66 |
| { |
67 |
| |
68 |
| } |
69 |
1
| nodeA = cache.getRoot().addChildDirect(A);
|
70 |
1
| nodeA.addChildDirect(B);
|
71 |
| } |
72 |
| |
73 |
1
| public void testDataImmutabilityAndDefensiveCopy()
|
74 |
| { |
75 |
| |
76 |
1
| root.put("k", "v");
|
77 |
1
| Map dataDirect = root.getDataDirect();
|
78 |
1
| Set keysDirect = root.getKeysDirect();
|
79 |
| |
80 |
1
| try
|
81 |
| { |
82 |
1
| dataDirect.remove("k");
|
83 |
0
| fail("getDataDirect() should return an unmodifiable collection object");
|
84 |
| } |
85 |
| catch (UnsupportedOperationException uoe) |
86 |
| { |
87 |
| |
88 |
| } |
89 |
| |
90 |
1
| try
|
91 |
| { |
92 |
1
| keysDirect.clear();
|
93 |
0
| fail("getKeysDirect() should return an unmodifiable collection object");
|
94 |
| } |
95 |
| catch (UnsupportedOperationException uoe) |
96 |
| { |
97 |
| |
98 |
| } |
99 |
| |
100 |
| |
101 |
1
| root.put("k2", "v2");
|
102 |
| |
103 |
1
| assertTrue("root.put() should have succeeded", root.getDataDirect().containsKey("k2"));
|
104 |
1
| assertTrue("getDataDirect() should have made a defensive copy of the data collection object", !dataDirect.containsKey("k2"));
|
105 |
1
| assertTrue("getKeysDirect() should have made a defensive copy of the data collection object", !keysDirect.contains("k2"));
|
106 |
| } |
107 |
| |
108 |
1
| public void testChildrenImmutabilityAndDefensiveCopy()
|
109 |
| { |
110 |
| |
111 |
1
| Object childName = "childName";
|
112 |
1
| Object newChild = "newChild";
|
113 |
1
| root.addChild(new Fqn(childName));
|
114 |
1
| Map childrenMapDirect = root.getChildrenMapDirect();
|
115 |
1
| Set childrenDirect = root.getChildrenDirect();
|
116 |
1
| Set childrenNamesDirect = root.getChildrenNamesDirect();
|
117 |
| |
118 |
1
| try
|
119 |
| { |
120 |
1
| childrenMapDirect.clear();
|
121 |
0
| fail("getChildrenMapDirect() should return an unmodifiable collection object");
|
122 |
| } |
123 |
| catch (UnsupportedOperationException uoe) |
124 |
| { |
125 |
| |
126 |
| } |
127 |
| |
128 |
1
| try
|
129 |
| { |
130 |
1
| childrenDirect.clear();
|
131 |
0
| fail("getChildrenDirect() should return an unmodifiable collection object");
|
132 |
| } |
133 |
| catch (UnsupportedOperationException uoe) |
134 |
| { |
135 |
| |
136 |
| } |
137 |
| |
138 |
1
| try
|
139 |
| { |
140 |
1
| childrenNamesDirect.clear();
|
141 |
0
| fail("getChildrenNamesDirect() should return an unmodifiable collection object");
|
142 |
| } |
143 |
| catch (UnsupportedOperationException uoe) |
144 |
| { |
145 |
| |
146 |
| } |
147 |
| |
148 |
| |
149 |
1
| root.addChild(new Fqn(newChild));
|
150 |
| |
151 |
1
| assertTrue("root.addChild() should have succeeded", root.getChildrenNamesDirect().contains(newChild));
|
152 |
1
| assertTrue("getChildrenMapDirect() should have made a defensive copy of the data collection object", !childrenMapDirect.containsKey(newChild));
|
153 |
1
| assertTrue("getChildrenDirect() should have made a defensive copy of the data collection object", !childrenDirect.contains(newChild));
|
154 |
1
| assertTrue("getChildrenNamesDirect() should have made a defensive copy of the data collection object", !childrenNamesDirect.contains(newChild));
|
155 |
| |
156 |
| } |
157 |
| |
158 |
1
| public void testNullCollections()
|
159 |
| { |
160 |
| |
161 |
1
| assertNotNull("Should not be null", root.getDataDirect());
|
162 |
1
| assertTrue("Should be empty", root.getDataDirect().isEmpty());
|
163 |
| |
164 |
1
| assertNotNull("Should not be null", root.getKeysDirect());
|
165 |
1
| assertTrue("Should be empty", root.getKeysDirect().isEmpty());
|
166 |
| |
167 |
1
| assertNotNull("Should not be null", root.getChildrenMapDirect());
|
168 |
1
| assertTrue("Should be empty", root.getChildrenMapDirect().isEmpty());
|
169 |
| |
170 |
1
| assertNotNull("Should not be null", root.getChildrenDirect());
|
171 |
1
| assertTrue("Should be empty", root.getChildrenDirect().isEmpty());
|
172 |
| |
173 |
1
| assertNotNull("Should not be null", root.getChildrenNamesDirect());
|
174 |
1
| assertTrue("Should be empty", root.getChildrenNamesDirect().isEmpty());
|
175 |
| } |
176 |
| |
177 |
| |
178 |
| } |