14 |
|
import java.io.*; |
15 |
|
|
16 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
+ |
|
18 |
+ |
public static class Fair extends BlockingQueueTest { |
19 |
+ |
protected BlockingQueue emptyCollection() { |
20 |
+ |
return new ArrayBlockingQueue(20, true); |
21 |
+ |
} |
22 |
+ |
} |
23 |
+ |
|
24 |
+ |
public static class NonFair extends BlockingQueueTest { |
25 |
+ |
protected BlockingQueue emptyCollection() { |
26 |
+ |
return new ArrayBlockingQueue(20, false); |
27 |
+ |
} |
28 |
+ |
} |
29 |
+ |
|
30 |
|
public static void main(String[] args) { |
31 |
|
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
+ |
|
34 |
|
public static Test suite() { |
35 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
35 |
> |
return newTestSuite(ArrayBlockingQueueTest.class, |
36 |
> |
new Fair().testSuite(), |
37 |
> |
new NonFair().testSuite()); |
38 |
|
} |
39 |
|
|
40 |
|
/** |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* offer(null) throws NPE |
164 |
> |
* offer(null) throws NPE |
165 |
|
*/ |
166 |
|
public void testOfferNull() { |
167 |
|
try { |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* add(null) throws NPE |
175 |
> |
* add(null) throws NPE |
176 |
|
*/ |
177 |
|
public void testAddNull() { |
178 |
|
try { |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* addAll(null) throws NPE |
210 |
> |
* addAll(null) throws NPE |
211 |
|
*/ |
212 |
|
public void testAddAll1() { |
213 |
|
try { |
230 |
|
|
231 |
|
|
232 |
|
/** |
233 |
< |
* addAll of a collection with null elements throws NPE |
233 |
> |
* addAll of a collection with null elements throws NPE |
234 |
|
*/ |
235 |
|
public void testAddAll2() { |
236 |
|
try { |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* put(null) throws NPE |
289 |
> |
* put(null) throws NPE |
290 |
|
*/ |
291 |
|
public void testPutNull() throws InterruptedException { |
292 |
|
try { |
488 |
|
} |
489 |
|
|
490 |
|
/** |
475 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
476 |
– |
* on interruption throws |
477 |
– |
*/ |
478 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
479 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
480 |
– |
Thread t = new Thread(new CheckedRunnable() { |
481 |
– |
public void realRun() throws InterruptedException { |
482 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
483 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
484 |
– |
try { |
485 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
– |
shouldThrow(); |
487 |
– |
} catch (InterruptedException success) {} |
488 |
– |
}}); |
489 |
– |
|
490 |
– |
t.start(); |
491 |
– |
Thread.sleep(SMALL_DELAY_MS); |
492 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
493 |
– |
t.interrupt(); |
494 |
– |
t.join(); |
495 |
– |
} |
496 |
– |
|
497 |
– |
|
498 |
– |
/** |
491 |
|
* peek returns next element, or null if empty |
492 |
|
*/ |
493 |
|
public void testPeek() { |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* toArray contains all elements |
626 |
> |
* toArray contains all elements |
627 |
|
*/ |
628 |
|
public void testToArray() throws InterruptedException { |
629 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
896 |
|
} |
897 |
|
|
898 |
|
/** |
899 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
899 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
900 |
|
*/ |
901 |
|
public void testDrainToN() { |
902 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
905 |
|
assertTrue(q.offer(new Integer(j))); |
906 |
|
ArrayList l = new ArrayList(); |
907 |
|
q.drainTo(l, i); |
908 |
< |
int k = (i < SIZE)? i : SIZE; |
908 |
> |
int k = (i < SIZE) ? i : SIZE; |
909 |
|
assertEquals(l.size(), k); |
910 |
|
assertEquals(q.size(), SIZE-k); |
911 |
|
for (int j = 0; j < k; ++j) |