1 |
| package org.jboss.cache.optimistic; |
2 |
| |
3 |
| import org.jboss.cache.Cache; |
4 |
| import org.jboss.cache.CacheImpl; |
5 |
| import org.jboss.cache.CacheSPI; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.Node; |
8 |
| import org.jboss.cache.VersionedNode; |
9 |
| |
10 |
| import javax.transaction.TransactionManager; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| public class ParentVersionTest extends AbstractOptimisticTestCase |
17 |
| { |
18 |
| private Cache cache; |
19 |
| private TransactionManager tm; |
20 |
| |
21 |
| protected boolean lockParentForChildInsertRemove = false; |
22 |
| private Fqn parent = Fqn.fromString("/parent"); |
23 |
| private Fqn child1 = Fqn.fromString("/parent/child1"); |
24 |
| private Fqn child2 = Fqn.fromString("/parent/child2"); |
25 |
| private Fqn deepchild = Fqn.fromString("/parent/deep/child"); |
26 |
| |
27 |
14
| public ParentVersionTest(String name)
|
28 |
| { |
29 |
14
| super(name);
|
30 |
| } |
31 |
| |
32 |
14
| protected void setUp() throws Exception
|
33 |
| { |
34 |
14
| if (lockParentForChildInsertRemove)
|
35 |
| { |
36 |
7
| cache = createCacheUnstarted();
|
37 |
7
| cache.getConfiguration().setLockParentForChildInsertRemove(true);
|
38 |
7
| cache.start();
|
39 |
| } |
40 |
| else |
41 |
7
| cache = createCache();
|
42 |
| |
43 |
14
| tm = ((CacheSPI) cache).getTransactionManager();
|
44 |
| } |
45 |
| |
46 |
14
| protected void tearDown()
|
47 |
| { |
48 |
14
| destroyCache(cache);
|
49 |
| } |
50 |
| |
51 |
28
| private long getVersion(Node n)
|
52 |
| { |
53 |
28
| return ((DefaultDataVersion) ((VersionedNode) n).getVersion()).getRawVersion();
|
54 |
| } |
55 |
| |
56 |
2
| public void testSimpleAdd()
|
57 |
| { |
58 |
2
| cache.put(parent, "k", "v");
|
59 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
60 |
2
| cache.put(child1, "k", "v");
|
61 |
| |
62 |
| |
63 |
2
| System.out.println(((CacheImpl) cache).printLockInfo());
|
64 |
| |
65 |
2
| if (lockParentForChildInsertRemove)
|
66 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
67 |
| else |
68 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
69 |
| |
70 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
71 |
2
| assertTrue(cache.getRoot().hasChild(child1));
|
72 |
| } |
73 |
| |
74 |
2
| public void testSimpleRemove()
|
75 |
| { |
76 |
2
| cache.put(parent, "k", "v");
|
77 |
2
| cache.put(child1, "k", "v");
|
78 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
79 |
2
| assertTrue(cache.getRoot().hasChild(child1));
|
80 |
| |
81 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
82 |
| |
83 |
2
| cache.removeNode(child1);
|
84 |
| |
85 |
2
| if (lockParentForChildInsertRemove)
|
86 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
87 |
| else |
88 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
89 |
| |
90 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
91 |
2
| assertFalse("Should have removed child1", cache.getRoot().hasChild(child1));
|
92 |
| } |
93 |
| |
94 |
2
| public void testAddAndRemove() throws Exception
|
95 |
| { |
96 |
2
| cache.put(parent, "k", "v");
|
97 |
2
| cache.put(child1, "k", "v");
|
98 |
| |
99 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
100 |
2
| assertTrue(cache.getRoot().hasChild(child1));
|
101 |
2
| assertFalse(cache.getRoot().hasChild(child2));
|
102 |
| |
103 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
104 |
| |
105 |
2
| tm.begin();
|
106 |
2
| cache.put(child2, "k", "v");
|
107 |
2
| cache.removeNode(child1);
|
108 |
2
| tm.commit();
|
109 |
| |
110 |
2
| if (lockParentForChildInsertRemove)
|
111 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
112 |
| else |
113 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
114 |
| |
115 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
116 |
2
| assertFalse("Should have removed child1", cache.getRoot().hasChild(child1));
|
117 |
2
| assertTrue(cache.getRoot().hasChild(child2));
|
118 |
| } |
119 |
| |
120 |
2
| public void testAddAndRemoveOverlap() throws Exception
|
121 |
| { |
122 |
2
| cache.put(parent, "k", "v");
|
123 |
2
| cache.put(child1, "k", "v");
|
124 |
| |
125 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
126 |
2
| assertTrue(cache.getRoot().hasChild(child1));
|
127 |
2
| assertFalse(cache.getRoot().hasChild(child2));
|
128 |
| |
129 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
130 |
| |
131 |
2
| tm.begin();
|
132 |
2
| cache.put(child2, "k", "v");
|
133 |
2
| cache.removeNode(child1);
|
134 |
2
| cache.removeNode(child2);
|
135 |
2
| cache.removeNode(child1);
|
136 |
2
| cache.put(child1, "k", "v");
|
137 |
2
| cache.removeNode(child1);
|
138 |
2
| cache.removeNode(child2);
|
139 |
2
| cache.put(child2, "k", "v");
|
140 |
2
| tm.commit();
|
141 |
| |
142 |
2
| if (lockParentForChildInsertRemove)
|
143 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
144 |
| else |
145 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
146 |
| |
147 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
148 |
2
| assertFalse("Should have removed child1", cache.getRoot().hasChild(child1));
|
149 |
2
| assertTrue(cache.getRoot().hasChild(child2));
|
150 |
| } |
151 |
| |
152 |
2
| public void testRemoveAndAdd() throws Exception
|
153 |
| { |
154 |
2
| cache.put(parent, "k", "v");
|
155 |
2
| cache.put(child1, "k", "v");
|
156 |
| |
157 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
158 |
2
| assertTrue(cache.getRoot().hasChild(child1));
|
159 |
2
| assertFalse(cache.getRoot().hasChild(child2));
|
160 |
| |
161 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
162 |
| |
163 |
2
| tm.begin();
|
164 |
2
| cache.removeNode(child1);
|
165 |
2
| cache.put(child2, "k", "v");
|
166 |
2
| tm.commit();
|
167 |
| |
168 |
2
| if (lockParentForChildInsertRemove)
|
169 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
170 |
| else |
171 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
172 |
| |
173 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
174 |
2
| assertFalse("Should have removed child1", cache.getRoot().hasChild(child1));
|
175 |
2
| assertTrue(cache.getRoot().hasChild(child2));
|
176 |
| } |
177 |
| |
178 |
2
| public void testDeepRemove()
|
179 |
| { |
180 |
2
| cache.put(parent, "k", "v");
|
181 |
2
| cache.put(deepchild, "k", "v");
|
182 |
| |
183 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
184 |
2
| assertTrue(cache.getRoot().hasChild(deepchild));
|
185 |
| |
186 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
187 |
| |
188 |
2
| cache.removeNode(deepchild);
|
189 |
| |
190 |
2
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
191 |
| |
192 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
193 |
2
| assertFalse("Should have removed deepchild", cache.getRoot().hasChild(deepchild));
|
194 |
| } |
195 |
| |
196 |
2
| public void testDeepAdd()
|
197 |
| { |
198 |
2
| cache.put(parent, "k", "v");
|
199 |
| |
200 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
201 |
2
| assertFalse(cache.getRoot().hasChild(deepchild));
|
202 |
| |
203 |
2
| long parentVersion = getVersion(cache.getRoot().getChild(parent));
|
204 |
| |
205 |
2
| cache.put(deepchild, "k", "v");
|
206 |
| |
207 |
2
| if (lockParentForChildInsertRemove)
|
208 |
1
| assertEquals(parentVersion + 1, getVersion(cache.getRoot().getChild(parent)));
|
209 |
| else |
210 |
1
| assertEquals(parentVersion, getVersion(cache.getRoot().getChild(parent)));
|
211 |
| |
212 |
2
| assertTrue(cache.getRoot().hasChild(parent));
|
213 |
2
| assertTrue(cache.getRoot().hasChild(deepchild));
|
214 |
| } |
215 |
| } |