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