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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:52 2003 UTC vs.
Revision 1.16 by jsr166, Sat Nov 21 02:33:20 2009 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/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 +
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 + import java.io.*;
15  
16 < public class ArrayBlockingQueueTest extends TestCase {
13 <
14 <    private static int N = 10;
15 <    private static long SHORT_DELAY_MS = 100;
16 <    private static long MEDIUM_DELAY_MS = 1000;
17 <    private static long LONG_DELAY_MS = 10000;
18 <
16 > public class ArrayBlockingQueueTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run (suite());
19      }
22
20      public static Test suite() {
21 <        return new TestSuite(ArrayBlockingQueueTest.class);
21 >        return new TestSuite(ArrayBlockingQueueTest.class);
22      }
23  
24      /**
25       * Create a queue of given size containing consecutive
26       * Integers 0 ... n.
27       */
28 <    private ArrayBlockingQueue fullQueue(int n) {
28 >    private ArrayBlockingQueue populatedQueue(int n) {
29          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
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(0, q.remainingCapacity());
35 <        assertEquals(n, q.size());
35 >        assertEquals(n, q.size());
36          return q;
37      }
38 <
39 <    public void testConstructor1(){
40 <        assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity());
38 >
39 >    /**
40 >     * A new queue has the indicated capacity
41 >     */
42 >    public void testConstructor1() {
43 >        assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
44      }
45  
46 <    public void testConstructor2(){
46 >    /**
47 >     * Constructor throws IAE if  capacity argument nonpositive
48 >     */
49 >    public void testConstructor2() {
50          try {
51              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
52 <            fail("Cannot make zero-sized");
52 >            shouldThrow();
53          }
54          catch (IllegalArgumentException success) {}
55      }
56  
57 <    public void testConstructor3(){
58 <
57 >    /**
58 >     * Initializing from null Collection throws NPE
59 >     */
60 >    public void testConstructor3() {
61          try {
62              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
63 <            fail("Cannot make from null collection");
63 >            shouldThrow();
64          }
65          catch (NullPointerException success) {}
66      }
67  
68 <    public void testConstructor4(){
68 >    /**
69 >     * Initializing from Collection of null elements throws NPE
70 >     */
71 >    public void testConstructor4() {
72          try {
73 <            Integer[] ints = new Integer[N];
74 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
75 <            fail("Cannot make with null elements");
73 >            Integer[] ints = new Integer[SIZE];
74 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
75 >            shouldThrow();
76          }
77          catch (NullPointerException success) {}
78      }
79  
80 <    public void testConstructor5(){
80 >    /**
81 >     * Initializing from Collection with some null elements throws NPE
82 >     */
83 >    public void testConstructor5() {
84          try {
85 <            Integer[] ints = new Integer[N];
86 <            for (int i = 0; i < N-1; ++i)
85 >            Integer[] ints = new Integer[SIZE];
86 >            for (int i = 0; i < SIZE-1; ++i)
87                  ints[i] = new Integer(i);
88 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
89 <            fail("Cannot make with null elements");
88 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
89 >            shouldThrow();
90          }
91          catch (NullPointerException success) {}
92      }
93  
94 <    public void testConstructor6(){
94 >    /**
95 >     * Initializing from too large collection throws IAE
96 >     */
97 >    public void testConstructor6() {
98          try {
99 <            Integer[] ints = new Integer[N];
100 <            for (int i = 0; i < N; ++i)
99 >            Integer[] ints = new Integer[SIZE];
100 >            for (int i = 0; i < SIZE; ++i)
101                  ints[i] = new Integer(i);
102              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
103 <            fail("Cannot make with insufficient capacity");
103 >            shouldThrow();
104          }
105          catch (IllegalArgumentException success) {}
106      }
107  
108 <    public void testConstructor7(){
108 >    /**
109 >     * Queue contains all elements of collection used to initialize
110 >     */
111 >    public void testConstructor7() {
112          try {
113 <            Integer[] ints = new Integer[N];
114 <            for (int i = 0; i < N; ++i)
113 >            Integer[] ints = new Integer[SIZE];
114 >            for (int i = 0; i < SIZE; ++i)
115                  ints[i] = new Integer(i);
116 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints));
117 <            for (int i = 0; i < N; ++i)
116 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
117 >            for (int i = 0; i < SIZE; ++i)
118                  assertEquals(ints[i], q.poll());
119          }
120          finally {}
121      }
122  
123 +    /**
124 +     * Queue transitions from empty to full when elements added
125 +     */
126      public void testEmptyFull() {
127          ArrayBlockingQueue q = new ArrayBlockingQueue(2);
128          assertTrue(q.isEmpty());
129 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
130 <        q.add(new Integer(1));
129 >        assertEquals(2, q.remainingCapacity());
130 >        q.add(one);
131          assertFalse(q.isEmpty());
132 <        q.add(new Integer(2));
132 >        q.add(two);
133          assertFalse(q.isEmpty());
134 <        assertEquals("queue should be full", 0, q.remainingCapacity());
135 <        assertFalse("offer should be rejected", q.offer(new Integer(3)));
134 >        assertEquals(0, q.remainingCapacity());
135 >        assertFalse(q.offer(three));
136      }
137  
138 <    public void testRemainingCapacity(){
139 <        ArrayBlockingQueue q = fullQueue(N);
140 <        for (int i = 0; i < N; ++i) {
138 >    /**
139 >     * remainingCapacity decreases on add, increases on remove
140 >     */
141 >    public void testRemainingCapacity() {
142 >        ArrayBlockingQueue q = populatedQueue(SIZE);
143 >        for (int i = 0; i < SIZE; ++i) {
144              assertEquals(i, q.remainingCapacity());
145 <            assertEquals(N-i, q.size());
145 >            assertEquals(SIZE-i, q.size());
146              q.remove();
147          }
148 <        for (int i = 0; i < N; ++i) {
149 <            assertEquals(N-i, q.remainingCapacity());
148 >        for (int i = 0; i < SIZE; ++i) {
149 >            assertEquals(SIZE-i, q.remainingCapacity());
150              assertEquals(i, q.size());
151              q.add(new Integer(i));
152          }
153      }
154  
155 <    public void testOfferNull(){
156 <        try {
155 >    /**
156 >     *  offer(null) throws NPE
157 >     */
158 >    public void testOfferNull() {
159 >        try {
160              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
161              q.offer(null);
162 <            fail("should throw NPE");
163 <        } catch (NullPointerException success) { }  
162 >            shouldThrow();
163 >        } catch (NullPointerException success) { }
164 >    }
165 >
166 >    /**
167 >     *  add(null) throws NPE
168 >     */
169 >    public void testAddNull() {
170 >        try {
171 >            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
172 >            q.add(null);
173 >            shouldThrow();
174 >        } catch (NullPointerException success) { }
175      }
176  
177 <    public void testOffer(){
177 >    /**
178 >     * Offer succeeds if not full; fails if full
179 >     */
180 >    public void testOffer() {
181          ArrayBlockingQueue q = new ArrayBlockingQueue(1);
182 <        assertTrue(q.offer(new Integer(0)));
183 <        assertFalse(q.offer(new Integer(1)));
182 >        assertTrue(q.offer(zero));
183 >        assertFalse(q.offer(one));
184      }
185  
186 <    public void testAdd(){
187 <        try {
188 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
189 <            for (int i = 0; i < N; ++i) {
186 >    /**
187 >     * add succeeds if not full; throws ISE if full
188 >     */
189 >    public void testAdd() {
190 >        try {
191 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
192 >            for (int i = 0; i < SIZE; ++i) {
193                  assertTrue(q.add(new Integer(i)));
194              }
195              assertEquals(0, q.remainingCapacity());
196 <            q.add(new Integer(N));
197 <        } catch (IllegalStateException success){
198 <        }  
196 >            q.add(new Integer(SIZE));
197 >        } catch (IllegalStateException success) {
198 >        }
199      }
200  
201 <    public void testAddAll1(){
201 >    /**
202 >     *  addAll(null) throws NPE
203 >     */
204 >    public void testAddAll1() {
205          try {
206              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
207              q.addAll(null);
208 <            fail("Cannot add null collection");
208 >            shouldThrow();
209          }
210          catch (NullPointerException success) {}
211      }
212 <    public void testAddAll2(){
212 >
213 >    /**
214 >     * addAll(this) throws IAE
215 >     */
216 >    public void testAddAllSelf() {
217 >        try {
218 >            ArrayBlockingQueue q = populatedQueue(SIZE);
219 >            q.addAll(q);
220 >            shouldThrow();
221 >        }
222 >        catch (IllegalArgumentException success) {}
223 >    }
224 >
225 >
226 >    /**
227 >     *  addAll of a collection with null elements throws NPE
228 >     */
229 >    public void testAddAll2() {
230          try {
231 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
232 <            Integer[] ints = new Integer[N];
231 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
232 >            Integer[] ints = new Integer[SIZE];
233              q.addAll(Arrays.asList(ints));
234 <            fail("Cannot add null elements");
234 >            shouldThrow();
235          }
236          catch (NullPointerException success) {}
237      }
238 <    public void testAddAll3(){
238 >    /**
239 >     * addAll of a collection with any null elements throws NPE after
240 >     * possibly adding some elements
241 >     */
242 >    public void testAddAll3() {
243          try {
244 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
245 <            Integer[] ints = new Integer[N];
246 <            for (int i = 0; i < N-1; ++i)
244 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
245 >            Integer[] ints = new Integer[SIZE];
246 >            for (int i = 0; i < SIZE-1; ++i)
247                  ints[i] = new Integer(i);
248              q.addAll(Arrays.asList(ints));
249 <            fail("Cannot add null elements");
249 >            shouldThrow();
250          }
251          catch (NullPointerException success) {}
252      }
253 <    public void testAddAll4(){
253 >    /**
254 >     * addAll throws ISE if not enough room
255 >     */
256 >    public void testAddAll4() {
257          try {
258              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
259 <            Integer[] ints = new Integer[N];
260 <            for (int i = 0; i < N; ++i)
259 >            Integer[] ints = new Integer[SIZE];
260 >            for (int i = 0; i < SIZE; ++i)
261                  ints[i] = new Integer(i);
262              q.addAll(Arrays.asList(ints));
263 <            fail("Cannot add with insufficient capacity");
263 >            shouldThrow();
264          }
265          catch (IllegalStateException success) {}
266      }
267 <    public void testAddAll5(){
267 >    /**
268 >     * Queue contains all elements, in traversal order, of successful addAll
269 >     */
270 >    public void testAddAll5() {
271          try {
272              Integer[] empty = new Integer[0];
273 <            Integer[] ints = new Integer[N];
274 <            for (int i = 0; i < N; ++i)
273 >            Integer[] ints = new Integer[SIZE];
274 >            for (int i = 0; i < SIZE; ++i)
275                  ints[i] = new Integer(i);
276 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
276 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
277              assertFalse(q.addAll(Arrays.asList(empty)));
278              assertTrue(q.addAll(Arrays.asList(ints)));
279 <            for (int i = 0; i < N; ++i)
279 >            for (int i = 0; i < SIZE; ++i)
280                  assertEquals(ints[i], q.poll());
281          }
282          finally {}
283      }
284  
285 +    /**
286 +     *  put(null) throws NPE
287 +     */
288       public void testPutNull() {
289 <        try {
290 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
289 >        try {
290 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
291              q.put(null);
292 <            fail("put should throw NPE");
293 <        }
294 <        catch (NullPointerException success){
295 <        }  
292 >            shouldThrow();
293 >        }
294 >        catch (NullPointerException success) {
295 >        }
296          catch (InterruptedException ie) {
297 <            fail("Unexpected exception");
297 >            unexpectedException();
298          }
299       }
300  
301 +    /**
302 +     * all elements successfully put are contained
303 +     */
304       public void testPut() {
305           try {
306 <             ArrayBlockingQueue q = new ArrayBlockingQueue(N);
307 <             for (int i = 0; i < N; ++i) {
306 >             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
307 >             for (int i = 0; i < SIZE; ++i) {
308                   Integer I = new Integer(i);
309                   q.put(I);
310                   assertTrue(q.contains(I));
# Line 233 | Line 312 | public class ArrayBlockingQueueTest exte
312               assertEquals(0, q.remainingCapacity());
313           }
314          catch (InterruptedException ie) {
315 <            fail("Unexpected exception");
315 >            unexpectedException();
316          }
317      }
318  
319 <    public void testBlockingPut(){
319 >    /**
320 >     * put blocks interruptibly if full
321 >     */
322 >    public void testBlockingPut() {
323 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
324          Thread t = new Thread(new Runnable() {
325                  public void run() {
326                      int added = 0;
327                      try {
328 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(N);
246 <                        for (int i = 0; i < N; ++i) {
328 >                        for (int i = 0; i < SIZE; ++i) {
329                              q.put(new Integer(i));
330                              ++added;
331                          }
332 <                        q.put(new Integer(N));
333 <                        fail("put should block");
334 <                    } catch (InterruptedException ie){
335 <                        assertEquals(added, N);
336 <                    }  
332 >                        q.put(new Integer(SIZE));
333 >                        threadShouldThrow();
334 >                    } catch (InterruptedException ie) {
335 >                        threadAssertEquals(added, SIZE);
336 >                    }
337                  }});
338 <        t.start();
339 <        try {
340 <           Thread.sleep(SHORT_DELAY_MS);
338 >        try {
339 >            t.start();
340 >           Thread.sleep(MEDIUM_DELAY_MS);
341             t.interrupt();
342             t.join();
343          }
344          catch (InterruptedException ie) {
345 <            fail("Unexpected exception");
345 >            unexpectedException();
346          }
347      }
348  
349 +    /**
350 +     * put blocks waiting for take when full
351 +     */
352      public void testPutWithTake() {
353          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
354          Thread t = new Thread(new Runnable() {
355 <                public void run(){
355 >                public void run() {
356                      int added = 0;
357                      try {
358                          q.put(new Object());
# Line 278 | Line 363 | public class ArrayBlockingQueueTest exte
363                          ++added;
364                          q.put(new Object());
365                          ++added;
366 <                        fail("Should block");
367 <                    } catch (InterruptedException e){
368 <                        assertTrue(added >= 2);
366 >                        threadShouldThrow();
367 >                    } catch (InterruptedException e) {
368 >                        threadAssertTrue(added >= 2);
369                      }
370                  }
371              });
# Line 290 | Line 375 | public class ArrayBlockingQueueTest exte
375              q.take();
376              t.interrupt();
377              t.join();
378 <        } catch (Exception e){
379 <            fail("Unexpected exception");
378 >        } catch (Exception e) {
379 >            unexpectedException();
380          }
381      }
382  
383 +    /**
384 +     * timed offer times out if full and elements not taken
385 +     */
386      public void testTimedOffer() {
387          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
388          Thread t = new Thread(new Runnable() {
389 <                public void run(){
389 >                public void run() {
390                      try {
391                          q.put(new Object());
392                          q.put(new Object());
393 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
394 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
395 <                        fail("Should block");
396 <                    } catch (InterruptedException success){}
393 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
394 >                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
395 >                        threadShouldThrow();
396 >                    } catch (InterruptedException success) {}
397                  }
398              });
399 <        
399 >
400          try {
401              t.start();
402              Thread.sleep(SHORT_DELAY_MS);
403              t.interrupt();
404              t.join();
405 <        } catch (Exception e){
406 <            fail("Unexpected exception");
405 >        } catch (Exception e) {
406 >            unexpectedException();
407          }
408      }
409  
410 <    public void testTake(){
411 <        try {
412 <            ArrayBlockingQueue q = fullQueue(N);
413 <            for (int i = 0; i < N; ++i) {
410 >    /**
411 >     * take retrieves elements in FIFO order
412 >     */
413 >    public void testTake() {
414 >        try {
415 >            ArrayBlockingQueue q = populatedQueue(SIZE);
416 >            for (int i = 0; i < SIZE; ++i) {
417                  assertEquals(i, ((Integer)q.take()).intValue());
418              }
419 <        } catch (InterruptedException e){
420 <            fail("Unexpected exception");
421 <        }  
419 >        } catch (InterruptedException e) {
420 >            unexpectedException();
421 >        }
422      }
423  
424 +    /**
425 +     * take blocks interruptibly when empty
426 +     */
427      public void testTakeFromEmpty() {
428          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
429          Thread t = new Thread(new Runnable() {
430 <                public void run(){
430 >                public void run() {
431                      try {
432                          q.take();
433 <                        fail("Should block");
434 <                    } catch (InterruptedException success){ }                
433 >                        threadShouldThrow();
434 >                    } catch (InterruptedException success) { }
435                  }
436              });
437          try {
# Line 345 | Line 439 | public class ArrayBlockingQueueTest exte
439              Thread.sleep(SHORT_DELAY_MS);
440              t.interrupt();
441              t.join();
442 <        } catch (Exception e){
443 <            fail("Unexpected exception");
442 >        } catch (Exception e) {
443 >            unexpectedException();
444          }
445      }
446  
447 <    public void testBlockingTake(){
447 >    /**
448 >     * Take removes existing elements until empty, then blocks interruptibly
449 >     */
450 >    public void testBlockingTake() {
451          Thread t = new Thread(new Runnable() {
452                  public void run() {
453                      try {
454 <                        ArrayBlockingQueue q = fullQueue(N);
455 <                        for (int i = 0; i < N; ++i) {
456 <                            assertEquals(i, ((Integer)q.take()).intValue());
454 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
455 >                        for (int i = 0; i < SIZE; ++i) {
456 >                            threadAssertEquals(i, ((Integer)q.take()).intValue());
457                          }
458                          q.take();
459 <                        fail("take should block");
460 <                    } catch (InterruptedException success){
461 <                    }  
459 >                        threadShouldThrow();
460 >                    } catch (InterruptedException success) {
461 >                    }
462                  }});
463 <        t.start();
464 <        try {
465 <           Thread.sleep(SHORT_DELAY_MS);
466 <           t.interrupt();
467 <           t.join();
463 >        try {
464 >            t.start();
465 >            Thread.sleep(SHORT_DELAY_MS);
466 >            t.interrupt();
467 >            t.join();
468          }
469          catch (InterruptedException ie) {
470 <            fail("Unexpected exception");
470 >            unexpectedException();
471          }
472      }
473  
474  
475 <    public void testPoll(){
476 <        ArrayBlockingQueue q = fullQueue(N);
477 <        for (int i = 0; i < N; ++i) {
475 >    /**
476 >     * poll succeeds unless empty
477 >     */
478 >    public void testPoll() {
479 >        ArrayBlockingQueue q = populatedQueue(SIZE);
480 >        for (int i = 0; i < SIZE; ++i) {
481              assertEquals(i, ((Integer)q.poll()).intValue());
482          }
483 <        assertNull(q.poll());
483 >        assertNull(q.poll());
484      }
485  
486 +    /**
487 +     * timed pool with zero timeout succeeds when non-empty, else times out
488 +     */
489      public void testTimedPoll0() {
490          try {
491 <            ArrayBlockingQueue q = fullQueue(N);
492 <            for (int i = 0; i < N; ++i) {
493 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
494 <            }
495 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
496 <        } catch (InterruptedException e){
497 <            fail("Unexpected exception");
498 <        }  
491 >            ArrayBlockingQueue q = populatedQueue(SIZE);
492 >            for (int i = 0; i < SIZE; ++i) {
493 >                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
494 >            }
495 >            assertNull(q.poll(0, MILLISECONDS));
496 >        } catch (InterruptedException e) {
497 >            unexpectedException();
498 >        }
499      }
500  
501 +    /**
502 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
503 +     */
504      public void testTimedPoll() {
505          try {
506 <            ArrayBlockingQueue q = fullQueue(N);
507 <            for (int i = 0; i < N; ++i) {
508 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
506 >            ArrayBlockingQueue q = populatedQueue(SIZE);
507 >            for (int i = 0; i < SIZE; ++i) {
508 >                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
509              }
510 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
511 <        } catch (InterruptedException e){
512 <            fail("Unexpected exception");
513 <        }  
510 >            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
511 >        } catch (InterruptedException e) {
512 >            unexpectedException();
513 >        }
514      }
515  
516 <    public void testInterruptedTimedPoll(){
516 >    /**
517 >     * Interrupted timed poll throws InterruptedException instead of
518 >     * returning timeout status
519 >     */
520 >    public void testInterruptedTimedPoll() {
521          Thread t = new Thread(new Runnable() {
522                  public void run() {
523                      try {
524 <                        ArrayBlockingQueue q = fullQueue(N);
525 <                        for (int i = 0; i < N; ++i) {
526 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
524 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
525 >                        for (int i = 0; i < SIZE; ++i) {
526 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
527                          }
528 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
529 <                    } catch (InterruptedException success){
530 <                    }  
528 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
529 >                    } catch (InterruptedException success) {
530 >                    }
531                  }});
532 <        t.start();
533 <        try {
534 <           Thread.sleep(SHORT_DELAY_MS);
535 <           t.interrupt();
536 <           t.join();
532 >        try {
533 >            t.start();
534 >            Thread.sleep(SHORT_DELAY_MS);
535 >            t.interrupt();
536 >            t.join();
537          }
538          catch (InterruptedException ie) {
539 <            fail("Unexpected exception");
539 >            unexpectedException();
540          }
541      }
542  
543 <    public void testTimedPollWithOffer(){
543 >    /**
544 >     *  timed poll before a delayed offer fails; after offer succeeds;
545 >     *  on interruption throws
546 >     */
547 >    public void testTimedPollWithOffer() {
548          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
549          Thread t = new Thread(new Runnable() {
550 <                public void run(){
550 >                public void run() {
551                      try {
552 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
553 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
555 <                        fail("Should block");
556 <                    } catch (InterruptedException success) { }                
552 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
553 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
554 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
555 >                        threadShouldThrow();
556 >                    } catch (InterruptedException success) { }
557                  }
558              });
559          try {
560              t.start();
561 <            Thread.sleep(SHORT_DELAY_MS * 2);
562 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
561 >            Thread.sleep(SMALL_DELAY_MS);
562 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
563              t.interrupt();
564              t.join();
565 <        } catch (Exception e){
566 <            fail("Unexpected exception");
565 >        } catch (Exception e) {
566 >            unexpectedException();
567          }
568 <    }  
568 >    }
569  
570  
571 <    public void testPeek(){
572 <        ArrayBlockingQueue q = fullQueue(N);
573 <        for (int i = 0; i < N; ++i) {
571 >    /**
572 >     * peek returns next element, or null if empty
573 >     */
574 >    public void testPeek() {
575 >        ArrayBlockingQueue q = populatedQueue(SIZE);
576 >        for (int i = 0; i < SIZE; ++i) {
577              assertEquals(i, ((Integer)q.peek()).intValue());
578              q.poll();
579              assertTrue(q.peek() == null ||
580                         i != ((Integer)q.peek()).intValue());
581          }
582 <        assertNull(q.peek());
582 >        assertNull(q.peek());
583      }
584  
585 <    public void testElement(){
586 <        ArrayBlockingQueue q = fullQueue(N);
587 <        for (int i = 0; i < N; ++i) {
585 >    /**
586 >     * element returns next element, or throws NSEE if empty
587 >     */
588 >    public void testElement() {
589 >        ArrayBlockingQueue q = populatedQueue(SIZE);
590 >        for (int i = 0; i < SIZE; ++i) {
591              assertEquals(i, ((Integer)q.element()).intValue());
592              q.poll();
593          }
594          try {
595              q.element();
596 <            fail("no such element");
596 >            shouldThrow();
597          }
598          catch (NoSuchElementException success) {}
599      }
600  
601 <    public void testRemove(){
602 <        ArrayBlockingQueue q = fullQueue(N);
603 <        for (int i = 0; i < N; ++i) {
601 >    /**
602 >     * remove removes next element, or throws NSEE if empty
603 >     */
604 >    public void testRemove() {
605 >        ArrayBlockingQueue q = populatedQueue(SIZE);
606 >        for (int i = 0; i < SIZE; ++i) {
607              assertEquals(i, ((Integer)q.remove()).intValue());
608          }
609          try {
610              q.remove();
611 <            fail("remove should throw");
612 <        } catch (NoSuchElementException success){
613 <        }  
611 >            shouldThrow();
612 >        } catch (NoSuchElementException success) {
613 >        }
614      }
615  
616 <    public void testRemoveElement(){
617 <        ArrayBlockingQueue q = fullQueue(N);
618 <        for (int i = 1; i < N; i+=2) {
616 >    /**
617 >     * remove(x) removes x and returns true if present
618 >     */
619 >    public void testRemoveElement() {
620 >        ArrayBlockingQueue q = populatedQueue(SIZE);
621 >        for (int i = 1; i < SIZE; i+=2) {
622              assertTrue(q.remove(new Integer(i)));
623          }
624 <        for (int i = 0; i < N; i+=2) {
624 >        for (int i = 0; i < SIZE; i+=2) {
625              assertTrue(q.remove(new Integer(i)));
626              assertFalse(q.remove(new Integer(i+1)));
627          }
628 <        assert(q.isEmpty());
628 >        assertTrue(q.isEmpty());
629      }
630 <        
631 <    public void testContains(){
632 <        ArrayBlockingQueue q = fullQueue(N);
633 <        for (int i = 0; i < N; ++i) {
630 >
631 >    /**
632 >     * contains(x) reports true when elements added but not yet removed
633 >     */
634 >    public void testContains() {
635 >        ArrayBlockingQueue q = populatedQueue(SIZE);
636 >        for (int i = 0; i < SIZE; ++i) {
637              assertTrue(q.contains(new Integer(i)));
638              q.poll();
639              assertFalse(q.contains(new Integer(i)));
640          }
641      }
642  
643 <    public void testClear(){
644 <        ArrayBlockingQueue q = fullQueue(N);
643 >    /**
644 >     * clear removes all elements
645 >     */
646 >    public void testClear() {
647 >        ArrayBlockingQueue q = populatedQueue(SIZE);
648          q.clear();
649          assertTrue(q.isEmpty());
650          assertEquals(0, q.size());
651 <        assertEquals(N, q.remainingCapacity());
652 <        q.add(new Integer(1));
651 >        assertEquals(SIZE, q.remainingCapacity());
652 >        q.add(one);
653          assertFalse(q.isEmpty());
654 +        assertTrue(q.contains(one));
655          q.clear();
656          assertTrue(q.isEmpty());
657      }
658  
659 <    public void testContainsAll(){
660 <        ArrayBlockingQueue q = fullQueue(N);
661 <        ArrayBlockingQueue p = new ArrayBlockingQueue(N);
662 <        for (int i = 0; i < N; ++i) {
659 >    /**
660 >     * containsAll(c) is true when c contains a subset of elements
661 >     */
662 >    public void testContainsAll() {
663 >        ArrayBlockingQueue q = populatedQueue(SIZE);
664 >        ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
665 >        for (int i = 0; i < SIZE; ++i) {
666              assertTrue(q.containsAll(p));
667              assertFalse(p.containsAll(q));
668              p.add(new Integer(i));
# Line 534 | Line 670 | public class ArrayBlockingQueueTest exte
670          assertTrue(p.containsAll(q));
671      }
672  
673 <    public void testRetainAll(){
674 <        ArrayBlockingQueue q = fullQueue(N);
675 <        ArrayBlockingQueue p = fullQueue(N);
676 <        for (int i = 0; i < N; ++i) {
673 >    /**
674 >     * retainAll(c) retains only those elements of c and reports true if changed
675 >     */
676 >    public void testRetainAll() {
677 >        ArrayBlockingQueue q = populatedQueue(SIZE);
678 >        ArrayBlockingQueue p = populatedQueue(SIZE);
679 >        for (int i = 0; i < SIZE; ++i) {
680              boolean changed = q.retainAll(p);
681              if (i == 0)
682                  assertFalse(changed);
# Line 545 | Line 684 | public class ArrayBlockingQueueTest exte
684                  assertTrue(changed);
685  
686              assertTrue(q.containsAll(p));
687 <            assertEquals(N-i, q.size());
687 >            assertEquals(SIZE-i, q.size());
688              p.remove();
689          }
690      }
691  
692 <    public void testRemoveAll(){
693 <        for (int i = 1; i < N; ++i) {
694 <            ArrayBlockingQueue q = fullQueue(N);
695 <            ArrayBlockingQueue p = fullQueue(i);
692 >    /**
693 >     * removeAll(c) removes only those elements of c and reports true if changed
694 >     */
695 >    public void testRemoveAll() {
696 >        for (int i = 1; i < SIZE; ++i) {
697 >            ArrayBlockingQueue q = populatedQueue(SIZE);
698 >            ArrayBlockingQueue p = populatedQueue(i);
699              assertTrue(q.removeAll(p));
700 <            assertEquals(N-i, q.size());
700 >            assertEquals(SIZE-i, q.size());
701              for (int j = 0; j < i; ++j) {
702                  Integer I = (Integer)(p.remove());
703                  assertFalse(q.contains(I));
# Line 563 | Line 705 | public class ArrayBlockingQueueTest exte
705          }
706      }
707  
708 +    /**
709 +     *  toArray contains all elements
710 +     */
711 +    public void testToArray() {
712 +        ArrayBlockingQueue q = populatedQueue(SIZE);
713 +        Object[] o = q.toArray();
714 +        try {
715 +        for (int i = 0; i < o.length; i++)
716 +            assertEquals(o[i], q.take());
717 +        } catch (InterruptedException e) {
718 +            unexpectedException();
719 +        }
720 +    }
721 +
722 +    /**
723 +     * toArray(a) contains all elements
724 +     */
725 +    public void testToArray2() {
726 +        ArrayBlockingQueue q = populatedQueue(SIZE);
727 +        Integer[] ints = new Integer[SIZE];
728 +        ints = (Integer[])q.toArray(ints);
729 +        try {
730 +            for (int i = 0; i < ints.length; i++)
731 +                assertEquals(ints[i], q.take());
732 +        } catch (InterruptedException e) {
733 +            unexpectedException();
734 +        }
735 +    }
736  
737 <    public void testToArray(){
738 <        ArrayBlockingQueue q = fullQueue(N);
739 <        Object[] o = q.toArray();
740 <        try {
741 <        for(int i = 0; i < o.length; i++)
742 <            assertEquals(o[i], q.take());
743 <        } catch (InterruptedException e){
744 <            fail("Unexpected exception");
745 <        }    
576 <    }
577 <
578 <    public void testToArray2(){
579 <        ArrayBlockingQueue q = fullQueue(N);
580 <        Integer[] ints = new Integer[N];
581 <        ints = (Integer[])q.toArray(ints);
582 <        try {
583 <            for(int i = 0; i < ints.length; i++)
584 <                assertEquals(ints[i], q.take());
585 <        } catch (InterruptedException e){
586 <            fail("Unexpected exception");
587 <        }    
588 <    }
589 <    
590 <    public void testIterator(){
591 <        ArrayBlockingQueue q = fullQueue(N);
592 <        Iterator it = q.iterator();
593 <        try {
594 <            while(it.hasNext()){
595 <                assertEquals(it.next(), q.take());
596 <            }
597 <        } catch (InterruptedException e){
598 <            fail("Unexpected exception");
599 <        }    
737 >    /**
738 >     * toArray(null) throws NPE
739 >     */
740 >    public void testToArray_BadArg() {
741 >        try {
742 >            ArrayBlockingQueue q = populatedQueue(SIZE);
743 >            Object o[] = q.toArray(null);
744 >            shouldThrow();
745 >        } catch (NullPointerException success) {}
746      }
747  
748 <    public void testIteratorOrdering() {
748 >    /**
749 >     * toArray with incompatible array type throws CCE
750 >     */
751 >    public void testToArray1_BadArg() {
752 >        try {
753 >            ArrayBlockingQueue q = populatedQueue(SIZE);
754 >            Object o[] = q.toArray(new String[10] );
755 >            shouldThrow();
756 >        } catch (ArrayStoreException  success) {}
757 >    }
758  
759 +
760 +    /**
761 +     * iterator iterates through all elements
762 +     */
763 +    public void testIterator() {
764 +        ArrayBlockingQueue q = populatedQueue(SIZE);
765 +        Iterator it = q.iterator();
766 +        try {
767 +            while (it.hasNext()) {
768 +                assertEquals(it.next(), q.take());
769 +            }
770 +        } catch (InterruptedException e) {
771 +            unexpectedException();
772 +        }
773 +    }
774 +
775 +    /**
776 +     * iterator.remove removes current element
777 +     */
778 +    public void testIteratorRemove () {
779          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
780 +        q.add(two);
781 +        q.add(one);
782 +        q.add(three);
783 +
784 +        Iterator it = q.iterator();
785 +        it.next();
786 +        it.remove();
787 +
788 +        it = q.iterator();
789 +        assertEquals(it.next(), one);
790 +        assertEquals(it.next(), three);
791 +        assertFalse(it.hasNext());
792 +    }
793  
794 <        q.add(new Integer(1));
795 <        q.add(new Integer(2));
796 <        q.add(new Integer(3));
794 >    /**
795 >     * iterator ordering is FIFO
796 >     */
797 >    public void testIteratorOrdering() {
798 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
799 >        q.add(one);
800 >        q.add(two);
801 >        q.add(three);
802  
803          assertEquals("queue should be full", 0, q.remainingCapacity());
804  
805          int k = 0;
806          for (Iterator it = q.iterator(); it.hasNext();) {
807              int i = ((Integer)(it.next())).intValue();
808 <            assertEquals("items should come out in order", ++k, i);
808 >            assertEquals(++k, i);
809          }
810 <
618 <        assertEquals("should go through 3 elements", 3, k);
810 >        assertEquals(3, k);
811      }
812  
813 +    /**
814 +     * Modifications do not cause iterators to fail
815 +     */
816      public void testWeaklyConsistentIteration () {
622
817          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
818 <
819 <        q.add(new Integer(1));
820 <        q.add(new Integer(2));
627 <        q.add(new Integer(3));
628 <
818 >        q.add(one);
819 >        q.add(two);
820 >        q.add(three);
821          try {
822              for (Iterator it = q.iterator(); it.hasNext();) {
823                  q.remove();
# Line 633 | Line 825 | public class ArrayBlockingQueueTest exte
825              }
826          }
827          catch (ConcurrentModificationException e) {
828 <            fail("weakly consistent iterator; should not get CME");
828 >            unexpectedException();
829          }
830 <
639 <        assertEquals("queue should be empty again", 0, q.size());
830 >        assertEquals(0, q.size());
831      }
832  
833  
834 <    public void testToString(){
835 <        ArrayBlockingQueue q = fullQueue(N);
834 >    /**
835 >     * toString contains toStrings of elements
836 >     */
837 >    public void testToString() {
838 >        ArrayBlockingQueue q = populatedQueue(SIZE);
839          String s = q.toString();
840 <        for (int i = 0; i < N; ++i) {
840 >        for (int i = 0; i < SIZE; ++i) {
841              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
842          }
843 <    }        
843 >    }
844  
845  
846 +    /**
847 +     * offer transfers elements across Executor tasks
848 +     */
849      public void testOfferInExecutor() {
653
850          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
851 <
852 <        q.add(new Integer(1));
657 <        q.add(new Integer(2));
658 <
851 >        q.add(one);
852 >        q.add(two);
853          ExecutorService executor = Executors.newFixedThreadPool(2);
660
854          executor.execute(new Runnable() {
855              public void run() {
856 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
856 >                threadAssertFalse(q.offer(three));
857                  try {
858 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
859 <                    assertEquals(0, q.remainingCapacity());
858 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
859 >                    threadAssertEquals(0, q.remainingCapacity());
860                  }
861                  catch (InterruptedException e) {
862 <                    fail("should not be interrupted");
862 >                    threadUnexpectedException();
863                  }
864              }
865          });
# Line 674 | Line 867 | public class ArrayBlockingQueueTest exte
867          executor.execute(new Runnable() {
868              public void run() {
869                  try {
870 <                    Thread.sleep(MEDIUM_DELAY_MS);
871 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
870 >                    Thread.sleep(SMALL_DELAY_MS);
871 >                    threadAssertEquals(one, q.take());
872                  }
873                  catch (InterruptedException e) {
874 <                    fail("should not be interrupted");
874 >                    threadUnexpectedException();
875                  }
876              }
877          });
878 <        
879 <        executor.shutdown();
878 >
879 >        joinPool(executor);
880  
881      }
882  
883 +    /**
884 +     * poll retrieves elements across Executor threads
885 +     */
886      public void testPollInExecutor() {
691
887          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
693
888          ExecutorService executor = Executors.newFixedThreadPool(2);
695
889          executor.execute(new Runnable() {
890              public void run() {
891 <                assertNull("poll should fail", q.poll());
891 >                threadAssertNull(q.poll());
892                  try {
893 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
894 <                    assertTrue(q.isEmpty());
893 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
894 >                    threadAssertTrue(q.isEmpty());
895                  }
896                  catch (InterruptedException e) {
897 <                    fail("should not be interrupted");
897 >                    threadUnexpectedException();
898                  }
899              }
900          });
# Line 709 | Line 902 | public class ArrayBlockingQueueTest exte
902          executor.execute(new Runnable() {
903              public void run() {
904                  try {
905 <                    Thread.sleep(MEDIUM_DELAY_MS);
906 <                    q.put(new Integer(1));
905 >                    Thread.sleep(SMALL_DELAY_MS);
906 >                    q.put(one);
907                  }
908                  catch (InterruptedException e) {
909 <                    fail("should not be interrupted");
909 >                    threadUnexpectedException();
910                  }
911              }
912          });
720        
721        executor.shutdown();
913  
914 +        joinPool(executor);
915 +    }
916 +
917 +    /**
918 +     * A deserialized serialized queue has same elements in same order
919 +     */
920 +    public void testSerialization() {
921 +        ArrayBlockingQueue q = populatedQueue(SIZE);
922 +
923 +        try {
924 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
925 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
926 +            out.writeObject(q);
927 +            out.close();
928 +
929 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
930 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
931 +            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
932 +            assertEquals(q.size(), r.size());
933 +            while (!q.isEmpty())
934 +                assertEquals(q.remove(), r.remove());
935 +        } catch (Exception e) {
936 +            unexpectedException();
937 +        }
938 +    }
939 +
940 +    /**
941 +     * drainTo(null) throws NPE
942 +     */
943 +    public void testDrainToNull() {
944 +        ArrayBlockingQueue q = populatedQueue(SIZE);
945 +        try {
946 +            q.drainTo(null);
947 +            shouldThrow();
948 +        } catch (NullPointerException success) {
949 +        }
950 +    }
951 +
952 +    /**
953 +     * drainTo(this) throws IAE
954 +     */
955 +    public void testDrainToSelf() {
956 +        ArrayBlockingQueue q = populatedQueue(SIZE);
957 +        try {
958 +            q.drainTo(q);
959 +            shouldThrow();
960 +        } catch (IllegalArgumentException success) {
961 +        }
962 +    }
963 +
964 +    /**
965 +     * drainTo(c) empties queue into another collection c
966 +     */
967 +    public void testDrainTo() {
968 +        ArrayBlockingQueue q = populatedQueue(SIZE);
969 +        ArrayList l = new ArrayList();
970 +        q.drainTo(l);
971 +        assertEquals(q.size(), 0);
972 +        assertEquals(l.size(), SIZE);
973 +        for (int i = 0; i < SIZE; ++i)
974 +            assertEquals(l.get(i), new Integer(i));
975 +        q.add(zero);
976 +        q.add(one);
977 +        assertFalse(q.isEmpty());
978 +        assertTrue(q.contains(zero));
979 +        assertTrue(q.contains(one));
980 +        l.clear();
981 +        q.drainTo(l);
982 +        assertEquals(q.size(), 0);
983 +        assertEquals(l.size(), 2);
984 +        for (int i = 0; i < 2; ++i)
985 +            assertEquals(l.get(i), new Integer(i));
986      }
987  
988 +    /**
989 +     * drainTo empties full queue, unblocking a waiting put.
990 +     */
991 +    public void testDrainToWithActivePut() {
992 +        final ArrayBlockingQueue q = populatedQueue(SIZE);
993 +        Thread t = new Thread(new Runnable() {
994 +                public void run() {
995 +                    try {
996 +                        q.put(new Integer(SIZE+1));
997 +                    } catch (InterruptedException ie) {
998 +                        threadUnexpectedException();
999 +                    }
1000 +                }
1001 +            });
1002 +        try {
1003 +            t.start();
1004 +            ArrayList l = new ArrayList();
1005 +            q.drainTo(l);
1006 +            assertTrue(l.size() >= SIZE);
1007 +            for (int i = 0; i < SIZE; ++i)
1008 +                assertEquals(l.get(i), new Integer(i));
1009 +            t.join();
1010 +            assertTrue(q.size() + l.size() >= SIZE);
1011 +        } catch (Exception e) {
1012 +            unexpectedException();
1013 +        }
1014 +    }
1015 +
1016 +    /**
1017 +     * drainTo(null, n) throws NPE
1018 +     */
1019 +    public void testDrainToNullN() {
1020 +        ArrayBlockingQueue q = populatedQueue(SIZE);
1021 +        try {
1022 +            q.drainTo(null, 0);
1023 +            shouldThrow();
1024 +        } catch (NullPointerException success) {
1025 +        }
1026 +    }
1027 +
1028 +    /**
1029 +     * drainTo(this, n) throws IAE
1030 +     */
1031 +    public void testDrainToSelfN() {
1032 +        ArrayBlockingQueue q = populatedQueue(SIZE);
1033 +        try {
1034 +            q.drainTo(q, 0);
1035 +            shouldThrow();
1036 +        } catch (IllegalArgumentException success) {
1037 +        }
1038 +    }
1039 +
1040 +    /**
1041 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1042 +     */
1043 +    public void testDrainToN() {
1044 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
1045 +        for (int i = 0; i < SIZE + 2; ++i) {
1046 +            for (int j = 0; j < SIZE; j++)
1047 +                assertTrue(q.offer(new Integer(j)));
1048 +            ArrayList l = new ArrayList();
1049 +            q.drainTo(l, i);
1050 +            int k = (i < SIZE)? i : SIZE;
1051 +            assertEquals(l.size(), k);
1052 +            assertEquals(q.size(), SIZE-k);
1053 +            for (int j = 0; j < k; ++j)
1054 +                assertEquals(l.get(j), new Integer(j));
1055 +            while (q.poll() != null) ;
1056 +        }
1057 +    }
1058 +
1059 +
1060   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines