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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.13 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.28 by dl, Wed Sep 29 12:33:48 2010 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class PriorityBlockingQueueTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(PriorityBlockingQueueTest.class);
20 >        return new TestSuite(PriorityBlockingQueueTest.class);
21      }
22  
23      private static final int NOCAP = Integer.MAX_VALUE;
# Line 24 | Line 25 | public class PriorityBlockingQueueTest e
25      /** Sample Comparator */
26      static class MyReverseComparator implements Comparator {
27          public int compare(Object x, Object y) {
28 <            int i = ((Integer)x).intValue();
28 <            int j = ((Integer)y).intValue();
29 <            if (i < j) return 1;
30 <            if (i > j) return -1;
31 <            return 0;
28 >            return ((Comparable)y).compareTo(x);
29          }
30      }
31  
# Line 39 | Line 36 | public class PriorityBlockingQueueTest e
36      private PriorityBlockingQueue populatedQueue(int n) {
37          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
38          assertTrue(q.isEmpty());
39 <        for (int i = n-1; i >= 0; i-=2)
40 <            assertTrue(q.offer(new Integer(i)));
41 <        for (int i = (n & 1); i < n; i+=2)
42 <            assertTrue(q.offer(new Integer(i)));
39 >        for (int i = n-1; i >= 0; i-=2)
40 >            assertTrue(q.offer(new Integer(i)));
41 >        for (int i = (n & 1); i < n; i+=2)
42 >            assertTrue(q.offer(new Integer(i)));
43          assertFalse(q.isEmpty());
44          assertEquals(NOCAP, q.remainingCapacity());
45 <        assertEquals(n, q.size());
45 >        assertEquals(n, q.size());
46          return q;
47      }
48  
# Line 57 | Line 54 | public class PriorityBlockingQueueTest e
54      }
55  
56      /**
57 <     * Constructor throws IAE if  capacity argument nonpositive
57 >     * Constructor throws IAE if capacity argument nonpositive
58       */
59      public void testConstructor2() {
60          try {
61              PriorityBlockingQueue q = new PriorityBlockingQueue(0);
62              shouldThrow();
63 <        }
67 <        catch (IllegalArgumentException success) {}
63 >        } catch (IllegalArgumentException success) {}
64      }
65  
66      /**
# Line 74 | Line 70 | public class PriorityBlockingQueueTest e
70          try {
71              PriorityBlockingQueue q = new PriorityBlockingQueue(null);
72              shouldThrow();
73 <        }
78 <        catch (NullPointerException success) {}
73 >        } catch (NullPointerException success) {}
74      }
75  
76      /**
# Line 86 | Line 81 | public class PriorityBlockingQueueTest e
81              Integer[] ints = new Integer[SIZE];
82              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
83              shouldThrow();
84 <        }
90 <        catch (NullPointerException success) {}
84 >        } catch (NullPointerException success) {}
85      }
86  
87      /**
# Line 100 | Line 94 | public class PriorityBlockingQueueTest e
94                  ints[i] = new Integer(i);
95              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
96              shouldThrow();
97 <        }
104 <        catch (NullPointerException success) {}
97 >        } catch (NullPointerException success) {}
98      }
99  
100      /**
101       * Queue contains all elements of collection used to initialize
102       */
103      public void testConstructor6() {
104 <        try {
105 <            Integer[] ints = new Integer[SIZE];
106 <            for (int i = 0; i < SIZE; ++i)
107 <                ints[i] = new Integer(i);
108 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
109 <            for (int i = 0; i < SIZE; ++i)
117 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
104 >        Integer[] ints = new Integer[SIZE];
105 >        for (int i = 0; i < SIZE; ++i)
106 >            ints[i] = new Integer(i);
107 >        PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
108 >        for (int i = 0; i < SIZE; ++i)
109 >            assertEquals(ints[i], q.poll());
110      }
111  
112      /**
113       * The comparator used in constructor is used
114       */
115      public void testConstructor7() {
116 <        try {
117 <            MyReverseComparator cmp = new MyReverseComparator();
118 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
119 <            assertEquals(cmp, q.comparator());
120 <            Integer[] ints = new Integer[SIZE];
121 <            for (int i = 0; i < SIZE; ++i)
122 <                ints[i] = new Integer(i);
123 <            q.addAll(Arrays.asList(ints));
124 <            for (int i = SIZE-1; i >= 0; --i)
135 <                assertEquals(ints[i], q.poll());
136 <        }
137 <        finally {}
116 >        MyReverseComparator cmp = new MyReverseComparator();
117 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
118 >        assertEquals(cmp, q.comparator());
119 >        Integer[] ints = new Integer[SIZE];
120 >        for (int i = 0; i < SIZE; ++i)
121 >            ints[i] = new Integer(i);
122 >        q.addAll(Arrays.asList(ints));
123 >        for (int i = SIZE-1; i >= 0; --i)
124 >            assertEquals(ints[i], q.poll());
125      }
126  
127      /**
# Line 174 | Line 161 | public class PriorityBlockingQueueTest e
161       * offer(null) throws NPE
162       */
163      public void testOfferNull() {
164 <        try {
164 >        try {
165              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
166              q.offer(null);
167              shouldThrow();
168 <        } catch (NullPointerException success) { }
168 >        } catch (NullPointerException success) {}
169      }
170  
171      /**
172       * add(null) throws NPE
173       */
174      public void testAddNull() {
175 <        try {
175 >        try {
176              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
177              q.add(null);
178              shouldThrow();
179 <        } catch (NullPointerException success) { }
179 >        } catch (NullPointerException success) {}
180      }
181  
182      /**
# Line 211 | Line 198 | public class PriorityBlockingQueueTest e
198              q.offer(new Object());
199              q.offer(new Object());
200              shouldThrow();
201 <        }
215 <        catch (ClassCastException success) {}
201 >        } catch (ClassCastException success) {}
202      }
203  
204      /**
# Line 234 | Line 220 | public class PriorityBlockingQueueTest e
220              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
221              q.addAll(null);
222              shouldThrow();
223 <        }
238 <        catch (NullPointerException success) {}
223 >        } catch (NullPointerException success) {}
224      }
225  
226      /**
# Line 246 | Line 231 | public class PriorityBlockingQueueTest e
231              PriorityBlockingQueue q = populatedQueue(SIZE);
232              q.addAll(q);
233              shouldThrow();
234 <        }
250 <        catch (IllegalArgumentException success) {}
234 >        } catch (IllegalArgumentException success) {}
235      }
236  
237      /**
# Line 259 | Line 243 | public class PriorityBlockingQueueTest e
243              Integer[] ints = new Integer[SIZE];
244              q.addAll(Arrays.asList(ints));
245              shouldThrow();
246 <        }
263 <        catch (NullPointerException success) {}
246 >        } catch (NullPointerException success) {}
247      }
248 +
249      /**
250       * addAll of a collection with any null elements throws NPE after
251       * possibly adding some elements
# Line 274 | Line 258 | public class PriorityBlockingQueueTest e
258                  ints[i] = new Integer(i);
259              q.addAll(Arrays.asList(ints));
260              shouldThrow();
261 <        }
278 <        catch (NullPointerException success) {}
261 >        } catch (NullPointerException success) {}
262      }
263  
264      /**
265       * Queue contains all elements of successful addAll
266       */
267      public void testAddAll5() {
268 <        try {
269 <            Integer[] empty = new Integer[0];
270 <            Integer[] ints = new Integer[SIZE];
271 <            for (int i = SIZE-1; i >= 0; --i)
272 <                ints[i] = new Integer(i);
273 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
274 <            assertFalse(q.addAll(Arrays.asList(empty)));
275 <            assertTrue(q.addAll(Arrays.asList(ints)));
276 <            for (int i = 0; i < SIZE; ++i)
294 <                assertEquals(ints[i], q.poll());
295 <        }
296 <        finally {}
268 >        Integer[] empty = new Integer[0];
269 >        Integer[] ints = new Integer[SIZE];
270 >        for (int i = SIZE-1; i >= 0; --i)
271 >            ints[i] = new Integer(i);
272 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
273 >        assertFalse(q.addAll(Arrays.asList(empty)));
274 >        assertTrue(q.addAll(Arrays.asList(ints)));
275 >        for (int i = 0; i < SIZE; ++i)
276 >            assertEquals(ints[i], q.poll());
277      }
278  
279      /**
280       * put(null) throws NPE
281       */
282       public void testPutNull() {
283 <        try {
283 >        try {
284              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
285              q.put(null);
286              shouldThrow();
287 <        }
308 <        catch (NullPointerException success){
309 <        }
287 >        } catch (NullPointerException success) {}
288       }
289  
290      /**
291       * all elements successfully put are contained
292       */
293       public void testPut() {
294 <         try {
295 <             PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
296 <             for (int i = 0; i < SIZE; ++i) {
297 <                 Integer I = new Integer(i);
298 <                 q.put(I);
321 <                 assertTrue(q.contains(I));
322 <             }
323 <             assertEquals(SIZE, q.size());
294 >         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
295 >         for (int i = 0; i < SIZE; ++i) {
296 >             Integer I = new Integer(i);
297 >             q.put(I);
298 >             assertTrue(q.contains(I));
299           }
300 <         finally {
326 <        }
300 >         assertEquals(SIZE, q.size());
301      }
302  
303      /**
304       * put doesn't block waiting for take
305       */
306 <    public void testPutWithTake() {
306 >    public void testPutWithTake() throws InterruptedException {
307          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
308 <        Thread t = new Thread(new Runnable() {
309 <                public void run() {
310 <                    int added = 0;
311 <                    try {
312 <                        q.put(new Integer(0));
313 <                        ++added;
314 <                        q.put(new Integer(0));
315 <                        ++added;
316 <                        q.put(new Integer(0));
317 <                        ++added;
318 <                        q.put(new Integer(0));
319 <                        ++added;
320 <                        threadAssertTrue(added == 4);
347 <                    } finally {
348 <                    }
349 <                }
350 <            });
351 <        try {
352 <            t.start();
353 <            Thread.sleep(SHORT_DELAY_MS);
354 <            q.take();
355 <            t.interrupt();
356 <            t.join();
357 <        } catch (Exception e){
358 <            unexpectedException();
359 <        }
308 >        final int size = 4;
309 >        Thread t = new Thread(new CheckedRunnable() {
310 >            public void realRun() {
311 >                for (int i = 0; i < size; i++)
312 >                    q.put(new Integer(0));
313 >            }});
314 >
315 >        t.start();
316 >        Thread.sleep(SHORT_DELAY_MS);
317 >        assertEquals(q.size(), size);
318 >        q.take();
319 >        t.interrupt();
320 >        t.join();
321      }
322  
323      /**
324       * timed offer does not time out
325       */
326 <    public void testTimedOffer() {
326 >    public void testTimedOffer() throws InterruptedException {
327          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
328 <        Thread t = new Thread(new Runnable() {
329 <                public void run() {
330 <                    try {
331 <                        q.put(new Integer(0));
332 <                        q.put(new Integer(0));
333 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
334 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
374 <                    } finally { }
375 <                }
376 <            });
328 >        Thread t = new Thread(new CheckedRunnable() {
329 >            public void realRun() {
330 >                q.put(new Integer(0));
331 >                q.put(new Integer(0));
332 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
333 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
334 >            }});
335  
336 <        try {
337 <            t.start();
338 <            Thread.sleep(SMALL_DELAY_MS);
339 <            t.interrupt();
382 <            t.join();
383 <        } catch (Exception e){
384 <            unexpectedException();
385 <        }
336 >        t.start();
337 >        Thread.sleep(SMALL_DELAY_MS);
338 >        t.interrupt();
339 >        t.join();
340      }
341  
342      /**
343       * take retrieves elements in priority order
344       */
345 <    public void testTake() {
346 <        try {
347 <            PriorityBlockingQueue q = populatedQueue(SIZE);
348 <            for (int i = 0; i < SIZE; ++i) {
349 <                assertEquals(i, ((Integer)q.take()).intValue());
396 <            }
397 <        } catch (InterruptedException e){
398 <            unexpectedException();
399 <        }
345 >    public void testTake() throws InterruptedException {
346 >        PriorityBlockingQueue q = populatedQueue(SIZE);
347 >        for (int i = 0; i < SIZE; ++i) {
348 >            assertEquals(i, q.take());
349 >        }
350      }
351  
352      /**
353       * take blocks interruptibly when empty
354       */
355 <    public void testTakeFromEmpty() {
355 >    public void testTakeFromEmpty() throws InterruptedException {
356          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
357 <        Thread t = new Thread(new Runnable() {
358 <                public void run() {
359 <                    try {
360 <                        q.take();
361 <                        threadShouldThrow();
362 <                    } catch (InterruptedException success){ }
363 <                }
364 <            });
365 <        try {
416 <            t.start();
417 <            Thread.sleep(SHORT_DELAY_MS);
418 <            t.interrupt();
419 <            t.join();
420 <        } catch (Exception e){
421 <            unexpectedException();
422 <        }
357 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
358 >            public void realRun() throws InterruptedException {
359 >                q.take();
360 >            }});
361 >
362 >        t.start();
363 >        Thread.sleep(SHORT_DELAY_MS);
364 >        t.interrupt();
365 >        t.join();
366      }
367  
368      /**
369       * Take removes existing elements until empty, then blocks interruptibly
370       */
371 <    public void testBlockingTake() {
372 <        Thread t = new Thread(new Runnable() {
373 <                public void run() {
374 <                    try {
375 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
376 <                        for (int i = 0; i < SIZE; ++i) {
377 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
378 <                        }
379 <                        q.take();
380 <                        threadShouldThrow();
381 <                    } catch (InterruptedException success){
382 <                    }
383 <                }});
371 >    public void testBlockingTake() throws InterruptedException {
372 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
373 >        Thread t = new Thread(new CheckedRunnable() {
374 >            public void realRun() throws InterruptedException {
375 >                for (int i = 0; i < SIZE; ++i) {
376 >                    assertEquals(i, q.take());
377 >                }
378 >                try {
379 >                    q.take();
380 >                    shouldThrow();
381 >                } catch (InterruptedException success) {}
382 >            }});
383 >
384          t.start();
385 <        try {
386 <           Thread.sleep(SHORT_DELAY_MS);
387 <           t.interrupt();
445 <           t.join();
446 <        }
447 <        catch (InterruptedException ie) {
448 <            unexpectedException();
449 <        }
385 >        Thread.sleep(SHORT_DELAY_MS);
386 >        t.interrupt();
387 >        t.join();
388      }
389  
390  
# Line 456 | Line 394 | public class PriorityBlockingQueueTest e
394      public void testPoll() {
395          PriorityBlockingQueue q = populatedQueue(SIZE);
396          for (int i = 0; i < SIZE; ++i) {
397 <            assertEquals(i, ((Integer)q.poll()).intValue());
397 >            assertEquals(i, q.poll());
398          }
399 <        assertNull(q.poll());
399 >        assertNull(q.poll());
400      }
401  
402      /**
403       * timed pool with zero timeout succeeds when non-empty, else times out
404       */
405 <    public void testTimedPoll0() {
406 <        try {
407 <            PriorityBlockingQueue q = populatedQueue(SIZE);
408 <            for (int i = 0; i < SIZE; ++i) {
409 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
410 <            }
473 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
474 <        } catch (InterruptedException e){
475 <            unexpectedException();
476 <        }
405 >    public void testTimedPoll0() throws InterruptedException {
406 >        PriorityBlockingQueue q = populatedQueue(SIZE);
407 >        for (int i = 0; i < SIZE; ++i) {
408 >            assertEquals(i, q.poll(0, MILLISECONDS));
409 >        }
410 >        assertNull(q.poll(0, MILLISECONDS));
411      }
412  
413      /**
414       * timed pool with nonzero timeout succeeds when non-empty, else times out
415       */
416 <    public void testTimedPoll() {
417 <        try {
418 <            PriorityBlockingQueue q = populatedQueue(SIZE);
419 <            for (int i = 0; i < SIZE; ++i) {
420 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
421 <            }
488 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489 <        } catch (InterruptedException e){
490 <            unexpectedException();
491 <        }
416 >    public void testTimedPoll() throws InterruptedException {
417 >        PriorityBlockingQueue q = populatedQueue(SIZE);
418 >        for (int i = 0; i < SIZE; ++i) {
419 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
420 >        }
421 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422      }
423  
424      /**
425       * Interrupted timed poll throws InterruptedException instead of
426       * returning timeout status
427       */
428 <    public void testInterruptedTimedPoll() {
429 <        Thread t = new Thread(new Runnable() {
430 <                public void run() {
431 <                    try {
432 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
433 <                        for (int i = 0; i < SIZE; ++i) {
434 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
435 <                        }
436 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
437 <                    } catch (InterruptedException success){
438 <                    }
439 <                }});
428 >    public void testInterruptedTimedPoll() throws InterruptedException {
429 >        Thread t = new Thread(new CheckedRunnable() {
430 >            public void realRun() throws InterruptedException {
431 >                PriorityBlockingQueue q = populatedQueue(SIZE);
432 >                for (int i = 0; i < SIZE; ++i) {
433 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
434 >                }
435 >                try {
436 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
437 >                    shouldThrow();
438 >                } catch (InterruptedException success) {}
439 >            }});
440 >
441          t.start();
442 <        try {
443 <           Thread.sleep(SHORT_DELAY_MS);
444 <           t.interrupt();
514 <           t.join();
515 <        }
516 <        catch (InterruptedException ie) {
517 <            unexpectedException();
518 <        }
442 >        Thread.sleep(SHORT_DELAY_MS);
443 >        t.interrupt();
444 >        t.join();
445      }
446  
447      /**
448       *  timed poll before a delayed offer fails; after offer succeeds;
449       *  on interruption throws
450       */
451 <    public void testTimedPollWithOffer() {
451 >    public void testTimedPollWithOffer() throws InterruptedException {
452          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
453 <        Thread t = new Thread(new Runnable() {
454 <                public void run() {
455 <                    try {
456 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
457 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
458 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
459 <                        threadShouldThrow();
460 <                    } catch (InterruptedException success) { }
461 <                }
462 <            });
463 <        try {
464 <            t.start();
465 <            Thread.sleep(SMALL_DELAY_MS);
466 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
467 <            t.interrupt();
542 <            t.join();
543 <        } catch (Exception e){
544 <            unexpectedException();
545 <        }
453 >        Thread t = new Thread(new CheckedRunnable() {
454 >            public void realRun() throws InterruptedException {
455 >                try {
456 >                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
457 >                    assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
458 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
459 >                    shouldThrow();
460 >                } catch (InterruptedException success) {}
461 >            }});
462 >
463 >        t.start();
464 >        Thread.sleep(SMALL_DELAY_MS);
465 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
466 >        t.interrupt();
467 >        t.join();
468      }
469  
470  
# Line 552 | Line 474 | public class PriorityBlockingQueueTest e
474      public void testPeek() {
475          PriorityBlockingQueue q = populatedQueue(SIZE);
476          for (int i = 0; i < SIZE; ++i) {
477 <            assertEquals(i, ((Integer)q.peek()).intValue());
478 <            q.poll();
477 >            assertEquals(i, q.peek());
478 >            assertEquals(i, q.poll());
479              assertTrue(q.peek() == null ||
480 <                       i != ((Integer)q.peek()).intValue());
480 >                       !q.peek().equals(i));
481          }
482 <        assertNull(q.peek());
482 >        assertNull(q.peek());
483      }
484  
485      /**
# Line 566 | Line 488 | public class PriorityBlockingQueueTest e
488      public void testElement() {
489          PriorityBlockingQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(i, ((Integer)q.element()).intValue());
492 <            q.poll();
491 >            assertEquals(i, q.element());
492 >            assertEquals(i, q.poll());
493          }
494          try {
495              q.element();
496              shouldThrow();
497 <        }
576 <        catch (NoSuchElementException success) {}
497 >        } catch (NoSuchElementException success) {}
498      }
499  
500      /**
# Line 582 | Line 503 | public class PriorityBlockingQueueTest e
503      public void testRemove() {
504          PriorityBlockingQueue q = populatedQueue(SIZE);
505          for (int i = 0; i < SIZE; ++i) {
506 <            assertEquals(i, ((Integer)q.remove()).intValue());
506 >            assertEquals(i, q.remove());
507          }
508          try {
509              q.remove();
510              shouldThrow();
511 <        } catch (NoSuchElementException success){
591 <        }
511 >        } catch (NoSuchElementException success) {}
512      }
513  
514      /**
# Line 685 | Line 605 | public class PriorityBlockingQueueTest e
605      /**
606       *  toArray contains all elements
607       */
608 <    public void testToArray() {
608 >    public void testToArray() throws InterruptedException {
609          PriorityBlockingQueue q = populatedQueue(SIZE);
610 <        Object[] o = q.toArray();
610 >        Object[] o = q.toArray();
611          Arrays.sort(o);
612 <        try {
613 <        for (int i = 0; i < o.length; i++)
694 <            assertEquals(o[i], q.take());
695 <        } catch (InterruptedException e){
696 <            unexpectedException();
697 <        }
612 >        for (int i = 0; i < o.length; i++)
613 >            assertEquals(o[i], q.take());
614      }
615  
616      /**
617       * toArray(a) contains all elements
618       */
619 <    public void testToArray2() {
619 >    public void testToArray2() throws InterruptedException {
620          PriorityBlockingQueue q = populatedQueue(SIZE);
621 <        Integer[] ints = new Integer[SIZE];
622 <        ints = (Integer[])q.toArray(ints);
621 >        Integer[] ints = new Integer[SIZE];
622 >        ints = (Integer[])q.toArray(ints);
623          Arrays.sort(ints);
624 <        try {
625 <            for (int i = 0; i < ints.length; i++)
710 <                assertEquals(ints[i], q.take());
711 <        } catch (InterruptedException e){
712 <            unexpectedException();
713 <        }
624 >        for (int i = 0; i < ints.length; i++)
625 >            assertEquals(ints[i], q.take());
626      }
627  
628      /**
629       * toArray(null) throws NPE
630       */
631      public void testToArray_BadArg() {
632 <        try {
633 <            PriorityBlockingQueue q = populatedQueue(SIZE);
634 <            Object o[] = q.toArray(null);
635 <            shouldThrow();
636 <        } catch (NullPointerException success){}
632 >        PriorityBlockingQueue q = populatedQueue(SIZE);
633 >        try {
634 >            Object o[] = q.toArray(null);
635 >            shouldThrow();
636 >        } catch (NullPointerException success) {}
637      }
638  
639      /**
640       * toArray with incompatible array type throws CCE
641       */
642      public void testToArray1_BadArg() {
643 <        try {
644 <            PriorityBlockingQueue q = populatedQueue(SIZE);
645 <            Object o[] = q.toArray(new String[10] );
646 <            shouldThrow();
647 <        } catch (ArrayStoreException  success){}
643 >        PriorityBlockingQueue q = populatedQueue(SIZE);
644 >        try {
645 >            Object o[] = q.toArray(new String[10]);
646 >            shouldThrow();
647 >        } catch (ArrayStoreException success) {}
648      }
649  
650      /**
# Line 741 | Line 653 | public class PriorityBlockingQueueTest e
653      public void testIterator() {
654          PriorityBlockingQueue q = populatedQueue(SIZE);
655          int i = 0;
656 <        Iterator it = q.iterator();
656 >        Iterator it = q.iterator();
657          while (it.hasNext()) {
658              assertTrue(q.contains(it.next()));
659              ++i;
# Line 752 | Line 664 | public class PriorityBlockingQueueTest e
664      /**
665       * iterator.remove removes current element
666       */
667 <    public void testIteratorRemove () {
667 >    public void testIteratorRemove() {
668          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
669          q.add(new Integer(2));
670          q.add(new Integer(1));
# Line 786 | Line 698 | public class PriorityBlockingQueueTest e
698      public void testPollInExecutor() {
699          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
700          ExecutorService executor = Executors.newFixedThreadPool(2);
701 <        executor.execute(new Runnable() {
702 <            public void run() {
703 <                threadAssertNull(q.poll());
704 <                try {
705 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
706 <                    threadAssertTrue(q.isEmpty());
707 <                }
708 <                catch (InterruptedException e) {
709 <                    threadUnexpectedException();
710 <                }
711 <            }
712 <        });
801 <
802 <        executor.execute(new Runnable() {
803 <            public void run() {
804 <                try {
805 <                    Thread.sleep(SMALL_DELAY_MS);
806 <                    q.put(new Integer(1));
807 <                }
808 <                catch (InterruptedException e) {
809 <                    threadUnexpectedException();
810 <                }
811 <            }
812 <        });
701 >        executor.execute(new CheckedRunnable() {
702 >            public void realRun() throws InterruptedException {
703 >                assertNull(q.poll());
704 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705 >                assertTrue(q.isEmpty());
706 >            }});
707 >
708 >        executor.execute(new CheckedRunnable() {
709 >            public void realRun() throws InterruptedException {
710 >                Thread.sleep(SMALL_DELAY_MS);
711 >                q.put(one);
712 >            }});
713  
714          joinPool(executor);
715      }
# Line 817 | Line 717 | public class PriorityBlockingQueueTest e
717      /**
718       * A deserialized serialized queue has same elements
719       */
720 <    public void testSerialization() {
720 >    public void testSerialization() throws Exception {
721          PriorityBlockingQueue q = populatedQueue(SIZE);
722 <        try {
723 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
724 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
725 <            out.writeObject(q);
726 <            out.close();
727 <
728 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
729 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
730 <            PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
731 <            assertEquals(q.size(), r.size());
732 <            while (!q.isEmpty())
833 <                assertEquals(q.remove(), r.remove());
834 <        } catch (Exception e){
835 <            unexpectedException();
836 <        }
722 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
723 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
724 >        out.writeObject(q);
725 >        out.close();
726 >
727 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
728 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
729 >        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
730 >        assertEquals(q.size(), r.size());
731 >        while (!q.isEmpty())
732 >            assertEquals(q.remove(), r.remove());
733      }
734  
735      /**
# Line 844 | Line 740 | public class PriorityBlockingQueueTest e
740          try {
741              q.drainTo(null);
742              shouldThrow();
743 <        } catch (NullPointerException success) {
848 <        }
743 >        } catch (NullPointerException success) {}
744      }
745  
746      /**
# Line 856 | Line 751 | public class PriorityBlockingQueueTest e
751          try {
752              q.drainTo(q);
753              shouldThrow();
754 <        } catch (IllegalArgumentException success) {
860 <        }
754 >        } catch (IllegalArgumentException success) {}
755      }
756  
757      /**
# Line 887 | Line 781 | public class PriorityBlockingQueueTest e
781      /**
782       * drainTo empties queue
783       */
784 <    public void testDrainToWithActivePut() {
784 >    public void testDrainToWithActivePut() throws InterruptedException {
785          final PriorityBlockingQueue q = populatedQueue(SIZE);
786 <        Thread t = new Thread(new Runnable() {
787 <                public void run() {
788 <                    q.put(new Integer(SIZE+1));
789 <                }
790 <            });
791 <        try {
792 <            t.start();
793 <            ArrayList l = new ArrayList();
794 <            q.drainTo(l);
795 <            assertTrue(l.size() >= SIZE);
796 <            for (int i = 0; i < SIZE; ++i)
797 <                assertEquals(l.get(i), new Integer(i));
798 <            t.join();
905 <            assertTrue(q.size() + l.size() >= SIZE);
906 <        } catch (Exception e){
907 <            unexpectedException();
908 <        }
786 >        Thread t = new Thread(new CheckedRunnable() {
787 >            public void realRun() {
788 >                q.put(new Integer(SIZE+1));
789 >            }});
790 >
791 >        t.start();
792 >        ArrayList l = new ArrayList();
793 >        q.drainTo(l);
794 >        assertTrue(l.size() >= SIZE);
795 >        for (int i = 0; i < SIZE; ++i)
796 >            assertEquals(l.get(i), new Integer(i));
797 >        t.join();
798 >        assertTrue(q.size() + l.size() >= SIZE);
799      }
800  
801      /**
# Line 916 | Line 806 | public class PriorityBlockingQueueTest e
806          try {
807              q.drainTo(null, 0);
808              shouldThrow();
809 <        } catch (NullPointerException success) {
920 <        }
809 >        } catch (NullPointerException success) {}
810      }
811  
812      /**
# Line 928 | Line 817 | public class PriorityBlockingQueueTest e
817          try {
818              q.drainTo(q, 0);
819              shouldThrow();
820 <        } catch (IllegalArgumentException success) {
932 <        }
820 >        } catch (IllegalArgumentException success) {}
821      }
822  
823      /**
# Line 951 | Line 839 | public class PriorityBlockingQueueTest e
839          }
840      }
841  
954
842   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines