1 package javatests; 3 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.Collections ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.ListIterator ; 11 import java.util.NoSuchElementException ; 12 13 16 public abstract class ListTest { 17 18 public static ListTest getArrayListTest(final boolean makeReadOnly) { 19 return new ListTest() { 20 public List newInstance(Collection c) { 21 List l = null; 22 if(c == null) { 23 l = new ArrayList (); 24 } else { 25 l = new ArrayList (c); 26 } 27 return (makeReadOnly) 28 ? Collections.unmodifiableList(l) 29 : l; 30 } 31 public boolean isReadOnly() { 32 return makeReadOnly; 33 } 34 }; 35 } 36 37 public static void verifyImutability(List l) { 38 39 String message = "Expected UnsupportedOperationException."; 40 41 try { 42 l.add(0, new Integer (0)); 43 TestSupport.assertThat(false, message); 44 } catch (UnsupportedOperationException e) {} 45 46 try { 47 l.add(new Integer (0)); 48 TestSupport.assertThat(false, message); 49 } catch (UnsupportedOperationException e) {} 50 51 try { 52 l.addAll(null); 53 TestSupport.assertThat(false, message); 54 } catch (UnsupportedOperationException e) {} 55 56 try { 57 l.addAll(0, null); 58 TestSupport.assertThat(false, message); 59 } catch (UnsupportedOperationException e) {} 60 61 try { 62 l.clear(); 63 TestSupport.assertThat(false, message); 64 } catch (UnsupportedOperationException e) {} 65 66 try { 67 l.remove(0); 68 TestSupport.assertThat(false, message); 69 } catch (UnsupportedOperationException e) {} 70 71 try { 72 l.remove(new Object ()); 73 TestSupport.assertThat(false, message); 74 } catch (UnsupportedOperationException e) {} 75 76 try { 77 l.removeAll(null); 78 TestSupport.assertThat(false, message); 79 } catch (UnsupportedOperationException e) {} 80 81 try { 82 l.retainAll(null); 83 TestSupport.assertThat(false, message); 84 } catch (UnsupportedOperationException e) {} 85 try { 86 l.set(0,new Integer (0)); 87 TestSupport.assertThat(false, message); 88 } catch (UnsupportedOperationException e) {} 89 } 90 91 private final List nullList; 92 93 protected List defaultList() { 94 List l = new ArrayList (); 95 for (int i = 0; i < 4; i++) { 96 l.add(new Integer (i)); 97 } 98 return newInstance(l); 99 } 100 101 106 public List newInstance(Collection c) { 107 throw new UnsupportedOperationException ("This method must be overridden"); 108 } 109 110 113 public boolean isReadOnly() { 114 throw new UnsupportedOperationException ("This method must be overridden"); 115 } 116 117 { 118 nullList = newInstance(Arrays.asList(new Object [] {null})); 119 } 120 121 public void testAll() { 122 123 test_get(); 124 test_equals(); 125 test_size(); 126 test_contains(); 127 test_containsAll(); 128 129 try { 130 defaultList().hashCode(); 131 test_hashCode(); 132 } catch (Exception e) { 133 } 135 136 test_subList(); 137 test_lastIndexOf(); 138 test_listIterator(); 139 test_toArray(); 140 test_toArray_typed(); 141 142 if(!isReadOnly()) { 143 test_add(); 144 test_add_index(); 145 test_set(); 146 test_clear(); 147 test_addAll(); 148 test_addAll_index(); 149 test_remove(); 150 test_remove_index(); 151 test_removeAll(); 152 test_retainAll(); 153 } else { 154 verifyImutability(newInstance(null)); 155 } 156 } 157 158 159 public void test_get() { 160 TestSupport.assertThat(defaultList().get(0).equals(new Integer (0)), 161 "get() did not return expected value of Integer(0)"); 162 try { 163 defaultList().get(-1); 164 TestSupport.assertThat(false, 165 "get(<negative index>) did not throw IndexOutOfBoundsException"); 166 } catch (IndexOutOfBoundsException e) {} 167 168 try { 169 defaultList().get(-1); 170 TestSupport.assertThat(false, 171 "get(<index too big>) did not throw IndexOutOfBoundsException"); 172 } catch (IndexOutOfBoundsException e) {} 173 } 174 175 176 public void test_set() { 177 178 try { 179 newInstance(null).set(-1, "spam"); 180 TestSupport.assertThat(false, 181 "get(<negative index>) did not throw IndexOutOfBoundsException"); 182 } catch (IndexOutOfBoundsException e) {} 183 184 try { 185 newInstance(null).set(0, "spam"); 186 TestSupport.assertThat(false, 187 "set(<index too big>) did not throw IndexOutOfBoundsException"); 188 } catch (IndexOutOfBoundsException e) {} 189 190 List a = defaultList(); 191 a.set(a.size() - 1 , "spam"); 192 TestSupport.assertThat(a.get(a.size() - 1).equals("spam"), 193 "set() object was not retrieved via get()"); 194 } 195 196 197 public void test_add() { 198 List a = newInstance(null); 199 for (int i = 0; i < 4; i++) { 200 a.add(new Integer (i)); 201 } 202 TestSupport.assertEquals(a, defaultList(), "add(Object o) failed"); 203 } 204 205 206 public void test_isEmpty() { 207 List a = newInstance(null); 208 TestSupport.assertThat(a.isEmpty(), 209 "isEmpty() is false on an emtpy List"); 210 a.addAll(defaultList()); 211 TestSupport.assertThat(!a.isEmpty(), 212 "isEmpty() is true on a non-empty List)" ); 213 a.clear(); 214 TestSupport.assertThat(a.isEmpty(), 215 "isEmpty() is false on an emtpy List"); 216 } 217 218 219 public void test_size() { 220 List b = newInstance(null); 221 TestSupport.assertThat(b.size() == 0, "empty list size was not 0"); 222 TestSupport.assertThat(defaultList().size() == 4, 223 "default list did not have a size of 4"); 224 } 225 226 227 public void test_add_index() { 228 List a = newInstance(null); 229 List b = defaultList(); 230 for (int i = 0; i < b.size(); i++) { 231 a.add(i, b.get(i)); 232 } 233 234 try { 235 a.add(a.size() + 1, new Integer (a.size() + 1)); 236 TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); 237 } catch (IndexOutOfBoundsException e) {} 238 239 try { 240 a.add(-1, new Integer (-1)); 241 TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); 242 } catch (IndexOutOfBoundsException e) {} 243 } 244 245 246 public void test_equals() { 247 TestSupport.assertEquals(defaultList(), defaultList(), 248 "Identical lists weren't equal()"); 249 TestSupport.assertNotEquals(newInstance(null), defaultList(), 250 "Different lists were equal()"); 251 TestSupport.assertNotEquals(newInstance(null), new Object (), 252 "List was equal to a non-List type"); 253 } 254 255 256 public void test_addAll() { 257 List a = defaultList(); 258 List b = defaultList(); 259 260 TestSupport.assertThat(a.addAll(b) == true, 261 "Mutating addAll(Collection) returned false"); 262 TestSupport.assertThat(a.addAll(newInstance(null)) == false, 263 "Idempotent addAll(Collection) returned true"); 264 TestSupport.assertThat(b.addAll(b) == true, 265 "Mutating addAll(Collection) returned false"); 266 TestSupport.assertEquals(a, b, 267 "Expected equal objects from addAll(collection)"); 268 TestSupport.assertThat(a.size() == 8, 269 "Expected List to have size 8 after addAll(Collection)"); 270 } 271 272 273 public void indexOf() { 274 TestSupport.assertThat(defaultList().indexOf(new Integer (3)) == 3, 275 "indexOf(3) did not return 3"); 276 TestSupport.assertThat(defaultList().indexOf(new Integer (42)) == -1, 277 "indexOf() non-existing entry did not return -1"); 278 TestSupport.assertThat(defaultList().indexOf(null) == -1, 279 "indexOf() non-existing null did not return -1"); 280 281 } 282 283 284 public void test_contains() { 285 TestSupport.assertThat(defaultList().contains(new Integer (42)) == false, 286 "contains() returned true for non-existing entry"); 287 TestSupport.assertThat(defaultList().contains(new Integer (0)) == true, 288 "contains() returned false for existing entry"); 289 TestSupport.assertThat(nullList.contains(null) == true, 290 "contains() returned false for existing null entry"); 291 TestSupport.assertThat(defaultList().contains(null) == false, 292 "contains() returned true for non-existing null entry"); 293 } 294 295 296 public void test_remove() { 297 List a = defaultList(); 298 a.add(null); 299 TestSupport.assertThat(a.remove(null) == true, 300 "remove() existing null entry returned false"); 301 TestSupport.assertThat(a.remove(null) == false, 302 "remove() non-existing null entry returned false"); 303 a.add("spam"); 304 TestSupport.assertThat(a.remove("spam") == true, 305 "remove() existing entry returned false"); 306 TestSupport.assertThat(a.remove("spam") == false, 307 "remove() non-existing entry returned true"); 308 } 309 310 311 312 public void test_remove_index() { 313 314 List a = defaultList(); 315 for (int i = 0, n = a.size(); i < n; i++) { 316 a.remove(0); 317 } 318 TestSupport.assertThat(a.size() == 0, 319 "remove()-d all entries but size() not 0"); 320 321 try { 322 a.remove(0); 323 TestSupport.assertThat(false, 324 "removing a non-existing index did not throw exception"); 325 } catch(IndexOutOfBoundsException e) {} 326 } 327 328 329 public void test_lastIndexOf() { 330 List l = new ArrayList (defaultList()); 332 l.add(new Integer (0)); 333 334 List a = newInstance(l); 336 337 TestSupport.assertThat(a.lastIndexOf(new Integer (0)) == 4, 338 "lastIndexOf() did not return 4"); 339 TestSupport.assertThat(a.lastIndexOf(new Integer (42)) == -1, 340 "lastIndexOf() non-existing value did not return -1"); 341 } 342 343 344 public void test_removeAll() { 345 List a = defaultList(); 346 TestSupport.assertThat(a.removeAll(a) == true, 347 "mutating removeAll() did not return true"); 348 TestSupport.assertThat(a.removeAll(a) == false, 349 "idempotent removeAll did not return false"); 350 TestSupport.assertThat(a.removeAll(nullList) == false, 351 "idempotent removeAll did not return false"); 352 353 List yanl = newInstance(null); 354 yanl.addAll(nullList); 355 TestSupport.assertThat(yanl.removeAll(nullList) == true, 356 "mutating removeAll() did not return true"); 357 TestSupport.assertThat(yanl.size() == 0, 358 "empty list had non-zero size"); 359 TestSupport.assertThat(yanl.removeAll(newInstance(null)) == false, 360 "idempotent removeAll did not return false"); 361 362 } 363 364 365 public void test_addAll_index() { 366 List a = defaultList(); 367 List b = newInstance(null); 368 TestSupport.assertThat(b.addAll(0,a) == true, 369 "mutating addAll(index, Collection) did not return true"); 370 TestSupport.assertEquals(a, b, 371 "addAll(index, Collection) instances failed equals test"); 372 TestSupport.assertThat(a.addAll(0, newInstance(null)) == false, 373 "idempotent addAll(index, Collection) did not return false"); 374 TestSupport.assertThat(b.addAll(0,b) == true, 375 "mutating addAll(index, Collection) did not return true"); 376 377 b = newInstance(null); 380 b.addAll(new ArrayList (defaultList())); 381 TestSupport.assertEquals(defaultList(), b, 382 "addAll(index, <ArrayList>) failed equals test"); 383 } 384 385 386 387 public void test_hashCode() { 388 List a = defaultList(); 389 TestSupport.assertThat(a.hashCode() == defaultList().hashCode(), 390 "Instances with same internal state have different hashcode"); 391 TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(), 392 "Instances with different internal state have the same hashcode"); 393 394 if (isReadOnly() == false) { 395 List b = newInstance(null); 396 b.addAll(a); 397 b.remove(0); 398 TestSupport.assertThat(a.hashCode()!= b.hashCode(), 399 "Instances with different internal state have the same hashcode"); 400 } 401 402 } 403 404 405 public void test_clear() { 406 List a = defaultList(); 407 a.clear(); 408 TestSupport.assertThat(a.size() == 0, 409 "clear()-ed list did not have size of 0"); 410 } 411 412 413 public void test_subList() { 414 List a = defaultList(); 415 TestSupport.assertThat((a.subList(0, a.size()) != a), 416 "subList() returned the same instance"); 417 TestSupport.assertEquals(a.subList(0, a.size()), a, 418 "Complete subList() did not equal original List"); 419 TestSupport.assertThat(a.subList(0,0).size() == 0, 420 "empty subList had non-zero size"); 421 422 try { 423 a.subList(-1,1); 424 TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); 425 } catch (IndexOutOfBoundsException e) {} 426 427 try { 428 a.subList(1,0); 429 TestSupport.assertThat(false, "Expected IllegalArgumentException"); 430 } catch (IllegalArgumentException e) {} 431 432 try { 433 a.subList(0,a.size() + 1); 434 TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); 435 } catch (IndexOutOfBoundsException e) {} 436 437 if (!isReadOnly()) { 438 439 a.subList(0, a.size()).clear(); 440 TestSupport.assertThat(a.size() == 0, 441 "clear()-ed sublist did not have zero size"); 442 List c = newInstance(null); 443 c.addAll(defaultList()); 444 List d = c.subList(1,3); 445 TestSupport.assertThat(d.size() == 2, 446 "Expected subList to have size of 2"); 447 TestSupport.assertThat(c.set(1,"canned").equals(new Integer (1)), 448 "subList.set() did not return Integer(1) from index 1" + 449 " of defaultList"); 450 TestSupport.assertThat(d.get(0).equals("canned"), 451 "subList does not update with changes to parent"); 452 d.set(0,"spam"); 453 TestSupport.assertThat(c.get(1).equals("spam"), 454 "parent does not update with changes to subList child"); 455 } else { 456 List b = a.subList(0, a.size()); 457 verifyImutability(b); 458 } 459 460 } 461 462 463 public void test_retainAll() { 464 List a = defaultList(); 465 a.retainAll(defaultList()); 466 TestSupport.assertEquals(a, defaultList(), 467 "retainAll(<equal List>) does not equal original list"); 468 a = defaultList(); 469 a.retainAll(newInstance(null)); 470 TestSupport.assertThat(a.size() == 0, 471 "retainAll(<empty List>))does not have size of zero"); 472 473 a = defaultList(); 474 a.remove(0); 475 a.remove(0); 476 a.add(new Integer (4)); 477 a.add(new Integer (5)); 478 List b = newInstance(null); 479 b.add(new Integer (2)); 480 b.add(new Integer (3)); 481 a.retainAll(b); 482 TestSupport.assertEquals(a, b, 483 "retainAll() on overlap of indices [2,3] did not return that List"); 484 } 485 486 487 public void test_containsAll() { 488 TestSupport.assertThat(defaultList().containsAll(defaultList()), 489 "containsAll(<identical List> was false"); 490 TestSupport.assertThat(defaultList().containsAll(newInstance(null)), 491 "containsAll(<empty List>) was false"); 492 TestSupport.assertThat(newInstance(null).containsAll(defaultList()) == false, 493 "containsAll(<disjoint List>) returned true"); 494 TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1,3)), 495 "containsAll(<subList>) was false"); 496 } 497 498 499 public void test_iterator() { 500 501 TestSupport.assertThat(newInstance(null).iterator().hasNext() == false, 502 "Iterator for empty list thinks it hasNext()"); 503 try { 504 newInstance(null).iterator().next(); 505 TestSupport.assertThat(false, "expected NoSuchElementException"); 506 } catch (NoSuchElementException e) {} 507 508 List a = defaultList(); 509 int i = 0; 510 for (Iterator iter = a.iterator(); iter.hasNext(); ) { 511 TestSupport.assertThat(iter.next() == a.get(i++), 512 "Iterator next() failed identity test"); 513 } 514 TestSupport.assertThat(i == a.size(), 515 "Iterator did not iterator over entire list"); 516 } 517 518 public void test_listIterator() { 519 520 ListIterator li = newInstance(null).listIterator(); 521 TestSupport.assertThat(li.hasNext() == false, 522 "ListIterator.hasNext() is true for empty List"); 523 524 TestSupport.assertThat(li.hasPrevious() == false, 525 "ListIterator.hasPrevious() is true for empty List"); 526 527 try { 528 li.next(); 529 TestSupport.assertThat(false, "expected NoSuchElementException"); 530 } catch (NoSuchElementException e) {} 531 532 try { 533 li.previous(); 534 TestSupport.assertThat(false, "expected NoSuchElementException"); 535 } catch (NoSuchElementException e) {} 536 537 int nextIndex = li.nextIndex(); 539 TestSupport.assertThat(nextIndex == -1 || nextIndex == 0, 540 "ListIterator.nextIndex() on empty List did not return 0 " + 541 "(java 1.4) or did not return -1 (java 1.3)"); 542 543 int prevIndex = li.previousIndex(); 545 TestSupport.assertThat(prevIndex == -1 || prevIndex == -2, 546 "ListIterator.previousIndex() on empty List did not return -1 " + 547 "(java 1.4) or -2 (java 1.3)"); 548 549 List l = new ArrayList (); 550 l.add(new Integer (1)); 551 li = newInstance(l).listIterator(); 552 TestSupport.assertThat(li.hasPrevious() == false, 553 "ListIterator.hasPrevious() is true with nothing previous"); 554 555 TestSupport.assertThat(li.hasNext() == true, 556 "ListIterator.hasNext() is false with next present"); 557 TestSupport.assertThat(li.next().equals(new Integer (1)), 558 "ListIterator.next() did not return expected Integer(1)"); 559 560 if (!isReadOnly()) { 561 li.remove(); 562 TestSupport.assertThat(li.hasNext() == false, 563 "ListIterator.hasNext() is true for empty List"); 564 565 TestSupport.assertThat(li.hasPrevious() == false, 566 "ListIterator.hasPrevious() is true for empty List"); 567 try { 568 li.set(new Integer (42)); 569 TestSupport.assertThat(false, "expected IllegalStateException"); 570 } catch (IllegalStateException e) {} 571 572 try { 573 li.remove(); 574 TestSupport.assertThat(false, "expected IllegalStateException"); 575 } catch (IllegalStateException e) {} 576 } 577 578 l = new ArrayList (); 579 l.add(new Integer (0)); 580 l.add(new Integer (1)); 581 l.add(new Integer (2)); 582 583 li = newInstance(l).listIterator(); 584 585 for (int i = 0, n = l.size(); i < n; i++) { 586 TestSupport.assertThat(li.next().equals(new Integer (i)), 587 "ListIterator.previous did not return expected value"); 588 } 589 590 while (!isReadOnly() && li.hasNext()) { 591 li.next(); 592 li.set(new Integer (42)); 593 TestSupport.assertThat(li.previous().equals(new Integer (42)), 594 "ListIterator.previous() did not return the value that was set()"); 595 li.remove(); 596 } 597 598 if(isReadOnly()) { 599 li = newInstance(null).listIterator(); 600 } 601 602 li = defaultList().listIterator(2); 603 TestSupport.assertThat(li.next().equals(new Integer (2)), 604 "List.listIteraor(index) did not return expected value"); 605 TestSupport.assertThat(li.next().equals(new Integer (3)), 606 "List.listIteraor(index) did not return expected value"); 607 TestSupport.assertThat(li.hasNext() == false, 608 "listIterator.hasNext() at end of list returned true"); 609 610 } 611 612 613 public void test_toArray() { 614 Object [] intObjArray = new Integer [] { 615 new Integer (0), new Integer (1), new Integer (2), new Integer (3)}; 616 TestSupport.assertThat(Arrays.equals(defaultList().toArray(), intObjArray), 617 "toArray() did not return the expected Integer[] array"); 618 } 619 620 621 public void test_toArray_typed() { 622 Object [] intObjArray = new Integer [] { 623 new Integer (0), new Integer (1), new Integer (2), new Integer (3)}; 624 TestSupport.assertThat(Arrays.equals( 625 defaultList().toArray(new Integer [] {}), intObjArray), 626 "toArray(Integer[]) did not return the expected Integer[] array"); 627 } 628 629 } 639 | Popular Tags |