1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.options; |
8 |
| |
9 |
| import junit.framework.Assert; |
10 |
| import junit.framework.TestCase; |
11 |
| import org.jboss.cache.CacheImpl; |
12 |
| import org.jboss.cache.DefaultCacheFactory; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.Node; |
15 |
| import org.jboss.cache.NodeSPI; |
16 |
| import org.jboss.cache.config.Configuration; |
17 |
| import org.jboss.cache.config.Option; |
18 |
| import org.jboss.cache.optimistic.DataVersion; |
19 |
| import org.jboss.cache.optimistic.DefaultDataVersion; |
20 |
| |
21 |
| import javax.transaction.TransactionManager; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class ExplicitVersionsTest extends TestCase |
29 |
| { |
30 |
| private CacheImpl cache; |
31 |
| private Fqn fqn = Fqn.fromString("/a"); |
32 |
| private String key = "key"; |
33 |
| |
34 |
7
| protected void setUp() throws Exception
|
35 |
| { |
36 |
0
| if (cache != null) tearDown();
|
37 |
7
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
38 |
7
| cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
|
39 |
7
| cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
|
40 |
7
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
41 |
7
| cache.start();
|
42 |
| } |
43 |
| |
44 |
7
| protected void tearDown()
|
45 |
| { |
46 |
7
| if (cache != null)
|
47 |
| { |
48 |
7
| cache.stop();
|
49 |
7
| cache = null;
|
50 |
| } |
51 |
| } |
52 |
| |
53 |
1
| public void testSimplePut() throws Exception
|
54 |
| { |
55 |
1
| DataVersion version = new TestVersion("99");
|
56 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
|
57 |
1
| cache.put(fqn, key, "value");
|
58 |
| |
59 |
| |
60 |
1
| Assert.assertEquals("value", cache.get(fqn, key));
|
61 |
| |
62 |
| |
63 |
1
| NodeSPI node = (NodeSPI) cache.get(fqn);
|
64 |
1
| DataVersion versionFromCache = node.getVersion();
|
65 |
| |
66 |
1
| Assert.assertEquals(TestVersion.class, versionFromCache.getClass());
|
67 |
1
| Assert.assertEquals("99", ((TestVersion) versionFromCache).getInternalVersion());
|
68 |
| } |
69 |
| |
70 |
1
| public void testFailingPut() throws Exception
|
71 |
| { |
72 |
1
| DataVersion version = new TestVersion("99");
|
73 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
|
74 |
1
| cache.put(fqn, key, "value");
|
75 |
| |
76 |
1
| version = new TestVersion("25");
|
77 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
|
78 |
1
| TransactionManager mgr = cache.getTransactionManager();
|
79 |
1
| mgr.begin();
|
80 |
1
| cache.put(fqn, key, "value2");
|
81 |
1
| try
|
82 |
| { |
83 |
1
| mgr.commit();
|
84 |
0
| Assert.assertTrue("expected to fail", false);
|
85 |
| } |
86 |
| catch (Exception e) |
87 |
| { |
88 |
| |
89 |
1
| Assert.assertTrue("expected to fail", true);
|
90 |
| } |
91 |
| } |
92 |
| |
93 |
1
| public void testIncompatibleVersionTypes() throws Exception
|
94 |
| { |
95 |
1
| DataVersion version = new TestVersion("99");
|
96 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
|
97 |
1
| cache.put(fqn, key, "value");
|
98 |
1
| TransactionManager mgr = cache.getTransactionManager();
|
99 |
1
| mgr.begin();
|
100 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(new DefaultDataVersion(777));
|
101 |
1
| cache.put(fqn, key, "value2");
|
102 |
1
| try
|
103 |
| { |
104 |
| |
105 |
1
| mgr.commit();
|
106 |
0
| Assert.assertTrue("expected to fail", false);
|
107 |
| } |
108 |
| catch (Exception e) |
109 |
| { |
110 |
| |
111 |
1
| Assert.assertTrue("expected to fail", true);
|
112 |
| } |
113 |
| } |
114 |
| |
115 |
1
| public void testExplicitVersionOnLeaf() throws Exception
|
116 |
| { |
117 |
1
| cache.put("/org/domain/Entity", null);
|
118 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
|
119 |
| |
120 |
1
| TestVersion v = new TestVersion("Arse");
|
121 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(v);
|
122 |
1
| cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
|
123 |
| |
124 |
1
| assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
|
125 |
1
| assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
126 |
| } |
127 |
| |
128 |
1
| public void testExplicitVersionOnLeafImplicitParentCreation() throws Exception
|
129 |
| { |
130 |
1
| TestVersion v = new TestVersion("Arse");
|
131 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(v);
|
132 |
1
| cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
|
133 |
| |
134 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
|
135 |
1
| assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
136 |
| } |
137 |
| |
138 |
1
| public void testExplicitVersionsOnParents()
|
139 |
| { |
140 |
1
| Node root = cache.getRoot();
|
141 |
| |
142 |
1
| TestVersion lev2V = new TestVersion("Lev2-v");
|
143 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(lev2V);
|
144 |
1
| root.addChild(Fqn.fromString("LEV2"));
|
145 |
| |
146 |
1
| NodeSPI lev2 = (NodeSPI) root.getChild(Fqn.fromString("LEV2"));
|
147 |
| |
148 |
1
| assertNotNull(lev2);
|
149 |
| |
150 |
1
| assertEquals(lev2V, lev2.getVersion());
|
151 |
| |
152 |
1
| TestVersion lev3V = new TestVersion("Lev3-v");
|
153 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(lev3V);
|
154 |
1
| lev2.addChild(Fqn.fromString("LEV3"));
|
155 |
| |
156 |
1
| NodeSPI lev3 = (NodeSPI) lev2.getChild(Fqn.fromString("LEV3"));
|
157 |
| |
158 |
1
| assertNotNull(lev3);
|
159 |
| |
160 |
1
| assertEquals(lev3V, lev3.getVersion());
|
161 |
| |
162 |
1
| TestVersion lev4V = new TestVersion("Lev4-v");
|
163 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(lev4V);
|
164 |
1
| lev3.addChild(Fqn.fromString("LEV4"));
|
165 |
| |
166 |
1
| NodeSPI lev4 = (NodeSPI) lev3.getChild(Fqn.fromString("LEV4"));
|
167 |
| |
168 |
1
| assertNotNull(lev4);
|
169 |
| |
170 |
1
| assertEquals(lev4V, lev4.getVersion());
|
171 |
| } |
172 |
| |
173 |
1
| public void testExplicitVersionOnParentAndChild() throws Exception
|
174 |
| { |
175 |
1
| TestVersion vParent = new TestVersion("Parent-Version");
|
176 |
| |
177 |
1
| cache.getTransactionManager().begin();
|
178 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(vParent);
|
179 |
1
| cache.put(Fqn.fromString("/parent"), "k", "v");
|
180 |
1
| cache.getTransactionManager().commit();
|
181 |
| |
182 |
1
| assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache.get("/")).getVersion()).getRawVersion());
|
183 |
1
| assertEquals(vParent, ((NodeSPI)cache.get("/parent")).getVersion());
|
184 |
| |
185 |
1
| TestVersion vChild = new TestVersion("Child-Version");
|
186 |
| |
187 |
1
| cache.getTransactionManager().begin();
|
188 |
1
| cache.getInvocationContext().getOptionOverrides().setDataVersion(vChild);
|
189 |
1
| cache.put(Fqn.fromString("/parent/child"), "k", "v");
|
190 |
1
| cache.getTransactionManager().commit();
|
191 |
| |
192 |
1
| assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache.get("/")).getVersion()).getRawVersion());
|
193 |
1
| assertEquals(vParent, ((NodeSPI)cache.get("/parent")).getVersion());
|
194 |
1
| assertEquals(vChild, ((NodeSPI)cache.get("/parent/child")).getVersion());
|
195 |
| } |
196 |
| |
197 |
| } |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| class TestVersion implements DataVersion |
203 |
| { |
204 |
| private String myVersion; |
205 |
| |
206 |
24
| public TestVersion(String version)
|
207 |
| { |
208 |
24
| myVersion = version;
|
209 |
| } |
210 |
| |
211 |
13
| public String getInternalVersion()
|
212 |
| { |
213 |
13
| return myVersion;
|
214 |
| } |
215 |
| |
216 |
0
| public void setInternalVersion(String version)
|
217 |
| { |
218 |
0
| myVersion = version;
|
219 |
| } |
220 |
| |
221 |
13
| public boolean newerThan(DataVersion other)
|
222 |
| { |
223 |
13
| if (other instanceof TestVersion)
|
224 |
| { |
225 |
12
| return myVersion.compareTo(((TestVersion) other).getInternalVersion()) > 0;
|
226 |
| } |
227 |
| else |
228 |
| { |
229 |
1
| throw new IllegalArgumentException("version type mismatch");
|
230 |
| } |
231 |
| } |
232 |
| |
233 |
| |
234 |
14
| public String toString()
|
235 |
| { |
236 |
14
| return "TestVersion-" + myVersion;
|
237 |
| } |
238 |
| |
239 |
22
| public boolean equals(Object other)
|
240 |
| { |
241 |
22
| if (other instanceof TestVersion)
|
242 |
| { |
243 |
22
| TestVersion oVersion = (TestVersion) other;
|
244 |
0
| if (oVersion.myVersion == null && myVersion == null) return true;
|
245 |
22
| if (myVersion != null) return myVersion.equals(oVersion.myVersion);
|
246 |
| } |
247 |
0
| return false;
|
248 |
| } |
249 |
| } |