1 |
|
/* |
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 |
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.Collection; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.Queue; |
15 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
16 |
|
|
17 |
|
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
18 |
|
|
25 |
|
} |
26 |
|
|
27 |
|
/** |
28 |
< |
* Create a queue of given size containing consecutive |
28 |
> |
* Creates a queue of given size containing consecutive |
29 |
|
* Integers 0 ... n. |
30 |
|
*/ |
31 |
|
private ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
142 |
|
} catch (NullPointerException success) {} |
143 |
|
} |
144 |
|
|
142 |
– |
|
145 |
|
/** |
146 |
|
* Offer returns true |
147 |
|
*/ |
485 |
|
assertFalse(it.hasNext()); |
486 |
|
} |
487 |
|
|
486 |
– |
|
488 |
|
/** |
489 |
|
* toString contains toStrings of elements |
490 |
|
*/ |
492 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
493 |
|
String s = q.toString(); |
494 |
|
for (int i = 0; i < SIZE; ++i) { |
495 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
495 |
> |
assertTrue(s.contains(String.valueOf(i))); |
496 |
|
} |
497 |
|
} |
498 |
|
|
500 |
|
* A deserialized serialized queue has same elements in same order |
501 |
|
*/ |
502 |
|
public void testSerialization() throws Exception { |
503 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
504 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
505 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
506 |
< |
out.writeObject(q); |
507 |
< |
out.close(); |
508 |
< |
|
509 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
510 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
511 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
512 |
< |
assertEquals(q.size(), r.size()); |
513 |
< |
while (!q.isEmpty()) |
514 |
< |
assertEquals(q.remove(), r.remove()); |
503 |
> |
Queue x = populatedQueue(SIZE); |
504 |
> |
Queue y = serialClone(x); |
505 |
> |
|
506 |
> |
assertTrue(x != y); |
507 |
> |
assertEquals(x.size(), y.size()); |
508 |
> |
assertEquals(x.toString(), y.toString()); |
509 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
510 |
> |
while (!x.isEmpty()) { |
511 |
> |
assertFalse(y.isEmpty()); |
512 |
> |
assertEquals(x.remove(), y.remove()); |
513 |
> |
} |
514 |
> |
assertTrue(y.isEmpty()); |
515 |
|
} |
516 |
|
|
517 |
|
} |