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