1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache; |
8 |
| |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| import org.jboss.cache.config.Configuration; |
14 |
| import org.jgroups.util.Util; |
15 |
| |
16 |
| import java.util.HashMap; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class FqnTest extends TestCase |
25 |
| { |
26 |
| private Cache cache; |
27 |
| |
28 |
26
| protected void setUp()
|
29 |
| { |
30 |
26
| cache = null;
|
31 |
| } |
32 |
| |
33 |
26
| protected void tearDown()
|
34 |
| { |
35 |
26
| if (cache != null)
|
36 |
| { |
37 |
1
| cache.stop();
|
38 |
1
| cache = null;
|
39 |
| } |
40 |
| } |
41 |
| |
42 |
26
| public FqnTest(String s)
|
43 |
| { |
44 |
26
| super(s);
|
45 |
| } |
46 |
| |
47 |
1
| public void testNull()
|
48 |
| { |
49 |
1
| Fqn fqn = new Fqn();
|
50 |
1
| log("null fqn is " + fqn);
|
51 |
1
| assertEquals(0, fqn.size());
|
52 |
1
| int hcode = fqn.hashCode();
|
53 |
1
| assertTrue(hcode != -1);
|
54 |
| } |
55 |
| |
56 |
1
| public void testOne()
|
57 |
| { |
58 |
1
| Fqn<Integer> fqn = new Fqn<Integer>(22);
|
59 |
1
| log("one fqn is " + fqn);
|
60 |
1
| assertEquals(1, fqn.size());
|
61 |
1
| int hcode = fqn.hashCode();
|
62 |
1
| assertTrue(hcode != -1);
|
63 |
| } |
64 |
| |
65 |
1
| public void testEmptyFqn()
|
66 |
| { |
67 |
1
| Fqn f1 = new Fqn();
|
68 |
1
| Fqn f2 = new Fqn();
|
69 |
1
| assertEquals(f1, f2);
|
70 |
| } |
71 |
| |
72 |
1
| public void testFqn()
|
73 |
| { |
74 |
1
| Fqn<String> fqn = Fqn.fromString("/a/b/c");
|
75 |
1
| log("fqn is " + fqn);
|
76 |
1
| assertEquals(3, fqn.size());
|
77 |
| |
78 |
1
| Fqn<String> fqn2 = new Fqn<String>("a", "b", "c");
|
79 |
1
| log("fqn2 is " + fqn2);
|
80 |
1
| assertEquals(3, fqn2.size());
|
81 |
1
| assertEquals("fqn should equal fqn2", fqn, fqn2);
|
82 |
1
| assertEquals(fqn.hashCode(), fqn2.hashCode());
|
83 |
| } |
84 |
| |
85 |
1
| public void testHereogeneousNames()
|
86 |
| { |
87 |
1
| Fqn<Object> fqn = new Fqn<Object>("string", 38, true);
|
88 |
1
| log("fqn is " + fqn);
|
89 |
1
| assertEquals(3, fqn.size());
|
90 |
| |
91 |
1
| Fqn<Object> fqn2 = new Fqn<Object>("string", 38, true);
|
92 |
1
| assertEquals(fqn, fqn2);
|
93 |
1
| assertEquals(fqn.hashCode(), fqn2.hashCode());
|
94 |
| } |
95 |
| |
96 |
1
| public void testHashcode()
|
97 |
| { |
98 |
1
| Fqn fqn1, fqn2;
|
99 |
1
| fqn1 = new Fqn<Object>("a", "b", "c");
|
100 |
1
| fqn2 = Fqn.fromString("/a/b/c");
|
101 |
1
| log("fqn is " + fqn1);
|
102 |
1
| assertEquals(fqn1, fqn2);
|
103 |
| |
104 |
1
| HashMap<Fqn, Integer> map = new HashMap<Fqn, Integer>();
|
105 |
1
| map.put(fqn1, 33);
|
106 |
1
| map.put(fqn2, 34);
|
107 |
1
| assertEquals(1, map.size());
|
108 |
1
| assertEquals(new Integer(34), map.get(fqn1));
|
109 |
| } |
110 |
| |
111 |
1
| public void testHashcode2()
|
112 |
| { |
113 |
1
| Fqn<Integer> fqn = new Fqn<Integer>(-1);
|
114 |
1
| log("one fqn is " + fqn);
|
115 |
1
| assertEquals(1, fqn.size());
|
116 |
1
| int hcode = fqn.hashCode();
|
117 |
1
| assertTrue(hcode == -1);
|
118 |
| } |
119 |
| |
120 |
1
| public void testEquals()
|
121 |
| { |
122 |
1
| Fqn<String> fqn1 = new Fqn<String>("person/test");
|
123 |
| |
124 |
1
| Fqn<String> f1;
|
125 |
1
| Fqn<String> f2;
|
126 |
1
| Fqn<String> f3;
|
127 |
| |
128 |
1
| f1 = new Fqn<String>(fqn1, "0");
|
129 |
1
| f2 = new Fqn<String>(fqn1, "1");
|
130 |
1
| f3 = new Fqn<String>(fqn1, "2");
|
131 |
| |
132 |
1
| HashMap<Fqn<String>, String> map = new HashMap<Fqn<String>, String>();
|
133 |
1
| map.put(f1, "0");
|
134 |
1
| map.put(f2, "1");
|
135 |
1
| map.put(f3, "2");
|
136 |
| |
137 |
1
| assertNotNull("f1 ", map.get(new Fqn<String>(fqn1, "0")));
|
138 |
1
| assertNotNull("f2 ", map.get(new Fqn<String>(fqn1, "1")));
|
139 |
1
| assertNotNull("f3 ", map.get(new Fqn<String>(fqn1, "2")));
|
140 |
| |
141 |
| } |
142 |
| |
143 |
| |
144 |
1
| public void testEquals2()
|
145 |
| { |
146 |
1
| Fqn<String> f1;
|
147 |
1
| Fqn<String> f2;
|
148 |
1
| f1 = Fqn.fromString("/a/b/c");
|
149 |
1
| f2 = Fqn.fromString("/a/b/c");
|
150 |
1
| assertEquals(f1, f2);
|
151 |
| |
152 |
1
| f2 = Fqn.fromString("/a/b");
|
153 |
1
| assertFalse(f1.equals(f2));
|
154 |
| |
155 |
1
| f2 = Fqn.fromString("/a/b/c/d");
|
156 |
1
| assertFalse(f1.equals(f2));
|
157 |
| } |
158 |
| |
159 |
1
| public void testEquals2WithMarshalling() throws Exception
|
160 |
| { |
161 |
1
| Fqn<String> f1, f2;
|
162 |
1
| f1 = Fqn.fromString("/a/b/c");
|
163 |
1
| f2 = marshalAndUnmarshal(f1);
|
164 |
1
| assertEquals(f1, f2);
|
165 |
| } |
166 |
| |
167 |
| |
168 |
1
| public void testEquals3()
|
169 |
| { |
170 |
1
| Fqn<Object> f1;
|
171 |
1
| Fqn<? extends Object> f2;
|
172 |
1
| f1 = new Fqn<Object>("a", 322649, Boolean.TRUE);
|
173 |
1
| f2 = new Fqn<String>();
|
174 |
1
| assertFalse(f1.equals(f2));
|
175 |
1
| assertFalse(f2.equals(f1));
|
176 |
| |
177 |
1
| f2 = Fqn.fromString("a/322649/TRUE");
|
178 |
1
| assertFalse(f1.equals(f2));
|
179 |
| |
180 |
1
| f2 = new Fqn<Object>("a", 322649, Boolean.FALSE);
|
181 |
1
| assertFalse(f1.equals(f2));
|
182 |
| |
183 |
1
| f2 = new Fqn<Object>("a", 322649, Boolean.TRUE);
|
184 |
1
| assertEquals(f1, f2);
|
185 |
| } |
186 |
| |
187 |
1
| public void testEquals3WithMarshalling() throws Exception
|
188 |
| { |
189 |
1
| Fqn f1, f2;
|
190 |
1
| f1 = new Fqn<Object>("a", 322649, Boolean.TRUE);
|
191 |
1
| f2 = marshalAndUnmarshal(f1);
|
192 |
1
| assertEquals(f1, f2);
|
193 |
1
| assertEquals(f2, f1);
|
194 |
| |
195 |
1
| f2 = Fqn.fromString("a/322649/TRUE");
|
196 |
1
| f2 = marshalAndUnmarshal(f2);
|
197 |
1
| assertFalse(f1.equals(f2));
|
198 |
| |
199 |
1
| f2 = new Fqn<Object>("a", 322649, Boolean.FALSE);
|
200 |
1
| f2 = marshalAndUnmarshal(f2);
|
201 |
1
| assertFalse(f1.equals(f2));
|
202 |
| |
203 |
1
| f2 = new Fqn<Object>("a", 322649, Boolean.TRUE);
|
204 |
1
| f2 = marshalAndUnmarshal(f2);
|
205 |
1
| assertEquals(f1, f2);
|
206 |
| } |
207 |
| |
208 |
1
| public void testEquals4()
|
209 |
| { |
210 |
1
| Fqn<String> fqn = Fqn.fromString("X");
|
211 |
| |
212 |
1
| assertFalse("Casting ok", fqn.equals("X"));
|
213 |
| |
214 |
1
| assertFalse("null ok", fqn.equals(null));
|
215 |
| } |
216 |
| |
217 |
1
| public void testClone() throws CloneNotSupportedException
|
218 |
| { |
219 |
1
| Fqn<String> fqn1 = Fqn.fromString("/a/b/c");
|
220 |
1
| Fqn<String> fqn2 = fqn1.clone();
|
221 |
1
| assertEquals(fqn1, fqn2);
|
222 |
1
| assertEquals(fqn1.hashCode(), fqn2.hashCode());
|
223 |
| } |
224 |
| |
225 |
1
| public void testNullElements() throws CloneNotSupportedException
|
226 |
| { |
227 |
1
| Fqn<Object> fqn0 = new Fqn<Object>((Object) null);
|
228 |
1
| assertEquals(1, fqn0.size());
|
229 |
| |
230 |
1
| Fqn<Object> fqn1 = new Fqn<Object>("NULL", null, 0);
|
231 |
1
| assertEquals(3, fqn1.size());
|
232 |
| |
233 |
1
| Fqn<Object> fqn2 = new Fqn<Object>("NULL", null, 0);
|
234 |
1
| assertEquals(fqn1.hashCode(), fqn2.hashCode());
|
235 |
1
| assertEquals(fqn1, fqn2);
|
236 |
1
| assertEquals(fqn1, fqn1.clone());
|
237 |
| } |
238 |
| |
239 |
1
| public void testIteration()
|
240 |
| { |
241 |
1
| Fqn<String> fqn = Fqn.fromString("/a/b/c");
|
242 |
1
| assertEquals(3, fqn.size());
|
243 |
1
| Fqn<Object> tmp_fqn = new Fqn<Object>();
|
244 |
1
| assertEquals(0, tmp_fqn.size());
|
245 |
1
| for (int i = 0; i < fqn.size(); i++)
|
246 |
| { |
247 |
3
| String s = fqn.get(i);
|
248 |
3
| tmp_fqn = new Fqn<Object>(tmp_fqn, s);
|
249 |
3
| assertEquals(tmp_fqn.size(), i + 1);
|
250 |
| } |
251 |
1
| assertEquals(3, tmp_fqn.size());
|
252 |
1
| assertEquals(fqn, tmp_fqn);
|
253 |
| } |
254 |
| |
255 |
1
| public void testIsChildOf()
|
256 |
| { |
257 |
1
| Fqn<String> child = Fqn.fromString("/a/b");
|
258 |
1
| Fqn<String> parent = Fqn.fromString("/a");
|
259 |
1
| assertTrue("Is child of ", child.isChildOf(parent));
|
260 |
1
| assertFalse("Is child of ", parent.isChildOf(child));
|
261 |
1
| assertTrue("Is same ", child.isChildOrEquals(child));
|
262 |
| |
263 |
1
| parent = Fqn.fromString("/a/b/c");
|
264 |
1
| child = Fqn.fromString("/a/b/c/d/e/f/g/h/e/r/e/r/t/tt/");
|
265 |
1
| assertTrue(child.isChildOf(parent));
|
266 |
| } |
267 |
| |
268 |
1
| public void testIsChildOf2()
|
269 |
| { |
270 |
1
| Fqn<String> child = Fqn.fromString("/a/b/c/d");
|
271 |
1
| assertEquals("Fqn ", "/b/c/d", child.getSubFqn(1, child.size()).toString());
|
272 |
| } |
273 |
| |
274 |
1
| public void testParentage()
|
275 |
| { |
276 |
1
| Fqn<String> fqnRoot = new Fqn<String>();
|
277 |
1
| Fqn<String> parent = fqnRoot.getParent();
|
278 |
1
| assertEquals(parent, fqnRoot);
|
279 |
| |
280 |
1
| Fqn<String> fqnOne = Fqn.fromString("/one");
|
281 |
1
| parent = fqnOne.getParent();
|
282 |
1
| assertEquals(parent, fqnRoot);
|
283 |
1
| assertTrue(fqnOne.isChildOf(parent));
|
284 |
| |
285 |
1
| Fqn<String> fqnTwo = Fqn.fromString("/one/two");
|
286 |
1
| parent = fqnTwo.getParent();
|
287 |
1
| assertEquals(parent, fqnOne);
|
288 |
1
| assertTrue(fqnTwo.isChildOf(parent));
|
289 |
| |
290 |
1
| Fqn<String> fqnThree = Fqn.fromString("/one/two/three");
|
291 |
1
| parent = fqnThree.getParent();
|
292 |
1
| assertEquals(parent, fqnTwo);
|
293 |
1
| assertTrue(fqnThree.isChildOf(parent));
|
294 |
| |
295 |
| } |
296 |
| |
297 |
1
| public void testRoot()
|
298 |
| { |
299 |
1
| Fqn<String> fqn = new Fqn<String>();
|
300 |
1
| assertTrue(fqn.isRoot());
|
301 |
| |
302 |
1
| fqn = Fqn.fromString("/one/two");
|
303 |
1
| assertFalse(fqn.isRoot());
|
304 |
| } |
305 |
| |
306 |
1
| public void testGetName()
|
307 |
| { |
308 |
1
| Fqn<Integer> integerFqn = new Fqn<Integer>(1);
|
309 |
1
| assertEquals("1", integerFqn.getLastElementAsString());
|
310 |
| |
311 |
1
| Object object = new Object();
|
312 |
1
| Fqn<Object> objectFqn = new Fqn<Object>(object);
|
313 |
1
| assertEquals(object.toString(), objectFqn.getLastElementAsString());
|
314 |
| } |
315 |
| |
316 |
1
| public void testCloningString() throws CloneNotSupportedException
|
317 |
| { |
318 |
1
| Fqn<String> f = Fqn.fromString("/a/b/c");
|
319 |
1
| assertEquals(f, f.clone());
|
320 |
| } |
321 |
| |
322 |
1
| public void testCloningOtherTypes() throws CloneNotSupportedException
|
323 |
| { |
324 |
1
| Fqn<Object> f = new Fqn<Object>("blah", 10, Boolean.TRUE);
|
325 |
1
| assertEquals(f, f.clone());
|
326 |
| } |
327 |
| |
328 |
1
| public void testRemovalNonString() throws Exception
|
329 |
| { |
330 |
1
| Fqn<Object> f = new Fqn<Object>("test", 1);
|
331 |
| |
332 |
1
| Configuration c = new Configuration();
|
333 |
1
| c.setCacheMode("LOCAL");
|
334 |
1
| cache = DefaultCacheFactory.getInstance().createCache(c);
|
335 |
| |
336 |
1
| cache.put(f, "key", "value");
|
337 |
| |
338 |
1
| assertEquals("value", cache.get(f, "key"));
|
339 |
1
| assertTrue(cache.getRoot().hasChild(f));
|
340 |
| |
341 |
1
| cache.removeNode(f);
|
342 |
| |
343 |
1
| assertNull(cache.get(f, "key"));
|
344 |
1
| assertFalse(cache.getRoot().hasChild(f));
|
345 |
| } |
346 |
| |
347 |
5
| Fqn marshalAndUnmarshal(Fqn fqn) throws Exception
|
348 |
| { |
349 |
5
| byte[] buf = Util.objectToByteBuffer(fqn);
|
350 |
5
| return (Fqn) Util.objectFromByteBuffer(buf);
|
351 |
| } |
352 |
| |
353 |
| |
354 |
| |
355 |
1
| public void testGenerics()
|
356 |
| { |
357 |
1
| Fqn<String> f = Fqn.fromString("/blah/blah");
|
358 |
1
| Fqn<String> f2 = Fqn.fromString("/blah/blah");
|
359 |
| |
360 |
1
| assertEquals(f, f2);
|
361 |
| |
362 |
1
| Fqn<Integer> f3 = new Fqn<Integer>(1, 2, 3, 5);
|
363 |
| |
364 |
1
| Fqn<Integer> f4 = new Fqn<Integer>(1, 2, 3);
|
365 |
| |
366 |
1
| assertEquals(f3.getParent(), f4);
|
367 |
| } |
368 |
| |
369 |
1
| public void testGenerations()
|
370 |
| { |
371 |
1
| Fqn<Integer> f = new Fqn<Integer>(1, 2, 3, 4, 5, 6, 7);
|
372 |
| |
373 |
1
| assertEquals(f, f.getAncestor(f.size()));
|
374 |
1
| assertEquals(f.getParent(), f.getAncestor(f.size() - 1));
|
375 |
1
| assertEquals(Fqn.ROOT, f.getAncestor(0));
|
376 |
1
| assertEquals(new Fqn<Integer>(1), f.getAncestor(1));
|
377 |
1
| assertEquals(new Fqn<Integer>(1, 2), f.getAncestor(2));
|
378 |
1
| assertEquals(new Fqn<Integer>(1, 2, 3), f.getAncestor(3));
|
379 |
1
| assertEquals(new Fqn<Integer>(1, 2, 3, 4), f.getAncestor(4));
|
380 |
1
| assertEquals(new Fqn<Integer>(1, 2, 3, 4, 5), f.getAncestor(5));
|
381 |
| |
382 |
1
| try
|
383 |
| { |
384 |
1
| f.getAncestor(-1);
|
385 |
0
| fail("Should fail");
|
386 |
| } |
387 |
| catch (Exception e) |
388 |
| { |
389 |
1
| e.printStackTrace();
|
390 |
| } |
391 |
| |
392 |
1
| try
|
393 |
| { |
394 |
1
| f.getAncestor(f.size() + 1);
|
395 |
0
| fail("Should fail");
|
396 |
| } |
397 |
| catch (Exception e) |
398 |
| { |
399 |
1
| e.printStackTrace();
|
400 |
| } |
401 |
| } |
402 |
| |
403 |
7
| void log(String msg)
|
404 |
| { |
405 |
7
| System.out.println("-- " + msg);
|
406 |
| } |
407 |
| |
408 |
1
| public static Test suite()
|
409 |
| { |
410 |
1
| return new TestSuite(FqnTest.class);
|
411 |
| } |
412 |
| |
413 |
0
| public static void main(String[] args)
|
414 |
| { |
415 |
0
| junit.textui.TestRunner.run(suite());
|
416 |
| } |
417 |
| } |