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 */
17package org.apache.harmony.tests.java.util;
18
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Collection;
22import java.util.ConcurrentModificationException;
23import java.util.HashSet;
24import java.util.Iterator;
25import java.util.List;
26import java.util.Set;
27import java.util.Vector;
28
29import tests.support.Support_ListTest;
30
31public class ArrayListTest extends junit.framework.TestCase {
32
33    List alist;
34
35    Object[] objArray;
36
37    /**
38     * java.util.ArrayList#ArrayList()
39     */
40    public void test_Constructor() {
41        // Test for method java.util.ArrayList()
42        new Support_ListTest("", alist).runTest();
43
44        ArrayList subList = new ArrayList();
45        for (int i = -50; i < 150; i++)
46            subList.add(new Integer(i));
47        new Support_ListTest("", subList.subList(50, 150)).runTest();
48    }
49
50    /**
51     * java.util.ArrayList#ArrayList(int)
52     */
53    public void test_ConstructorI() {
54        // Test for method java.util.ArrayList(int)
55        ArrayList al = new ArrayList(5);
56        assertEquals("Incorrect arrayList created", 0, al.size());
57
58        al = new ArrayList(0);
59        assertEquals("Incorrect arrayList created", 0, al.size());
60
61        try {
62            new ArrayList(-1);
63            fail("IllegalArgumentException expected");
64        } catch (IllegalArgumentException expected) {
65        }
66    }
67
68    /**
69     * java.util.ArrayList#ArrayList(java.util.Collection)
70     */
71    public void test_ConstructorLjava_util_Collection() {
72        // Test for method java.util.ArrayList(java.util.Collection)
73        ArrayList al = new ArrayList(Arrays.asList(objArray));
74        assertTrue("arrayList created from collection has incorrect size", al
75                .size() == objArray.length);
76        for (int counter = 0; counter < objArray.length; counter++)
77            assertTrue(
78                    "arrayList created from collection has incorrect elements",
79                    al.get(counter) == objArray[counter]);
80        try {
81            new ArrayList(null);
82            fail("NullPointerException expected");
83        } catch (NullPointerException e) {
84            //expected
85        }
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     * 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     * 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     * 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     * 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     * 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     * 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// BEGIN android-removed
342// The spec does not mandate that IndexOutOfBoundsException be thrown in
343// preference to NullPointerException when the caller desserves both.
344//
345//    /**
346//     * java.util.ArrayList#addAll(int, java.util.Collection)
347//     */
348//    public void test_addAllILjava_util_Collection_2() {
349//        // Regression for HARMONY-467
350//        ArrayList obj = new ArrayList();
351//        try {
352//            obj.addAll((int) -1, (Collection) null);
353//            fail("IndexOutOfBoundsException expected");
354//        } catch (IndexOutOfBoundsException e) {
355//        }
356//    }
357// END android-removed
358
359    /**
360     * java.util.ArrayList#addAll(java.util.Collection)
361     */
362    public void test_addAllLjava_util_Collection() {
363        // Test for method boolean
364        // java.util.ArrayList.addAll(java.util.Collection)
365        List l = new ArrayList();
366        l.addAll(alist);
367        for (int i = 0; i < alist.size(); i++)
368            assertTrue("Failed to add elements properly", l.get(i).equals(
369                    alist.get(i)));
370        alist.addAll(alist);
371        assertEquals("Returned incorrect size after adding to existing list",
372                200, alist.size());
373        for (int i = 0; i < 100; i++) {
374            assertTrue("Added to list in incorrect order", alist.get(i).equals(
375                    l.get(i)));
376            assertTrue("Failed to add to existing list", alist.get(i + 100)
377                    .equals(l.get(i)));
378        }
379        Set setWithNulls = new HashSet();
380        setWithNulls.add(null);
381        setWithNulls.add(null);
382        setWithNulls.add("yoink");
383        setWithNulls.add("kazoo");
384        setWithNulls.add(null);
385        alist.addAll(100, setWithNulls);
386        Iterator i = setWithNulls.iterator();
387        assertTrue("Item at slot 100 is wrong: " + alist.get(100), alist
388                .get(100) == i.next());
389        assertTrue("Item at slot 101 is wrong: " + alist.get(101), alist
390                .get(101) == i.next());
391        assertTrue("Item at slot 103 is wrong: " + alist.get(102), alist
392                .get(102) == i.next());
393
394        try {
395            alist.addAll(null);
396            fail("Should throw NullPointerException");
397        } catch (NullPointerException e) {
398            // Excepted
399        }
400
401        // Regression test for Harmony-3481
402        ArrayList<Integer> originalList = new ArrayList<Integer>(12);
403        for (int j = 0; j < 12; j++) {
404            originalList.add(j);
405        }
406
407        originalList.remove(0);
408        originalList.remove(0);
409
410        ArrayList<Integer> additionalList = new ArrayList<Integer>(11);
411        for (int j = 0; j < 11; j++) {
412            additionalList.add(j);
413        }
414        assertTrue(originalList.addAll(additionalList));
415        assertEquals(21, originalList.size());
416
417    }
418
419    public void test_ArrayList_addAll_scenario1() {
420        ArrayList arrayListA = new ArrayList();
421        arrayListA.add(1);
422        ArrayList arrayListB = new ArrayList();
423        arrayListB.add(1);
424        arrayListA.addAll(1, arrayListB);
425        int size = arrayListA.size();
426        assertEquals(2, size);
427        for (int index = 0; index < size; index++) {
428            assertEquals(1, arrayListA.get(index));
429        }
430    }
431
432    public void test_ArrayList_addAll_scenario2() {
433        ArrayList arrayList = new ArrayList();
434        arrayList.add(1);
435        arrayList.addAll(1, arrayList);
436        int size = arrayList.size();
437        assertEquals(2, size);
438        for (int index = 0; index < size; index++) {
439            assertEquals(1, arrayList.get(index));
440        }
441    }
442
443    // Regression test for HARMONY-5839
444    public void testaddAllHarmony5839() {
445        Collection coll = Arrays.asList(new String[] { "1", "2" });
446        List list = new ArrayList();
447        list.add("a");
448        list.add(0, "b");
449        list.add(0, "c");
450        list.add(0, "d");
451        list.add(0, "e");
452        list.add(0, "f");
453        list.add(0, "g");
454        list.add(0, "h");
455        list.add(0, "i");
456
457        list.addAll(6, coll);
458
459        assertEquals(11, list.size());
460        assertFalse(list.contains(null));
461    }
462
463    /**
464     * java.util.ArrayList#clear()
465     */
466    public void test_clear() {
467        // Test for method void java.util.ArrayList.clear()
468        alist.clear();
469        assertEquals("List did not clear", 0, alist.size());
470        alist.add(null);
471        alist.add(null);
472        alist.add(null);
473        alist.add("bam");
474        alist.clear();
475        assertEquals("List with nulls did not clear", 0, alist.size());
476        /*
477         * for (int i = 0; i < alist.size(); i++) assertNull("Failed to clear
478         * list", alist.get(i));
479         */
480
481    }
482
483    /**
484     * java.util.ArrayList#clone()
485     */
486    public void test_clone() {
487        // Test for method java.lang.Object java.util.ArrayList.clone()
488        ArrayList x = (ArrayList) (((ArrayList) (alist)).clone());
489        assertTrue("Cloned list was inequal to original", x.equals(alist));
490        for (int i = 0; i < alist.size(); i++)
491            assertTrue("Cloned list contains incorrect elements",
492                    alist.get(i) == x.get(i));
493
494        alist.add(null);
495        alist.add(25, null);
496        x = (ArrayList) (((ArrayList) (alist)).clone());
497        assertTrue("nulls test - Cloned list was inequal to original", x
498                .equals(alist));
499        for (int i = 0; i < alist.size(); i++)
500            assertTrue("nulls test - Cloned list contains incorrect elements",
501                    alist.get(i) == x.get(i));
502
503    }
504
505    /**
506     * java.util.ArrayList#contains(java.lang.Object)
507     */
508    public void test_containsLjava_lang_Object() {
509        // Test for method boolean
510        // java.util.ArrayList.contains(java.lang.Object)
511        assertTrue("Returned false for valid element", alist
512                .contains(objArray[99]));
513        assertTrue("Returned false for equal element", alist
514                .contains(new Integer(8)));
515        assertTrue("Returned true for invalid element", !alist
516                .contains(new Object()));
517        assertTrue("Returned true for null but should have returned false",
518                !alist.contains(null));
519        alist.add(null);
520        assertTrue("Returned false for null but should have returned true",
521                alist.contains(null));
522    }
523
524    /**
525     * java.util.ArrayList#ensureCapacity(int)
526     */
527    public void test_ensureCapacityI() throws Exception {
528        // Test for method void java.util.ArrayList.ensureCapacity(int)
529        // TODO : There is no good way to test this as it only really impacts on
530        // the private implementation.
531
532        Object testObject = new Object();
533        int capacity = 20;
534        ArrayList al = new ArrayList(capacity);
535        int i;
536        for (i = 0; i < capacity / 2; i++) {
537            al.add(i, new Object());
538        }
539        al.add(i, testObject);
540        int location = al.indexOf(testObject);
541        al.ensureCapacity(capacity);
542        assertTrue("EnsureCapacity moved objects around in array1.",
543                location == al.indexOf(testObject));
544        al.remove(0);
545        al.ensureCapacity(capacity);
546        assertTrue("EnsureCapacity moved objects around in array2.",
547                --location == al.indexOf(testObject));
548        al.ensureCapacity(capacity + 2);
549        assertTrue("EnsureCapacity did not change location.", location == al
550                .indexOf(testObject));
551
552        ArrayList<String> list = new ArrayList<String>(1);
553        list.add("hello");
554        list.ensureCapacity(Integer.MIN_VALUE);
555    }
556
557    /**
558     * java.util.ArrayList#get(int)
559     */
560    public void test_getI() {
561        // Test for method java.lang.Object java.util.ArrayList.get(int)
562        assertTrue("Returned incorrect element", alist.get(22) == objArray[22]);
563        try {
564            alist.get(8765);
565            fail("Failed to throw expected exception for index > size");
566        } catch (IndexOutOfBoundsException e) {
567            // Expected
568            assertNotNull(e.getMessage());
569        }
570    }
571
572    /**
573     * java.util.ArrayList#indexOf(java.lang.Object)
574     */
575    public void test_indexOfLjava_lang_Object() {
576        // Test for method int java.util.ArrayList.indexOf(java.lang.Object)
577        assertEquals("Returned incorrect index", 87, alist
578                .indexOf(objArray[87]));
579        assertEquals("Returned index for invalid Object", -1, alist
580                .indexOf(new Object()));
581        alist.add(25, null);
582        alist.add(50, null);
583        assertTrue("Wrong indexOf for null.  Wanted 25 got: "
584                + alist.indexOf(null), alist.indexOf(null) == 25);
585    }
586
587    /**
588     * java.util.ArrayList#isEmpty()
589     */
590    public void test_isEmpty() {
591        // Test for method boolean java.util.ArrayList.isEmpty()
592        assertTrue("isEmpty returned false for new list", new ArrayList()
593                .isEmpty());
594        assertTrue("Returned true for existing list with elements", !alist
595                .isEmpty());
596    }
597
598    /**
599     * java.util.ArrayList#lastIndexOf(java.lang.Object)
600     */
601    public void test_lastIndexOfLjava_lang_Object() {
602        // Test for method int java.util.ArrayList.lastIndexOf(java.lang.Object)
603        alist.add(new Integer(99));
604        assertEquals("Returned incorrect index", 100, alist
605                .lastIndexOf(objArray[99]));
606        assertEquals("Returned index for invalid Object", -1, alist
607                .lastIndexOf(new Object()));
608        alist.add(25, null);
609        alist.add(50, null);
610        assertTrue("Wrong lastIndexOf for null.  Wanted 50 got: "
611                + alist.lastIndexOf(null), alist.lastIndexOf(null) == 50);
612    }
613
614    /**
615     * {@link java.util.ArrayList#removeRange(int, int)}
616     */
617    public void test_removeRange() {
618        MockArrayList mylist = new MockArrayList();
619        mylist.removeRange(0, 0);
620
621        try {
622            mylist.removeRange(0, 1);
623            fail("Should throw IndexOutOfBoundsException");
624        } catch (IndexOutOfBoundsException e) {
625            // Expected
626            assertNotNull(e.getMessage());
627        }
628
629        int[] data = { 1, 2, 3 };
630        for (int i = 0; i < data.length; i++) {
631            mylist.add(i, data[i]);
632        }
633
634        mylist.removeRange(0, 1);
635        assertEquals(data[1], mylist.get(0));
636        assertEquals(data[2], mylist.get(1));
637
638        try {
639            mylist.removeRange(-1, 1);
640            fail("Should throw IndexOutOfBoundsException");
641        } catch (IndexOutOfBoundsException e) {
642            // Expected
643            assertNotNull(e.getMessage());
644        }
645
646        try {
647            mylist.removeRange(0, -1);
648            fail("Should throw IndexOutOfBoundsException");
649        } catch (IndexOutOfBoundsException e) {
650            // Expected
651            assertNotNull(e.getMessage());
652        }
653
654        try {
655            mylist.removeRange(1, 0);
656            fail("Should throw IndexOutOfBoundsException");
657        } catch (IndexOutOfBoundsException e) {
658            // Expected
659            assertNotNull(e.getMessage());
660        }
661
662        try {
663            mylist.removeRange(2, 1);
664            fail("Should throw IndexOutOfBoundsException");
665        } catch (IndexOutOfBoundsException e) {
666            // Expected
667            assertNotNull(e.getMessage());
668        }
669    }
670
671    /**
672     * java.util.ArrayList#remove(int)
673     */
674    public void test_removeI() {
675        // Test for method java.lang.Object java.util.ArrayList.remove(int)
676        alist.remove(10);
677        assertEquals("Failed to remove element", -1, alist
678                .indexOf(objArray[10]));
679        try {
680            alist.remove(999);
681            fail("Failed to throw exception when index out of range");
682        } catch (IndexOutOfBoundsException e) {
683            // Expected
684            assertNotNull(e.getMessage());
685        }
686
687        ArrayList myList = (ArrayList) (((ArrayList) (alist)).clone());
688        alist.add(25, null);
689        alist.add(50, null);
690        alist.remove(50);
691        alist.remove(25);
692        assertTrue("Removing nulls did not work", alist.equals(myList));
693
694        List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
695                "d", "e", "f", "g" }));
696        assertTrue("Removed wrong element 1", list.remove(0) == "a");
697        assertTrue("Removed wrong element 2", list.remove(4) == "f");
698        String[] result = new String[5];
699        list.toArray(result);
700        assertTrue("Removed wrong element 3", Arrays.equals(result,
701                new String[] { "b", "c", "d", "e", "g" }));
702
703        List l = new ArrayList(0);
704        l.add(new Object());
705        l.add(new Object());
706        l.remove(0);
707        l.remove(0);
708        try {
709            l.remove(-1);
710            fail("-1 should cause exception");
711        } catch (IndexOutOfBoundsException e) {
712            // Expected
713            assertNotNull(e.getMessage());
714        }
715        try {
716            l.remove(0);
717            fail("0 should case exception");
718        } catch (IndexOutOfBoundsException e) {
719            // Expected
720            assertNotNull(e.getMessage());
721        }
722    }
723
724    /**
725     * java.util.ArrayList#set(int, java.lang.Object)
726     */
727    public void test_setILjava_lang_Object() {
728        // Test for method java.lang.Object java.util.ArrayList.set(int,
729        // java.lang.Object)
730        Object obj;
731        alist.set(65, obj = new Object());
732        assertTrue("Failed to set object", alist.get(65) == obj);
733        alist.set(50, null);
734        assertNull("Setting to null did not work", alist.get(50));
735        assertTrue("Setting increased the list's size to: " + alist.size(),
736                alist.size() == 100);
737
738        obj = new Object();
739        alist.set(0, obj);
740        assertTrue("Failed to set object", alist.get(0) == obj);
741
742        try {
743            alist.set(-1, obj);
744            fail("Should throw IndexOutOfBoundsException");
745        } catch (IndexOutOfBoundsException e) {
746            // Expected
747            assertNotNull(e.getMessage());
748        }
749
750        try {
751            alist.set(alist.size(), obj);
752            fail("Should throw IndexOutOfBoundsException");
753        } catch (IndexOutOfBoundsException e) {
754            // Expected
755            assertNotNull(e.getMessage());
756        }
757
758        try {
759            alist.set(-1, null);
760            fail("Should throw IndexOutOfBoundsException");
761        } catch (IndexOutOfBoundsException e) {
762            // Expected
763            assertNotNull(e.getMessage());
764        }
765
766        try {
767            alist.set(alist.size(), null);
768            fail("Should throw IndexOutOfBoundsException");
769        } catch (IndexOutOfBoundsException e) {
770            // Expected
771            assertNotNull(e.getMessage());
772        }
773    }
774
775    /**
776     * java.util.ArrayList#size()
777     */
778    public void test_size() {
779        // Test for method int java.util.ArrayList.size()
780        assertEquals("Returned incorrect size for exiting list", 100, alist
781                .size());
782        assertEquals("Returned incorrect size for new list", 0, new ArrayList()
783                .size());
784    }
785
786    /**
787     * java.util.AbstractCollection#toString()
788     */
789    public void test_toString() {
790        ArrayList l = new ArrayList(1);
791        l.add(l);
792        String result = l.toString();
793        assertTrue("should contain self ref", result.indexOf("(this") > -1);
794    }
795
796    /**
797     * java.util.ArrayList#toArray()
798     */
799    public void test_toArray() {
800        // Test for method java.lang.Object [] java.util.ArrayList.toArray()
801        alist.set(25, null);
802        alist.set(75, null);
803        Object[] obj = alist.toArray();
804        assertEquals("Returned array of incorrect size", objArray.length,
805                obj.length);
806
807        for (int i = 0; i < obj.length; i++) {
808            if ((i == 25) || (i == 75))
809                assertNull("Should be null at: " + i + " but instead got: "
810                        + obj[i], obj[i]);
811            else
812                assertTrue("Returned incorrect array: " + i,
813                        obj[i] == objArray[i]);
814        }
815
816    }
817
818    /**
819     * java.util.ArrayList#toArray(java.lang.Object[])
820     */
821    public void test_toArray$Ljava_lang_Object() {
822        // Test for method java.lang.Object []
823        // java.util.ArrayList.toArray(java.lang.Object [])
824        alist.set(25, null);
825        alist.set(75, null);
826        Integer[] argArray = new Integer[100];
827        Object[] retArray;
828        retArray = alist.toArray(argArray);
829        assertTrue("Returned different array than passed", retArray == argArray);
830        argArray = new Integer[1000];
831        retArray = alist.toArray(argArray);
832        assertNull("Failed to set first extra element to null", argArray[alist
833                .size()]);
834        for (int i = 0; i < 100; i++) {
835            if ((i == 25) || (i == 75))
836                assertNull("Should be null: " + i, retArray[i]);
837            else
838                assertTrue("Returned incorrect array: " + i,
839                        retArray[i] == objArray[i]);
840        }
841
842        String[] strArray = new String[100];
843        try {
844            alist.toArray(strArray);
845            fail("ArrayStoreException expected");
846        } catch (ArrayStoreException e) {
847            //expected
848        }
849    }
850
851    /**
852     * java.util.ArrayList#trimToSize()
853     */
854    public void test_trimToSize() {
855        // Test for method void java.util.ArrayList.trimToSize()
856        for (int i = 99; i > 24; i--)
857            alist.remove(i);
858        ((ArrayList) alist).trimToSize();
859        assertEquals("Returned incorrect size after trim", 25, alist.size());
860        for (int i = 0; i < alist.size(); i++)
861            assertTrue("Trimmed list contained incorrect elements", alist
862                    .get(i) == objArray[i]);
863        Vector v = new Vector();
864        v.add("a");
865        ArrayList al = new ArrayList(v);
866        al.add("b");
867        Iterator it = al.iterator();
868        al.trimToSize();
869        try {
870            it.next();
871            fail("should throw a ConcurrentModificationException");
872        } catch (ConcurrentModificationException ioobe) {
873            // expected
874        }
875    }
876
877    public void test_trimToSize_02() {
878        ArrayList list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
879                "d", "e", "f", "g" }));
880        list.remove("a");
881        list.remove("f");
882        list.trimToSize();
883    }
884
885    public void test_addAll() {
886        ArrayList list = new ArrayList();
887        list.add("one");
888        list.add("two");
889        assertEquals(2, list.size());
890
891        list.remove(0);
892        assertEquals(1, list.size());
893
894        ArrayList collection = new ArrayList();
895        collection.add("1");
896        collection.add("2");
897        collection.add("3");
898        assertEquals(3, collection.size());
899
900        list.addAll(0, collection);
901        assertEquals(4, list.size());
902
903        list.remove(0);
904        list.remove(0);
905        assertEquals(2, list.size());
906
907        collection.add("4");
908        collection.add("5");
909        collection.add("6");
910        collection.add("7");
911        collection.add("8");
912        collection.add("9");
913        collection.add("10");
914        collection.add("11");
915        collection.add("12");
916
917        assertEquals(12, collection.size());
918
919        list.addAll(0, collection);
920        assertEquals(14, list.size());
921    }
922
923    public void test_removeLjava_lang_Object() {
924        List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
925                "d", "e", "f", "g" }));
926        assertTrue("Removed wrong element 1", list.remove("a"));
927        assertTrue("Removed wrong element 2", list.remove("f"));
928        String[] result = new String[5];
929        list.toArray(result);
930        assertTrue("Removed wrong element 3", Arrays.equals(result,
931                new String[] { "b", "c", "d", "e", "g" }));
932    }
933
934    public void testAddAllWithConcurrentCollection() {
935        ArrayList<String> list = new ArrayList<String>();
936        list.addAll(shrinksOnSize("A", "B", "C", "D"));
937        assertFalse(list.contains(null));
938    }
939
940    public void testAddAllAtPositionWithConcurrentCollection() {
941        ArrayList<String> list = new ArrayList<String>(
942                Arrays.asList("A", "B", "C", "D"));
943
944        list.addAll(3, shrinksOnSize("E", "F", "G", "H"));
945        assertFalse(list.contains(null));
946    }
947
948    public void test_override_size() throws Exception {
949        ArrayList testlist = new MockArrayList();
950        // though size is overriden, it should passed without exception
951        testlist.add("test_0");
952        testlist.add("test_1");
953        testlist.add("test_2");
954        testlist.add(1, "test_3");
955        testlist.get(1);
956        testlist.remove(2);
957        testlist.set(1, "test_4");
958    }
959
960    public class MockArrayList extends ArrayList {
961        public int size() {
962            return 0;
963        }
964
965        public void removeRange(int begin, int end) {
966            super.removeRange(begin, end);
967        }
968    }
969
970    public void test_removeRangeII() {
971        MockArrayList mal = new MockArrayList();
972        mal.add("a");
973        mal.add("b");
974        mal.add("c");
975        mal.add("d");
976        mal.add("e");
977        mal.add("f");
978        mal.add("g");
979        mal.add("h");
980
981        mal.removeRange(2, 4);
982
983        String[] result = new String[6];
984        mal.toArray(result);
985        assertTrue("Removed wrong element 3", Arrays.equals(result,
986                new String[] { "a", "b", "e", "f", "g", "h"}));
987    }
988
989    public static class ArrayListExtend extends ArrayList {
990
991        private int size = 0;
992
993        public ArrayListExtend() {
994            super(10);
995        }
996
997        public boolean add(Object o) {
998            size++;
999            return super.add(o);
1000        }
1001
1002        public int size() {
1003            return size;
1004        }
1005    }
1006
1007    public void test_subclassing() {
1008        ArrayListExtend a = new ArrayListExtend();
1009        /*
1010         * Regression test for subclasses that override size() (which used to
1011         * cause an exception when growing 'a').
1012         */
1013        for (int i = 0; i < 100; i++) {
1014            a.add(new Object());
1015        }
1016    }
1017
1018
1019    /**
1020     * Sets up the fixture, for example, open a network connection. This method
1021     * is called before a test is executed.
1022     */
1023    protected void setUp() throws Exception {
1024        super.setUp();
1025
1026        objArray = new Object[100];
1027        for (int i = 0; i < objArray.length; i++) {
1028            objArray[i] = new Integer(i);
1029        }
1030
1031        alist = new ArrayList();
1032        for (int i = 0; i < objArray.length; i++) {
1033            alist.add(objArray[i]);
1034        }
1035    }
1036
1037    @Override
1038    protected void tearDown() throws Exception {
1039        objArray = null;
1040        alist = null;
1041
1042        super.tearDown();
1043    }
1044
1045    /**
1046     * Returns a collection that emulates another thread calling remove() each
1047     * time the current thread calls size().
1048     */
1049    private <T> Collection<T> shrinksOnSize(T... elements) {
1050        return new HashSet<T>(Arrays.asList(elements)) {
1051            boolean shrink = true;
1052
1053            @Override
1054            public int size() {
1055                int result = super.size();
1056                if (shrink) {
1057                    Iterator<T> i = iterator();
1058                    i.next();
1059                    i.remove();
1060                }
1061                return result;
1062            }
1063
1064            @Override
1065            public Object[] toArray() {
1066                shrink = false;
1067                return super.toArray();
1068            }
1069        };
1070    }
1071}
1072