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.cache.pojo.PojoCache; 9 import org.jboss.cache.pojo.PojoCacheFactory; 10 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.LinkedHashMap ; 16 import java.util.LinkedHashSet ; 17 import java.util.LinkedList ; 18 import java.util.Map ; 19 20 25 26 public class CollectionTest extends TestCase 27 { 28 Log log_ = LogFactory.getLog(CollectionTest.class); 29 PojoCache cache_; 30 31 public CollectionTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 log_.info("setUp() ...."); 40 String configFile = "META-INF/local-service.xml"; 41 boolean toStart = false; 42 cache_ = PojoCacheFactory.createCache(configFile, toStart); 43 cache_.start(); 44 } 45 46 protected void tearDown() throws Exception 47 { 48 super.tearDown(); 49 cache_.stop(); 50 } 51 52 57 public void testLinkedList() throws Exception 58 { 59 LinkedList list = new LinkedList (); 60 LinkedList list1; 61 list.add("English"); 62 try 63 { 64 cache_.attach("/aop/list", list); 65 list = (LinkedList ) cache_.find("/aop/list"); 66 list.add("French"); 67 list1 = (LinkedList ) cache_.find("/aop/list"); 68 assertEquals("Size of list ", 2, list1.size()); 69 } catch (Exception e) 70 { 71 fail("pubtObject fails"); 72 throw e; 73 } 74 } 75 76 81 public void testLinkedMap() throws Exception 82 { 83 LinkedHashMap map = new LinkedHashMap (); 84 LinkedHashMap map1; 85 map.put("1", "English"); 86 try 87 { 88 cache_.attach("/aop/map", map); 89 map = (LinkedHashMap ) cache_.find("/aop/map"); 90 map.put("2", "French"); 91 map1 = (LinkedHashMap ) cache_.find("/aop/map"); 92 assertEquals("Size of map ", 2, map1.size()); 93 } catch (Exception e) 94 { 95 fail("pubtObject fails"); 96 throw e; 97 } 98 } 99 100 105 public void testLinkedSet() throws Exception 106 { 107 LinkedHashSet set = new LinkedHashSet (); 108 LinkedHashSet set1; 109 set.add("English"); 110 Map map; 111 try 112 { 113 cache_.attach("/aop/set", set); 114 set = (LinkedHashSet ) cache_.find("/aop/set"); 115 set.add("French"); 116 set1 = (LinkedHashSet ) cache_.find("/aop/set"); 117 assertEquals("Size of set ", 2, set1.size()); 118 } catch (Exception e) 119 { 120 fail("pubtObject fails"); 121 throw e; 122 } 123 } 124 125 public static Test suite() throws Exception 126 { 127 return new TestSuite(CollectionTest.class); 128 } 129 130 public static void main(String [] args) throws Exception 131 { 132 junit.textui.TestRunner.run(suite()); 133 } 134 135 public void testSize() throws Exception 137 { 138 139 LinkedList list = new LinkedList (); 140 list.add("java"); 141 try 142 { 143 cache_.attach("/language/list", list); 144 list = (LinkedList ) cache_.find("/language/list"); 145 assertEquals("size of collection", 1, list.size()); 146 list.add("c"); 147 list.add("lisp"); 148 assertEquals("size of collection", 3, list.size()); 149 list.remove("lisp"); 150 assertEquals("size of collection", 2, list.size()); 151 list.remove("c"); 152 assertEquals("size of collection", 1, list.size()); 153 list.clear(); 154 assertEquals("size of collection", 0, list.size()); 155 } 156 catch (Exception e) 157 { 158 fail("testSize " + e.getMessage()); 159 throw e; 160 } 161 162 } 163 164 public void testIsEmpty() throws Exception 165 { 166 LinkedList list = new LinkedList (); 167 168 try 169 { 170 cache_.attach("/language/list", list); 171 list = (LinkedList ) cache_.find("/language/list"); 172 assertTrue("collection is empty", list.isEmpty()); 173 list.add("c"); 174 list.add("lisp"); 175 assertFalse("collection is not empty", list.isEmpty()); 176 list.remove("lisp"); 177 assertFalse("collection is not empty", list.isEmpty()); 178 list.remove("c"); 179 assertTrue("collection is empty", list.isEmpty()); 180 } 181 catch (Exception e) 182 { 183 fail("testIsEmpty " + e.getMessage()); 184 throw e; 185 } 186 } 187 188 public void testContains() throws Exception 189 { 190 LinkedList list = new LinkedList (); 191 list.add("java"); 192 try 193 { 194 cache_.attach("/language/list", list); 195 list = (LinkedList ) cache_.find("/language/list"); 196 assertFalse("collection doesn't contains", list.contains("lisp")); 197 list.add("c"); 198 list.add("lisp"); 199 assertTrue("collection contains", list.contains("lisp")); 200 list.remove("lisp"); 201 assertFalse("collection doesn't contain", list.contains("lisp")); 202 list.remove("c"); 203 list.clear(); 204 assertFalse("collection doesn't contains", list.contains("c")); 205 } 206 catch (Exception e) 207 { 208 fail("testContains " + e.getMessage()); 209 throw e; 210 } 211 212 } 213 214 public void testIterator() throws Exception 215 { 216 LinkedList list = new LinkedList (); 217 LinkedList list2 = new LinkedList (); 218 list2.add("java"); 219 list2.add("c"); 220 list2.add("lisp"); 221 list2.add("c++"); 222 list2.add("forth"); 223 try 224 { 225 cache_.attach("/language/list", list); 226 list = (LinkedList ) cache_.find("/language/list"); 227 list.addAll(list2); 228 Iterator iter = list.iterator(); 229 while (iter.hasNext()) 230 { 231 Object o = iter.next(); 232 assertTrue("Iteration test", list2.contains(o)); 233 } 234 } 235 catch (Exception e) 236 { 237 fail("testIterator " + e.getMessage()); 238 throw e; 239 } 240 } 241 242 public void testToArray() throws Exception 243 { 244 LinkedList list = new LinkedList (); 245 LinkedList list2 = new LinkedList (); 246 list2.add("java"); 247 list2.add("c"); 248 list2.add("lisp"); 249 list2.add("c++"); 250 list2.add("forth"); 251 list.addAll(list2); 252 try 253 { 254 cache_.attach("/language/list", list); 255 list = (LinkedList ) cache_.find("/language/list"); 256 Object [] values = list.toArray(); 257 258 for (int looper = 0; looper < values.length; looper ++) 259 { 260 assertTrue("toArray test", list2.contains(values[looper])); 261 } 262 263 values = new Object [1]; 264 values = list.toArray(values); for (int looper = 0; looper < values.length; looper ++) 266 { 267 assertTrue("toArray test", list2.contains(values[looper])); 268 } 269 270 values = new Object [10]; 271 values = list.toArray(values); for (int looper = 0; looper < values.length; looper ++) 273 { 274 assertTrue("toArray test", (values[looper] == null || list2.contains(values[looper]))); 275 } 276 277 } 278 catch (Exception e) 279 { 280 fail("testToArray " + e.getMessage()); 281 throw e; 282 } 283 } 284 285 public void testAdd() throws Exception 286 { 287 LinkedList list = new LinkedList (); 288 try 289 { 290 cache_.attach("/language/list", list); 291 list = (LinkedList ) cache_.find("/language/list"); 292 list.add("java"); 293 assertTrue("add test", list.contains("java")); 294 assertFalse("add test", list.contains("c#")); 295 } 296 catch (Exception e) 297 { 298 fail("testAdd " + e.getMessage()); 299 throw e; 300 } 301 302 } 303 304 public void testRemove() throws Exception 305 { 306 LinkedList list = new LinkedList (); 307 try 308 { 309 cache_.attach("/language/list", list); 310 list = (LinkedList ) cache_.find("/language/list"); 311 list.add("java"); 312 assertTrue(list.remove("java")); 313 assertFalse("remove test", list.contains("java")); 314 } 315 catch (Exception e) 316 { 317 fail("testRemove " + e.getMessage()); 318 throw e; 319 } 320 321 } 322 323 public void testAddAll() throws Exception 324 { 325 LinkedList list = new LinkedList (); 326 LinkedList list2 = new LinkedList (); 327 list2.add("java"); 328 list2.add("c"); 329 list2.add("lisp"); 330 list2.add("c++"); 331 list2.add("forth"); 332 333 try 334 { 335 cache_.attach("/language/list", list); 336 list = (LinkedList ) cache_.find("/language/list"); 337 assertTrue(list.addAll(list2)); 338 Object [] values = list.toArray(); 339 340 for (int looper = 0; looper < values.length; looper ++) 341 { 342 assertTrue("testAddAll", list2.contains(values[looper])); 343 } 344 } 345 catch (Exception e) 346 { 347 fail("testAddAll " + e.getMessage()); 348 throw e; 349 } 350 351 } 352 353 public void testClear() throws Exception 354 { 355 LinkedList list = new LinkedList (); 356 LinkedList list2 = new LinkedList (); 357 list2.add("java"); 358 list2.add("c"); 359 list2.add("lisp"); 360 list2.add("c++"); 361 list2.add("forth"); 362 363 try 364 { 365 cache_.attach("/language/list", list); 366 list = (LinkedList ) cache_.find("/language/list"); 367 assertTrue(list.addAll(list2)); 368 assertTrue("testClear", list.size() > 0); 369 list.clear(); 370 assertTrue("testClear", list.size() == 0); 371 } 372 catch (Exception e) 373 { 374 fail("testClear " + e.getMessage()); 375 throw e; 376 } 377 378 } 379 380 public void testRetainAll() throws Exception 381 { 382 LinkedList list = new LinkedList (); 383 LinkedList list2 = new LinkedList (); 384 list2.add("java"); 385 list2.add("c"); 386 list2.add("lisp"); 387 list2.add("c++"); 388 list2.add("forth"); 389 390 try 391 { 392 cache_.attach("/language/list", list); 393 list = (LinkedList ) cache_.find("/language/list"); 394 assertTrue(list.addAll(list2)); 395 list2.remove("c"); 396 list2.remove("lisp"); 397 list2.remove("c++"); 398 list2.remove("forth"); 399 assertTrue("testRetainAll", list.retainAll(list2)); 400 assertTrue("testRetainAll, list size should be 1 but is " + list.size(), list.size() == 1); 402 assertTrue("testRetainAll", list.contains("java")); 403 } 404 catch (Exception e) 405 { 406 fail("testRetainAll " + e.getMessage()); 407 throw e; 408 } 409 410 411 } 412 413 public void testRemoveAll() throws Exception 414 { 415 LinkedList list = new LinkedList (); 416 LinkedList list2 = new LinkedList (); 417 list2.add("java"); 418 list2.add("c"); 419 list2.add("lisp"); 420 list2.add("c++"); 421 list2.add("forth"); 422 423 try 424 { 425 cache_.attach("/language/list", list); 426 list = (LinkedList ) cache_.find("/language/list"); 427 assertTrue(list.addAll(list2)); 428 list2.remove("java"); 429 assertTrue("testRemoveAll", list.removeAll(list2)); 430 assertTrue("testRemoveAll", list.size() == 1); 432 assertTrue("testRemoveAll", list.contains("java")); 433 } 434 435 catch (Exception e) 436 { 437 fail("testRemoveAll " + e.getMessage()); 438 throw e; 439 } 440 441 442 } 443 444 public void testContainsAll() throws Exception 445 { 446 LinkedList list = new LinkedList (); 447 LinkedList list2 = new LinkedList (); 448 list2.add("java"); 449 list2.add("c"); 450 list2.add("lisp"); 451 list2.add("c++"); 452 list2.add("forth"); 453 454 try 455 { 456 cache_.attach("/language/list", list); 457 list = (LinkedList ) cache_.find("/language/list"); 458 assertTrue(list.addAll(list2)); 459 list2.remove("java"); 460 assertTrue("testContainsAll", list.containsAll(list2)); 461 list.remove("c"); 462 assertFalse("testContainsAll", list.containsAll(list2)); 463 } 464 catch (Exception e) 465 { 466 fail("testContainsAll " + e.getMessage()); 467 throw e; 468 } 469 } 470 471 } 472 473 | Popular Tags |