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.5 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.39 by jsr166, Wed Nov 3 16:46:34 2010 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 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());  
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      /**
# Line 26 | Line 44 | public class ArrayBlockingQueueTest exte
44      private ArrayBlockingQueue populatedQueue(int n) {
45          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
46          assertTrue(q.isEmpty());
47 <        for(int i = 0; i < n; i++)
48 <            assertTrue(q.offer(new Integer(i)));
47 >        for (int i = 0; i < n; i++)
48 >            assertTrue(q.offer(new Integer(i)));
49          assertFalse(q.isEmpty());
50          assertEquals(0, q.remainingCapacity());
51 <        assertEquals(n, q.size());
51 >        assertEquals(n, q.size());
52          return q;
53      }
54 <
54 >
55      /**
56       * A new queue has the indicated capacity
57       */
# Line 42 | Line 60 | public class ArrayBlockingQueueTest exte
60      }
61  
62      /**
63 <     * Constructor throws IAE if  capacity argument nonpositive
63 >     * Constructor throws IAE if capacity argument nonpositive
64       */
65      public void testConstructor2() {
66          try {
67              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
68              shouldThrow();
69 <        }
52 <        catch (IllegalArgumentException success) {}
69 >        } catch (IllegalArgumentException success) {}
70      }
71  
72      /**
# Line 59 | Line 76 | public class ArrayBlockingQueueTest exte
76          try {
77              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
78              shouldThrow();
79 <        }
63 <        catch (NullPointerException success) {}
79 >        } catch (NullPointerException success) {}
80      }
81  
82      /**
# Line 71 | Line 87 | public class ArrayBlockingQueueTest exte
87              Integer[] ints = new Integer[SIZE];
88              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
89              shouldThrow();
90 <        }
75 <        catch (NullPointerException success) {}
90 >        } catch (NullPointerException success) {}
91      }
92  
93      /**
# Line 85 | Line 100 | public class ArrayBlockingQueueTest exte
100                  ints[i] = new Integer(i);
101              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
102              shouldThrow();
103 <        }
89 <        catch (NullPointerException success) {}
103 >        } catch (NullPointerException success) {}
104      }
105  
106      /**
# Line 99 | Line 113 | public class ArrayBlockingQueueTest exte
113                  ints[i] = new Integer(i);
114              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
115              shouldThrow();
116 <        }
103 <        catch (IllegalArgumentException success) {}
116 >        } catch (IllegalArgumentException success) {}
117      }
118  
119      /**
120       * Queue contains all elements of collection used to initialize
121       */
122      public void testConstructor7() {
123 <        try {
124 <            Integer[] ints = new Integer[SIZE];
125 <            for (int i = 0; i < SIZE; ++i)
126 <                ints[i] = new Integer(i);
127 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
128 <            for (int i = 0; i < SIZE; ++i)
116 <                assertEquals(ints[i], q.poll());
117 <        }
118 <        finally {}
123 >        Integer[] ints = new Integer[SIZE];
124 >        for (int i = 0; i < SIZE; ++i)
125 >            ints[i] = new Integer(i);
126 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
127 >        for (int i = 0; i < SIZE; ++i)
128 >            assertEquals(ints[i], q.poll());
129      }
130  
131      /**
# Line 151 | Line 161 | public class ArrayBlockingQueueTest exte
161      }
162  
163      /**
164 <     *  offer(null) throws NPE
164 >     * offer(null) throws NPE
165       */
166      public void testOfferNull() {
167 <        try {
167 >        try {
168              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
169              q.offer(null);
170              shouldThrow();
171 <        } catch (NullPointerException success) { }  
171 >        } catch (NullPointerException success) {}
172 >    }
173 >
174 >    /**
175 >     * add(null) throws NPE
176 >     */
177 >    public void testAddNull() {
178 >        try {
179 >            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
180 >            q.add(null);
181 >            shouldThrow();
182 >        } catch (NullPointerException success) {}
183      }
184  
185      /**
# Line 174 | Line 195 | public class ArrayBlockingQueueTest exte
195       * add succeeds if not full; throws ISE if full
196       */
197      public void testAdd() {
198 <        try {
198 >        try {
199              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
200              for (int i = 0; i < SIZE; ++i) {
201                  assertTrue(q.add(new Integer(i)));
202              }
203              assertEquals(0, q.remainingCapacity());
204              q.add(new Integer(SIZE));
205 <        } catch (IllegalStateException success){
206 <        }  
205 >            shouldThrow();
206 >        } catch (IllegalStateException success) {}
207      }
208  
209      /**
210 <     *  addAll(null) throws NPE
210 >     * addAll(null) throws NPE
211       */
212      public void testAddAll1() {
213          try {
214              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
215              q.addAll(null);
216              shouldThrow();
217 <        }
197 <        catch (NullPointerException success) {}
217 >        } catch (NullPointerException success) {}
218      }
219 +
220      /**
221 <     *  addAll of a collection with null elements throws NPE
221 >     * addAll(this) throws IAE
222 >     */
223 >    public void testAddAllSelf() {
224 >        try {
225 >            ArrayBlockingQueue q = populatedQueue(SIZE);
226 >            q.addAll(q);
227 >            shouldThrow();
228 >        } catch (IllegalArgumentException success) {}
229 >    }
230 >
231 >
232 >    /**
233 >     * addAll of a collection with null elements throws NPE
234       */
235      public void testAddAll2() {
236          try {
# Line 205 | Line 238 | public class ArrayBlockingQueueTest exte
238              Integer[] ints = new Integer[SIZE];
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
209 <        catch (NullPointerException success) {}
241 >        } catch (NullPointerException success) {}
242      }
243 +
244      /**
245       * addAll of a collection with any null elements throws NPE after
246       * possibly adding some elements
# Line 220 | Line 253 | public class ArrayBlockingQueueTest exte
253                  ints[i] = new Integer(i);
254              q.addAll(Arrays.asList(ints));
255              shouldThrow();
256 <        }
224 <        catch (NullPointerException success) {}
256 >        } catch (NullPointerException success) {}
257      }
258 +
259      /**
260       * addAll throws ISE if not enough room
261       */
# Line 234 | Line 267 | public class ArrayBlockingQueueTest exte
267                  ints[i] = new Integer(i);
268              q.addAll(Arrays.asList(ints));
269              shouldThrow();
270 <        }
238 <        catch (IllegalStateException success) {}
270 >        } catch (IllegalStateException success) {}
271      }
272 +
273      /**
274       * Queue contains all elements, in traversal order, of successful addAll
275       */
276      public void testAddAll5() {
277 <        try {
278 <            Integer[] empty = new Integer[0];
279 <            Integer[] ints = new Integer[SIZE];
280 <            for (int i = 0; i < SIZE; ++i)
281 <                ints[i] = new Integer(i);
282 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
283 <            assertFalse(q.addAll(Arrays.asList(empty)));
284 <            assertTrue(q.addAll(Arrays.asList(ints)));
285 <            for (int i = 0; i < SIZE; ++i)
253 <                assertEquals(ints[i], q.poll());
254 <        }
255 <        finally {}
277 >        Integer[] empty = new Integer[0];
278 >        Integer[] ints = new Integer[SIZE];
279 >        for (int i = 0; i < SIZE; ++i)
280 >            ints[i] = new Integer(i);
281 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
282 >        assertFalse(q.addAll(Arrays.asList(empty)));
283 >        assertTrue(q.addAll(Arrays.asList(ints)));
284 >        for (int i = 0; i < SIZE; ++i)
285 >            assertEquals(ints[i], q.poll());
286      }
287  
288      /**
289 <     *  put(null) throws NPE
289 >     * put(null) throws NPE
290       */
291 <     public void testPutNull() {
292 <        try {
291 >    public void testPutNull() throws InterruptedException {
292 >        try {
293              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
294              q.put(null);
295              shouldThrow();
296 <        }
267 <        catch (NullPointerException success){
268 <        }  
269 <        catch (InterruptedException ie) {
270 <            unexpectedException();
271 <        }
296 >        } catch (NullPointerException success) {}
297       }
298  
299      /**
300       * all elements successfully put are contained
301       */
302 <     public void testPut() {
303 <         try {
304 <             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
305 <             for (int i = 0; i < SIZE; ++i) {
306 <                 Integer I = new Integer(i);
307 <                 q.put(I);
283 <                 assertTrue(q.contains(I));
284 <             }
285 <             assertEquals(0, q.remainingCapacity());
286 <         }
287 <        catch (InterruptedException ie) {
288 <            unexpectedException();
302 >    public void testPut() throws InterruptedException {
303 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
304 >        for (int i = 0; i < SIZE; ++i) {
305 >            Integer I = new Integer(i);
306 >            q.put(I);
307 >            assertTrue(q.contains(I));
308          }
309 +        assertEquals(0, q.remainingCapacity());
310      }
311  
312      /**
313       * put blocks interruptibly if full
314       */
315 <    public void testBlockingPut() {
316 <        Thread t = new Thread(new Runnable() {
317 <                public void run() {
318 <                    int added = 0;
319 <                    try {
320 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
321 <                        for (int i = 0; i < SIZE; ++i) {
322 <                            q.put(new Integer(i));
323 <                            ++added;
324 <                        }
325 <                        q.put(new Integer(SIZE));
326 <                        threadShouldThrow();
327 <                    } catch (InterruptedException ie){
328 <                        threadAssertEquals(added, SIZE);
329 <                    }  
330 <                }});
331 <        try {
332 <            t.start();
333 <           Thread.sleep(SHORT_DELAY_MS);
334 <           t.interrupt();
315 <           t.join();
316 <        }
317 <        catch (InterruptedException ie) {
318 <            unexpectedException();
319 <        }
315 >    public void testBlockingPut() throws InterruptedException {
316 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
317 >        Thread t = new Thread(new CheckedRunnable() {
318 >            public void realRun() throws InterruptedException {
319 >                for (int i = 0; i < SIZE; ++i)
320 >                    q.put(i);
321 >                assertEquals(SIZE, q.size());
322 >                assertEquals(0, q.remainingCapacity());
323 >                try {
324 >                    q.put(99);
325 >                    shouldThrow();
326 >                } catch (InterruptedException success) {}
327 >            }});
328 >
329 >        t.start();
330 >        Thread.sleep(SHORT_DELAY_MS);
331 >        t.interrupt();
332 >        t.join();
333 >        assertEquals(SIZE, q.size());
334 >        assertEquals(0, q.remainingCapacity());
335      }
336  
337      /**
338       * put blocks waiting for take when full
339       */
340 <    public void testPutWithTake() {
341 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
342 <        Thread t = new Thread(new Runnable() {
343 <                public void run() {
344 <                    int added = 0;
345 <                    try {
346 <                        q.put(new Object());
347 <                        ++added;
348 <                        q.put(new Object());
349 <                        ++added;
350 <                        q.put(new Object());
351 <                        ++added;
352 <                        q.put(new Object());
353 <                        ++added;
354 <                        threadShouldThrow();
355 <                    } catch (InterruptedException e){
356 <                        threadAssertTrue(added >= 2);
357 <                    }
358 <                }
359 <            });
360 <        try {
346 <            t.start();
347 <            Thread.sleep(SHORT_DELAY_MS);
348 <            q.take();
349 <            t.interrupt();
350 <            t.join();
351 <        } catch (Exception e){
352 <            unexpectedException();
353 <        }
340 >    public void testPutWithTake() throws InterruptedException {
341 >        final int capacity = 2;
342 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
343 >        Thread t = new Thread(new CheckedRunnable() {
344 >            public void realRun() throws InterruptedException {
345 >                for (int i = 0; i < capacity + 1; i++)
346 >                    q.put(i);
347 >                try {
348 >                    q.put(99);
349 >                    shouldThrow();
350 >                } catch (InterruptedException success) {}
351 >            }});
352 >
353 >        t.start();
354 >        Thread.sleep(SHORT_DELAY_MS);
355 >        assertEquals(q.remainingCapacity(), 0);
356 >        assertEquals(0, q.take());
357 >        Thread.sleep(SHORT_DELAY_MS);
358 >        t.interrupt();
359 >        t.join();
360 >        assertEquals(q.remainingCapacity(), 0);
361      }
362  
363      /**
364       * timed offer times out if full and elements not taken
365       */
366 <    public void testTimedOffer() {
366 >    public void testTimedOffer() throws InterruptedException {
367          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
368 <        Thread t = new Thread(new Runnable() {
369 <                public void run() {
370 <                    try {
371 <                        q.put(new Object());
372 <                        q.put(new Object());
373 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
374 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
375 <                        threadShouldThrow();
376 <                    } catch (InterruptedException success){}
377 <                }
378 <            });
379 <        
380 <        try {
381 <            t.start();
382 <            Thread.sleep(SHORT_DELAY_MS);
376 <            t.interrupt();
377 <            t.join();
378 <        } catch (Exception e){
379 <            unexpectedException();
380 <        }
368 >        Thread t = new Thread(new CheckedRunnable() {
369 >            public void realRun() throws InterruptedException {
370 >                q.put(new Object());
371 >                q.put(new Object());
372 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
373 >                try {
374 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
375 >                    shouldThrow();
376 >                } catch (InterruptedException success) {}
377 >            }});
378 >
379 >        t.start();
380 >        Thread.sleep(SHORT_DELAY_MS);
381 >        t.interrupt();
382 >        t.join();
383      }
384  
385      /**
386       * take retrieves elements in FIFO order
387       */
388 <    public void testTake() {
389 <        try {
390 <            ArrayBlockingQueue q = populatedQueue(SIZE);
391 <            for (int i = 0; i < SIZE; ++i) {
390 <                assertEquals(i, ((Integer)q.take()).intValue());
391 <            }
392 <        } catch (InterruptedException e){
393 <            unexpectedException();
394 <        }  
395 <    }
396 <
397 <    /**
398 <     * take blocks interruptibly when empty
399 <     */
400 <    public void testTakeFromEmpty() {
401 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
402 <        Thread t = new Thread(new Runnable() {
403 <                public void run() {
404 <                    try {
405 <                        q.take();
406 <                        threadShouldThrow();
407 <                    } catch (InterruptedException success){ }                
408 <                }
409 <            });
410 <        try {
411 <            t.start();
412 <            Thread.sleep(SHORT_DELAY_MS);
413 <            t.interrupt();
414 <            t.join();
415 <        } catch (Exception e){
416 <            unexpectedException();
388 >    public void testTake() throws InterruptedException {
389 >        ArrayBlockingQueue q = populatedQueue(SIZE);
390 >        for (int i = 0; i < SIZE; ++i) {
391 >            assertEquals(i, q.take());
392          }
393      }
394  
395      /**
396       * Take removes existing elements until empty, then blocks interruptibly
397       */
398 <    public void testBlockingTake() {
399 <        Thread t = new Thread(new Runnable() {
400 <                public void run() {
401 <                    try {
402 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
403 <                        for (int i = 0; i < SIZE; ++i) {
404 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
405 <                        }
406 <                        q.take();
407 <                        threadShouldThrow();
408 <                    } catch (InterruptedException success){
409 <                    }  
410 <                }});
411 <        try {
412 <            t.start();
413 <            Thread.sleep(SHORT_DELAY_MS);
414 <            t.interrupt();
440 <            t.join();
441 <        }
442 <        catch (InterruptedException ie) {
443 <            unexpectedException();
444 <        }
398 >    public void testBlockingTake() throws InterruptedException {
399 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
400 >        Thread t = new Thread(new CheckedRunnable() {
401 >            public void realRun() throws InterruptedException {
402 >                for (int i = 0; i < SIZE; ++i) {
403 >                    assertEquals(i, q.take());
404 >                }
405 >                try {
406 >                    q.take();
407 >                    shouldThrow();
408 >                } catch (InterruptedException success) {}
409 >            }});
410 >
411 >        t.start();
412 >        Thread.sleep(SHORT_DELAY_MS);
413 >        t.interrupt();
414 >        t.join();
415      }
416  
417  
# Line 451 | Line 421 | public class ArrayBlockingQueueTest exte
421      public void testPoll() {
422          ArrayBlockingQueue q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.poll()).intValue());
424 >            assertEquals(i, q.poll());
425          }
426 <        assertNull(q.poll());
426 >        assertNull(q.poll());
427      }
428  
429      /**
430 <     * timed pool with zero timeout succeeds when non-empty, else times out
430 >     * timed poll with zero timeout succeeds when non-empty, else times out
431       */
432 <    public void testTimedPoll0() {
433 <        try {
434 <            ArrayBlockingQueue q = populatedQueue(SIZE);
435 <            for (int i = 0; i < SIZE; ++i) {
436 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
437 <            }
468 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
469 <        } catch (InterruptedException e){
470 <            unexpectedException();
471 <        }  
432 >    public void testTimedPoll0() throws InterruptedException {
433 >        ArrayBlockingQueue q = populatedQueue(SIZE);
434 >        for (int i = 0; i < SIZE; ++i) {
435 >            assertEquals(i, q.poll(0, MILLISECONDS));
436 >        }
437 >        assertNull(q.poll(0, MILLISECONDS));
438      }
439  
440      /**
441 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
441 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
442       */
443 <    public void testTimedPoll() {
444 <        try {
445 <            ArrayBlockingQueue q = populatedQueue(SIZE);
446 <            for (int i = 0; i < SIZE; ++i) {
447 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
448 <            }
483 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
484 <        } catch (InterruptedException e){
485 <            unexpectedException();
486 <        }  
443 >    public void testTimedPoll() throws InterruptedException {
444 >        ArrayBlockingQueue q = populatedQueue(SIZE);
445 >        for (int i = 0; i < SIZE; ++i) {
446 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
447 >        }
448 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
449      }
450  
451      /**
452       * Interrupted timed poll throws InterruptedException instead of
453       * returning timeout status
454       */
455 <    public void testInterruptedTimedPoll() {
456 <        Thread t = new Thread(new Runnable() {
457 <                public void run() {
458 <                    try {
459 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
460 <                        for (int i = 0; i < SIZE; ++i) {
499 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
500 <                        }
501 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
502 <                    } catch (InterruptedException success){
503 <                    }  
504 <                }});
505 <        try {
506 <            t.start();
507 <            Thread.sleep(SHORT_DELAY_MS);
508 <            t.interrupt();
509 <            t.join();
510 <        }
511 <        catch (InterruptedException ie) {
512 <            unexpectedException();
513 <        }
514 <    }
515 <
516 <    /**
517 <     *  timed poll before a delayed offer fails; after offer succeeds;
518 <     *  on interruption throws
519 <     */
520 <    public void testTimedPollWithOffer() {
521 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
522 <        Thread t = new Thread(new Runnable() {
523 <                public void run() {
524 <                    try {
525 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
527 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
528 <                        threadShouldThrow();
529 <                    } catch (InterruptedException success) { }                
455 >    public void testInterruptedTimedPoll() throws InterruptedException {
456 >        Thread t = new Thread(new CheckedRunnable() {
457 >            public void realRun() throws InterruptedException {
458 >                ArrayBlockingQueue q = populatedQueue(SIZE);
459 >                for (int i = 0; i < SIZE; ++i) {
460 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
461                  }
462 <            });
463 <        try {
464 <            t.start();
465 <            Thread.sleep(SMALL_DELAY_MS);
466 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
467 <            t.interrupt();
468 <            t.join();
469 <        } catch (Exception e){
470 <            unexpectedException();
471 <        }
472 <    }  
542 <
462 >                try {
463 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
464 >                    shouldThrow();
465 >                } catch (InterruptedException success) {}
466 >            }});
467 >
468 >        t.start();
469 >        Thread.sleep(SHORT_DELAY_MS);
470 >        t.interrupt();
471 >        t.join();
472 >    }
473  
474      /**
475       * peek returns next element, or null if empty
# Line 547 | Line 477 | public class ArrayBlockingQueueTest exte
477      public void testPeek() {
478          ArrayBlockingQueue q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480 <            assertEquals(i, ((Integer)q.peek()).intValue());
481 <            q.poll();
480 >            assertEquals(i, q.peek());
481 >            assertEquals(i, q.poll());
482              assertTrue(q.peek() == null ||
483 <                       i != ((Integer)q.peek()).intValue());
483 >                       !q.peek().equals(i));
484          }
485 <        assertNull(q.peek());
485 >        assertNull(q.peek());
486      }
487  
488      /**
# Line 561 | Line 491 | public class ArrayBlockingQueueTest exte
491      public void testElement() {
492          ArrayBlockingQueue q = populatedQueue(SIZE);
493          for (int i = 0; i < SIZE; ++i) {
494 <            assertEquals(i, ((Integer)q.element()).intValue());
495 <            q.poll();
494 >            assertEquals(i, q.element());
495 >            assertEquals(i, q.poll());
496          }
497          try {
498              q.element();
499              shouldThrow();
500 <        }
571 <        catch (NoSuchElementException success) {}
500 >        } catch (NoSuchElementException success) {}
501      }
502  
503      /**
# Line 577 | Line 506 | public class ArrayBlockingQueueTest exte
506      public void testRemove() {
507          ArrayBlockingQueue q = populatedQueue(SIZE);
508          for (int i = 0; i < SIZE; ++i) {
509 <            assertEquals(i, ((Integer)q.remove()).intValue());
509 >            assertEquals(i, q.remove());
510          }
511          try {
512              q.remove();
513              shouldThrow();
514 <        } catch (NoSuchElementException success){
586 <        }  
514 >        } catch (NoSuchElementException success) {}
515      }
516  
517      /**
# Line 600 | Line 528 | public class ArrayBlockingQueueTest exte
528          }
529          assertTrue(q.isEmpty());
530      }
531 <        
531 >
532      /**
533       * contains(x) reports true when elements added but not yet removed
534       */
# Line 608 | Line 536 | public class ArrayBlockingQueueTest exte
536          ArrayBlockingQueue q = populatedQueue(SIZE);
537          for (int i = 0; i < SIZE; ++i) {
538              assertTrue(q.contains(new Integer(i)));
539 <            q.poll();
539 >            assertEquals(i, q.poll());
540              assertFalse(q.contains(new Integer(i)));
541          }
542      }
# Line 624 | Line 552 | public class ArrayBlockingQueueTest exte
552          assertEquals(SIZE, q.remainingCapacity());
553          q.add(one);
554          assertFalse(q.isEmpty());
555 +        assertTrue(q.contains(one));
556          q.clear();
557          assertTrue(q.isEmpty());
558      }
# Line 678 | Line 607 | public class ArrayBlockingQueueTest exte
607      }
608  
609      /**
610 <     *  toArray contains all elements
610 >     * toArray contains all elements
611       */
612 <    public void testToArray() {
612 >    public void testToArray() throws InterruptedException {
613          ArrayBlockingQueue q = populatedQueue(SIZE);
614 <        Object[] o = q.toArray();
615 <        try {
616 <        for(int i = 0; i < o.length; i++)
688 <            assertEquals(o[i], q.take());
689 <        } catch (InterruptedException e){
690 <            unexpectedException();
691 <        }    
614 >        Object[] o = q.toArray();
615 >        for (int i = 0; i < o.length; i++)
616 >            assertEquals(o[i], q.take());
617      }
618  
619      /**
620       * toArray(a) contains all elements
621       */
622 <    public void testToArray2() {
622 >    public void testToArray2() throws InterruptedException {
623 >        ArrayBlockingQueue q = populatedQueue(SIZE);
624 >        Integer[] ints = new Integer[SIZE];
625 >        ints = (Integer[])q.toArray(ints);
626 >        for (int i = 0; i < ints.length; i++)
627 >            assertEquals(ints[i], q.take());
628 >    }
629 >
630 >    /**
631 >     * toArray(null) throws NullPointerException
632 >     */
633 >    public void testToArray_NullArg() {
634 >        ArrayBlockingQueue q = populatedQueue(SIZE);
635 >        try {
636 >            q.toArray(null);
637 >            shouldThrow();
638 >        } catch (NullPointerException success) {}
639 >    }
640 >
641 >    /**
642 >     * toArray(incompatible array type) throws ArrayStoreException
643 >     */
644 >    public void testToArray1_BadArg() {
645          ArrayBlockingQueue q = populatedQueue(SIZE);
646 <        Integer[] ints = new Integer[SIZE];
647 <        ints = (Integer[])q.toArray(ints);
648 <        try {
649 <            for(int i = 0; i < ints.length; i++)
703 <                assertEquals(ints[i], q.take());
704 <        } catch (InterruptedException e){
705 <            unexpectedException();
706 <        }    
646 >        try {
647 >            q.toArray(new String[10]);
648 >            shouldThrow();
649 >        } catch (ArrayStoreException success) {}
650      }
651 <    
651 >
652 >
653      /**
654       * iterator iterates through all elements
655       */
656 <    public void testIterator() {
656 >    public void testIterator() throws InterruptedException {
657          ArrayBlockingQueue q = populatedQueue(SIZE);
658 <        Iterator it = q.iterator();
659 <        try {
660 <            while(it.hasNext()){
661 <                assertEquals(it.next(), q.take());
718 <            }
719 <        } catch (InterruptedException e){
720 <            unexpectedException();
721 <        }    
658 >        Iterator it = q.iterator();
659 >        while (it.hasNext()) {
660 >            assertEquals(it.next(), q.take());
661 >        }
662      }
663  
664      /**
665       * iterator.remove removes current element
666       */
667 <    public void testIteratorRemove () {
667 >    public void testIteratorRemove() {
668          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
669          q.add(two);
670          q.add(one);
# Line 733 | Line 673 | public class ArrayBlockingQueueTest exte
673          Iterator it = q.iterator();
674          it.next();
675          it.remove();
676 <        
676 >
677          it = q.iterator();
678 <        assertEquals(it.next(), one);
679 <        assertEquals(it.next(), three);
678 >        assertSame(it.next(), one);
679 >        assertSame(it.next(), three);
680          assertFalse(it.hasNext());
681      }
682  
# Line 753 | Line 693 | public class ArrayBlockingQueueTest exte
693  
694          int k = 0;
695          for (Iterator it = q.iterator(); it.hasNext();) {
696 <            int i = ((Integer)(it.next())).intValue();
757 <            assertEquals(++k, i);
696 >            assertEquals(++k, it.next());
697          }
698          assertEquals(3, k);
699      }
# Line 762 | Line 701 | public class ArrayBlockingQueueTest exte
701      /**
702       * Modifications do not cause iterators to fail
703       */
704 <    public void testWeaklyConsistentIteration () {
704 >    public void testWeaklyConsistentIteration() {
705          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
706          q.add(one);
707          q.add(two);
708          q.add(three);
709 <        try {
710 <            for (Iterator it = q.iterator(); it.hasNext();) {
711 <                q.remove();
773 <                it.next();
774 <            }
775 <        }
776 <        catch (ConcurrentModificationException e) {
777 <            unexpectedException();
709 >        for (Iterator it = q.iterator(); it.hasNext();) {
710 >            q.remove();
711 >            it.next();
712          }
713          assertEquals(0, q.size());
714      }
# Line 789 | Line 723 | public class ArrayBlockingQueueTest exte
723          for (int i = 0; i < SIZE; ++i) {
724              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
725          }
726 <    }        
726 >    }
727  
728  
729      /**
# Line 800 | Line 734 | public class ArrayBlockingQueueTest exte
734          q.add(one);
735          q.add(two);
736          ExecutorService executor = Executors.newFixedThreadPool(2);
737 <        executor.execute(new Runnable() {
738 <            public void run() {
739 <                threadAssertFalse(q.offer(three));
740 <                try {
741 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
742 <                    threadAssertEquals(0, q.remainingCapacity());
743 <                }
744 <                catch (InterruptedException e) {
745 <                    threadUnexpectedException();
746 <                }
747 <            }
748 <        });
737 >        executor.execute(new CheckedRunnable() {
738 >            public void realRun() throws InterruptedException {
739 >                assertFalse(q.offer(three));
740 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
741 >                assertEquals(0, q.remainingCapacity());
742 >            }});
743 >
744 >        executor.execute(new CheckedRunnable() {
745 >            public void realRun() throws InterruptedException {
746 >                Thread.sleep(SMALL_DELAY_MS);
747 >                assertSame(one, q.take());
748 >            }});
749  
816        executor.execute(new Runnable() {
817            public void run() {
818                try {
819                    Thread.sleep(SMALL_DELAY_MS);
820                    threadAssertEquals(one, q.take());
821                }
822                catch (InterruptedException e) {
823                    threadUnexpectedException();
824                }
825            }
826        });
827        
750          joinPool(executor);
829
751      }
752  
753      /**
# Line 835 | Line 756 | public class ArrayBlockingQueueTest exte
756      public void testPollInExecutor() {
757          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
758          ExecutorService executor = Executors.newFixedThreadPool(2);
759 <        executor.execute(new Runnable() {
760 <            public void run() {
761 <                threadAssertNull(q.poll());
762 <                try {
763 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
764 <                    threadAssertTrue(q.isEmpty());
765 <                }
766 <                catch (InterruptedException e) {
767 <                    threadUnexpectedException();
768 <                }
769 <            }
770 <        });
759 >        executor.execute(new CheckedRunnable() {
760 >            public void realRun() throws InterruptedException {
761 >                assertNull(q.poll());
762 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
763 >                assertTrue(q.isEmpty());
764 >            }});
765 >
766 >        executor.execute(new CheckedRunnable() {
767 >            public void realRun() throws InterruptedException {
768 >                Thread.sleep(SMALL_DELAY_MS);
769 >                q.put(one);
770 >            }});
771  
851        executor.execute(new Runnable() {
852            public void run() {
853                try {
854                    Thread.sleep(SMALL_DELAY_MS);
855                    q.put(one);
856                }
857                catch (InterruptedException e) {
858                    threadUnexpectedException();
859                }
860            }
861        });
862        
772          joinPool(executor);
773      }
774  
775      /**
776       * A deserialized serialized queue has same elements in same order
777       */
778 <    public void testSerialization() {
778 >    public void testSerialization() throws Exception {
779          ArrayBlockingQueue q = populatedQueue(SIZE);
780  
781 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
782 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
783 +        out.writeObject(q);
784 +        out.close();
785 +
786 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
787 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
788 +        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
789 +        assertEquals(q.size(), r.size());
790 +        while (!q.isEmpty())
791 +            assertEquals(q.remove(), r.remove());
792 +    }
793 +
794 +    /**
795 +     * drainTo(null) throws NPE
796 +     */
797 +    public void testDrainToNull() {
798 +        ArrayBlockingQueue q = populatedQueue(SIZE);
799          try {
800 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
801 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
802 <            out.writeObject(q);
803 <            out.close();
804 <
805 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
806 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
807 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
808 <            assertEquals(q.size(), r.size());
809 <            while (!q.isEmpty())
810 <                assertEquals(q.remove(), r.remove());
811 <        } catch(Exception e){
812 <            unexpectedException();
813 <        }
800 >            q.drainTo(null);
801 >            shouldThrow();
802 >        } catch (NullPointerException success) {}
803 >    }
804 >
805 >    /**
806 >     * drainTo(this) throws IAE
807 >     */
808 >    public void testDrainToSelf() {
809 >        ArrayBlockingQueue q = populatedQueue(SIZE);
810 >        try {
811 >            q.drainTo(q);
812 >            shouldThrow();
813 >        } catch (IllegalArgumentException success) {}
814 >    }
815 >
816 >    /**
817 >     * drainTo(c) empties queue into another collection c
818 >     */
819 >    public void testDrainTo() {
820 >        ArrayBlockingQueue q = populatedQueue(SIZE);
821 >        ArrayList l = new ArrayList();
822 >        q.drainTo(l);
823 >        assertEquals(q.size(), 0);
824 >        assertEquals(l.size(), SIZE);
825 >        for (int i = 0; i < SIZE; ++i)
826 >            assertEquals(l.get(i), new Integer(i));
827 >        q.add(zero);
828 >        q.add(one);
829 >        assertFalse(q.isEmpty());
830 >        assertTrue(q.contains(zero));
831 >        assertTrue(q.contains(one));
832 >        l.clear();
833 >        q.drainTo(l);
834 >        assertEquals(q.size(), 0);
835 >        assertEquals(l.size(), 2);
836 >        for (int i = 0; i < 2; ++i)
837 >            assertEquals(l.get(i), new Integer(i));
838 >    }
839 >
840 >    /**
841 >     * drainTo empties full queue, unblocking a waiting put.
842 >     */
843 >    public void testDrainToWithActivePut() throws InterruptedException {
844 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
845 >        Thread t = new Thread(new CheckedRunnable() {
846 >            public void realRun() throws InterruptedException {
847 >                q.put(new Integer(SIZE+1));
848 >            }});
849 >
850 >        t.start();
851 >        ArrayList l = new ArrayList();
852 >        q.drainTo(l);
853 >        assertTrue(l.size() >= SIZE);
854 >        for (int i = 0; i < SIZE; ++i)
855 >            assertEquals(l.get(i), new Integer(i));
856 >        t.join();
857 >        assertTrue(q.size() + l.size() >= SIZE);
858      }
859  
860 +    /**
861 +     * drainTo(null, n) throws NPE
862 +     */
863 +    public void testDrainToNullN() {
864 +        ArrayBlockingQueue q = populatedQueue(SIZE);
865 +        try {
866 +            q.drainTo(null, 0);
867 +            shouldThrow();
868 +        } catch (NullPointerException success) {}
869 +    }
870 +
871 +    /**
872 +     * drainTo(this, n) throws IAE
873 +     */
874 +    public void testDrainToSelfN() {
875 +        ArrayBlockingQueue q = populatedQueue(SIZE);
876 +        try {
877 +            q.drainTo(q, 0);
878 +            shouldThrow();
879 +        } catch (IllegalArgumentException success) {}
880 +    }
881 +
882 +    /**
883 +     * drainTo(c, n) empties first min(n, size) elements of queue into c
884 +     */
885 +    public void testDrainToN() {
886 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
887 +        for (int i = 0; i < SIZE + 2; ++i) {
888 +            for (int j = 0; j < SIZE; j++)
889 +                assertTrue(q.offer(new Integer(j)));
890 +            ArrayList l = new ArrayList();
891 +            q.drainTo(l, i);
892 +            int k = (i < SIZE) ? i : SIZE;
893 +            assertEquals(l.size(), k);
894 +            assertEquals(q.size(), SIZE-k);
895 +            for (int j = 0; j < k; ++j)
896 +                assertEquals(l.get(j), new Integer(j));
897 +            while (q.poll() != null) ;
898 +        }
899 +    }
900  
901   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines