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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
21 |
> |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
28 |
|
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
for(int i = 0; i < n; ++i) |
32 |
< |
assertTrue(q.offer(new Integer(i))); |
31 |
> |
for (int i = 0; i < n; ++i) |
32 |
> |
assertTrue(q.offer(new Integer(i))); |
33 |
|
assertFalse(q.isEmpty()); |
34 |
< |
assertEquals(n, q.size()); |
34 |
> |
assertEquals(n, q.size()); |
35 |
|
return q; |
36 |
|
} |
37 |
< |
|
37 |
> |
|
38 |
|
/** |
39 |
|
* new queue is empty |
40 |
|
*/ |
49 |
|
try { |
50 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
51 |
|
shouldThrow(); |
52 |
< |
} |
52 |
< |
catch (NullPointerException success) {} |
52 |
> |
} catch (NullPointerException success) {} |
53 |
|
} |
54 |
|
|
55 |
|
/** |
60 |
|
Integer[] ints = new Integer[SIZE]; |
61 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
62 |
|
shouldThrow(); |
63 |
< |
} |
64 |
< |
catch (NullPointerException success) {} |
63 |
> |
} catch (NullPointerException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
125 |
|
* offer(null) throws NPE |
126 |
|
*/ |
127 |
|
public void testOfferNull() { |
128 |
< |
try { |
128 |
> |
try { |
129 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
130 |
|
q.offer(null); |
131 |
|
shouldThrow(); |
132 |
< |
} catch (NullPointerException success) { } |
132 |
> |
} catch (NullPointerException success) {} |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
|
* add(null) throws NPE |
137 |
|
*/ |
138 |
|
public void testAddNull() { |
139 |
< |
try { |
139 |
> |
try { |
140 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
141 |
|
q.add(null); |
142 |
|
shouldThrow(); |
143 |
< |
} catch (NullPointerException success) { } |
143 |
> |
} catch (NullPointerException success) {} |
144 |
|
} |
145 |
|
|
146 |
|
|
172 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
173 |
|
q.addAll(null); |
174 |
|
shouldThrow(); |
175 |
< |
} |
177 |
< |
catch (NullPointerException success) {} |
175 |
> |
} catch (NullPointerException success) {} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
183 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
184 |
|
q.addAll(q); |
185 |
|
shouldThrow(); |
186 |
< |
} |
189 |
< |
catch (IllegalArgumentException success) {} |
186 |
> |
} catch (IllegalArgumentException success) {} |
187 |
|
} |
188 |
|
|
189 |
|
/** |
195 |
|
Integer[] ints = new Integer[SIZE]; |
196 |
|
q.addAll(Arrays.asList(ints)); |
197 |
|
shouldThrow(); |
198 |
< |
} |
202 |
< |
catch (NullPointerException success) {} |
198 |
> |
} catch (NullPointerException success) {} |
199 |
|
} |
200 |
|
/** |
201 |
|
* addAll of a collection with any null elements throws NPE after |
209 |
|
ints[i] = new Integer(i); |
210 |
|
q.addAll(Arrays.asList(ints)); |
211 |
|
shouldThrow(); |
212 |
< |
} |
217 |
< |
catch (NullPointerException success) {} |
212 |
> |
} catch (NullPointerException success) {} |
213 |
|
} |
214 |
|
|
215 |
|
/** |
238 |
|
for (int i = 0; i < SIZE; ++i) { |
239 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
240 |
|
} |
241 |
< |
assertNull(q.poll()); |
241 |
> |
assertNull(q.poll()); |
242 |
|
} |
243 |
|
|
244 |
|
/** |
252 |
|
assertTrue(q.peek() == null || |
253 |
|
i != ((Integer)q.peek()).intValue()); |
254 |
|
} |
255 |
< |
assertNull(q.peek()); |
255 |
> |
assertNull(q.peek()); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
267 |
|
try { |
268 |
|
q.element(); |
269 |
|
shouldThrow(); |
270 |
< |
} |
276 |
< |
catch (NoSuchElementException success) {} |
270 |
> |
} catch (NoSuchElementException success) {} |
271 |
|
} |
272 |
|
|
273 |
|
/** |
281 |
|
try { |
282 |
|
q.remove(); |
283 |
|
shouldThrow(); |
284 |
< |
} catch (NoSuchElementException success){ |
291 |
< |
} |
284 |
> |
} catch (NoSuchElementException success) {} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
298 |
|
} |
299 |
|
assertTrue(q.isEmpty()); |
300 |
|
} |
301 |
< |
|
301 |
> |
|
302 |
|
/** |
303 |
|
* contains(x) reports true when elements added but not yet removed |
304 |
|
*/ |
379 |
|
*/ |
380 |
|
public void testToArray() { |
381 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
382 |
< |
Object[] o = q.toArray(); |
382 |
> |
Object[] o = q.toArray(); |
383 |
|
Arrays.sort(o); |
384 |
< |
for(int i = 0; i < o.length; i++) |
385 |
< |
assertEquals(o[i], q.poll()); |
384 |
> |
for (int i = 0; i < o.length; i++) |
385 |
> |
assertEquals(o[i], q.poll()); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
390 |
|
*/ |
391 |
|
public void testToArray2() { |
392 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
393 |
< |
Integer[] ints = new Integer[SIZE]; |
394 |
< |
ints = (Integer[])q.toArray(ints); |
393 |
> |
Integer[] ints = new Integer[SIZE]; |
394 |
> |
ints = (Integer[])q.toArray(ints); |
395 |
|
Arrays.sort(ints); |
396 |
< |
for(int i = 0; i < ints.length; i++) |
396 |
> |
for (int i = 0; i < ints.length; i++) |
397 |
|
assertEquals(ints[i], q.poll()); |
398 |
|
} |
399 |
|
|
401 |
|
* toArray(null) throws NPE |
402 |
|
*/ |
403 |
|
public void testToArray_BadArg() { |
404 |
< |
try { |
404 |
> |
try { |
405 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
406 |
< |
Object o[] = q.toArray(null); |
407 |
< |
shouldThrow(); |
408 |
< |
} catch(NullPointerException success){} |
406 |
> |
Object o[] = q.toArray(null); |
407 |
> |
shouldThrow(); |
408 |
> |
} catch (NullPointerException success) {} |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* toArray with incompatable array type throws CCE |
412 |
> |
* toArray with incompatible array type throws ArrayStoreException |
413 |
|
*/ |
414 |
|
public void testToArray1_BadArg() { |
415 |
< |
try { |
415 |
> |
try { |
416 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
417 |
< |
Object o[] = q.toArray(new String[10] ); |
418 |
< |
shouldThrow(); |
419 |
< |
} catch(ArrayStoreException success){} |
417 |
> |
Object o[] = q.toArray(new String[10] ); |
418 |
> |
shouldThrow(); |
419 |
> |
} catch (ArrayStoreException success) {} |
420 |
|
} |
421 |
< |
|
421 |
> |
|
422 |
|
/** |
423 |
|
* iterator iterates through all elements |
424 |
|
*/ |
425 |
|
public void testIterator() { |
426 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
427 |
|
int i = 0; |
428 |
< |
Iterator it = q.iterator(); |
429 |
< |
while(it.hasNext()) { |
428 |
> |
Iterator it = q.iterator(); |
429 |
> |
while (it.hasNext()) { |
430 |
|
assertTrue(q.contains(it.next())); |
431 |
|
++i; |
432 |
|
} |
460 |
|
q.add(two); |
461 |
|
q.add(three); |
462 |
|
|
463 |
< |
try { |
464 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
465 |
< |
q.remove(); |
473 |
< |
it.next(); |
474 |
< |
} |
475 |
< |
} |
476 |
< |
catch (ConcurrentModificationException e) { |
477 |
< |
shouldThrow(); |
463 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
464 |
> |
q.remove(); |
465 |
> |
it.next(); |
466 |
|
} |
467 |
|
|
468 |
|
assertEquals("queue should be empty again", 0, q.size()); |
495 |
|
for (int i = 0; i < SIZE; ++i) { |
496 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
497 |
|
} |
498 |
< |
} |
498 |
> |
} |
499 |
|
|
500 |
|
/** |
501 |
|
* A deserialized serialized queue has same elements in same order |
502 |
|
*/ |
503 |
< |
public void testSerialization() { |
503 |
> |
public void testSerialization() throws Exception { |
504 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
505 |
< |
try { |
506 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
507 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
508 |
< |
out.writeObject(q); |
509 |
< |
out.close(); |
510 |
< |
|
511 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
512 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
513 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
514 |
< |
assertEquals(q.size(), r.size()); |
515 |
< |
while (!q.isEmpty()) |
528 |
< |
assertEquals(q.remove(), r.remove()); |
529 |
< |
} catch(Exception e){ |
530 |
< |
unexpectedException(); |
531 |
< |
} |
505 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
506 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
507 |
> |
out.writeObject(q); |
508 |
> |
out.close(); |
509 |
> |
|
510 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
511 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
512 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
513 |
> |
assertEquals(q.size(), r.size()); |
514 |
> |
while (!q.isEmpty()) |
515 |
> |
assertEquals(q.remove(), r.remove()); |
516 |
|
} |
517 |
|
|
518 |
|
} |