1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23/* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 * Other contributors include Andrew Wright, Jeffrey Hayes, 33 * Pat Fisher, Mike Judd. 34 */ 35 36import java.util.ArrayList; 37import java.util.Arrays; 38import java.util.Collection; 39import java.util.Iterator; 40import java.util.LinkedList; 41import java.util.List; 42import java.util.ListIterator; 43import java.util.NoSuchElementException; 44import java.util.concurrent.CopyOnWriteArrayList; 45 46import junit.framework.Test; 47import junit.framework.TestSuite; 48 49public class CopyOnWriteArrayListTest extends JSR166TestCase { 50 51 public static void main(String[] args) { 52 main(suite(), args); 53 } 54 55 public static Test suite() { 56 class Implementation implements CollectionImplementation { 57 public Class<?> klazz() { return CopyOnWriteArrayList.class; } 58 public List emptyCollection() { return new CopyOnWriteArrayList(); } 59 public Object makeElement(int i) { return i; } 60 public boolean isConcurrent() { return true; } 61 public boolean permitsNulls() { return true; } 62 } 63 class SubListImplementation extends Implementation { 64 public List emptyCollection() { 65 return super.emptyCollection().subList(0, 0); 66 } 67 } 68 return newTestSuite( 69 CopyOnWriteArrayListTest.class, 70 CollectionTest.testSuite(new Implementation()), 71 CollectionTest.testSuite(new SubListImplementation())); 72 } 73 74 static CopyOnWriteArrayList<Integer> populatedArray(int n) { 75 CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); 76 assertTrue(a.isEmpty()); 77 for (int i = 0; i < n; i++) 78 a.add(i); 79 assertFalse(a.isEmpty()); 80 assertEquals(n, a.size()); 81 return a; 82 } 83 84 static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { 85 CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); 86 assertTrue(a.isEmpty()); 87 for (int i = 0; i < elements.length; i++) 88 a.add(elements[i]); 89 assertFalse(a.isEmpty()); 90 assertEquals(elements.length, a.size()); 91 return a; 92 } 93 94 /** 95 * a new list is empty 96 */ 97 public void testConstructor() { 98 CopyOnWriteArrayList a = new CopyOnWriteArrayList(); 99 assertTrue(a.isEmpty()); 100 } 101 102 /** 103 * new list contains all elements of initializing array 104 */ 105 public void testConstructor2() { 106 Integer[] ints = new Integer[SIZE]; 107 for (int i = 0; i < SIZE - 1; ++i) 108 ints[i] = new Integer(i); 109 CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); 110 for (int i = 0; i < SIZE; ++i) 111 assertEquals(ints[i], a.get(i)); 112 } 113 114 /** 115 * new list contains all elements of initializing collection 116 */ 117 public void testConstructor3() { 118 Integer[] ints = new Integer[SIZE]; 119 for (int i = 0; i < SIZE - 1; ++i) 120 ints[i] = new Integer(i); 121 CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); 122 for (int i = 0; i < SIZE; ++i) 123 assertEquals(ints[i], a.get(i)); 124 } 125 126 /** 127 * addAll adds each element from the given collection, including duplicates 128 */ 129 public void testAddAll() { 130 CopyOnWriteArrayList full = populatedArray(3); 131 assertTrue(full.addAll(Arrays.asList(three, four, five))); 132 assertEquals(6, full.size()); 133 assertTrue(full.addAll(Arrays.asList(three, four, five))); 134 assertEquals(9, full.size()); 135 } 136 137 /** 138 * addAllAbsent adds each element from the given collection that did not 139 * already exist in the List 140 */ 141 public void testAddAllAbsent() { 142 CopyOnWriteArrayList full = populatedArray(3); 143 // "one" is duplicate and will not be added 144 assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); 145 assertEquals(5, full.size()); 146 assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); 147 assertEquals(5, full.size()); 148 } 149 150 /** 151 * addIfAbsent will not add the element if it already exists in the list 152 */ 153 public void testAddIfAbsent() { 154 CopyOnWriteArrayList full = populatedArray(SIZE); 155 full.addIfAbsent(one); 156 assertEquals(SIZE, full.size()); 157 } 158 159 /** 160 * addIfAbsent adds the element when it does not exist in the list 161 */ 162 public void testAddIfAbsent2() { 163 CopyOnWriteArrayList full = populatedArray(SIZE); 164 full.addIfAbsent(three); 165 assertTrue(full.contains(three)); 166 } 167 168 /** 169 * clear removes all elements from the list 170 */ 171 public void testClear() { 172 CopyOnWriteArrayList full = populatedArray(SIZE); 173 full.clear(); 174 assertEquals(0, full.size()); 175 } 176 177 /** 178 * Cloned list is equal 179 */ 180 public void testClone() { 181 CopyOnWriteArrayList l1 = populatedArray(SIZE); 182 CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); 183 assertEquals(l1, l2); 184 l1.clear(); 185 assertFalse(l1.equals(l2)); 186 } 187 188 /** 189 * contains is true for added elements 190 */ 191 public void testContains() { 192 CopyOnWriteArrayList full = populatedArray(3); 193 assertTrue(full.contains(one)); 194 assertFalse(full.contains(five)); 195 } 196 197 /** 198 * adding at an index places it in the indicated index 199 */ 200 public void testAddIndex() { 201 CopyOnWriteArrayList full = populatedArray(3); 202 full.add(0, m1); 203 assertEquals(4, full.size()); 204 assertEquals(m1, full.get(0)); 205 assertEquals(zero, full.get(1)); 206 207 full.add(2, m2); 208 assertEquals(5, full.size()); 209 assertEquals(m2, full.get(2)); 210 assertEquals(two, full.get(4)); 211 } 212 213 /** 214 * lists with same elements are equal and have same hashCode 215 */ 216 public void testEquals() { 217 CopyOnWriteArrayList a = populatedArray(3); 218 CopyOnWriteArrayList b = populatedArray(3); 219 assertTrue(a.equals(b)); 220 assertTrue(b.equals(a)); 221 assertTrue(a.containsAll(b)); 222 assertTrue(b.containsAll(a)); 223 assertEquals(a.hashCode(), b.hashCode()); 224 a.add(m1); 225 assertFalse(a.equals(b)); 226 assertFalse(b.equals(a)); 227 assertTrue(a.containsAll(b)); 228 assertFalse(b.containsAll(a)); 229 b.add(m1); 230 assertTrue(a.equals(b)); 231 assertTrue(b.equals(a)); 232 assertTrue(a.containsAll(b)); 233 assertTrue(b.containsAll(a)); 234 assertEquals(a.hashCode(), b.hashCode()); 235 236 assertFalse(a.equals(null)); 237 } 238 239 /** 240 * containsAll returns true for collections with subset of elements 241 */ 242 public void testContainsAll() { 243 CopyOnWriteArrayList full = populatedArray(3); 244 assertTrue(full.containsAll(Arrays.asList())); 245 assertTrue(full.containsAll(Arrays.asList(one))); 246 assertTrue(full.containsAll(Arrays.asList(one, two))); 247 assertFalse(full.containsAll(Arrays.asList(one, two, six))); 248 assertFalse(full.containsAll(Arrays.asList(six))); 249 250 try { 251 full.containsAll(null); 252 shouldThrow(); 253 } catch (NullPointerException success) {} 254 } 255 256 /** 257 * get returns the value at the given index 258 */ 259 public void testGet() { 260 CopyOnWriteArrayList full = populatedArray(3); 261 assertEquals(0, full.get(0)); 262 } 263 264 /** 265 * indexOf gives the index for the given object 266 */ 267 public void testIndexOf() { 268 CopyOnWriteArrayList full = populatedArray(3); 269 assertEquals(1, full.indexOf(one)); 270 assertEquals(-1, full.indexOf("puppies")); 271 } 272 273 /** 274 * indexOf gives the index based on the given index 275 * at which to start searching 276 */ 277 public void testIndexOf2() { 278 CopyOnWriteArrayList full = populatedArray(3); 279 assertEquals(1, full.indexOf(one, 0)); 280 assertEquals(-1, full.indexOf(one, 2)); 281 } 282 283 /** 284 * isEmpty returns true when empty, else false 285 */ 286 public void testIsEmpty() { 287 CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); 288 CopyOnWriteArrayList full = populatedArray(SIZE); 289 assertTrue(empty.isEmpty()); 290 assertFalse(full.isEmpty()); 291 } 292 293 /** 294 * iterator() returns an iterator containing the elements of the 295 * list in insertion order 296 */ 297 public void testIterator() { 298 Collection empty = new CopyOnWriteArrayList(); 299 assertFalse(empty.iterator().hasNext()); 300 try { 301 empty.iterator().next(); 302 shouldThrow(); 303 } catch (NoSuchElementException success) {} 304 305 Integer[] elements = new Integer[SIZE]; 306 for (int i = 0; i < SIZE; i++) 307 elements[i] = i; 308 shuffle(elements); 309 Collection<Integer> full = populatedArray(elements); 310 311 Iterator it = full.iterator(); 312 for (int j = 0; j < SIZE; j++) { 313 assertTrue(it.hasNext()); 314 assertEquals(elements[j], it.next()); 315 } 316 assertIteratorExhausted(it); 317 } 318 319 /** 320 * iterator of empty collection has no elements 321 */ 322 public void testEmptyIterator() { 323 Collection c = new CopyOnWriteArrayList(); 324 assertIteratorExhausted(c.iterator()); 325 } 326 327 /** 328 * iterator.remove throws UnsupportedOperationException 329 */ 330 public void testIteratorRemove() { 331 CopyOnWriteArrayList full = populatedArray(SIZE); 332 Iterator it = full.iterator(); 333 it.next(); 334 try { 335 it.remove(); 336 shouldThrow(); 337 } catch (UnsupportedOperationException success) {} 338 } 339 340 /** 341 * toString contains toString of elements 342 */ 343 public void testToString() { 344 assertEquals("[]", new CopyOnWriteArrayList().toString()); 345 CopyOnWriteArrayList full = populatedArray(3); 346 String s = full.toString(); 347 for (int i = 0; i < 3; ++i) 348 assertTrue(s.contains(String.valueOf(i))); 349 assertEquals(new ArrayList(full).toString(), 350 full.toString()); 351 } 352 353 /** 354 * lastIndexOf returns the index for the given object 355 */ 356 public void testLastIndexOf1() { 357 CopyOnWriteArrayList full = populatedArray(3); 358 full.add(one); 359 full.add(three); 360 assertEquals(3, full.lastIndexOf(one)); 361 assertEquals(-1, full.lastIndexOf(six)); 362 } 363 364 /** 365 * lastIndexOf returns the index from the given starting point 366 */ 367 public void testLastIndexOf2() { 368 CopyOnWriteArrayList full = populatedArray(3); 369 full.add(one); 370 full.add(three); 371 assertEquals(3, full.lastIndexOf(one, 4)); 372 assertEquals(-1, full.lastIndexOf(three, 3)); 373 } 374 375 /** 376 * listIterator traverses all elements 377 */ 378 public void testListIterator1() { 379 CopyOnWriteArrayList full = populatedArray(SIZE); 380 ListIterator i = full.listIterator(); 381 int j; 382 for (j = 0; i.hasNext(); j++) 383 assertEquals(j, i.next()); 384 assertEquals(SIZE, j); 385 } 386 387 /** 388 * listIterator only returns those elements after the given index 389 */ 390 public void testListIterator2() { 391 CopyOnWriteArrayList full = populatedArray(3); 392 ListIterator i = full.listIterator(1); 393 int j; 394 for (j = 0; i.hasNext(); j++) 395 assertEquals(j + 1, i.next()); 396 assertEquals(2, j); 397 } 398 399 /** 400 * remove(int) removes and returns the object at the given index 401 */ 402 public void testRemove_int() { 403 int SIZE = 3; 404 for (int i = 0; i < SIZE; i++) { 405 CopyOnWriteArrayList full = populatedArray(SIZE); 406 assertEquals(i, full.remove(i)); 407 assertEquals(SIZE - 1, full.size()); 408 assertFalse(full.contains(new Integer(i))); 409 } 410 } 411 412 /** 413 * remove(Object) removes the object if found and returns true 414 */ 415 public void testRemove_Object() { 416 int SIZE = 3; 417 for (int i = 0; i < SIZE; i++) { 418 CopyOnWriteArrayList full = populatedArray(SIZE); 419 assertFalse(full.remove(new Integer(-42))); 420 assertTrue(full.remove(new Integer(i))); 421 assertEquals(SIZE - 1, full.size()); 422 assertFalse(full.contains(new Integer(i))); 423 } 424 CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); 425 assertTrue(x.remove(new Integer(6))); 426 assertEquals(x, Arrays.asList(4, 5)); 427 assertTrue(x.remove(new Integer(4))); 428 assertEquals(x, Arrays.asList(5)); 429 assertTrue(x.remove(new Integer(5))); 430 assertEquals(x, Arrays.asList()); 431 assertFalse(x.remove(new Integer(5))); 432 } 433 434 /** 435 * removeAll removes all elements from the given collection 436 */ 437 public void testRemoveAll() { 438 CopyOnWriteArrayList full = populatedArray(3); 439 assertTrue(full.removeAll(Arrays.asList(one, two))); 440 assertEquals(1, full.size()); 441 assertFalse(full.removeAll(Arrays.asList(one, two))); 442 assertEquals(1, full.size()); 443 } 444 445 /** 446 * set changes the element at the given index 447 */ 448 public void testSet() { 449 CopyOnWriteArrayList full = populatedArray(3); 450 assertEquals(2, full.set(2, four)); 451 assertEquals(4, full.get(2)); 452 } 453 454 /** 455 * size returns the number of elements 456 */ 457 public void testSize() { 458 CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); 459 CopyOnWriteArrayList full = populatedArray(SIZE); 460 assertEquals(SIZE, full.size()); 461 assertEquals(0, empty.size()); 462 } 463 464 /** 465 * toArray() returns an Object array containing all elements from 466 * the list in insertion order 467 */ 468 public void testToArray() { 469 Object[] a = new CopyOnWriteArrayList().toArray(); 470 assertTrue(Arrays.equals(new Object[0], a)); 471 assertSame(Object[].class, a.getClass()); 472 473 Integer[] elements = new Integer[SIZE]; 474 for (int i = 0; i < SIZE; i++) 475 elements[i] = i; 476 shuffle(elements); 477 Collection<Integer> full = populatedArray(elements); 478 479 assertTrue(Arrays.equals(elements, full.toArray())); 480 assertSame(Object[].class, full.toArray().getClass()); 481 } 482 483 /** 484 * toArray(Integer array) returns an Integer array containing all 485 * elements from the list in insertion order 486 */ 487 public void testToArray2() { 488 Collection empty = new CopyOnWriteArrayList(); 489 Integer[] a; 490 491 a = new Integer[0]; 492 assertSame(a, empty.toArray(a)); 493 494 a = new Integer[SIZE / 2]; 495 Arrays.fill(a, 42); 496 assertSame(a, empty.toArray(a)); 497 assertNull(a[0]); 498 for (int i = 1; i < a.length; i++) 499 assertEquals(42, (int) a[i]); 500 501 Integer[] elements = new Integer[SIZE]; 502 for (int i = 0; i < SIZE; i++) 503 elements[i] = i; 504 shuffle(elements); 505 Collection<Integer> full = populatedArray(elements); 506 507 Arrays.fill(a, 42); 508 assertTrue(Arrays.equals(elements, full.toArray(a))); 509 for (int i = 0; i < a.length; i++) 510 assertEquals(42, (int) a[i]); 511 assertSame(Integer[].class, full.toArray(a).getClass()); 512 513 a = new Integer[SIZE]; 514 Arrays.fill(a, 42); 515 assertSame(a, full.toArray(a)); 516 assertTrue(Arrays.equals(elements, a)); 517 518 a = new Integer[2 * SIZE]; 519 Arrays.fill(a, 42); 520 assertSame(a, full.toArray(a)); 521 assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); 522 assertNull(a[SIZE]); 523 for (int i = SIZE + 1; i < a.length; i++) 524 assertEquals(42, (int) a[i]); 525 } 526 527 /** 528 * sublists contains elements at indexes offset from their base 529 */ 530 public void testSubList() { 531 CopyOnWriteArrayList a = populatedArray(10); 532 assertTrue(a.subList(1,1).isEmpty()); 533 for (int j = 0; j < 9; ++j) { 534 for (int i = j ; i < 10; ++i) { 535 List b = a.subList(j,i); 536 for (int k = j; k < i; ++k) { 537 assertEquals(new Integer(k), b.get(k-j)); 538 } 539 } 540 } 541 542 List s = a.subList(2, 5); 543 assertEquals(3, s.size()); 544 s.set(2, m1); 545 assertEquals(a.get(4), m1); 546 s.clear(); 547 assertEquals(7, a.size()); 548 } 549 550 // Exception tests 551 552 /** 553 * toArray throws an ArrayStoreException when the given array 554 * can not store the objects inside the list 555 */ 556 public void testToArray_ArrayStoreException() { 557 CopyOnWriteArrayList c = new CopyOnWriteArrayList(); 558 c.add("zfasdfsdf"); 559 c.add("asdadasd"); 560 try { 561 c.toArray(new Long[5]); 562 shouldThrow(); 563 } catch (ArrayStoreException success) {} 564 } 565 566 /** 567 * get throws an IndexOutOfBoundsException on a negative index 568 */ 569 public void testGet1_IndexOutOfBoundsException() { 570 CopyOnWriteArrayList c = populatedArray(5); 571 List[] lists = { c, c.subList(1, c.size() - 1) }; 572 for (List list : lists) { 573 try { 574 list.get(-1); 575 shouldThrow(); 576 } catch (IndexOutOfBoundsException success) {} 577 } 578 } 579 580 /** 581 * get throws an IndexOutOfBoundsException on a too high index 582 */ 583 public void testGet2_IndexOutOfBoundsException() { 584 CopyOnWriteArrayList c = populatedArray(5); 585 List[] lists = { c, c.subList(1, c.size() - 1) }; 586 for (List list : lists) { 587 try { 588 list.get(list.size()); 589 shouldThrow(); 590 } catch (IndexOutOfBoundsException success) {} 591 } 592 } 593 594 /** 595 * set throws an IndexOutOfBoundsException on a negative index 596 */ 597 public void testSet1_IndexOutOfBoundsException() { 598 CopyOnWriteArrayList c = populatedArray(5); 599 List[] lists = { c, c.subList(1, c.size() - 1) }; 600 for (List list : lists) { 601 try { 602 list.set(-1, "qwerty"); 603 shouldThrow(); 604 } catch (IndexOutOfBoundsException success) {} 605 } 606 } 607 608 /** 609 * set throws an IndexOutOfBoundsException on a too high index 610 */ 611 public void testSet2() { 612 CopyOnWriteArrayList c = populatedArray(5); 613 List[] lists = { c, c.subList(1, c.size() - 1) }; 614 for (List list : lists) { 615 try { 616 list.set(list.size(), "qwerty"); 617 shouldThrow(); 618 } catch (IndexOutOfBoundsException success) {} 619 } 620 } 621 622 /** 623 * add throws an IndexOutOfBoundsException on a negative index 624 */ 625 public void testAdd1_IndexOutOfBoundsException() { 626 CopyOnWriteArrayList c = populatedArray(5); 627 List[] lists = { c, c.subList(1, c.size() - 1) }; 628 for (List list : lists) { 629 try { 630 list.add(-1, "qwerty"); 631 shouldThrow(); 632 } catch (IndexOutOfBoundsException success) {} 633 } 634 } 635 636 /** 637 * add throws an IndexOutOfBoundsException on a too high index 638 */ 639 public void testAdd2_IndexOutOfBoundsException() { 640 CopyOnWriteArrayList c = populatedArray(5); 641 List[] lists = { c, c.subList(1, c.size() - 1) }; 642 for (List list : lists) { 643 try { 644 list.add(list.size() + 1, "qwerty"); 645 shouldThrow(); 646 } catch (IndexOutOfBoundsException success) {} 647 } 648 } 649 650 /** 651 * remove throws an IndexOutOfBoundsException on a negative index 652 */ 653 public void testRemove1_IndexOutOfBounds() { 654 CopyOnWriteArrayList c = populatedArray(5); 655 List[] lists = { c, c.subList(1, c.size() - 1) }; 656 for (List list : lists) { 657 try { 658 list.remove(-1); 659 shouldThrow(); 660 } catch (IndexOutOfBoundsException success) {} 661 } 662 } 663 664 /** 665 * remove throws an IndexOutOfBoundsException on a too high index 666 */ 667 public void testRemove2_IndexOutOfBounds() { 668 CopyOnWriteArrayList c = populatedArray(5); 669 List[] lists = { c, c.subList(1, c.size() - 1) }; 670 for (List list : lists) { 671 try { 672 list.remove(list.size()); 673 shouldThrow(); 674 } catch (IndexOutOfBoundsException success) {} 675 } 676 } 677 678 /** 679 * addAll throws an IndexOutOfBoundsException on a negative index 680 */ 681 public void testAddAll1_IndexOutOfBoundsException() { 682 CopyOnWriteArrayList c = populatedArray(5); 683 List[] lists = { c, c.subList(1, c.size() - 1) }; 684 for (List list : lists) { 685 try { 686 list.addAll(-1, new LinkedList()); 687 shouldThrow(); 688 } catch (IndexOutOfBoundsException success) {} 689 } 690 } 691 692 /** 693 * addAll throws an IndexOutOfBoundsException on a too high index 694 */ 695 public void testAddAll2_IndexOutOfBoundsException() { 696 CopyOnWriteArrayList c = populatedArray(5); 697 List[] lists = { c, c.subList(1, c.size() - 1) }; 698 for (List list : lists) { 699 try { 700 list.addAll(list.size() + 1, new LinkedList()); 701 shouldThrow(); 702 } catch (IndexOutOfBoundsException success) {} 703 } 704 } 705 706 /** 707 * listIterator throws an IndexOutOfBoundsException on a negative index 708 */ 709 public void testListIterator1_IndexOutOfBoundsException() { 710 CopyOnWriteArrayList c = populatedArray(5); 711 List[] lists = { c, c.subList(1, c.size() - 1) }; 712 for (List list : lists) { 713 try { 714 list.listIterator(-1); 715 shouldThrow(); 716 } catch (IndexOutOfBoundsException success) {} 717 } 718 } 719 720 /** 721 * listIterator throws an IndexOutOfBoundsException on a too high index 722 */ 723 public void testListIterator2_IndexOutOfBoundsException() { 724 CopyOnWriteArrayList c = populatedArray(5); 725 List[] lists = { c, c.subList(1, c.size() - 1) }; 726 for (List list : lists) { 727 try { 728 list.listIterator(list.size() + 1); 729 shouldThrow(); 730 } catch (IndexOutOfBoundsException success) {} 731 } 732 } 733 734 /** 735 * subList throws an IndexOutOfBoundsException on a negative index 736 */ 737 public void testSubList1_IndexOutOfBoundsException() { 738 CopyOnWriteArrayList c = populatedArray(5); 739 List[] lists = { c, c.subList(1, c.size() - 1) }; 740 for (List list : lists) { 741 try { 742 list.subList(-1, list.size()); 743 shouldThrow(); 744 } catch (IndexOutOfBoundsException success) {} 745 } 746 } 747 748 /** 749 * subList throws an IndexOutOfBoundsException on a too high index 750 */ 751 public void testSubList2_IndexOutOfBoundsException() { 752 CopyOnWriteArrayList c = populatedArray(5); 753 List[] lists = { c, c.subList(1, c.size() - 1) }; 754 for (List list : lists) { 755 try { 756 list.subList(0, list.size() + 1); 757 shouldThrow(); 758 } catch (IndexOutOfBoundsException success) {} 759 } 760 } 761 762 /** 763 * subList throws IndexOutOfBoundsException when the second index 764 * is lower then the first 765 */ 766 public void testSubList3_IndexOutOfBoundsException() { 767 CopyOnWriteArrayList c = populatedArray(5); 768 List[] lists = { c, c.subList(1, c.size() - 1) }; 769 for (List list : lists) { 770 try { 771 list.subList(list.size() - 1, 1); 772 shouldThrow(); 773 } catch (IndexOutOfBoundsException success) {} 774 } 775 } 776 777 /** 778 * a deserialized serialized list is equal 779 */ 780 public void testSerialization() throws Exception { 781 List x = populatedArray(SIZE); 782 List y = serialClone(x); 783 784 assertNotSame(x, y); 785 assertEquals(x.size(), y.size()); 786 assertEquals(x.toString(), y.toString()); 787 assertTrue(Arrays.equals(x.toArray(), y.toArray())); 788 assertEquals(x, y); 789 assertEquals(y, x); 790 while (!x.isEmpty()) { 791 assertFalse(y.isEmpty()); 792 assertEquals(x.remove(0), y.remove(0)); 793 } 794 assertTrue(y.isEmpty()); 795 } 796 797} 798