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.16 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.41 by jsr166, Fri Nov 5 00:17:22 2010 UTC

# Line 14 | Line 14 | import static java.util.concurrent.TimeU
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      /**
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private ArrayBlockingQueue populatedQueue(int n) {
45 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
44 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
46          assertTrue(q.isEmpty());
47          for (int i = 0; i < n; i++)
48              assertTrue(q.offer(new Integer(i)));
# Line 44 | 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 <        }
54 <        catch (IllegalArgumentException success) {}
69 >        } catch (IllegalArgumentException success) {}
70      }
71  
72      /**
# Line 61 | Line 76 | public class ArrayBlockingQueueTest exte
76          try {
77              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
78              shouldThrow();
79 <        }
65 <        catch (NullPointerException success) {}
79 >        } catch (NullPointerException success) {}
80      }
81  
82      /**
# Line 73 | 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 <        }
77 <        catch (NullPointerException success) {}
90 >        } catch (NullPointerException success) {}
91      }
92  
93      /**
# Line 87 | 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 <        }
91 <        catch (NullPointerException success) {}
103 >        } catch (NullPointerException success) {}
104      }
105  
106      /**
# Line 101 | 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 <        }
105 <        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)
118 <                assertEquals(ints[i], q.poll());
119 <        }
120 <        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 153 | 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 {
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
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) { }
182 >        } catch (NullPointerException success) {}
183      }
184  
185      /**
# Line 194 | Line 202 | public class ArrayBlockingQueueTest exte
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 <        }
210 <        catch (NullPointerException success) {}
217 >        } catch (NullPointerException success) {}
218      }
219  
220      /**
# Line 218 | Line 225 | public class ArrayBlockingQueueTest exte
225              ArrayBlockingQueue q = populatedQueue(SIZE);
226              q.addAll(q);
227              shouldThrow();
228 <        }
222 <        catch (IllegalArgumentException success) {}
228 >        } catch (IllegalArgumentException success) {}
229      }
230  
231  
232      /**
233 <     *  addAll of a collection with null elements throws NPE
233 >     * addAll of a collection with null elements throws NPE
234       */
235      public void testAddAll2() {
236          try {
# Line 232 | Line 238 | public class ArrayBlockingQueueTest exte
238              Integer[] ints = new Integer[SIZE];
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
236 <        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 247 | Line 253 | public class ArrayBlockingQueueTest exte
253                  ints[i] = new Integer(i);
254              q.addAll(Arrays.asList(ints));
255              shouldThrow();
256 <        }
251 <        catch (NullPointerException success) {}
256 >        } catch (NullPointerException success) {}
257      }
258 +
259      /**
260       * addAll throws ISE if not enough room
261       */
# Line 261 | Line 267 | public class ArrayBlockingQueueTest exte
267                  ints[i] = new Integer(i);
268              q.addAll(Arrays.asList(ints));
269              shouldThrow();
270 <        }
265 <        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)
280 <                assertEquals(ints[i], q.poll());
281 <        }
282 <        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() {
291 >    public void testPutNull() throws InterruptedException {
292          try {
293              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
294              q.put(null);
295              shouldThrow();
296 <        }
294 <        catch (NullPointerException success) {
295 <        }
296 <        catch (InterruptedException ie) {
297 <            unexpectedException();
298 <        }
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);
310 <                 assertTrue(q.contains(I));
311 <             }
312 <             assertEquals(0, q.remainingCapacity());
313 <         }
314 <        catch (InterruptedException ie) {
315 <            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() {
315 >    public void testBlockingPut() throws InterruptedException {
316          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
317 <        Thread t = new Thread(new Runnable() {
318 <                public void run() {
319 <                    int added = 0;
320 <                    try {
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(MEDIUM_DELAY_MS);
334 <           t.interrupt();
342 <           t.join();
343 <        }
344 <        catch (InterruptedException ie) {
345 <            unexpectedException();
346 <        }
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 {
373 <            t.start();
374 <            Thread.sleep(SHORT_DELAY_MS);
375 <            q.take();
376 <            t.interrupt();
377 <            t.join();
378 <        } catch (Exception e) {
379 <            unexpectedException();
380 <        }
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, MILLISECONDS));
374 <                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
375 <                        threadShouldThrow();
376 <                    } catch (InterruptedException success) {}
377 <                }
378 <            });
379 <
380 <        try {
381 <            t.start();
382 <            Thread.sleep(SHORT_DELAY_MS);
403 <            t.interrupt();
404 <            t.join();
405 <        } catch (Exception e) {
406 <            unexpectedException();
407 <        }
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) {
417 <                assertEquals(i, ((Integer)q.take()).intValue());
418 <            }
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() {
431 <                    try {
432 <                        q.take();
433 <                        threadShouldThrow();
434 <                    } catch (InterruptedException success) { }
435 <                }
436 <            });
437 <        try {
438 <            t.start();
439 <            Thread.sleep(SHORT_DELAY_MS);
440 <            t.interrupt();
441 <            t.join();
442 <        } catch (Exception e) {
443 <            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();
467 <            t.join();
468 <        }
469 <        catch (InterruptedException ie) {
470 <            unexpectedException();
471 <        }
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 478 | 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());
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) {
493 <                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
494 <            }
495 <            assertNull(q.poll(0, MILLISECONDS));
496 <        } catch (InterruptedException e) {
497 <            unexpectedException();
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) {
508 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
509 <            }
510 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
511 <        } catch (InterruptedException e) {
512 <            unexpectedException();
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) {
526 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
527 <                        }
528 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
529 <                    } catch (InterruptedException success) {
530 <                    }
531 <                }});
532 <        try {
533 <            t.start();
534 <            Thread.sleep(SHORT_DELAY_MS);
535 <            t.interrupt();
536 <            t.join();
537 <        }
538 <        catch (InterruptedException ie) {
539 <            unexpectedException();
540 <        }
541 <    }
542 <
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() {
551 <                    try {
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) { }
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, MILLISECONDS));
467 <            t.interrupt();
468 <            t.join();
469 <        } catch (Exception e) {
470 <            unexpectedException();
471 <        }
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  
570
474      /**
475       * peek returns next element, or null if empty
476       */
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());
486      }
# Line 588 | 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 <        }
598 <        catch (NoSuchElementException success) {}
500 >        } catch (NoSuchElementException success) {}
501      }
502  
503      /**
# Line 604 | 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) {
613 <        }
514 >        } catch (NoSuchElementException success) {}
515      }
516  
517      /**
# Line 635 | 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 706 | Line 607 | public class ArrayBlockingQueueTest exte
607      }
608  
609      /**
610 <     *  toArray contains all elements
610 >     * toArray contains all elements in FIFO order
611       */
612      public void testToArray() {
613          ArrayBlockingQueue q = populatedQueue(SIZE);
614          Object[] o = q.toArray();
714        try {
615          for (int i = 0; i < o.length; i++)
616 <            assertEquals(o[i], q.take());
717 <        } catch (InterruptedException e) {
718 <            unexpectedException();
719 <        }
616 >            assertSame(o[i], q.poll());
617      }
618  
619      /**
620 <     * toArray(a) contains all elements
620 >     * toArray(a) contains all elements in FIFO order
621       */
622      public void testToArray2() {
623 <        ArrayBlockingQueue q = populatedQueue(SIZE);
623 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
624          Integer[] ints = new Integer[SIZE];
625 <        ints = (Integer[])q.toArray(ints);
626 <        try {
627 <            for (int i = 0; i < ints.length; i++)
628 <                assertEquals(ints[i], q.take());
732 <        } catch (InterruptedException e) {
733 <            unexpectedException();
734 <        }
625 >        Integer[] array = q.toArray(ints);
626 >        assertSame(ints, array);
627 >        for (int i = 0; i < ints.length; i++)
628 >            assertSame(ints[i], q.poll());
629      }
630  
631      /**
632 <     * toArray(null) throws NPE
632 >     * toArray(null) throws NullPointerException
633       */
634 <    public void testToArray_BadArg() {
634 >    public void testToArray_NullArg() {
635 >        ArrayBlockingQueue q = populatedQueue(SIZE);
636          try {
637 <            ArrayBlockingQueue q = populatedQueue(SIZE);
743 <            Object o[] = q.toArray(null);
637 >            q.toArray(null);
638              shouldThrow();
639          } catch (NullPointerException success) {}
640      }
641  
642      /**
643 <     * toArray with incompatible array type throws CCE
643 >     * toArray(incompatible array type) throws ArrayStoreException
644       */
645      public void testToArray1_BadArg() {
646 +        ArrayBlockingQueue q = populatedQueue(SIZE);
647          try {
648 <            ArrayBlockingQueue q = populatedQueue(SIZE);
754 <            Object o[] = q.toArray(new String[10] );
648 >            q.toArray(new String[10]);
649              shouldThrow();
650 <        } catch (ArrayStoreException  success) {}
650 >        } catch (ArrayStoreException success) {}
651      }
652  
653  
654      /**
655       * iterator iterates through all elements
656       */
657 <    public void testIterator() {
657 >    public void testIterator() throws InterruptedException {
658          ArrayBlockingQueue q = populatedQueue(SIZE);
659          Iterator it = q.iterator();
660 <        try {
661 <            while (it.hasNext()) {
768 <                assertEquals(it.next(), q.take());
769 <            }
770 <        } catch (InterruptedException e) {
771 <            unexpectedException();
660 >        while (it.hasNext()) {
661 >            assertEquals(it.next(), q.take());
662          }
663      }
664  
665      /**
666       * iterator.remove removes current element
667       */
668 <    public void testIteratorRemove () {
668 >    public void testIteratorRemove() {
669          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
670          q.add(two);
671          q.add(one);
# Line 786 | Line 676 | public class ArrayBlockingQueueTest exte
676          it.remove();
677  
678          it = q.iterator();
679 <        assertEquals(it.next(), one);
680 <        assertEquals(it.next(), three);
679 >        assertSame(it.next(), one);
680 >        assertSame(it.next(), three);
681          assertFalse(it.hasNext());
682      }
683  
# Line 804 | Line 694 | public class ArrayBlockingQueueTest exte
694  
695          int k = 0;
696          for (Iterator it = q.iterator(); it.hasNext();) {
697 <            int i = ((Integer)(it.next())).intValue();
808 <            assertEquals(++k, i);
697 >            assertEquals(++k, it.next());
698          }
699          assertEquals(3, k);
700      }
# Line 813 | Line 702 | public class ArrayBlockingQueueTest exte
702      /**
703       * Modifications do not cause iterators to fail
704       */
705 <    public void testWeaklyConsistentIteration () {
705 >    public void testWeaklyConsistentIteration() {
706          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
707          q.add(one);
708          q.add(two);
709          q.add(three);
710 <        try {
711 <            for (Iterator it = q.iterator(); it.hasNext();) {
712 <                q.remove();
824 <                it.next();
825 <            }
826 <        }
827 <        catch (ConcurrentModificationException e) {
828 <            unexpectedException();
710 >        for (Iterator it = q.iterator(); it.hasNext();) {
711 >            q.remove();
712 >            it.next();
713          }
714          assertEquals(0, q.size());
715      }
# Line 851 | Line 735 | public class ArrayBlockingQueueTest exte
735          q.add(one);
736          q.add(two);
737          ExecutorService executor = Executors.newFixedThreadPool(2);
738 <        executor.execute(new Runnable() {
739 <            public void run() {
740 <                threadAssertFalse(q.offer(three));
741 <                try {
742 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
743 <                    threadAssertEquals(0, q.remainingCapacity());
744 <                }
745 <                catch (InterruptedException e) {
746 <                    threadUnexpectedException();
747 <                }
748 <            }
749 <        });
866 <
867 <        executor.execute(new Runnable() {
868 <            public void run() {
869 <                try {
870 <                    Thread.sleep(SMALL_DELAY_MS);
871 <                    threadAssertEquals(one, q.take());
872 <                }
873 <                catch (InterruptedException e) {
874 <                    threadUnexpectedException();
875 <                }
876 <            }
877 <        });
738 >        executor.execute(new CheckedRunnable() {
739 >            public void realRun() throws InterruptedException {
740 >                assertFalse(q.offer(three));
741 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
742 >                assertEquals(0, q.remainingCapacity());
743 >            }});
744 >
745 >        executor.execute(new CheckedRunnable() {
746 >            public void realRun() throws InterruptedException {
747 >                Thread.sleep(SMALL_DELAY_MS);
748 >                assertSame(one, q.take());
749 >            }});
750  
751          joinPool(executor);
880
752      }
753  
754      /**
# Line 886 | Line 757 | public class ArrayBlockingQueueTest exte
757      public void testPollInExecutor() {
758          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
759          ExecutorService executor = Executors.newFixedThreadPool(2);
760 <        executor.execute(new Runnable() {
761 <            public void run() {
762 <                threadAssertNull(q.poll());
763 <                try {
764 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
765 <                    threadAssertTrue(q.isEmpty());
766 <                }
767 <                catch (InterruptedException e) {
768 <                    threadUnexpectedException();
769 <                }
770 <            }
771 <        });
901 <
902 <        executor.execute(new Runnable() {
903 <            public void run() {
904 <                try {
905 <                    Thread.sleep(SMALL_DELAY_MS);
906 <                    q.put(one);
907 <                }
908 <                catch (InterruptedException e) {
909 <                    threadUnexpectedException();
910 <                }
911 <            }
912 <        });
760 >        executor.execute(new CheckedRunnable() {
761 >            public void realRun() throws InterruptedException {
762 >                assertNull(q.poll());
763 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
764 >                assertTrue(q.isEmpty());
765 >            }});
766 >
767 >        executor.execute(new CheckedRunnable() {
768 >            public void realRun() throws InterruptedException {
769 >                Thread.sleep(SMALL_DELAY_MS);
770 >                q.put(one);
771 >            }});
772  
773          joinPool(executor);
774      }
# Line 917 | Line 776 | public class ArrayBlockingQueueTest exte
776      /**
777       * A deserialized serialized queue has same elements in same order
778       */
779 <    public void testSerialization() {
779 >    public void testSerialization() throws Exception {
780          ArrayBlockingQueue q = populatedQueue(SIZE);
781  
782 <        try {
783 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
784 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
785 <            out.writeObject(q);
786 <            out.close();
787 <
788 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
789 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
790 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
791 <            assertEquals(q.size(), r.size());
792 <            while (!q.isEmpty())
934 <                assertEquals(q.remove(), r.remove());
935 <        } catch (Exception e) {
936 <            unexpectedException();
937 <        }
782 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
783 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
784 >        out.writeObject(q);
785 >        out.close();
786 >
787 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
788 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
789 >        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
790 >        assertEquals(q.size(), r.size());
791 >        while (!q.isEmpty())
792 >            assertEquals(q.remove(), r.remove());
793      }
794  
795      /**
# Line 945 | Line 800 | public class ArrayBlockingQueueTest exte
800          try {
801              q.drainTo(null);
802              shouldThrow();
803 <        } catch (NullPointerException success) {
949 <        }
803 >        } catch (NullPointerException success) {}
804      }
805  
806      /**
# Line 957 | Line 811 | public class ArrayBlockingQueueTest exte
811          try {
812              q.drainTo(q);
813              shouldThrow();
814 <        } catch (IllegalArgumentException success) {
961 <        }
814 >        } catch (IllegalArgumentException success) {}
815      }
816  
817      /**
# Line 988 | Line 841 | public class ArrayBlockingQueueTest exte
841      /**
842       * drainTo empties full queue, unblocking a waiting put.
843       */
844 <    public void testDrainToWithActivePut() {
844 >    public void testDrainToWithActivePut() throws InterruptedException {
845          final ArrayBlockingQueue q = populatedQueue(SIZE);
846 <        Thread t = new Thread(new Runnable() {
847 <                public void run() {
848 <                    try {
849 <                        q.put(new Integer(SIZE+1));
850 <                    } catch (InterruptedException ie) {
851 <                        threadUnexpectedException();
852 <                    }
853 <                }
854 <            });
855 <        try {
856 <            t.start();
857 <            ArrayList l = new ArrayList();
858 <            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 <        }
846 >        Thread t = new Thread(new CheckedRunnable() {
847 >            public void realRun() throws InterruptedException {
848 >                q.put(new Integer(SIZE+1));
849 >            }});
850 >
851 >        t.start();
852 >        ArrayList l = new ArrayList();
853 >        q.drainTo(l);
854 >        assertTrue(l.size() >= SIZE);
855 >        for (int i = 0; i < SIZE; ++i)
856 >            assertEquals(l.get(i), new Integer(i));
857 >        t.join();
858 >        assertTrue(q.size() + l.size() >= SIZE);
859      }
860  
861      /**
# Line 1021 | Line 866 | public class ArrayBlockingQueueTest exte
866          try {
867              q.drainTo(null, 0);
868              shouldThrow();
869 <        } catch (NullPointerException success) {
1025 <        }
869 >        } catch (NullPointerException success) {}
870      }
871  
872      /**
# Line 1033 | Line 877 | public class ArrayBlockingQueueTest exte
877          try {
878              q.drainTo(q, 0);
879              shouldThrow();
880 <        } catch (IllegalArgumentException success) {
1037 <        }
880 >        } catch (IllegalArgumentException success) {}
881      }
882  
883      /**
884 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
884 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
885       */
886      public void testDrainToN() {
887          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 1047 | Line 890 | public class ArrayBlockingQueueTest exte
890                  assertTrue(q.offer(new Integer(j)));
891              ArrayList l = new ArrayList();
892              q.drainTo(l, i);
893 <            int k = (i < SIZE)? i : SIZE;
893 >            int k = (i < SIZE) ? i : SIZE;
894              assertEquals(l.size(), k);
895              assertEquals(q.size(), SIZE-k);
896              for (int j = 0; j < k; ++j)
# Line 1056 | Line 899 | public class ArrayBlockingQueueTest exte
899          }
900      }
901  
1059
902   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines