1 package org.jboss.cache.pojo.collection; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.aop.proxy.ClassProxy; 9 import org.jboss.cache.pojo.PojoCache; 10 import org.jboss.cache.pojo.PojoCacheFactory; 11 import org.jboss.cache.pojo.PojoCacheException; 12 import org.jboss.cache.pojo.test.Address; 13 14 import java.io.Serializable ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 27 28 public class CachedMapTest extends TestCase 29 { 30 Log log = LogFactory.getLog(CachedMapTest.class); 31 PojoCache cache_; 32 Map hobbies; 33 34 public CachedMapTest(String name) 35 { 36 super(name); 37 } 38 39 protected void setUp() throws Exception 40 { 41 super.setUp(); 42 log.info("setUp() ...."); 43 String configFile = "META-INF/local-service.xml"; 44 boolean toStart = false; 45 cache_ = PojoCacheFactory.createCache(configFile, toStart); 46 cache_.start(); 47 48 stage(); 49 } 50 51 protected void tearDown() throws Exception 52 { 53 super.tearDown(); 54 cache_.stop(); 55 } 56 57 protected void stage() throws Exception 58 { 59 hobbies = new HashMap (); 60 hobbies.put("1", "golf"); 61 hobbies.put("2", "tennis"); 62 hobbies.put("3", "polo"); 63 64 cache_.attach("/person/test7", hobbies); 65 66 hobbies = (Map ) cache_.find("/person/test7"); 67 assertEquals("Map size", 3, hobbies.size()); 68 69 if (!(hobbies instanceof ClassProxy || hobbies instanceof Map )) 70 { 71 fail("testPut(): hobbies is not instance of ClassProxy nor Map"); 72 } 73 } 74 75 80 public void testPut() throws Throwable 81 { 82 int size = hobbies.size(); 83 assertEquals("Size is ", 3, size); 84 85 hobbies.put("6", "baseball"); 86 size = hobbies.size(); 87 assertEquals("Size is ", 4, size); 88 89 } 90 91 public void testAddAndRemoveIndex() throws Throwable 92 { 93 hobbies.put("4", "baseball"); 94 int size = hobbies.size(); 95 assertEquals("Size is ", 4, size); 96 97 assertTrue("Skill contain Golf ", hobbies.containsKey("3")); 98 99 hobbies.remove("3"); 100 size = hobbies.size(); 101 assertEquals("Size is ", 3, size); 102 assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3")); 103 104 hobbies.clear(); 105 size = hobbies.size(); 106 assertEquals("Size is ", 0, size); 107 108 assertTrue("Should be empty", hobbies.isEmpty()); 109 } 110 111 public void testPutAllEtc() throws Throwable 112 { 113 Map map = new HashMap (); 114 map.put("4", "pingpong"); 115 map.put("5", "handball"); 116 117 hobbies.putAll(map); 118 int size = hobbies.size(); 119 assertEquals("Size is ", 5, size); 120 121 assertTrue("Key is ", hobbies.containsKey("4")); 122 123 Set keys = hobbies.keySet(); 124 assertEquals("Key size ", 5, keys.size()); 125 126 Set entries = hobbies.entrySet(); 127 assertEquals("Entry size ", 5, entries.size()); 128 129 } 130 131 public void testLongValue() throws Throwable 132 { 133 Long val = new Long ("8225676592564383"); 134 Long val2 = 8225676592564383L; 135 assertTrue(0 == val.compareTo(val2)); hobbies.put(val, "prateek"); 137 assertTrue(hobbies.containsKey(val)); 138 assertTrue(hobbies.get(val).equals("prateek")); 139 } 140 141 public void testEntrySet() throws Throwable 142 { 143 System.out.println("Map " + hobbies.toString()); 144 for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();) 145 { 146 Map.Entry entry = (Map.Entry ) i.next(); 147 System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue()); 148 } 149 } 150 151 public void testValues() throws Throwable 152 { 153 System.out.println("Map " + hobbies.toString()); 154 155 Set correct = new HashSet (); 156 correct.add("golf"); 157 correct.add("tennis"); 158 correct.add("polo"); 159 160 Collection values = hobbies.values(); 161 assertEquals("Correct number of elements in value collection", 162 correct.size(), values.size()); 163 164 Iterator iter = null; 165 for (iter = correct.iterator(); iter.hasNext();) 166 assertTrue(values.contains(iter.next())); 167 168 for (iter = values.iterator(); iter.hasNext();) 169 { 170 Object value = iter.next(); 171 assertTrue(value + " expected", correct.remove(value)); 172 } 173 assertTrue("No missing elements from iterator", correct.size() == 0); 174 175 iter.remove(); 176 assertTrue("2 elements left after remove via iter", values.size() == 2); 177 assertTrue("Iter removal reflected in map", hobbies.size() == 2); 178 179 Object [] data = values.toArray(); 180 assertTrue("2 elements in values array", data.length == 2); 181 182 values.remove(data[0]); 183 assertTrue("1 element left after remove", values.size() == 1); 184 assertTrue("Removal reflected in map", hobbies.size() == 1); 185 186 values.clear(); 187 assertTrue("0 elements left after clear", values.size() == 0); 188 assertTrue("Clear reflected in map", hobbies.size() == 0); 189 } 190 191 public void testContainsValue() throws Throwable 192 { 193 System.out.println("Map " + hobbies.toString()); 194 assertTrue("contains golf", hobbies.containsValue("golf")); 195 assertTrue("contains tennis", hobbies.containsValue("tennis")); 196 assertTrue("contains polo", hobbies.containsValue("polo")); 197 assertFalse("does not contain squash", hobbies.containsValue("squash")); 198 } 199 200 public void testEquals1() throws Throwable 201 { 202 Map map = new HashMap (); 203 map.put("1", "test"); 204 map.put("4", "test"); 205 map.put("2", "tennis"); 206 assertFalse("Map should not be the same ", map.equals(hobbies)); 207 } 208 209 public void testEquals2() throws Throwable 210 { 211 Map map1 = new HashMap (); 212 cache_.attach("map1", map1); 213 map1 = (Map ) cache_.find("map1"); 214 map1.put("1", "test"); 215 216 Map map2 = new HashMap (); 217 cache_.attach("map2", map2); 218 map2 = (Map ) cache_.find("map2"); 219 map2.put("1", "me"); 220 221 assertFalse("Map should not be the same ", map1.equals(map2)); 222 } 223 224 public void testEquals3() throws Throwable 225 { 226 Map map1 = new HashMap (); 227 cache_.attach("map1", map1); 228 map1 = (Map ) cache_.find("map1"); 229 map1.put("1", "test"); 230 map1.put("2", "test"); 231 232 Map map2 = new HashMap (); 233 cache_.attach("map2", map2); 234 map2 = (Map ) cache_.find("map2"); 235 map2.put("1", "me"); 236 map2.put("2", "me"); 237 238 assertFalse("Map should not be the same ", map1.equals(map2)); 239 } 240 241 public void testAttachAndDetach() throws Exception 242 { 243 Map map = new HashMap (); 244 map.put("1", "English"); 245 map.put("2", "French"); 246 map.put("3", "Taiwanese"); 247 248 cache_.attach("/test", map); map = (Map ) cache_.find("/test"); 250 assertEquals("Size ", 3, map.size()); 251 252 map = (Map ) cache_.detach("/test"); assertEquals("Size ", 3, map.size()); 254 255 System.out.println("**** End of cache content **** "); 256 map.remove("2"); 257 map.put(2, "Hoklo"); 258 assertEquals("Size ", 3, map.size()); 259 assertEquals("Content ", "Hoklo", map.get(2)); 260 261 cache_.attach("/test", map); 263 map.remove("3"); 264 assertEquals("Size ", 2, map.size()); 265 } 266 267 public void testPojoAttachAndDetach() throws Exception 268 { 269 Address add1 = new Address(); 270 add1.setCity("San Jose"); 271 add1.setZip(95123); 272 273 Address add2 = new Address(); 274 add2.setCity("Sunnyvale"); 275 add2.setZip(94086); 276 277 Address add3 = new Address(); 278 add3.setCity("Santa Clara"); 279 add3.setZip(951131); 280 281 Map map = new HashMap (); 282 map.put("1", add1); 283 map.put("2", add2); 284 map.put("3", add3); 285 286 cache_.attach("/test", map); map = (Map ) cache_.find("/test"); 288 assertEquals("Size ", 3, map.size()); 289 290 map = (Map ) cache_.detach("/test"); 291 assertEquals("Size ", 3, map.size()); 292 293 System.out.println("**** End of cache content **** "); 294 map.remove("2"); 295 map.put("2", add2); 296 assertEquals("Size ", 3, map.size()); 297 assertEquals("Content ", add2, map.get("2")); 298 299 cache_.attach("/test", map); 301 map.remove("2"); 302 assertEquals("Size ", 2, map.size()); 303 } 304 305 public void testKeyAsObject() throws Exception 306 { 307 SerializableKeyValue key = new SerializableKeyValue(42, "Novell"); 308 Address add1 = new Address(); 309 add1.setCity("Waltham"); 310 add1.setStreet("404 Wyman Street"); 311 add1.setZip(02451); 312 313 Map map = new HashMap (); 314 map.put(key, add1); 315 cache_.attach("/keytest", map); 316 Map ref = (Map ) cache_.find("/keytest"); 317 318 Iterator iter = ref.keySet().iterator(); 319 assertTrue(iter.hasNext()); 320 Object keyFromMap = iter.next(); 321 assertEquals("original key is same as key in pojocache map, key class=" + 322 key.getClass().getName() + ", keyFromMap class=" + keyFromMap.getClass().getName(), 323 key, keyFromMap); 324 assertEquals("local map is equal to pojocache map", map, ref); 325 } 326 327 public void testNonSerializableKeyAsObject() throws Exception 328 { 329 NotSerializableKeyValue key = new NotSerializableKeyValue(42, "Novell"); 331 Address add1 = new Address(); 332 add1.setCity("Waltham"); 333 add1.setStreet("404 Wyman Street"); 334 add1.setZip(02451); 335 336 Map map = new HashMap (); 337 map.put(key, add1); 338 try 339 { 340 cache_.attach("/keytest", map); fail("failed to get expected runtimeException when adding nonserializable key to pojocache map"); 342 } 343 catch (PojoCacheException e) 344 { 345 Throwable t = e; 346 347 assertTrue("we got expected PojoCacheException " 348 + t.getCause().getClass().getName(), t.getCause() instanceof PojoCacheException); 349 } 350 map.clear(); 351 cache_.attach("/keytest", map); Map ref = (Map ) cache_.find("/keytest"); 353 354 try 356 { 357 ref.put(key, add1); fail("failed to get expected runtimeException when putting nonserializable key to pojocache map"); 359 } 360 catch (RuntimeException e) 361 { 362 Throwable t = e; 363 364 assertTrue("we got expected PojoCacheException " 365 + t.getClass().getName(), t instanceof PojoCacheException); 366 } 367 } 368 369 public static Test suite() throws Exception 370 { 371 return new TestSuite(CachedMapTest.class); 372 } 373 374 public static void main(String [] args) throws Exception 375 { 376 junit.textui.TestRunner.run(suite()); 377 } 378 379 static class SerializableKeyValue implements Serializable 380 { 381 Integer ivalue; 382 String svalue; 383 384 SerializableKeyValue(Integer ivalue, String svalue) 385 { 386 this.ivalue = ivalue; 387 this.svalue = svalue; 388 } 389 390 public int hashCode() 391 { 392 return ivalue.hashCode() + svalue.hashCode(); 393 } 394 395 public boolean equals(Object o) 396 { 397 if (o == this) 398 { 399 return true; 400 } 401 if (!(o instanceof SerializableKeyValue)) 402 { 403 System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName()); 404 return false; 405 } 406 SerializableKeyValue val = (SerializableKeyValue) o; 407 return val.ivalue == this.ivalue && val.svalue == this.svalue; 408 } 409 } 410 411 static class NotSerializableKeyValue 412 { 413 Integer ivalue; 414 String svalue; 415 416 NotSerializableKeyValue(Integer ivalue, String svalue) 417 { 418 this.ivalue = ivalue; 419 this.svalue = svalue; 420 } 421 422 public int hashCode() 423 { 424 return ivalue.hashCode() + svalue.hashCode(); 425 } 426 427 public boolean equals(Object o) 428 { 429 if (o == this) 430 { 431 return true; 432 } 433 if (!(o instanceof SerializableKeyValue)) 434 { 435 System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName()); 436 return false; 437 } 438 SerializableKeyValue val = (SerializableKeyValue) o; 439 return val.ivalue == this.ivalue && val.svalue == this.svalue; 440 } 441 } 442 443 } 444 445 | Popular Tags |