Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.harmony.luni.tests.java.util;
     18 
     19 import java.util.ArrayList;
     20 import java.util.Arrays;
     21 import java.util.Collection;
     22 import java.util.ConcurrentModificationException;
     23 import java.util.HashSet;
     24 import java.util.Iterator;
     25 import java.util.List;
     26 import java.util.Set;
     27 import java.util.Vector;
     28 
     29 import tests.support.Support_ListTest;
     30 
     31 public class ArrayListTest extends junit.framework.TestCase {
     32 
     33     List alist;
     34 
     35     static Object[] objArray;
     36     {
     37         objArray = new Object[100];
     38         for (int i = 0; i < objArray.length; i++)
     39             objArray[i] = new Integer(i);
     40     }
     41 
     42     /**
     43      * @tests java.util.ArrayList#ArrayList()
     44      */
     45     public void test_Constructor() {
     46         // Test for method java.util.ArrayList()
     47         new Support_ListTest("", alist).runTest();
     48 
     49         ArrayList subList = new ArrayList();
     50         for (int i = -50; i < 150; i++)
     51             subList.add(new Integer(i));
     52         new Support_ListTest("", subList.subList(50, 150)).runTest();
     53     }
     54 
     55     /**
     56      * @tests java.util.ArrayList#ArrayList(int)
     57      */
     58     public void test_ConstructorI() {
     59         // Test for method java.util.ArrayList(int)
     60         ArrayList al = new ArrayList(5);
     61         assertEquals("Incorrect arrayList created", 0, al.size());
     62 
     63         al = new ArrayList(0);
     64         assertEquals("Incorrect arrayList created", 0, al.size());
     65 
     66         try {
     67             al = new ArrayList(-1);
     68             fail("Should throw IllegalArgumentException");
     69         } catch (IllegalArgumentException e) {
     70             // Excepted
     71         }
     72     }
     73 
     74     /**
     75      * @tests java.util.ArrayList#ArrayList(java.util.Collection)
     76      */
     77     public void test_ConstructorLjava_util_Collection() {
     78         // Test for method java.util.ArrayList(java.util.Collection)
     79         ArrayList al = new ArrayList(Arrays.asList(objArray));
     80         assertTrue("arrayList created from collection has incorrect size", al
     81                 .size() == objArray.length);
     82         for (int counter = 0; counter < objArray.length; counter++)
     83             assertTrue(
     84                     "arrayList created from collection has incorrect elements",
     85                     al.get(counter) == objArray[counter]);
     86 
     87     }
     88 
     89     public void testConstructorWithConcurrentCollection() {
     90         Collection<String> collection = shrinksOnSize("A", "B", "C", "D");
     91         ArrayList<String> list = new ArrayList<String>(collection);
     92         assertFalse(list.contains(null));
     93     }
     94 
     95     /**
     96      * @tests java.util.ArrayList#add(int, java.lang.Object)
     97      */
     98     public void test_addILjava_lang_Object() {
     99         // Test for method void java.util.ArrayList.add(int, java.lang.Object)
    100         Object o;
    101         alist.add(50, o = new Object());
    102         assertTrue("Failed to add Object", alist.get(50) == o);
    103         assertTrue("Failed to fix up list after insert",
    104                 alist.get(51) == objArray[50]
    105                         && (alist.get(52) == objArray[51]));
    106         Object oldItem = alist.get(25);
    107         alist.add(25, null);
    108         assertNull("Should have returned null", alist.get(25));
    109         assertTrue("Should have returned the old item from slot 25", alist
    110                 .get(26) == oldItem);
    111 
    112         alist.add(0, o = new Object());
    113         assertEquals("Failed to add Object", alist.get(0), o);
    114         assertEquals(alist.get(1), objArray[0]);
    115         assertEquals(alist.get(2), objArray[1]);
    116 
    117         oldItem = alist.get(0);
    118         alist.add(0, null);
    119         assertNull("Should have returned null", alist.get(0));
    120         assertEquals("Should have returned the old item from slot 0", alist
    121                 .get(1), oldItem);
    122 
    123         try {
    124             alist.add(-1, new Object());
    125             fail("Should throw IndexOutOfBoundsException");
    126         } catch (IndexOutOfBoundsException e) {
    127             // Expected
    128             assertNotNull(e.getMessage());
    129         }
    130 
    131         try {
    132             alist.add(-1, null);
    133             fail("Should throw IndexOutOfBoundsException");
    134         } catch (IndexOutOfBoundsException e) {
    135             // Expected
    136             assertNotNull(e.getMessage());
    137         }
    138 
    139         try {
    140             alist.add(alist.size() + 1, new Object());
    141             fail("Should throw IndexOutOfBoundsException");
    142         } catch (IndexOutOfBoundsException e) {
    143             // Expected
    144             assertNotNull(e.getMessage());
    145         }
    146 
    147         try {
    148             alist.add(alist.size() + 1, null);
    149             fail("Should throw IndexOutOfBoundsException");
    150         } catch (IndexOutOfBoundsException e) {
    151             // Expected
    152             assertNotNull(e.getMessage());
    153         }
    154     }
    155 
    156     /**
    157      * @tests java.util.ArrayList#add(int, java.lang.Object)
    158      */
    159     public void test_addILjava_lang_Object_2() {
    160         Object o = new Object();
    161         int size = alist.size();
    162         alist.add(size, o);
    163         assertEquals("Failed to add Object", alist.get(size), o);
    164         assertEquals(alist.get(size - 2), objArray[size - 2]);
    165         assertEquals(alist.get(size - 1), objArray[size - 1]);
    166 
    167         alist.remove(size);
    168 
    169         size = alist.size();
    170         alist.add(size, null);
    171         assertNull("Should have returned null", alist.get(size));
    172         assertEquals(alist.get(size - 2), objArray[size - 2]);
    173         assertEquals(alist.get(size - 1), objArray[size - 1]);
    174     }
    175 
    176     /**
    177      * @tests java.util.ArrayList#add(java.lang.Object)
    178      */
    179     public void test_addLjava_lang_Object() {
    180         // Test for method boolean java.util.ArrayList.add(java.lang.Object)
    181         Object o = new Object();
    182         alist.add(o);
    183         assertTrue("Failed to add Object", alist.get(alist.size() - 1) == o);
    184         alist.add(null);
    185         assertNull("Failed to add null", alist.get(alist.size() - 1));
    186     }
    187 
    188     /**
    189      * @tests java.util.ArrayList#addAll(int, java.util.Collection)
    190      */
    191     public void test_addAllILjava_util_Collection() {
    192         // Test for method boolean java.util.ArrayList.addAll(int,
    193         // java.util.Collection)
    194         alist.addAll(50, alist);
    195         assertEquals("Returned incorrect size after adding to existing list",
    196                 200, alist.size());
    197         for (int i = 0; i < 50; i++)
    198             assertTrue("Manipulated elements < index",
    199                     alist.get(i) == objArray[i]);
    200         for (int i = 0; i >= 50 && (i < 150); i++)
    201             assertTrue("Failed to ad elements properly",
    202                     alist.get(i) == objArray[i - 50]);
    203         for (int i = 0; i >= 150 && (i < 200); i++)
    204             assertTrue("Failed to ad elements properly",
    205                     alist.get(i) == objArray[i - 100]);
    206         ArrayList listWithNulls = new ArrayList();
    207         listWithNulls.add(null);
    208         listWithNulls.add(null);
    209         listWithNulls.add("yoink");
    210         listWithNulls.add("kazoo");
    211         listWithNulls.add(null);
    212         alist.addAll(100, listWithNulls);
    213         assertTrue("Incorrect size: " + alist.size(), alist.size() == 205);
    214         assertNull("Item at slot 100 should be null", alist.get(100));
    215         assertNull("Item at slot 101 should be null", alist.get(101));
    216         assertEquals("Item at slot 102 should be 'yoink'", "yoink", alist
    217                 .get(102));
    218         assertEquals("Item at slot 103 should be 'kazoo'", "kazoo", alist
    219                 .get(103));
    220         assertNull("Item at slot 104 should be null", alist.get(104));
    221         alist.addAll(205, listWithNulls);
    222         assertTrue("Incorrect size2: " + alist.size(), alist.size() == 210);
    223     }
    224 
    225     /**
    226      * @tests java.util.ArrayList#addAll(int, java.util.Collection)
    227      */
    228     @SuppressWarnings("unchecked")
    229     public void test_addAllILjava_util_Collection_2() {
    230         // Regression for HARMONY-467
    231         ArrayList obj = new ArrayList();
    232         try {
    233             obj.addAll((int) -1, (Collection) null);
    234             fail("IndexOutOfBoundsException expected");
    235         } catch (IndexOutOfBoundsException e) {
    236             // Expected
    237             assertNotNull(e.getMessage());
    238         }
    239 
    240         // Regression for HARMONY-5705
    241         String[] data = new String[] { "1", "2", "3", "4", "5", "6", "7", "8" };
    242         ArrayList list1 = new ArrayList();
    243         ArrayList list2 = new ArrayList();
    244         for (String d : data) {
    245             list1.add(d);
    246             list2.add(d);
    247             list2.add(d);
    248         }
    249         while (list1.size() > 0)
    250             list1.remove(0);
    251         list1.addAll(list2);
    252         assertTrue("The object list is not the same as original list", list1
    253                 .containsAll(list2)
    254                 && list2.containsAll(list1));
    255 
    256         obj = new ArrayList();
    257         for (int i = 0; i < 100; i++) {
    258             if (list1.size() > 0) {
    259                 obj.removeAll(list1);
    260                 obj.addAll(list1);
    261             }
    262         }
    263         assertTrue("The object list is not the same as original list", obj
    264                 .containsAll(list1)
    265                 && list1.containsAll(obj));
    266 
    267         // Regression for Harmony-5799
    268         list1 = new ArrayList();
    269         list2 = new ArrayList();
    270         int location = 2;
    271 
    272         String[] strings = { "0", "1", "2", "3", "4", "5", "6" };
    273         int[] integers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    274         for (int i = 0; i < 7; i++) {
    275             list1.add(strings[i]);
    276         }
    277         for (int i = 0; i < 10; i++) {
    278             list2.add(integers[i]);
    279         }
    280         list1.remove(location);
    281         list1.addAll(location, list2);
    282 
    283         // Inserted elements should be equal to integers array
    284         for (int i = 0; i < integers.length; i++) {
    285             assertEquals(integers[i], list1.get(location + i));
    286         }
    287         // Elements after inserted location should
    288         // be equals to related elements in strings array
    289         for (int i = location + 1; i < strings.length; i++) {
    290             assertEquals(strings[i], list1.get(i + integers.length - 1));
    291         }
    292     }
    293 
    294     /**
    295      * @tests java.util.ArrayList#addAll(int, java.util.Collection)
    296      */
    297     public void test_addAllILjava_util_Collection_3() {
    298         ArrayList obj = new ArrayList();
    299         obj.addAll(0, obj);
    300         obj.addAll(obj.size(), obj);
    301         try {
    302             obj.addAll(-1, obj);
    303             fail("Should throw IndexOutOfBoundsException");
    304         } catch (IndexOutOfBoundsException e) {
    305             // Expected
    306             assertNotNull(e.getMessage());
    307         }
    308 
    309         try {
    310             obj.addAll(obj.size() + 1, obj);
    311             fail("Should throw IndexOutOfBoundsException");
    312         } catch (IndexOutOfBoundsException e) {
    313             // Expected
    314             assertNotNull(e.getMessage());
    315         }
    316 
    317         try {
    318             obj.addAll(0, null);
    319             fail("Should throw NullPointerException");
    320         } catch (NullPointerException e) {
    321             // Excepted
    322         }
    323 
    324         try {
    325             obj.addAll(obj.size() + 1, null);
    326             fail("Should throw IndexOutOfBoundsException");
    327         } catch (IndexOutOfBoundsException e) {
    328             // Expected
    329             assertNotNull(e.getMessage());
    330         }
    331 
    332         try {
    333             obj.addAll((int) -1, (Collection) null);
    334             fail("IndexOutOfBoundsException expected");
    335         } catch (IndexOutOfBoundsException e) {
    336             // Expected
    337             assertNotNull(e.getMessage());
    338         }
    339     }
    340 
    341     public void test_addAllCollectionOfQextendsE() {
    342         // Regression for HARMONY-539
    343         // https://issues.apache.org/jira/browse/HARMONY-539
    344         ArrayList<String> alist = new ArrayList<String>();
    345         ArrayList<String> blist = new ArrayList<String>();
    346         alist.add("a");
    347         alist.add("b");
    348         blist.add("c");
    349         blist.add("d");
    350         blist.remove(0);
    351         blist.addAll(0, alist);
    352         assertEquals("a", blist.get(0));
    353         assertEquals("b", blist.get(1));
    354         assertEquals("d", blist.get(2));
    355     }
    356 
    357     /**
    358      * @tests java.util.ArrayList#addAll(java.util.Collection)
    359      */
    360     public void test_addAllLjava_util_Collection() {
    361         // Test for method boolean
    362         // java.util.ArrayList.addAll(java.util.Collection)
    363         List l = new ArrayList();
    364         l.addAll(alist);
    365         for (int i = 0; i < alist.size(); i++)
    366             assertTrue("Failed to add elements properly", l.get(i).equals(
    367                     alist.get(i)));
    368         alist.addAll(alist);
    369         assertEquals("Returned incorrect size after adding to existing list",
    370                 200, alist.size());
    371         for (int i = 0; i < 100; i++) {
    372             assertTrue("Added to list in incorrect order", alist.get(i).equals(
    373                     l.get(i)));
    374             assertTrue("Failed to add to existing list", alist.get(i + 100)
    375                     .equals(l.get(i)));
    376         }
    377         Set setWithNulls = new HashSet();
    378         setWithNulls.add(null);
    379         setWithNulls.add(null);
    380         setWithNulls.add("yoink");
    381         setWithNulls.add("kazoo");
    382         setWithNulls.add(null);
    383         alist.addAll(100, setWithNulls);
    384         Iterator i = setWithNulls.iterator();
    385         assertTrue("Item at slot 100 is wrong: " + alist.get(100), alist
    386                 .get(100) == i.next());
    387         assertTrue("Item at slot 101 is wrong: " + alist.get(101), alist
    388                 .get(101) == i.next());
    389         assertTrue("Item at slot 103 is wrong: " + alist.get(102), alist
    390                 .get(102) == i.next());
    391 
    392         try {
    393             alist.addAll(null);
    394             fail("Should throw NullPointerException");
    395         } catch (NullPointerException e) {
    396             // Excepted
    397         }
    398 
    399         // Regression test for Harmony-3481
    400         ArrayList<Integer> originalList = new ArrayList<Integer>(12);
    401         for (int j = 0; j < 12; j++) {
    402             originalList.add(j);
    403         }
    404 
    405         originalList.remove(0);
    406         originalList.remove(0);
    407 
    408         ArrayList<Integer> additionalList = new ArrayList<Integer>(11);
    409         for (int j = 0; j < 11; j++) {
    410             additionalList.add(j);
    411         }
    412         assertTrue(originalList.addAll(additionalList));
    413         assertEquals(21, originalList.size());
    414 
    415     }
    416 
    417         public void test_ArrayList_addAll_scenario1() {
    418         ArrayList arrayListA = new ArrayList();
    419         arrayListA.add(1);
    420         ArrayList arrayListB = new ArrayList();
    421         arrayListB.add(1);
    422         arrayListA.addAll(1, arrayListB);
    423         int size = arrayListA.size();
    424         assertEquals(2, size);
    425         for (int index = 0; index < size; index++) {
    426             assertEquals(1, arrayListA.get(index));
    427         }
    428     }
    429 
    430     public void test_ArrayList_addAll_scenario2() {
    431         ArrayList arrayList = new ArrayList();
    432         arrayList.add(1);
    433         arrayList.addAll(1, arrayList);
    434         int size = arrayList.size();
    435         assertEquals(2, size);
    436         for (int index = 0; index < size; index++) {
    437             assertEquals(1, arrayList.get(index));
    438         }
    439     }
    440 
    441     // Regression test for HARMONY-5839
    442     public void testaddAllHarmony5839() {
    443         Collection coll = Arrays.asList(new String[] { "1", "2" });
    444         List list = new ArrayList();
    445         list.add("a");
    446         list.add(0, "b");
    447         list.add(0, "c");
    448         list.add(0, "d");
    449         list.add(0, "e");
    450         list.add(0, "f");
    451         list.add(0, "g");
    452         list.add(0, "h");
    453         list.add(0, "i");
    454 
    455         list.addAll(6, coll);
    456 
    457         assertEquals(11, list.size());
    458         assertFalse(list.contains(null));
    459     }
    460 
    461     /**
    462      * @tests java.util.ArrayList#clear()
    463      */
    464     public void test_clear() {
    465         // Test for method void java.util.ArrayList.clear()
    466         alist.clear();
    467         assertEquals("List did not clear", 0, alist.size());
    468         alist.add(null);
    469         alist.add(null);
    470         alist.add(null);
    471         alist.add("bam");
    472         alist.clear();
    473         assertEquals("List with nulls did not clear", 0, alist.size());
    474         /*
    475          * for (int i = 0; i < alist.size(); i++) assertNull("Failed to clear
    476          * list", alist.get(i));
    477          */
    478 
    479     }
    480 
    481     /**
    482      * @tests java.util.ArrayList#clone()
    483      */
    484     public void test_clone() {
    485         // Test for method java.lang.Object java.util.ArrayList.clone()
    486         ArrayList x = (ArrayList) (((ArrayList) (alist)).clone());
    487         assertTrue("Cloned list was inequal to original", x.equals(alist));
    488         for (int i = 0; i < alist.size(); i++)
    489             assertTrue("Cloned list contains incorrect elements",
    490                     alist.get(i) == x.get(i));
    491 
    492         alist.add(null);
    493         alist.add(25, null);
    494         x = (ArrayList) (((ArrayList) (alist)).clone());
    495         assertTrue("nulls test - Cloned list was inequal to original", x
    496                 .equals(alist));
    497         for (int i = 0; i < alist.size(); i++)
    498             assertTrue("nulls test - Cloned list contains incorrect elements",
    499                     alist.get(i) == x.get(i));
    500 
    501     }
    502 
    503     /**
    504      * @tests java.util.ArrayList#contains(java.lang.Object)
    505      */
    506     public void test_containsLjava_lang_Object() {
    507         // Test for method boolean
    508         // java.util.ArrayList.contains(java.lang.Object)
    509         assertTrue("Returned false for valid element", alist
    510                 .contains(objArray[99]));
    511         assertTrue("Returned false for equal element", alist
    512                 .contains(new Integer(8)));
    513         assertTrue("Returned true for invalid element", !alist
    514                 .contains(new Object()));
    515         assertTrue("Returned true for null but should have returned false",
    516                 !alist.contains(null));
    517         alist.add(null);
    518         assertTrue("Returned false for null but should have returned true",
    519                 alist.contains(null));
    520     }
    521 
    522     /**
    523      * @tests java.util.ArrayList#ensureCapacity(int)
    524      */
    525     public void test_ensureCapacityI() {
    526         // Test for method void java.util.ArrayList.ensureCapacity(int)
    527         // TODO : There is no good way to test this as it only really impacts on
    528         // the private implementation.
    529 
    530         Object testObject = new Object();
    531         int capacity = 20;
    532         ArrayList al = new ArrayList(capacity);
    533         int i;
    534         for (i = 0; i < capacity / 2; i++) {
    535             al.add(i, new Object());
    536         }
    537         al.add(i, testObject);
    538         int location = al.indexOf(testObject);
    539         al.ensureCapacity(capacity);
    540         assertTrue("EnsureCapacity moved objects around in array1.",
    541                 location == al.indexOf(testObject));
    542         al.remove(0);
    543         al.ensureCapacity(capacity);
    544         assertTrue("EnsureCapacity moved objects around in array2.",
    545                 --location == al.indexOf(testObject));
    546         al.ensureCapacity(capacity + 2);
    547         assertTrue("EnsureCapacity did not change location.", location == al
    548                 .indexOf(testObject));
    549 
    550         ArrayList<String> list = new ArrayList<String>(1);
    551         list.add("hello");
    552         list.ensureCapacity(Integer.MIN_VALUE);
    553     }
    554 
    555     /**
    556      * @tests java.util.ArrayList#get(int)
    557      */
    558     public void test_getI() {
    559         // Test for method java.lang.Object java.util.ArrayList.get(int)
    560         assertTrue("Returned incorrect element", alist.get(22) == objArray[22]);
    561         try {
    562             alist.get(8765);
    563             fail("Failed to throw expected exception for index > size");
    564         } catch (IndexOutOfBoundsException e) {
    565             // Expected
    566             assertNotNull(e.getMessage());
    567         }
    568     }
    569 
    570     /**
    571      * @tests java.util.ArrayList#indexOf(java.lang.Object)
    572      */
    573     public void test_indexOfLjava_lang_Object() {
    574         // Test for method int java.util.ArrayList.indexOf(java.lang.Object)
    575         assertEquals("Returned incorrect index", 87, alist
    576                 .indexOf(objArray[87]));
    577         assertEquals("Returned index for invalid Object", -1, alist
    578                 .indexOf(new Object()));
    579         alist.add(25, null);
    580         alist.add(50, null);
    581         assertTrue("Wrong indexOf for null.  Wanted 25 got: "
    582                 + alist.indexOf(null), alist.indexOf(null) == 25);
    583     }
    584 
    585     /**
    586      * @tests java.util.ArrayList#isEmpty()
    587      */
    588     public void test_isEmpty() {
    589         // Test for method boolean java.util.ArrayList.isEmpty()
    590         assertTrue("isEmpty returned false for new list", new ArrayList()
    591                 .isEmpty());
    592         assertTrue("Returned true for existing list with elements", !alist
    593                 .isEmpty());
    594     }
    595 
    596     /**
    597      * @tests java.util.ArrayList#lastIndexOf(java.lang.Object)
    598      */
    599     public void test_lastIndexOfLjava_lang_Object() {
    600         // Test for method int java.util.ArrayList.lastIndexOf(java.lang.Object)
    601         alist.add(new Integer(99));
    602         assertEquals("Returned incorrect index", 100, alist
    603                 .lastIndexOf(objArray[99]));
    604         assertEquals("Returned index for invalid Object", -1, alist
    605                 .lastIndexOf(new Object()));
    606         alist.add(25, null);
    607         alist.add(50, null);
    608         assertTrue("Wrong lastIndexOf for null.  Wanted 50 got: "
    609                 + alist.lastIndexOf(null), alist.lastIndexOf(null) == 50);
    610     }
    611 
    612     /**
    613      * @tests {@link java.util.ArrayList#removeRange(int, int)}
    614      */
    615     public void test_removeRange() {
    616         MockArrayList mylist = new MockArrayList();
    617         mylist.removeRange(0, 0);
    618 
    619         try {
    620             mylist.removeRange(0, 1);
    621             fail("Should throw IndexOutOfBoundsException");
    622         } catch (IndexOutOfBoundsException e) {
    623             // Expected
    624             assertNotNull(e.getMessage());
    625         }
    626 
    627         int[] data = { 1, 2, 3 };
    628         for (int i = 0; i < data.length; i++) {
    629             mylist.add(i, data[i]);
    630         }
    631 
    632         mylist.removeRange(0, 1);
    633         assertEquals(data[1], mylist.get(0));
    634         assertEquals(data[2], mylist.get(1));
    635 
    636         try {
    637             mylist.removeRange(-1, 1);
    638             fail("Should throw IndexOutOfBoundsException");
    639         } catch (IndexOutOfBoundsException e) {
    640             // Expected
    641             assertNotNull(e.getMessage());
    642         }
    643 
    644         try {
    645             mylist.removeRange(0, -1);
    646             fail("Should throw IndexOutOfBoundsException");
    647         } catch (IndexOutOfBoundsException e) {
    648             // Expected
    649             assertNotNull(e.getMessage());
    650         }
    651 
    652         try {
    653             mylist.removeRange(1, 0);
    654             fail("Should throw IndexOutOfBoundsException");
    655         } catch (IndexOutOfBoundsException e) {
    656             // Expected
    657             assertNotNull(e.getMessage());
    658         }
    659 
    660         try {
    661             mylist.removeRange(2, 1);
    662             fail("Should throw IndexOutOfBoundsException");
    663         } catch (IndexOutOfBoundsException e) {
    664             // Expected
    665             assertNotNull(e.getMessage());
    666         }
    667     }
    668 
    669     /**
    670      * @tests java.util.ArrayList#remove(int)
    671      */
    672     public void test_removeI() {
    673         // Test for method java.lang.Object java.util.ArrayList.remove(int)
    674         alist.remove(10);
    675         assertEquals("Failed to remove element", -1, alist
    676                 .indexOf(objArray[10]));
    677         try {
    678             alist.remove(999);
    679             fail("Failed to throw exception when index out of range");
    680         } catch (IndexOutOfBoundsException e) {
    681             // Expected
    682             assertNotNull(e.getMessage());
    683         }
    684 
    685         ArrayList myList = (ArrayList) (((ArrayList) (alist)).clone());
    686         alist.add(25, null);
    687         alist.add(50, null);
    688         alist.remove(50);
    689         alist.remove(25);
    690         assertTrue("Removing nulls did not work", alist.equals(myList));
    691 
    692         List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
    693                 "d", "e", "f", "g" }));
    694         assertTrue("Removed wrong element 1", list.remove(0) == "a");
    695         assertTrue("Removed wrong element 2", list.remove(4) == "f");
    696         String[] result = new String[5];
    697         list.toArray(result);
    698         assertTrue("Removed wrong element 3", Arrays.equals(result,
    699                 new String[] { "b", "c", "d", "e", "g" }));
    700 
    701         List l = new ArrayList(0);
    702         l.add(new Object());
    703         l.add(new Object());
    704         l.remove(0);
    705         l.remove(0);
    706         try {
    707             l.remove(-1);
    708             fail("-1 should cause exception");
    709         } catch (IndexOutOfBoundsException e) {
    710             // Expected
    711             assertNotNull(e.getMessage());
    712         }
    713         try {
    714             l.remove(0);
    715             fail("0 should case exception");
    716         } catch (IndexOutOfBoundsException e) {
    717             // Expected
    718             assertNotNull(e.getMessage());
    719         }
    720     }
    721 
    722     /**
    723      * @tests java.util.ArrayList#set(int, java.lang.Object)
    724      */
    725     public void test_setILjava_lang_Object() {
    726         // Test for method java.lang.Object java.util.ArrayList.set(int,
    727         // java.lang.Object)
    728         Object obj;
    729         alist.set(65, obj = new Object());
    730         assertTrue("Failed to set object", alist.get(65) == obj);
    731         alist.set(50, null);
    732         assertNull("Setting to null did not work", alist.get(50));
    733         assertTrue("Setting increased the list's size to: " + alist.size(),
    734                 alist.size() == 100);
    735 
    736         obj = new Object();
    737         alist.set(0, obj);
    738         assertTrue("Failed to set object", alist.get(0) == obj);
    739 
    740         try {
    741             alist.set(-1, obj);
    742             fail("Should throw IndexOutOfBoundsException");
    743         } catch (IndexOutOfBoundsException e) {
    744             // Expected
    745             assertNotNull(e.getMessage());
    746         }
    747 
    748         try {
    749             alist.set(alist.size(), obj);
    750             fail("Should throw IndexOutOfBoundsException");
    751         } catch (IndexOutOfBoundsException e) {
    752             // Expected
    753             assertNotNull(e.getMessage());
    754         }
    755 
    756         try {
    757             alist.set(-1, null);
    758             fail("Should throw IndexOutOfBoundsException");
    759         } catch (IndexOutOfBoundsException e) {
    760             // Expected
    761             assertNotNull(e.getMessage());
    762         }
    763 
    764         try {
    765             alist.set(alist.size(), null);
    766             fail("Should throw IndexOutOfBoundsException");
    767         } catch (IndexOutOfBoundsException e) {
    768             // Expected
    769             assertNotNull(e.getMessage());
    770         }
    771     }
    772 
    773     /**
    774      * @tests java.util.ArrayList#size()
    775      */
    776     public void test_size() {
    777         // Test for method int java.util.ArrayList.size()
    778         assertEquals("Returned incorrect size for exiting list", 100, alist
    779                 .size());
    780         assertEquals("Returned incorrect size for new list", 0, new ArrayList()
    781                 .size());
    782     }
    783 
    784     /**
    785      * @tests java.util.AbstractCollection#toString()
    786      */
    787     public void test_toString() {
    788         ArrayList l = new ArrayList(1);
    789         l.add(l);
    790         String result = l.toString();
    791         assertTrue("should contain self ref", result.indexOf("(this") > -1);
    792     }
    793 
    794     /**
    795      * @tests java.util.ArrayList#toArray()
    796      */
    797     public void test_toArray() {
    798         // Test for method java.lang.Object [] java.util.ArrayList.toArray()
    799         alist.set(25, null);
    800         alist.set(75, null);
    801         Object[] obj = alist.toArray();
    802         assertEquals("Returned array of incorrect size", objArray.length,
    803                 obj.length);
    804 
    805         for (int i = 0; i < obj.length; i++) {
    806             if ((i == 25) || (i == 75))
    807                 assertNull("Should be null at: " + i + " but instead got: "
    808                         + obj[i], obj[i]);
    809             else
    810                 assertTrue("Returned incorrect array: " + i,
    811                         obj[i] == objArray[i]);
    812         }
    813 
    814     }
    815 
    816     /**
    817      * @tests java.util.ArrayList#toArray(java.lang.Object[])
    818      */
    819     public void test_toArray$Ljava_lang_Object() {
    820         // Test for method java.lang.Object []
    821         // java.util.ArrayList.toArray(java.lang.Object [])
    822         alist.set(25, null);
    823         alist.set(75, null);
    824         Integer[] argArray = new Integer[100];
    825         Object[] retArray;
    826         retArray = alist.toArray(argArray);
    827         assertTrue("Returned different array than passed", retArray == argArray);
    828         argArray = new Integer[1000];
    829         retArray = alist.toArray(argArray);
    830         assertNull("Failed to set first extra element to null", argArray[alist
    831                 .size()]);
    832         for (int i = 0; i < 100; i++) {
    833             if ((i == 25) || (i == 75))
    834                 assertNull("Should be null: " + i, retArray[i]);
    835             else
    836                 assertTrue("Returned incorrect array: " + i,
    837                         retArray[i] == objArray[i]);
    838         }
    839     }
    840 
    841     /**
    842      * @tests java.util.ArrayList#trimToSize()
    843      */
    844     public void test_trimToSize() {
    845         // Test for method void java.util.ArrayList.trimToSize()
    846         for (int i = 99; i > 24; i--)
    847             alist.remove(i);
    848         ((ArrayList) alist).trimToSize();
    849         assertEquals("Returned incorrect size after trim", 25, alist.size());
    850         for (int i = 0; i < alist.size(); i++)
    851             assertTrue("Trimmed list contained incorrect elements", alist
    852                     .get(i) == objArray[i]);
    853         Vector v = new Vector();
    854         v.add("a");
    855         ArrayList al = new ArrayList(v);
    856         al.add("b");
    857         Iterator it = al.iterator();
    858         al.trimToSize();
    859         try {
    860             it.next();
    861             fail("should throw a ConcurrentModificationException");
    862         } catch (ConcurrentModificationException ioobe) {
    863             // expected
    864         }
    865     }
    866 
    867     /**
    868      * @test java.util.ArrayList#addAll(int, Collection)
    869      */
    870     public void test_addAll() {
    871         ArrayList list = new ArrayList();
    872         list.add("one");
    873         list.add("two");
    874         assertEquals(2, list.size());
    875 
    876         list.remove(0);
    877         assertEquals(1, list.size());
    878 
    879         ArrayList collection = new ArrayList();
    880         collection.add("1");
    881         collection.add("2");
    882         collection.add("3");
    883         assertEquals(3, collection.size());
    884 
    885         list.addAll(0, collection);
    886         assertEquals(4, list.size());
    887 
    888         list.remove(0);
    889         list.remove(0);
    890         assertEquals(2, list.size());
    891 
    892         collection.add("4");
    893         collection.add("5");
    894         collection.add("6");
    895         collection.add("7");
    896         collection.add("8");
    897         collection.add("9");
    898         collection.add("10");
    899         collection.add("11");
    900         collection.add("12");
    901 
    902         assertEquals(12, collection.size());
    903 
    904         list.addAll(0, collection);
    905         assertEquals(14, list.size());
    906     }
    907 
    908     public void testAddAllWithConcurrentCollection() {
    909         ArrayList<String> list = new ArrayList<String>();
    910         list.addAll(shrinksOnSize("A", "B", "C", "D"));
    911         assertFalse(list.contains(null));
    912     }
    913 
    914     public void testAddAllAtPositionWithConcurrentCollection() {
    915         ArrayList<String> list = new ArrayList<String>(
    916                 Arrays.asList("A", "B", "C", "D"));
    917 
    918         list.addAll(3, shrinksOnSize("E", "F", "G", "H"));
    919         assertFalse(list.contains(null));
    920     }
    921 
    922     public void test_override_size() throws Exception {
    923         ArrayList testlist = new MockArrayList();
    924         // though size is overriden, it should passed without exception
    925         testlist.add("test_0");
    926         testlist.add("test_1");
    927         testlist.add("test_2");
    928         testlist.add(1, "test_3");
    929         testlist.get(1);
    930         testlist.remove(2);
    931         testlist.set(1, "test_4");
    932     }
    933 
    934     public static class ArrayListExtend extends ArrayList {
    935 
    936         private int size = 0;
    937 
    938         public ArrayListExtend() {
    939             super(10);
    940         }
    941 
    942         public boolean add(Object o) {
    943             size++;
    944             return super.add(o);
    945         }
    946 
    947         public int size() {
    948             return size;
    949         }
    950     }
    951 
    952     public class MockArrayList extends ArrayList {
    953         public int size() {
    954             return 0;
    955         }
    956 
    957         public void removeRange(int start, int end) {
    958             super.removeRange(start, end);
    959         }
    960     }
    961 
    962     public void test_subclassing() {
    963         ArrayListExtend a = new ArrayListExtend();
    964         /*
    965          * Regression test for subclasses that override size() (which used to
    966          * cause an exception when growing 'a').
    967          */
    968         for (int i = 0; i < 100; i++) {
    969             a.add(new Object());
    970         }
    971     }
    972 
    973     /**
    974      * Sets up the fixture, for example, open a network connection. This method
    975      * is called before a test is executed.
    976      */
    977     protected void setUp() throws Exception {
    978         super.setUp();
    979         alist = new ArrayList();
    980         for (int i = 0; i < objArray.length; i++)
    981             alist.add(objArray[i]);
    982     }
    983 
    984     /**
    985      * Returns a collection that emulates another thread calling remove() each
    986      * time the current thread calls size().
    987      */
    988     private <T> Collection<T> shrinksOnSize(T... elements) {
    989         return new HashSet<T>(Arrays.asList(elements)) {
    990             boolean shrink = true;
    991 
    992             @Override
    993             public int size() {
    994                 int result = super.size();
    995                 if (shrink) {
    996                     Iterator<T> i = iterator();
    997                     i.next();
    998                     i.remove();
    999                 }
   1000                 return result;
   1001             }
   1002 
   1003             @Override
   1004             public Object[] toArray() {
   1005                 shrink = false;
   1006                 return super.toArray();
   1007             }
   1008         };
   1009     }
   1010 }
   1011