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