1 22 23 package org.jboss.cache.pojo.optimistic; 24 25 import junit.framework.TestCase; 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.jboss.cache.pojo.PojoCache; 31 import org.jboss.cache.pojo.PojoCacheFactory; 32 import org.jboss.cache.pojo.test.Address; 33 34 import java.util.List ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.ListIterator ; 38 import java.util.NoSuchElementException ; 39 40 45 46 public class CachedListTest extends AbstractOptimisticTestCase 47 { 48 Log log = LogFactory.getLog(CachedListTest.class); 49 PojoCache cache_; 50 List languages; 51 List languages2; 52 53 public CachedListTest(String name) 54 { 55 super(name); 56 } 57 58 59 protected void setUp() throws Exception 60 { 61 super.setUp(); 62 cache_ = createCache(); 63 log.info("setUp() ...."); 65 } 66 67 protected void tearDown() 68 { 69 super.tearDown(); 70 cache_.stop(); 71 } 72 73 public void testAddAndRemoveIndex() throws Throwable 74 { 75 stage(); 76 77 languages.add(1, "Taiwanese"); 78 assertEquals("Languages size ", 4, languages.size()); 79 assertEquals("Language ", (Object ) "Taiwanese", (Object ) languages.get(1)); 80 languages.remove(2); 81 assertEquals("Languages size ", 3, languages.size()); 82 assertEquals("Language ", (Object ) "English", (Object ) languages.get(2)); 83 84 languages.add("Mandarin"); 85 assertEquals("Languages size ", 4, languages.size()); 86 languages.remove("Mandarin"); 87 assertEquals("Languages size ", 3, languages.size()); 88 } 89 90 protected void stage() throws Throwable 91 { 92 languages = new ArrayList (); 93 languages.add("English"); 94 languages.add("French"); 95 languages.add("English"); 96 cache_.attach("/person/test6", languages); 97 languages = (List ) cache_.find("/person/test6"); 98 int size = languages.size(); 99 assertEquals("Size of list ", 3, size); 100 101 languages2 = new ArrayList (); 102 languages2.addAll(languages); 103 assertEquals("New ArrayList().addAll(CachedList)", languages, languages2); 104 } 105 106 public void testAddAllAndClear() throws Throwable 107 { 108 stage(); 109 List list = new ArrayList (); 110 list.add("Taiwanese"); 111 list.add("Madarin"); 112 113 assertTrue("Language is Taiwanese ", list.contains("Taiwanese")); 114 115 languages.addAll(list); 116 assertEquals("Languages size ", 5, languages.size()); 117 118 languages.removeAll(list); 119 assertEquals("Languages size ", 3, languages.size()); 120 121 assertEquals("Index of French ", 1, languages.indexOf("French")); 122 123 languages.clear(); 124 assertEquals("Languages size ", 0, languages.size()); 125 126 assertTrue("Languages empty ", languages.isEmpty()); 127 } 128 129 public void testEquals() throws Throwable 130 { 131 stage(); 132 133 List list = (List ) cache_.find("/person/test6"); 134 assertTrue("List should be the same ", list.equals(languages)); 135 list = new ArrayList (); 136 list.add("German"); 137 list.add("test"); 138 list.add("English"); 139 assertFalse("List should not be the same ", languages.equals(list)); 140 assertFalse("List should not be the same ", list.equals(languages)); 141 } 142 143 public void testIterator() throws Throwable 144 { 145 languages = new ArrayList (); 146 Iterator it0 = languages.iterator(); 147 assertFalse("Iterator should be empty ", it0.hasNext()); 148 149 stage(); 150 151 Iterator it = languages.iterator(); 152 Iterator it2 = languages2.iterator(); 153 int counter = 0; 154 while (it.hasNext()) 155 { 156 counter++; 157 assertEquals(it.next(), it2.next()); 158 it.remove(); 159 it2.remove(); 160 } 161 162 assertEquals("Size should be ", 3, counter); 163 assertEquals("Skills should be empty ", 0, languages.size()); 164 } 165 166 public void testListIterator() throws Throwable 167 { 168 languages = new ArrayList (); 169 ListIterator it0 = languages.listIterator(); 170 assertFalse("Iterator should be empty ", it0.hasNext()); 171 assertFalse("Iterator should be empty ", it0.hasPrevious()); 172 173 stage(); 174 175 ListIterator li = languages.listIterator(); 176 ListIterator li2 = languages2.listIterator(); 177 assertFalse("LI has no previous element ", li.hasPrevious()); 178 assertFalse("LI2 has no previous element ", li2.hasPrevious()); 179 assertTrue("LI has next element ", li.hasNext()); 180 assertTrue("LI2 has next element ", li2.hasNext()); 181 assertEquals(li.next(), li2.next()); 182 assertEquals("Index is ", 1, li.nextIndex()); 183 assertEquals("Index is ", 1, li2.nextIndex()); 184 assertEquals("Index is ", 0, li.previousIndex()); 185 assertEquals("Index is ", 0, li2.previousIndex()); 186 assertEquals(li.next(), li2.next()); 187 assertEquals(li.next(), li2.next()); try 189 { 190 li.next(); 191 fail("Should throw an exception here "); 192 } 193 catch (NoSuchElementException ex) 194 { 195 ; 196 } 197 try 198 { 199 li2.next(); 200 fail("Should throw an exception here "); 201 } 202 catch (NoSuchElementException ex) 203 { 204 ; 205 } 206 207 assertEquals("Index is ", 3, li.nextIndex()); 208 assertEquals("Index is ", 3, li2.nextIndex()); 209 assertEquals("Index is ", 2, li.previousIndex()); 210 assertEquals("Index is ", 2, li2.previousIndex()); 211 li.previous(); 212 li2.previous(); 213 assertEquals("Index is ", 2, li.nextIndex()); 214 assertEquals("Index is ", 2, li2.nextIndex()); 215 assertEquals("Index is ", 1, li.previousIndex()); 216 assertEquals("Index is ", 1, li2.previousIndex()); 217 li.previous(); 218 li2.previous(); 219 li.previous(); 220 li2.previous(); 221 222 try 223 { 224 li.previous(); 225 fail("Should throw an exception here "); 226 } 227 catch (NoSuchElementException ex) 228 { 229 ; 230 } 231 232 try 233 { 234 li2.previous(); 235 fail("Should throw an exception here "); 236 } 237 catch (NoSuchElementException ex) 238 { 239 ; 240 } 241 242 try 243 { 244 assertEquals(li.next(), li2.next()); 245 li.remove(); 246 li2.remove(); 247 } 248 catch (Exception e) 249 { 250 fail("ListIterator.remove failed" + e); 251 } 252 253 254 try 255 { 256 assertEquals(li.next(), li2.next()); 257 li.remove(); 258 li2.remove(); 259 } 260 catch (Exception e) 261 { 262 fail("ListIterator.remove failed" + e); 263 } 264 265 try 266 { 267 assertEquals(li.next(), li2.next()); 268 assertEquals("ListIterator.remove test problem with nextIndex, cache next index=" + li.nextIndex() + 269 ", jdk next index=" + li2.nextIndex() + "cache list size = " + languages.size() + ", jdk list size = " + languages.size(), 270 li.nextIndex(), li2.nextIndex()); 271 li2.set("German"); 272 li.set("German"); 273 String s1 = (String ) li.previous(); 274 String s2 = (String ) li2.previous(); 275 assertEquals(s1, s2); 276 assertEquals(s2, "German"); 277 } 278 catch (Exception e) 279 { 280 fail("ListIterator.remove failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size()); 281 } 282 283 try 284 { 285 assertEquals(li.next(), li2.next()); 286 li2.add("Vulcan"); 287 li.add("Vulcan"); 288 String s1 = (String ) li.previous(); 289 String s2 = (String ) li2.previous(); 290 assertEquals(s1, s2); 291 assertEquals(s2, "Vulcan"); 292 } 293 catch (Exception e) 294 { 295 fail("ListIterator.add failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size()); 296 } 297 298 } 299 300 301 public void testAttachAndDetach() throws Exception 302 { 303 List list = new ArrayList (); 304 list.add("English"); 305 list.add("French"); 306 list.add("Taiwanese"); 307 308 cache_.attach("/test", list); list = (List ) cache_.find("/test"); 310 assertEquals("Size ", 3, list.size()); 311 312 list = (List ) cache_.detach("/test"); 313 assertEquals("Size ", 3, list.size()); 314 315 System.out.println("**** End of cache content **** "); 316 list.remove(2); 317 list.add("Hoklo"); 318 assertEquals("Size ", 3, list.size()); 319 assertEquals("Content ", "Hoklo", list.get(2)); 320 321 cache_.attach("/test", list); 323 list.remove(2); 324 assertEquals("Size ", 2, list.size()); 325 } 326 327 public void testPojoAttachAndDetach() throws Exception 328 { 329 Address add1 = new Address(); 330 add1.setCity("San Jose"); 331 add1.setZip(95123); 332 333 Address add2 = new Address(); 334 add2.setCity("Sunnyvale"); 335 add2.setZip(94086); 336 337 Address add3 = new Address(); 338 add3.setCity("Santa Clara"); 339 add3.setZip(951131); 340 341 List list = new ArrayList (); 342 list.add(add1); 343 list.add(add2); 344 list.add(add3); 345 346 cache_.attach("/test", list); list = (List ) cache_.find("/test"); 348 assertEquals("Size ", 3, list.size()); 349 350 list = (List ) cache_.detach("/test"); 351 assertEquals("Size ", 3, list.size()); 352 353 System.out.println("**** End of cache content **** "); 354 list.remove(2); 355 list.add(add2); 356 assertEquals("Size ", 3, list.size()); 357 assertEquals("Content ", add2, list.get(2)); 358 359 cache_.attach("/test", list); 361 list.remove(2); 362 assertEquals("Size ", 2, list.size()); 363 } 364 365 public void testEqual1() throws Exception 366 { 367 List list1 = new ArrayList (); 368 list1.add("ID1"); 369 list1.add("ID2"); 370 cache_.attach("test1", list1); 371 list1 = (List )cache_.find("test1"); 372 373 List list2 = new ArrayList (); 374 list2.add("ID1"); 375 list2.add("ID2"); 376 cache_.attach("test2", list2); 377 list2 = (List )cache_.find("test2"); 378 379 List list3 = new ArrayList (); 380 list3.add("ID2"); 381 list3.add("ID1"); 382 cache_.attach("test3", list3); 383 list3 = (List )cache_.find("test3"); 384 385 assertEquals("List should be equal: ", list1, list1); 386 assertTrue("List should be equal: ", list1.equals(list1)); 387 assertTrue("List should be equal: ", list1.equals(list2)); 388 assertFalse("List should not be equal: ", list1.equals(list3)); 389 } 390 391 public void testEqual2() throws Exception 392 { 393 List list1 = new ArrayList (); 394 cache_.attach("test1", list1); 395 list1 = (List )cache_.find("test1"); 396 list1.add("ID1"); 397 list1.add("ID2"); 398 399 List list2 = new ArrayList (); 400 cache_.attach("test2", list2); 401 list2 = (List )cache_.find("test2"); 402 list2.add("ID1"); 403 list2.add("ID2"); 404 405 List list3 = new ArrayList (); 406 cache_.attach("test3", list3); 407 list3 = (List )cache_.find("test3"); 408 list3.add("ID2"); 409 list3.add("ID1"); 410 411 assertEquals("List should be equal: ", list1, list1); 412 assertTrue("List should be equal: ", list1.equals(list1)); 413 assertTrue("List should be equal: ", list1.equals(list2)); 414 assertFalse("List should not be equal: ", list1.equals(list3)); 415 } 416 417 public static Test suite() throws Exception 418 { 419 return new TestSuite(CachedListTest.class); 420 } 421 422 public static void main(String [] args) throws Exception 423 { 424 junit.textui.TestRunner.run(suite()); 425 } 426 427 } 428 | Popular Tags |