ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.23 by jsr166, Sat Jun 18 14:32:14 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
10 > import java.util.Arrays;
11 > import java.util.Iterator;
12 > import java.util.LinkedList;
13 > import java.util.List;
14 > import java.util.ListIterator;
15 > import java.util.Vector;
16 > import java.util.concurrent.CopyOnWriteArrayList;
17 >
18 > public class CopyOnWriteArrayListTest extends JSR166TestCase {
19  
13 public class CopyOnWriteArrayListTest extends JSR166TestCase{
14    
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());  
21 >        junit.textui.TestRunner.run(suite());
22      }
23  
24      public static Test suite() {
25 <        return new TestSuite(CopyOnWriteArrayListTest.class);
25 >        return new TestSuite(CopyOnWriteArrayListTest.class);
26      }
27  
28 <    static CopyOnWriteArrayList populatedArray(int n){
29 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
28 >    static CopyOnWriteArrayList populatedArray(int n) {
29 >        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
30          assertTrue(a.isEmpty());
31 <        for (int i = 0; i < n; ++i)
31 >        for (int i = 0; i < n; ++i)
32              a.add(new Integer(i));
33          assertFalse(a.isEmpty());
34          assertEquals(n, a.size());
35          return a;
36      }
37  
38 <
38 >    /**
39 >     * a new list is empty
40 >     */
41      public void testConstructor() {
42 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
42 >        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
43          assertTrue(a.isEmpty());
44      }
45  
46 +    /**
47 +     * new list contains all elements of initializing array
48 +     */
49      public void testConstructor2() {
50          Integer[] ints = new Integer[SIZE];
51          for (int i = 0; i < SIZE-1; ++i)
52              ints[i] = new Integer(i);
53 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
54 <        for (int i = 0; i < SIZE; ++i)
53 >        CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
54 >        for (int i = 0; i < SIZE; ++i)
55              assertEquals(ints[i], a.get(i));
56      }
57  
58 +    /**
59 +     * new list contains all elements of initializing collection
60 +     */
61      public void testConstructor3() {
62          Integer[] ints = new Integer[SIZE];
63          for (int i = 0; i < SIZE-1; ++i)
64              ints[i] = new Integer(i);
65 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
66 <        for (int i = 0; i < SIZE; ++i)
65 >        CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
66 >        for (int i = 0; i < SIZE; ++i)
67              assertEquals(ints[i], a.get(i));
68      }
56        
69  
70      /**
71 <     *   addAll correctly adds each element from the given collection
71 >     * addAll adds each element from the given collection
72       */
73 <    public void testAddAll(){
74 <        CopyOnWriteArrayList full = populatedArray(3);
75 <        Vector v = new Vector();
76 <        v.add(three);
77 <        v.add(four);
78 <        v.add(five);
79 <        full.addAll(v);
80 <        assertEquals(6, full.size());
73 >    public void testAddAll() {
74 >        CopyOnWriteArrayList full = populatedArray(3);
75 >        Vector v = new Vector();
76 >        v.add(three);
77 >        v.add(four);
78 >        v.add(five);
79 >        full.addAll(v);
80 >        assertEquals(6, full.size());
81      }
82  
83      /**
84 <     *   addAllAbsent adds each element from the given collection that did not
85 <     *  already exist in the List
86 <     */
87 <    public void testAddAllAbsent(){
88 <        CopyOnWriteArrayList full = populatedArray(3);
89 <        Vector v = new Vector();
90 <        v.add(three);
91 <        v.add(four);
92 <        v.add(one); // will not add this element
93 <        full.addAllAbsent(v);
94 <        assertEquals(5, full.size());
84 >     * addAllAbsent adds each element from the given collection that did not
85 >     * already exist in the List
86 >     */
87 >    public void testAddAllAbsent() {
88 >        CopyOnWriteArrayList full = populatedArray(3);
89 >        Vector v = new Vector();
90 >        v.add(three);
91 >        v.add(four);
92 >        v.add(one); // will not add this element
93 >        full.addAllAbsent(v);
94 >        assertEquals(5, full.size());
95      }
96  
97      /**
98 <     *   addIfAbsent will not add the element if it already exists in the list
98 >     * addIfAbsent will not add the element if it already exists in the list
99       */
100 <    public void testAddIfAbsent(){
101 <        CopyOnWriteArrayList full = populatedArray(3);
102 <        full.addIfAbsent(one);
103 <        assertEquals(3, full.size());
100 >    public void testAddIfAbsent() {
101 >        CopyOnWriteArrayList full = populatedArray(SIZE);
102 >        full.addIfAbsent(one);
103 >        assertEquals(SIZE, full.size());
104      }
105  
106      /**
107 <     *   addIfAbsent correctly adds the element when it does not exist in the list
107 >     * addIfAbsent adds the element when it does not exist in the list
108       */
109 <    public void testAddIfAbsent2(){
110 <        CopyOnWriteArrayList full = populatedArray(3);
109 >    public void testAddIfAbsent2() {
110 >        CopyOnWriteArrayList full = populatedArray(SIZE);
111          full.addIfAbsent(three);
112          assertTrue(full.contains(three));
113      }
114  
115      /**
116 <     *   clear correctly removes all elements from the list
116 >     * clear removes all elements from the list
117 >     */
118 >    public void testClear() {
119 >        CopyOnWriteArrayList full = populatedArray(SIZE);
120 >        full.clear();
121 >        assertEquals(0, full.size());
122 >    }
123 >
124 >    /**
125 >     * Cloned list is equal
126       */
127 <    public void testClear(){
128 <        CopyOnWriteArrayList full = populatedArray(3);
129 <        full.clear();
130 <        assertEquals(0, full.size());
127 >    public void testClone() {
128 >        CopyOnWriteArrayList l1 = populatedArray(SIZE);
129 >        CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
130 >        assertEquals(l1, l2);
131 >        l1.clear();
132 >        assertFalse(l1.equals(l2));
133      }
134  
135      /**
136 <     *   contains returns the correct values
136 >     * contains is true for added elements
137       */
138 <    public void testContains(){
139 <        CopyOnWriteArrayList full = populatedArray(3);
140 <        assertTrue(full.contains(one));
141 <        assertFalse(full.contains(five));
138 >    public void testContains() {
139 >        CopyOnWriteArrayList full = populatedArray(3);
140 >        assertTrue(full.contains(one));
141 >        assertFalse(full.contains(five));
142      }
143  
144 +    /**
145 +     * adding at an index places it in the indicated index
146 +     */
147      public void testAddIndex() {
148 <        CopyOnWriteArrayList full = populatedArray(3);
148 >        CopyOnWriteArrayList full = populatedArray(3);
149          full.add(0, m1);
150          assertEquals(4, full.size());
151          assertEquals(m1, full.get(0));
# Line 131 | Line 157 | public class CopyOnWriteArrayListTest ex
157          assertEquals(two, full.get(4));
158      }
159  
160 +    /**
161 +     * lists with same elements are equal and have same hashCode
162 +     */
163      public void testEquals() {
164 <        CopyOnWriteArrayList a = populatedArray(3);
165 <        CopyOnWriteArrayList b = populatedArray(3);
164 >        CopyOnWriteArrayList a = populatedArray(3);
165 >        CopyOnWriteArrayList b = populatedArray(3);
166          assertTrue(a.equals(b));
167          assertTrue(b.equals(a));
168          assertEquals(a.hashCode(), b.hashCode());
# Line 146 | Line 175 | public class CopyOnWriteArrayListTest ex
175          assertEquals(a.hashCode(), b.hashCode());
176      }
177  
149    
178      /**
179 <     *   containsAll returns the correct values
179 >     * containsAll returns true for collection with subset of elements
180       */
181 <    public void testContainsAll(){
182 <        CopyOnWriteArrayList full = populatedArray(3);
183 <        Vector v = new Vector();
184 <        v.add(one);
185 <        v.add(two);
186 <        assertTrue(full.containsAll(v));
187 <        v.add(six);
188 <        assertFalse(full.containsAll(v));
181 >    public void testContainsAll() {
182 >        CopyOnWriteArrayList full = populatedArray(3);
183 >        Vector v = new Vector();
184 >        v.add(one);
185 >        v.add(two);
186 >        assertTrue(full.containsAll(v));
187 >        v.add(six);
188 >        assertFalse(full.containsAll(v));
189      }
190  
191      /**
192 <     *   get returns the correct value for the given index
192 >     * get returns the value at the given index
193       */
194 <    public void testGet(){
195 <        CopyOnWriteArrayList full = populatedArray(3);
196 <        assertEquals(0, ((Integer)full.get(0)).intValue());
194 >    public void testGet() {
195 >        CopyOnWriteArrayList full = populatedArray(3);
196 >        assertEquals(0, full.get(0));
197      }
198  
199      /**
200 <     *   indexOf gives the correct index for the given object
200 >     * indexOf gives the index for the given object
201       */
202 <    public void testIndexOf(){
203 <        CopyOnWriteArrayList full = populatedArray(3);
204 <        assertEquals(1, full.indexOf(one));
205 <        assertEquals(-1, full.indexOf("puppies"));
202 >    public void testIndexOf() {
203 >        CopyOnWriteArrayList full = populatedArray(3);
204 >        assertEquals(1, full.indexOf(one));
205 >        assertEquals(-1, full.indexOf("puppies"));
206      }
207  
208      /**
209 <     *   indexOf gives the correct index based on the given index
210 <     *  at which to start searching
209 >     * indexOf gives the index based on the given index
210 >     * at which to start searching
211       */
212 <    public void testIndexOf2(){
213 <        CopyOnWriteArrayList full = populatedArray(3);
214 <        assertEquals(1, full.indexOf(one, 0));
215 <        assertEquals(-1, full.indexOf(one, 2));
212 >    public void testIndexOf2() {
213 >        CopyOnWriteArrayList full = populatedArray(3);
214 >        assertEquals(1, full.indexOf(one, 0));
215 >        assertEquals(-1, full.indexOf(one, 2));
216      }
217  
218      /**
219 <     *   isEmpty returns the correct values
219 >     * isEmpty returns true when empty, else false
220       */
221 <    public void testIsEmpty(){
222 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
223 <        CopyOnWriteArrayList full = populatedArray(3);
224 <        assertTrue(empty.isEmpty());
225 <        assertFalse(full.isEmpty());
221 >    public void testIsEmpty() {
222 >        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
223 >        CopyOnWriteArrayList full = populatedArray(SIZE);
224 >        assertTrue(empty.isEmpty());
225 >        assertFalse(full.isEmpty());
226      }
227  
228      /**
229 <     *   iterator() returns an iterator containing the elements of the list
229 >     * iterator() returns an iterator containing the elements of the list
230       */
231 <    public void testIterator(){
232 <        CopyOnWriteArrayList full = populatedArray(3);
233 <        Iterator i = full.iterator();
234 <        int j;
235 <        for(j = 0; i.hasNext(); j++)
236 <            assertEquals(j, ((Integer)i.next()).intValue());
237 <        assertEquals(3, j);
231 >    public void testIterator() {
232 >        CopyOnWriteArrayList full = populatedArray(SIZE);
233 >        Iterator i = full.iterator();
234 >        int j;
235 >        for (j = 0; i.hasNext(); j++)
236 >            assertEquals(j, i.next());
237 >        assertEquals(SIZE, j);
238      }
239  
240 <    public void testIteratorRemove () {
241 <        CopyOnWriteArrayList full = populatedArray(3);
240 >    /**
241 >     * iterator.remove throws UnsupportedOperationException
242 >     */
243 >    public void testIteratorRemove() {
244 >        CopyOnWriteArrayList full = populatedArray(SIZE);
245          Iterator it = full.iterator();
246          it.next();
247          try {
248              it.remove();
249 <            fail("should throw");
250 <        }
220 <        catch (UnsupportedOperationException success) {}
249 >            shouldThrow();
250 >        } catch (UnsupportedOperationException success) {}
251      }
252  
253 <    public void testToString(){
254 <        CopyOnWriteArrayList full = populatedArray(3);
253 >    /**
254 >     * toString contains toString of elements
255 >     */
256 >    public void testToString() {
257 >        CopyOnWriteArrayList full = populatedArray(3);
258          String s = full.toString();
259          for (int i = 0; i < 3; ++i) {
260 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
260 >            assertTrue(s.contains(String.valueOf(i)));
261          }
262 <    }        
262 >    }
263  
264      /**
265 <     *   lastIndexOf returns the correct index for the given object
265 >     * lastIndexOf returns the index for the given object
266       */
267 <    public void testLastIndexOf1(){
268 <        CopyOnWriteArrayList full = populatedArray(3);
269 <        full.add(one);
270 <        full.add(three);
271 <        assertEquals(3, full.lastIndexOf(one));
272 <        assertEquals(-1, full.lastIndexOf(six));
267 >    public void testLastIndexOf1() {
268 >        CopyOnWriteArrayList full = populatedArray(3);
269 >        full.add(one);
270 >        full.add(three);
271 >        assertEquals(3, full.lastIndexOf(one));
272 >        assertEquals(-1, full.lastIndexOf(six));
273      }
274  
275      /**
276 <     *   lastIndexOf returns the correct index from the given starting point
276 >     * lastIndexOf returns the index from the given starting point
277       */
278 <    public void testlastIndexOf2(){
279 <        CopyOnWriteArrayList full = populatedArray(3);
280 <        full.add(one);
281 <        full.add(three);
282 <        assertEquals(3, full.lastIndexOf(one, 4));
283 <        assertEquals(-1, full.lastIndexOf(three, 3));
278 >    public void testLastIndexOf2() {
279 >        CopyOnWriteArrayList full = populatedArray(3);
280 >        full.add(one);
281 >        full.add(three);
282 >        assertEquals(3, full.lastIndexOf(one, 4));
283 >        assertEquals(-1, full.lastIndexOf(three, 3));
284      }
285  
286      /**
287 <     *  Identical to testIterator, except ListInterator has more functionality
287 >     * listIterator traverses all elements
288       */
289 <    public void testListIterator1(){
290 <        CopyOnWriteArrayList full = populatedArray(3);
291 <        ListIterator i = full.listIterator();
292 <        int j;
293 <        for(j = 0; i.hasNext(); j++)
294 <            assertEquals(j, ((Integer)i.next()).intValue());
295 <        assertEquals(3, j);
289 >    public void testListIterator1() {
290 >        CopyOnWriteArrayList full = populatedArray(SIZE);
291 >        ListIterator i = full.listIterator();
292 >        int j;
293 >        for (j = 0; i.hasNext(); j++)
294 >            assertEquals(j, i.next());
295 >        assertEquals(SIZE, j);
296      }
297  
298      /**
299 <     *  Identical to testIterator and testListIterator1, but only returns those elements
267 <     *  after the given index
299 >     * listIterator only returns those elements after the given index
300       */
301 <    public void testListIterator2(){
302 <        CopyOnWriteArrayList full = populatedArray(3);
303 <        ListIterator i = full.listIterator(1);
304 <        int j;
305 <        for(j = 0; i.hasNext(); j++)
306 <            assertEquals(j+1, ((Integer)i.next()).intValue());
307 <        assertEquals(2, j);
301 >    public void testListIterator2() {
302 >        CopyOnWriteArrayList full = populatedArray(3);
303 >        ListIterator i = full.listIterator(1);
304 >        int j;
305 >        for (j = 0; i.hasNext(); j++)
306 >            assertEquals(j+1, i.next());
307 >        assertEquals(2, j);
308      }
309  
310      /**
311 <     *   remove correctly removes and returns the object at the given index
311 >     * remove removes and returns the object at the given index
312       */
313 <    public void testRemove(){
314 <        CopyOnWriteArrayList full = populatedArray(3);
315 <        assertEquals(two, full.remove(2));
316 <        assertEquals(2, full.size());
313 >    public void testRemove() {
314 >        CopyOnWriteArrayList full = populatedArray(3);
315 >        assertEquals(2, full.remove(2));
316 >        assertEquals(2, full.size());
317      }
318  
319      /**
320 <     *   removeAll correctly removes all elements from the given collection
320 >     * removeAll removes all elements from the given collection
321       */
322 <    public void testRemoveAll(){
323 <        CopyOnWriteArrayList full = populatedArray(3);
324 <        Vector v = new Vector();
325 <        v.add(one);
326 <        v.add(two);
327 <        full.removeAll(v);
328 <        assertEquals(1, full.size());
322 >    public void testRemoveAll() {
323 >        CopyOnWriteArrayList full = populatedArray(3);
324 >        Vector v = new Vector();
325 >        v.add(one);
326 >        v.add(two);
327 >        full.removeAll(v);
328 >        assertEquals(1, full.size());
329      }
330  
331      /**
332 <     *   set correctly changes the element at the given index
332 >     * set changes the element at the given index
333       */
334 <    public void testSet(){
335 <        CopyOnWriteArrayList full = populatedArray(3);
336 <        assertEquals(two, full.set(2, four));
337 <        assertEquals(4, ((Integer)full.get(2)).intValue());
334 >    public void testSet() {
335 >        CopyOnWriteArrayList full = populatedArray(3);
336 >        assertEquals(2, full.set(2, four));
337 >        assertEquals(4, full.get(2));
338      }
339  
340      /**
341 <     *   size returns the correct values
341 >     * size returns the number of elements
342       */
343 <    public void testSize(){
344 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
345 <        CopyOnWriteArrayList full = populatedArray(3);
346 <        assertEquals(3, full.size());
347 <        assertEquals(0, empty.size());
343 >    public void testSize() {
344 >        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
345 >        CopyOnWriteArrayList full = populatedArray(SIZE);
346 >        assertEquals(SIZE, full.size());
347 >        assertEquals(0, empty.size());
348      }
349  
350      /**
351 <     *   toArray returns an Object array containing all elements from the list
351 >     * toArray returns an Object array containing all elements from the list
352       */
353 <    public void testToArray(){
354 <        CopyOnWriteArrayList full = populatedArray(3);
355 <        Object[] o = full.toArray();
356 <        assertEquals(3, o.length);
357 <        assertEquals(0, ((Integer)o[0]).intValue());
358 <        assertEquals(1, ((Integer)o[1]).intValue());
359 <        assertEquals(2, ((Integer)o[2]).intValue());
353 >    public void testToArray() {
354 >        CopyOnWriteArrayList full = populatedArray(3);
355 >        Object[] o = full.toArray();
356 >        assertEquals(3, o.length);
357 >        assertEquals(0, o[0]);
358 >        assertEquals(1, o[1]);
359 >        assertEquals(2, o[2]);
360      }
361  
362      /**
363 <     *  test to verify toArray returns an Integer array containing all elements from the list
363 >     * toArray returns an Integer array containing all elements from
364 >     * the list
365       */
366 <    public void testToArray2(){
367 <        CopyOnWriteArrayList full = populatedArray(3);
368 <        Integer[] i = new Integer[3];
369 <        i = (Integer[])full.toArray(i);
370 <        assertEquals(3, i.length);
371 <        assertEquals(0, i[0].intValue());
372 <        assertEquals(1, i[1].intValue());
373 <        assertEquals(2, i[2].intValue());
366 >    public void testToArray2() {
367 >        CopyOnWriteArrayList full = populatedArray(3);
368 >        Integer[] i = new Integer[3];
369 >        i = (Integer[])full.toArray(i);
370 >        assertEquals(3, i.length);
371 >        assertEquals(0, i[0].intValue());
372 >        assertEquals(1, i[1].intValue());
373 >        assertEquals(2, i[2].intValue());
374      }
375  
376 <
376 >    /**
377 >     * sublists contains elements at indexes offset from their base
378 >     */
379      public void testSubList() {
380 <        CopyOnWriteArrayList a = populatedArray(10);
380 >        CopyOnWriteArrayList a = populatedArray(10);
381          assertTrue(a.subList(1,1).isEmpty());
382 <        for(int j = 0; j < 9; ++j) {
383 <            for(int i = j ; i < 10; ++i) {
384 <                List b = a.subList(j,i);
385 <                for(int k = j; k < i; ++k) {
386 <                    assertEquals(new Integer(k), b.get(k-j));
387 <                }
388 <            }
389 <        }
382 >        for (int j = 0; j < 9; ++j) {
383 >            for (int i = j ; i < 10; ++i) {
384 >                List b = a.subList(j,i);
385 >                for (int k = j; k < i; ++k) {
386 >                    assertEquals(new Integer(k), b.get(k-j));
387 >                }
388 >            }
389 >        }
390  
391 <        List s = a.subList(2, 5);
391 >        List s = a.subList(2, 5);
392          assertEquals(s.size(), 3);
393          s.set(2, m1);
394          assertEquals(a.get(4), m1);
395 <        s.clear();
395 >        s.clear();
396          assertEquals(a.size(), 7);
397      }
398  
399      // Exception tests
400  
401      /**
402 <     *   toArray throws an ArrayStoreException when the given array
403 <     *  can not store the objects inside the list
402 >     * toArray throws an ArrayStoreException when the given array
403 >     * can not store the objects inside the list
404       */
405 <    public void testToArray_ArrayStoreException(){
406 <        try{
405 >    public void testToArray_ArrayStoreException() {
406 >        try {
407              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
408              c.add("zfasdfsdf");
409              c.add("asdadasd");
410              c.toArray(new Long[5]);
411 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
412 <        }catch(ArrayStoreException e){}
411 >            shouldThrow();
412 >        } catch (ArrayStoreException success) {}
413      }
414  
415      /**
416 <     *   get throws an IndexOutOfBoundsException on a negative index
416 >     * get throws an IndexOutOfBoundsException on a negative index
417       */
418 <    public void testGet1_IndexOutOfBoundsException(){
419 <        try{
418 >    public void testGet1_IndexOutOfBoundsException() {
419 >        try {
420              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
421              c.get(-1);
422 <            fail("Object get(int) should throw IndexOutOfBounds exception");
423 <        }catch(IndexOutOfBoundsException e){}
422 >            shouldThrow();
423 >        } catch (IndexOutOfBoundsException success) {}
424      }
425 <    
425 >
426      /**
427 <     *   get throws an IndexOutOfBoundsException on a too high index
427 >     * get throws an IndexOutOfBoundsException on a too high index
428       */
429 <    public void testGet2_IndexOutOfBoundsException(){
430 <        try{
429 >    public void testGet2_IndexOutOfBoundsException() {
430 >        try {
431              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
432              c.add("asdasd");
433              c.add("asdad");
434              c.get(100);
435 <            fail("Object get(int) should throw IndexOutOfBounds exception");
436 <        }catch(IndexOutOfBoundsException e){}
435 >            shouldThrow();
436 >        } catch (IndexOutOfBoundsException success) {}
437      }
438  
439      /**
440 <     *   set throws an IndexOutOfBoundsException on a negative index
440 >     * set throws an IndexOutOfBoundsException on a negative index
441       */
442 <    public void testSet1_IndexOutOfBoundsException(){
443 <        try{
442 >    public void testSet1_IndexOutOfBoundsException() {
443 >        try {
444              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
445              c.set(-1,"qwerty");
446 <            fail("Object get(int, Object) should throw IndexOutOfBounds exception");
447 <        }catch(IndexOutOfBoundsException e){}
446 >            shouldThrow();
447 >        } catch (IndexOutOfBoundsException success) {}
448      }
449 <    
449 >
450      /**
451 <     *   set throws an IndexOutOfBoundsException on a too high index
451 >     * set throws an IndexOutOfBoundsException on a too high index
452       */
453 <    public void testSet2(){
454 <        try{
453 >    public void testSet2() {
454 >        try {
455              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
456              c.add("asdasd");
457              c.add("asdad");
458              c.set(100, "qwerty");
459 <            fail("Object set(int, Object) should throw IndexOutOfBounds exception");
460 <        }catch(IndexOutOfBoundsException e){}
459 >            shouldThrow();
460 >        } catch (IndexOutOfBoundsException success) {}
461      }
462  
463      /**
464 <     *   add throws an IndexOutOfBoundsException on a negative index
464 >     * add throws an IndexOutOfBoundsException on a negative index
465       */
466 <    public void testAdd1_IndexOutOfBoundsException(){
467 <        try{
466 >    public void testAdd1_IndexOutOfBoundsException() {
467 >        try {
468              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
469              c.add(-1,"qwerty");
470 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
471 <        }catch(IndexOutOfBoundsException e){}
470 >            shouldThrow();
471 >        } catch (IndexOutOfBoundsException success) {}
472      }
473 <    
473 >
474      /**
475 <     *   add throws an IndexOutOfBoundsException on a too high index
475 >     * add throws an IndexOutOfBoundsException on a too high index
476       */
477 <    public void testAdd2_IndexOutOfBoundsException(){
478 <        try{
477 >    public void testAdd2_IndexOutOfBoundsException() {
478 >        try {
479              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
480              c.add("asdasd");
481              c.add("asdasdasd");
482              c.add(100, "qwerty");
483 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
484 <        }catch(IndexOutOfBoundsException e){}
483 >            shouldThrow();
484 >        } catch (IndexOutOfBoundsException success) {}
485      }
486  
487      /**
488 <     *   remove throws an IndexOutOfBoundsException on a negative index
488 >     * remove throws an IndexOutOfBoundsException on a negative index
489       */
490 <    public void testRemove1_IndexOutOfBounds(){
491 <        try{
490 >    public void testRemove1_IndexOutOfBounds() {
491 >        try {
492              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
493              c.remove(-1);
494 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
495 <        }catch(IndexOutOfBoundsException e){}
494 >            shouldThrow();
495 >        } catch (IndexOutOfBoundsException success) {}
496      }
497  
498      /**
499 <     *   remove throws an IndexOutOfBoundsException on a too high index
499 >     * remove throws an IndexOutOfBoundsException on a too high index
500       */
501 <    public void testRemove2_IndexOutOfBounds(){
502 <        try{
501 >    public void testRemove2_IndexOutOfBounds() {
502 >        try {
503              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
504              c.add("asdasd");
505              c.add("adasdasd");
506              c.remove(100);
507 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
508 <        }catch(IndexOutOfBoundsException e){}
507 >            shouldThrow();
508 >        } catch (IndexOutOfBoundsException success) {}
509      }
510 <    
510 >
511      /**
512 <     *   addAll throws an IndexOutOfBoundsException on a negative index
512 >     * addAll throws an IndexOutOfBoundsException on a negative index
513       */
514 <    public void testAddAll1_IndexOutOfBoundsException(){
515 <        try{
514 >    public void testAddAll1_IndexOutOfBoundsException() {
515 >        try {
516              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
517              c.addAll(-1,new LinkedList());
518 <            fail("boolean add(int, Collection) should throw IndexOutOfBounds exception");
519 <        }catch(IndexOutOfBoundsException e){}
518 >            shouldThrow();
519 >        } catch (IndexOutOfBoundsException success) {}
520      }
521 <    
521 >
522      /**
523 <     *   addAll throws an IndexOutOfBoundsException on a too high index
523 >     * addAll throws an IndexOutOfBoundsException on a too high index
524       */
525 <    public void testAddAll2_IndexOutOfBoundsException(){
526 <        try{
525 >    public void testAddAll2_IndexOutOfBoundsException() {
526 >        try {
527              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
528              c.add("asdasd");
529              c.add("asdasdasd");
530              c.addAll(100, new LinkedList());
531 <            fail("boolean addAll(int, Collection) should throw IndexOutOfBounds exception");
532 <        }catch(IndexOutOfBoundsException e){}
531 >            shouldThrow();
532 >        } catch (IndexOutOfBoundsException success) {}
533      }
534  
535      /**
536 <     *   listIterator throws an IndexOutOfBoundsException on a negative index
536 >     * listIterator throws an IndexOutOfBoundsException on a negative index
537       */
538 <    public void testListIterator1_IndexOutOfBoundsException(){
539 <        try{
538 >    public void testListIterator1_IndexOutOfBoundsException() {
539 >        try {
540              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
541              c.listIterator(-1);
542 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exceptione");
543 <        }catch(IndexOutOfBoundsException e){}
542 >            shouldThrow();
543 >        } catch (IndexOutOfBoundsException success) {}
544      }
545  
546      /**
547 <     *   listIterator throws an IndexOutOfBoundsException on a too high index
547 >     * listIterator throws an IndexOutOfBoundsException on a too high index
548       */
549 <    public void testListIterator2_IndexOutOfBoundsException(){
550 <        try{
549 >    public void testListIterator2_IndexOutOfBoundsException() {
550 >        try {
551              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
552              c.add("adasd");
553              c.add("asdasdas");
554              c.listIterator(100);
555 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exception");
556 <        }catch(IndexOutOfBoundsException e){}
555 >            shouldThrow();
556 >        } catch (IndexOutOfBoundsException success) {}
557      }
558  
559      /**
560 <     *   subList throws an IndexOutOfBoundsException on a negative index
560 >     * subList throws an IndexOutOfBoundsException on a negative index
561       */
562 <    public void testSubList1_IndexOutOfBoundsException(){
563 <        try{
562 >    public void testSubList1_IndexOutOfBoundsException() {
563 >        try {
564              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
565              c.subList(-1,100);
566 <
567 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
533 <        }catch(IndexOutOfBoundsException e){}
566 >            shouldThrow();
567 >        } catch (IndexOutOfBoundsException success) {}
568      }
569  
570      /**
571 <     *   subList throws an IndexOutOfBoundsException on a too high index
571 >     * subList throws an IndexOutOfBoundsException on a too high index
572       */
573 <    public void testSubList2_IndexOutOfBoundsException(){
574 <        try{
573 >    public void testSubList2_IndexOutOfBoundsException() {
574 >        try {
575              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
576              c.add("asdasd");
577              c.subList(1,100);
578 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
579 <        }catch(IndexOutOfBoundsException e){}
578 >            shouldThrow();
579 >        } catch (IndexOutOfBoundsException success) {}
580      }
581  
582      /**
583 <     *   subList throws IndexOutOfBoundsException when the second index
584 <     *  is lower then the first
583 >     * subList throws IndexOutOfBoundsException when the second index
584 >     * is lower then the first
585       */
586 <    public void testSubList3_IndexOutOfBoundsException(){
587 <        try{
586 >    public void testSubList3_IndexOutOfBoundsException() {
587 >        try {
588              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
589              c.subList(3,1);
590 <
591 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
558 <        }catch(IndexOutOfBoundsException e){}
590 >            shouldThrow();
591 >        } catch (IndexOutOfBoundsException success) {}
592      }
593  
594 <    public void testSerialization() {
595 <        CopyOnWriteArrayList q = populatedArray(SIZE);
596 <
597 <        try {
598 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
599 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
600 <            out.writeObject(q);
601 <            out.close();
602 <
603 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
604 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
605 <            CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
606 <            assertEquals(q.size(), r.size());
607 <            assertTrue(q.equals(r));
608 <            assertTrue(r.equals(q));
609 <        } catch(Exception e){
577 <            e.printStackTrace();
578 <            fail("unexpected exception");
594 >    /**
595 >     * a deserialized serialized list is equal
596 >     */
597 >    public void testSerialization() throws Exception {
598 >        List x = populatedArray(SIZE);
599 >        List y = serialClone(x);
600 >
601 >        assertTrue(x != y);
602 >        assertEquals(x.size(), y.size());
603 >        assertEquals(x.toString(), y.toString());
604 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
605 >        assertEquals(x, y);
606 >        assertEquals(y, x);
607 >        while (!x.isEmpty()) {
608 >            assertFalse(y.isEmpty());
609 >            assertEquals(x.remove(0), y.remove(0));
610          }
611 +        assertTrue(y.isEmpty());
612      }
613 <    
613 >
614   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines