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