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.12 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.35 by jsr166, Wed Nov 3 07:54:52 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 +
17 +    public static class Generic extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new PriorityBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class InitialCapacity extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new PriorityBlockingQueue(20);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32 +
33      public static Test suite() {
34 <        return new TestSuite(PriorityBlockingQueueTest.class);
34 >        return newTestSuite(PriorityBlockingQueueTest.class,
35 >                            new Generic().testSuite(),
36 >                            new InitialCapacity().testSuite());
37      }
38  
39      private static final int NOCAP = Integer.MAX_VALUE;
# Line 24 | Line 41 | public class PriorityBlockingQueueTest e
41      /** Sample Comparator */
42      static class MyReverseComparator implements Comparator {
43          public int compare(Object x, Object y) {
44 <            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;
44 >            return ((Comparable)y).compareTo(x);
45          }
46      }
47  
# Line 39 | Line 52 | public class PriorityBlockingQueueTest e
52      private PriorityBlockingQueue populatedQueue(int n) {
53          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
54          assertTrue(q.isEmpty());
55 <        for(int i = n-1; i >= 0; i-=2)
56 <            assertTrue(q.offer(new Integer(i)));
57 <        for(int i = (n & 1); i < n; i+=2)
58 <            assertTrue(q.offer(new Integer(i)));
55 >        for (int i = n-1; i >= 0; i-=2)
56 >            assertTrue(q.offer(new Integer(i)));
57 >        for (int i = (n & 1); i < n; i+=2)
58 >            assertTrue(q.offer(new Integer(i)));
59          assertFalse(q.isEmpty());
60          assertEquals(NOCAP, q.remainingCapacity());
61 <        assertEquals(n, q.size());
61 >        assertEquals(n, q.size());
62          return q;
63      }
64  
# Line 57 | Line 70 | public class PriorityBlockingQueueTest e
70      }
71  
72      /**
73 <     * Constructor throws IAE if  capacity argument nonpositive
73 >     * Constructor throws IAE if capacity argument nonpositive
74       */
75      public void testConstructor2() {
76          try {
77              PriorityBlockingQueue q = new PriorityBlockingQueue(0);
78              shouldThrow();
79 <        }
67 <        catch (IllegalArgumentException success) {}
79 >        } catch (IllegalArgumentException success) {}
80      }
81  
82      /**
# Line 74 | Line 86 | public class PriorityBlockingQueueTest e
86          try {
87              PriorityBlockingQueue q = new PriorityBlockingQueue(null);
88              shouldThrow();
89 <        }
78 <        catch (NullPointerException success) {}
89 >        } catch (NullPointerException success) {}
90      }
91  
92      /**
# Line 86 | Line 97 | public class PriorityBlockingQueueTest e
97              Integer[] ints = new Integer[SIZE];
98              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
99              shouldThrow();
100 <        }
90 <        catch (NullPointerException success) {}
100 >        } catch (NullPointerException success) {}
101      }
102  
103      /**
# Line 100 | Line 110 | public class PriorityBlockingQueueTest e
110                  ints[i] = new Integer(i);
111              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
112              shouldThrow();
113 <        }
104 <        catch (NullPointerException success) {}
113 >        } catch (NullPointerException success) {}
114      }
115  
116      /**
117       * Queue contains all elements of collection used to initialize
118       */
119      public void testConstructor6() {
120 <        try {
121 <            Integer[] ints = new Integer[SIZE];
122 <            for (int i = 0; i < SIZE; ++i)
123 <                ints[i] = new Integer(i);
124 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
125 <            for (int i = 0; i < SIZE; ++i)
117 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
120 >        Integer[] ints = new Integer[SIZE];
121 >        for (int i = 0; i < SIZE; ++i)
122 >            ints[i] = new Integer(i);
123 >        PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
124 >        for (int i = 0; i < SIZE; ++i)
125 >            assertEquals(ints[i], q.poll());
126      }
127  
128      /**
129       * The comparator used in constructor is used
130       */
131      public void testConstructor7() {
132 <        try {
133 <            MyReverseComparator cmp = new MyReverseComparator();
134 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
135 <            assertEquals(cmp, q.comparator());
136 <            Integer[] ints = new Integer[SIZE];
137 <            for (int i = 0; i < SIZE; ++i)
138 <                ints[i] = new Integer(i);
139 <            q.addAll(Arrays.asList(ints));
140 <            for (int i = SIZE-1; i >= 0; --i)
135 <                assertEquals(ints[i], q.poll());
136 <        }
137 <        finally {}
132 >        MyReverseComparator cmp = new MyReverseComparator();
133 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
134 >        assertEquals(cmp, q.comparator());
135 >        Integer[] ints = new Integer[SIZE];
136 >        for (int i = 0; i < SIZE; ++i)
137 >            ints[i] = new Integer(i);
138 >        q.addAll(Arrays.asList(ints));
139 >        for (int i = SIZE-1; i >= 0; --i)
140 >            assertEquals(ints[i], q.poll());
141      }
142  
143      /**
# Line 174 | Line 177 | public class PriorityBlockingQueueTest e
177       * offer(null) throws NPE
178       */
179      public void testOfferNull() {
180 <        try {
180 >        try {
181              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
182              q.offer(null);
183              shouldThrow();
184 <        } catch (NullPointerException success) { }
184 >        } catch (NullPointerException success) {}
185      }
186  
187      /**
188       * add(null) throws NPE
189       */
190      public void testAddNull() {
191 <        try {
191 >        try {
192              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
193              q.add(null);
194              shouldThrow();
195 <        } catch (NullPointerException success) { }
195 >        } catch (NullPointerException success) {}
196      }
197  
198      /**
# Line 211 | Line 214 | public class PriorityBlockingQueueTest e
214              q.offer(new Object());
215              q.offer(new Object());
216              shouldThrow();
217 <        }
215 <        catch(ClassCastException success) {}
217 >        } catch (ClassCastException success) {}
218      }
219  
220      /**
# Line 234 | Line 236 | public class PriorityBlockingQueueTest e
236              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
237              q.addAll(null);
238              shouldThrow();
239 <        }
238 <        catch (NullPointerException success) {}
239 >        } catch (NullPointerException success) {}
240      }
241  
242      /**
# Line 246 | Line 247 | public class PriorityBlockingQueueTest e
247              PriorityBlockingQueue q = populatedQueue(SIZE);
248              q.addAll(q);
249              shouldThrow();
250 <        }
250 <        catch (IllegalArgumentException success) {}
250 >        } catch (IllegalArgumentException success) {}
251      }
252  
253      /**
# Line 259 | Line 259 | public class PriorityBlockingQueueTest e
259              Integer[] ints = new Integer[SIZE];
260              q.addAll(Arrays.asList(ints));
261              shouldThrow();
262 <        }
263 <        catch (NullPointerException success) {}
262 >        } catch (NullPointerException success) {}
263      }
264 +
265      /**
266       * addAll of a collection with any null elements throws NPE after
267       * possibly adding some elements
# Line 274 | Line 274 | public class PriorityBlockingQueueTest e
274                  ints[i] = new Integer(i);
275              q.addAll(Arrays.asList(ints));
276              shouldThrow();
277 <        }
278 <        catch (NullPointerException success) {}
277 >        } catch (NullPointerException success) {}
278      }
279  
280      /**
281       * Queue contains all elements of successful addAll
282       */
283      public void testAddAll5() {
284 <        try {
285 <            Integer[] empty = new Integer[0];
286 <            Integer[] ints = new Integer[SIZE];
287 <            for (int i = SIZE-1; i >= 0; --i)
288 <                ints[i] = new Integer(i);
289 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
290 <            assertFalse(q.addAll(Arrays.asList(empty)));
291 <            assertTrue(q.addAll(Arrays.asList(ints)));
292 <            for (int i = 0; i < SIZE; ++i)
294 <                assertEquals(ints[i], q.poll());
295 <        }
296 <        finally {}
284 >        Integer[] empty = new Integer[0];
285 >        Integer[] ints = new Integer[SIZE];
286 >        for (int i = SIZE-1; i >= 0; --i)
287 >            ints[i] = new Integer(i);
288 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
289 >        assertFalse(q.addAll(Arrays.asList(empty)));
290 >        assertTrue(q.addAll(Arrays.asList(ints)));
291 >        for (int i = 0; i < SIZE; ++i)
292 >            assertEquals(ints[i], q.poll());
293      }
294  
295      /**
296       * put(null) throws NPE
297       */
298       public void testPutNull() {
299 <        try {
299 >        try {
300              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
301              q.put(null);
302              shouldThrow();
303 <        }
308 <        catch (NullPointerException success){
309 <        }
303 >        } catch (NullPointerException success) {}
304       }
305  
306      /**
307       * all elements successfully put are contained
308       */
309       public void testPut() {
310 <         try {
311 <             PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
312 <             for (int i = 0; i < SIZE; ++i) {
313 <                 Integer I = new Integer(i);
314 <                 q.put(I);
321 <                 assertTrue(q.contains(I));
322 <             }
323 <             assertEquals(SIZE, q.size());
310 >         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
311 >         for (int i = 0; i < SIZE; ++i) {
312 >             Integer I = new Integer(i);
313 >             q.put(I);
314 >             assertTrue(q.contains(I));
315           }
316 <         finally {
326 <        }
316 >         assertEquals(SIZE, q.size());
317      }
318  
319      /**
320       * put doesn't block waiting for take
321       */
322 <    public void testPutWithTake() {
322 >    public void testPutWithTake() throws InterruptedException {
323          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
324 <        Thread t = new Thread(new Runnable() {
325 <                public void run() {
326 <                    int added = 0;
327 <                    try {
328 <                        q.put(new Integer(0));
329 <                        ++added;
330 <                        q.put(new Integer(0));
331 <                        ++added;
332 <                        q.put(new Integer(0));
333 <                        ++added;
334 <                        q.put(new Integer(0));
335 <                        ++added;
336 <                        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 <        }
324 >        final int size = 4;
325 >        Thread t = new Thread(new CheckedRunnable() {
326 >            public void realRun() {
327 >                for (int i = 0; i < size; i++)
328 >                    q.put(new Integer(0));
329 >            }});
330 >
331 >        t.start();
332 >        Thread.sleep(SHORT_DELAY_MS);
333 >        assertEquals(q.size(), size);
334 >        q.take();
335 >        t.interrupt();
336 >        t.join();
337      }
338  
339      /**
340       * timed offer does not time out
341       */
342 <    public void testTimedOffer() {
342 >    public void testTimedOffer() throws InterruptedException {
343          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
344 <        Thread t = new Thread(new Runnable() {
345 <                public void run() {
346 <                    try {
347 <                        q.put(new Integer(0));
348 <                        q.put(new Integer(0));
349 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
350 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
374 <                    } finally { }
375 <                }
376 <            });
344 >        Thread t = new Thread(new CheckedRunnable() {
345 >            public void realRun() {
346 >                q.put(new Integer(0));
347 >                q.put(new Integer(0));
348 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
349 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
350 >            }});
351  
352 <        try {
353 <            t.start();
354 <            Thread.sleep(SMALL_DELAY_MS);
355 <            t.interrupt();
382 <            t.join();
383 <        } catch (Exception e){
384 <            unexpectedException();
385 <        }
352 >        t.start();
353 >        Thread.sleep(SMALL_DELAY_MS);
354 >        t.interrupt();
355 >        t.join();
356      }
357  
358      /**
359       * take retrieves elements in priority order
360       */
361 <    public void testTake() {
362 <        try {
363 <            PriorityBlockingQueue q = populatedQueue(SIZE);
364 <            for (int i = 0; i < SIZE; ++i) {
395 <                assertEquals(i, ((Integer)q.take()).intValue());
396 <            }
397 <        } catch (InterruptedException e){
398 <            unexpectedException();
399 <        }
400 <    }
401 <
402 <    /**
403 <     * take blocks interruptibly when empty
404 <     */
405 <    public void testTakeFromEmpty() {
406 <        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
407 <        Thread t = new Thread(new Runnable() {
408 <                public void run() {
409 <                    try {
410 <                        q.take();
411 <                        threadShouldThrow();
412 <                    } catch (InterruptedException success){ }
413 <                }
414 <            });
415 <        try {
416 <            t.start();
417 <            Thread.sleep(SHORT_DELAY_MS);
418 <            t.interrupt();
419 <            t.join();
420 <        } catch (Exception e){
421 <            unexpectedException();
361 >    public void testTake() throws InterruptedException {
362 >        PriorityBlockingQueue q = populatedQueue(SIZE);
363 >        for (int i = 0; i < SIZE; ++i) {
364 >            assertEquals(i, q.take());
365          }
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
403 >     * timed poll 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
414 >     * timed poll 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) {
504 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
505 <                        }
506 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
507 <                    } catch (InterruptedException success){
508 <                    }
509 <                }});
510 <        t.start();
511 <        try {
512 <           Thread.sleep(SHORT_DELAY_MS);
513 <           t.interrupt();
514 <           t.join();
515 <        }
516 <        catch (InterruptedException ie) {
517 <            unexpectedException();
518 <        }
519 <    }
520 <
521 <    /**
522 <     *  timed poll before a delayed offer fails; after offer succeeds;
523 <     *  on interruption throws
524 <     */
525 <    public void testTimedPollWithOffer() {
526 <        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
527 <        Thread t = new Thread(new Runnable() {
528 <                public void run() {
529 <                    try {
530 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
531 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
532 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
533 <                        threadShouldThrow();
534 <                    } catch (InterruptedException success) { }
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 <            });
436 <        try {
437 <            t.start();
438 <            Thread.sleep(SMALL_DELAY_MS);
439 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
541 <            t.interrupt();
542 <            t.join();
543 <        } catch (Exception e){
544 <            unexpectedException();
545 <        }
546 <    }
435 >                try {
436 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
437 >                    shouldThrow();
438 >                } catch (InterruptedException success) {}
439 >            }});
440  
441 +        t.start();
442 +        Thread.sleep(SHORT_DELAY_MS);
443 +        t.interrupt();
444 +        t.join();
445 +    }
446  
447      /**
448       * peek returns next element, or null if empty
# Line 552 | Line 450 | public class PriorityBlockingQueueTest e
450      public void testPeek() {
451          PriorityBlockingQueue q = populatedQueue(SIZE);
452          for (int i = 0; i < SIZE; ++i) {
453 <            assertEquals(i, ((Integer)q.peek()).intValue());
454 <            q.poll();
453 >            assertEquals(i, q.peek());
454 >            assertEquals(i, q.poll());
455              assertTrue(q.peek() == null ||
456 <                       i != ((Integer)q.peek()).intValue());
456 >                       !q.peek().equals(i));
457          }
458 <        assertNull(q.peek());
458 >        assertNull(q.peek());
459      }
460  
461      /**
# Line 566 | Line 464 | public class PriorityBlockingQueueTest e
464      public void testElement() {
465          PriorityBlockingQueue q = populatedQueue(SIZE);
466          for (int i = 0; i < SIZE; ++i) {
467 <            assertEquals(i, ((Integer)q.element()).intValue());
468 <            q.poll();
467 >            assertEquals(i, q.element());
468 >            assertEquals(i, q.poll());
469          }
470          try {
471              q.element();
472              shouldThrow();
473 <        }
576 <        catch (NoSuchElementException success) {}
473 >        } catch (NoSuchElementException success) {}
474      }
475  
476      /**
# Line 582 | Line 479 | public class PriorityBlockingQueueTest e
479      public void testRemove() {
480          PriorityBlockingQueue q = populatedQueue(SIZE);
481          for (int i = 0; i < SIZE; ++i) {
482 <            assertEquals(i, ((Integer)q.remove()).intValue());
482 >            assertEquals(i, q.remove());
483          }
484          try {
485              q.remove();
486              shouldThrow();
487 <        } catch (NoSuchElementException success){
591 <        }
487 >        } catch (NoSuchElementException success) {}
488      }
489  
490      /**
# Line 683 | Line 579 | public class PriorityBlockingQueueTest e
579      }
580  
581      /**
582 <     *  toArray contains all elements
582 >     * toArray contains all elements
583       */
584 <    public void testToArray() {
584 >    public void testToArray() throws InterruptedException {
585          PriorityBlockingQueue q = populatedQueue(SIZE);
586 <        Object[] o = q.toArray();
586 >        Object[] o = q.toArray();
587          Arrays.sort(o);
588 <        try {
589 <        for(int i = 0; i < o.length; i++)
694 <            assertEquals(o[i], q.take());
695 <        } catch (InterruptedException e){
696 <            unexpectedException();
697 <        }
588 >        for (int i = 0; i < o.length; i++)
589 >            assertEquals(o[i], q.take());
590      }
591  
592      /**
593       * toArray(a) contains all elements
594       */
595 <    public void testToArray2() {
595 >    public void testToArray2() throws InterruptedException {
596          PriorityBlockingQueue q = populatedQueue(SIZE);
597 <        Integer[] ints = new Integer[SIZE];
598 <        ints = (Integer[])q.toArray(ints);
597 >        Integer[] ints = new Integer[SIZE];
598 >        ints = (Integer[])q.toArray(ints);
599          Arrays.sort(ints);
600 <        try {
601 <            for(int i = 0; i < ints.length; i++)
710 <                assertEquals(ints[i], q.take());
711 <        } catch (InterruptedException e){
712 <            unexpectedException();
713 <        }
600 >        for (int i = 0; i < ints.length; i++)
601 >            assertEquals(ints[i], q.take());
602      }
603  
604      /**
605       * toArray(null) throws NPE
606       */
607      public void testToArray_BadArg() {
608 <        try {
609 <            PriorityBlockingQueue q = populatedQueue(SIZE);
610 <            Object o[] = q.toArray(null);
611 <            shouldThrow();
612 <        } catch(NullPointerException success){}
608 >        PriorityBlockingQueue q = populatedQueue(SIZE);
609 >        try {
610 >            Object o[] = q.toArray(null);
611 >            shouldThrow();
612 >        } catch (NullPointerException success) {}
613      }
614  
615      /**
616 <     * toArray with incompatible array type throws CCE
616 >     * toArray(incompatible array type) throws ArrayStoreException
617       */
618      public void testToArray1_BadArg() {
619 <        try {
620 <            PriorityBlockingQueue q = populatedQueue(SIZE);
621 <            Object o[] = q.toArray(new String[10] );
622 <            shouldThrow();
623 <        } catch(ArrayStoreException  success){}
619 >        PriorityBlockingQueue q = populatedQueue(SIZE);
620 >        try {
621 >            q.toArray(new String[10]);
622 >            shouldThrow();
623 >        } catch (ArrayStoreException success) {}
624      }
625  
626      /**
# Line 741 | Line 629 | public class PriorityBlockingQueueTest e
629      public void testIterator() {
630          PriorityBlockingQueue q = populatedQueue(SIZE);
631          int i = 0;
632 <        Iterator it = q.iterator();
633 <        while(it.hasNext()) {
632 >        Iterator it = q.iterator();
633 >        while (it.hasNext()) {
634              assertTrue(q.contains(it.next()));
635              ++i;
636          }
# Line 752 | Line 640 | public class PriorityBlockingQueueTest e
640      /**
641       * iterator.remove removes current element
642       */
643 <    public void testIteratorRemove () {
643 >    public void testIteratorRemove() {
644          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
645          q.add(new Integer(2));
646          q.add(new Integer(1));
# Line 786 | Line 674 | public class PriorityBlockingQueueTest e
674      public void testPollInExecutor() {
675          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
676          ExecutorService executor = Executors.newFixedThreadPool(2);
677 <        executor.execute(new Runnable() {
678 <            public void run() {
679 <                threadAssertNull(q.poll());
680 <                try {
681 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
682 <                    threadAssertTrue(q.isEmpty());
683 <                }
684 <                catch (InterruptedException e) {
685 <                    threadUnexpectedException();
686 <                }
687 <            }
688 <        });
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 <        });
677 >        executor.execute(new CheckedRunnable() {
678 >            public void realRun() throws InterruptedException {
679 >                assertNull(q.poll());
680 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
681 >                assertTrue(q.isEmpty());
682 >            }});
683 >
684 >        executor.execute(new CheckedRunnable() {
685 >            public void realRun() throws InterruptedException {
686 >                Thread.sleep(SMALL_DELAY_MS);
687 >                q.put(one);
688 >            }});
689  
690          joinPool(executor);
691      }
# Line 817 | Line 693 | public class PriorityBlockingQueueTest e
693      /**
694       * A deserialized serialized queue has same elements
695       */
696 <    public void testSerialization() {
696 >    public void testSerialization() throws Exception {
697          PriorityBlockingQueue q = populatedQueue(SIZE);
698 <        try {
699 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
700 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
701 <            out.writeObject(q);
702 <            out.close();
703 <
704 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
705 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
706 <            PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
707 <            assertEquals(q.size(), r.size());
708 <            while (!q.isEmpty())
833 <                assertEquals(q.remove(), r.remove());
834 <        } catch(Exception e){
835 <            unexpectedException();
836 <        }
698 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
699 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
700 >        out.writeObject(q);
701 >        out.close();
702 >
703 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
704 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
705 >        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
706 >        assertEquals(q.size(), r.size());
707 >        while (!q.isEmpty())
708 >            assertEquals(q.remove(), r.remove());
709      }
710  
711      /**
# Line 844 | Line 716 | public class PriorityBlockingQueueTest e
716          try {
717              q.drainTo(null);
718              shouldThrow();
719 <        } catch(NullPointerException success) {
848 <        }
719 >        } catch (NullPointerException success) {}
720      }
721  
722      /**
# Line 856 | Line 727 | public class PriorityBlockingQueueTest e
727          try {
728              q.drainTo(q);
729              shouldThrow();
730 <        } catch(IllegalArgumentException success) {
860 <        }
730 >        } catch (IllegalArgumentException success) {}
731      }
732  
733      /**
# Line 887 | Line 757 | public class PriorityBlockingQueueTest e
757      /**
758       * drainTo empties queue
759       */
760 <    public void testDrainToWithActivePut() {
760 >    public void testDrainToWithActivePut() throws InterruptedException {
761          final PriorityBlockingQueue q = populatedQueue(SIZE);
762 <        Thread t = new Thread(new Runnable() {
763 <                public void run() {
764 <                    q.put(new Integer(SIZE+1));
765 <                }
766 <            });
767 <        try {
768 <            t.start();
769 <            ArrayList l = new ArrayList();
770 <            q.drainTo(l);
771 <            assertTrue(l.size() >= SIZE);
772 <            for (int i = 0; i < SIZE; ++i)
773 <                assertEquals(l.get(i), new Integer(i));
774 <            t.join();
905 <            assertTrue(q.size() + l.size() >= SIZE);
906 <        } catch(Exception e){
907 <            unexpectedException();
908 <        }
762 >        Thread t = new Thread(new CheckedRunnable() {
763 >            public void realRun() {
764 >                q.put(new Integer(SIZE+1));
765 >            }});
766 >
767 >        t.start();
768 >        ArrayList l = new ArrayList();
769 >        q.drainTo(l);
770 >        assertTrue(l.size() >= SIZE);
771 >        for (int i = 0; i < SIZE; ++i)
772 >            assertEquals(l.get(i), new Integer(i));
773 >        t.join();
774 >        assertTrue(q.size() + l.size() >= SIZE);
775      }
776  
777      /**
# Line 916 | Line 782 | public class PriorityBlockingQueueTest e
782          try {
783              q.drainTo(null, 0);
784              shouldThrow();
785 <        } catch(NullPointerException success) {
920 <        }
785 >        } catch (NullPointerException success) {}
786      }
787  
788      /**
# Line 928 | Line 793 | public class PriorityBlockingQueueTest e
793          try {
794              q.drainTo(q, 0);
795              shouldThrow();
796 <        } catch(IllegalArgumentException success) {
932 <        }
796 >        } catch (IllegalArgumentException success) {}
797      }
798  
799      /**
800 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
800 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
801       */
802      public void testDrainToN() {
803          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
804          for (int i = 0; i < SIZE + 2; ++i) {
805 <            for(int j = 0; j < SIZE; j++)
805 >            for (int j = 0; j < SIZE; j++)
806                  assertTrue(q.offer(new Integer(j)));
807              ArrayList l = new ArrayList();
808              q.drainTo(l, i);
809 <            int k = (i < SIZE)? i : SIZE;
809 >            int k = (i < SIZE) ? i : SIZE;
810              assertEquals(l.size(), k);
811              assertEquals(q.size(), SIZE-k);
812              for (int j = 0; j < k; ++j)
# Line 951 | Line 815 | public class PriorityBlockingQueueTest e
815          }
816      }
817  
954
818   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines