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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.6 by dl, Sun Oct 5 23:00:40 2003 UTC

# Line 36 | Line 36 | public class LinkedBlockingQueueTest ext
36          return q;
37      }
38  
39 <    public void testConstructor1(){
39 >    /**
40 >     * A new queue has the indicated capacity, or Integer.MAX_VALUE if
41 >     * none given
42 >     */
43 >    public void testConstructor1() {
44          assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
45 +        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
46      }
47  
48 <    public void testConstructor2(){
48 >    /**
49 >     * Constructor throws IAE if  capacity argument nonpositive
50 >     */
51 >    public void testConstructor2() {
52          try {
53              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
54 <            fail("Cannot make zero-sized");
54 >            shouldThrow();
55          }
56          catch (IllegalArgumentException success) {}
57      }
58  
59 <    public void testConstructor3(){
60 <
59 >    /**
60 >     * Initializing from null Collection throws NPE
61 >     */
62 >    public void testConstructor3() {
63          try {
64              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
65 <            fail("Cannot make from null collection");
65 >            shouldThrow();
66          }
67          catch (NullPointerException success) {}
68      }
69  
70 <    public void testConstructor4(){
70 >    /**
71 >     * Initializing from Collection of null elements throws NPE
72 >     */
73 >    public void testConstructor4() {
74          try {
75              Integer[] ints = new Integer[SIZE];
76              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
77 <            fail("Cannot make with null elements");
77 >            shouldThrow();
78          }
79          catch (NullPointerException success) {}
80      }
81  
82 <    public void testConstructor5(){
82 >    /**
83 >     * Initializing from Collection with some null elements throws NPE
84 >     */
85 >    public void testConstructor5() {
86          try {
87              Integer[] ints = new Integer[SIZE];
88              for (int i = 0; i < SIZE-1; ++i)
89                  ints[i] = new Integer(i);
90              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
91 <            fail("Cannot make with null elements");
91 >            shouldThrow();
92          }
93          catch (NullPointerException success) {}
94      }
95  
96 <    public void testConstructor6(){
96 >    /**
97 >     * Queue contains all elements of collection used to initialize
98 >     */
99 >    public void testConstructor6() {
100          try {
101              Integer[] ints = new Integer[SIZE];
102              for (int i = 0; i < SIZE; ++i)
# Line 89 | Line 108 | public class LinkedBlockingQueueTest ext
108          finally {}
109      }
110  
111 +    /**
112 +     * Queue transitions from empty to full when elements added
113 +     */
114      public void testEmptyFull() {
115          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
116          assertTrue(q.isEmpty());
# Line 97 | Line 119 | public class LinkedBlockingQueueTest ext
119          assertFalse(q.isEmpty());
120          q.add(two);
121          assertFalse(q.isEmpty());
122 <        assertEquals("queue should be full", 0, q.remainingCapacity());
123 <        assertFalse("offer should be rejected", q.offer(three));
122 >        assertEquals(0, q.remainingCapacity());
123 >        assertFalse(q.offer(three));
124      }
125  
126 <    public void testRemainingCapacity(){
126 >    /**
127 >     * remainingCapacity decreases on add, increases on remove
128 >     */
129 >    public void testRemainingCapacity() {
130          LinkedBlockingQueue q = populatedQueue(SIZE);
131          for (int i = 0; i < SIZE; ++i) {
132              assertEquals(i, q.remainingCapacity());
# Line 115 | Line 140 | public class LinkedBlockingQueueTest ext
140          }
141      }
142  
143 <    public void testOfferNull(){
143 >    /**
144 >     * offer(null) throws NPE
145 >     */
146 >    public void testOfferNull() {
147          try {
148              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
149              q.offer(null);
150 <            fail("should throw NPE");
150 >            shouldThrow();
151 >        } catch (NullPointerException success) { }  
152 >    }
153 >
154 >    /**
155 >     * add(null) throws NPE
156 >     */
157 >    public void testAddNull() {
158 >        try {
159 >            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
160 >            q.add(null);
161 >            shouldThrow();
162          } catch (NullPointerException success) { }  
163      }
164  
165 <    public void testOffer(){
165 >    /**
166 >     * Offer succeeds if not full; fails if full
167 >     */
168 >    public void testOffer() {
169          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170          assertTrue(q.offer(zero));
171          assertFalse(q.offer(one));
172      }
173  
174 <    public void testAdd(){
174 >    /**
175 >     * add succeeds if not full; throws ISE if full
176 >     */
177 >    public void testAdd() {
178          try {
179              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
180              for (int i = 0; i < SIZE; ++i) {
# Line 141 | Line 186 | public class LinkedBlockingQueueTest ext
186          }  
187      }
188  
189 <    public void testAddAll1(){
189 >    /**
190 >     * addAll(null) throws NPE
191 >     */
192 >    public void testAddAll1() {
193          try {
194              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
195              q.addAll(null);
196 <            fail("Cannot add null collection");
196 >            shouldThrow();
197          }
198          catch (NullPointerException success) {}
199      }
200 <    public void testAddAll2(){
200 >
201 >    /**
202 >     * addAll(this) throws IAE
203 >     */
204 >    public void testAddAllSelf() {
205 >        try {
206 >            LinkedBlockingQueue q = populatedQueue(SIZE);
207 >            q.addAll(q);
208 >            shouldThrow();
209 >        }
210 >        catch (IllegalArgumentException success) {}
211 >    }
212 >
213 >    /**
214 >     * addAll of a collection with null elements throws NPE
215 >     */
216 >    public void testAddAll2() {
217          try {
218              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
219              Integer[] ints = new Integer[SIZE];
220              q.addAll(Arrays.asList(ints));
221 <            fail("Cannot add null elements");
221 >            shouldThrow();
222          }
223          catch (NullPointerException success) {}
224      }
225 <    public void testAddAll3(){
225 >    /**
226 >     * addAll of a collection with any null elements throws NPE after
227 >     * possibly adding some elements
228 >     */
229 >    public void testAddAll3() {
230          try {
231              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
232              Integer[] ints = new Integer[SIZE];
233              for (int i = 0; i < SIZE-1; ++i)
234                  ints[i] = new Integer(i);
235              q.addAll(Arrays.asList(ints));
236 <            fail("Cannot add null elements");
236 >            shouldThrow();
237          }
238          catch (NullPointerException success) {}
239      }
240 <    public void testAddAll4(){
240 >    /**
241 >     * addAll throws ISE if not enough room
242 >     */
243 >    public void testAddAll4() {
244          try {
245              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
246              Integer[] ints = new Integer[SIZE];
247              for (int i = 0; i < SIZE; ++i)
248                  ints[i] = new Integer(i);
249              q.addAll(Arrays.asList(ints));
250 <            fail("Cannot add with insufficient capacity");
250 >            shouldThrow();
251          }
252          catch (IllegalStateException success) {}
253      }
254 <    public void testAddAll5(){
254 >    /**
255 >     * Queue contains all elements, in traversal order, of successful addAll
256 >     */
257 >    public void testAddAll5() {
258          try {
259              Integer[] empty = new Integer[0];
260              Integer[] ints = new Integer[SIZE];
# Line 195 | Line 269 | public class LinkedBlockingQueueTest ext
269          finally {}
270      }
271  
272 +    /**
273 +     * put(null) throws NPE
274 +     */
275       public void testPutNull() {
276          try {
277              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
278              q.put(null);
279 <            fail("put should throw NPE");
279 >            shouldThrow();
280          }
281          catch (NullPointerException success){
282          }  
283          catch (InterruptedException ie) {
284 <            fail("Unexpected exception");
284 >            unexpectedException();
285          }
286       }
287  
288 +    /**
289 +     * all elements successfully put are contained
290 +     */
291       public void testPut() {
292           try {
293               LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
# Line 219 | Line 299 | public class LinkedBlockingQueueTest ext
299               assertEquals(0, q.remainingCapacity());
300           }
301          catch (InterruptedException ie) {
302 <            fail("Unexpected exception");
302 >            unexpectedException();
303          }
304      }
305  
306 <    public void testBlockingPut(){
306 >    /**
307 >     * put blocks interruptibly if full
308 >     */
309 >    public void testBlockingPut() {
310          Thread t = new Thread(new Runnable() {
311                  public void run() {
312                      int added = 0;
# Line 234 | Line 317 | public class LinkedBlockingQueueTest ext
317                              ++added;
318                          }
319                          q.put(new Integer(SIZE));
320 <                        threadFail("put should block");
320 >                        threadShouldThrow();
321                      } catch (InterruptedException ie){
322                          threadAssertEquals(added, SIZE);
323                      }  
# Line 246 | Line 329 | public class LinkedBlockingQueueTest ext
329             t.join();
330          }
331          catch (InterruptedException ie) {
332 <            fail("Unexpected exception");
332 >            unexpectedException();
333          }
334      }
335  
336 +    /**
337 +     * put blocks waiting for take when full
338 +     */
339      public void testPutWithTake() {
340          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
341          Thread t = new Thread(new Runnable() {
342 <                public void run(){
342 >                public void run() {
343                      int added = 0;
344                      try {
345                          q.put(new Object());
# Line 264 | Line 350 | public class LinkedBlockingQueueTest ext
350                          ++added;
351                          q.put(new Object());
352                          ++added;
353 <                        threadFail("Should block");
353 >                        threadShouldThrow();
354                      } catch (InterruptedException e){
355                          threadAssertTrue(added >= 2);
356                      }
# Line 277 | Line 363 | public class LinkedBlockingQueueTest ext
363              t.interrupt();
364              t.join();
365          } catch (Exception e){
366 <            fail("Unexpected exception");
366 >            unexpectedException();
367          }
368      }
369  
370 +    /**
371 +     * timed offer times out if full and elements not taken
372 +     */
373      public void testTimedOffer() {
374          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
375          Thread t = new Thread(new Runnable() {
376 <                public void run(){
376 >                public void run() {
377                      try {
378                          q.put(new Object());
379                          q.put(new Object());
380                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
381                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
382 <                        threadFail("Should block");
382 >                        threadShouldThrow();
383                      } catch (InterruptedException success){}
384                  }
385              });
# Line 301 | Line 390 | public class LinkedBlockingQueueTest ext
390              t.interrupt();
391              t.join();
392          } catch (Exception e){
393 <            fail("Unexpected exception");
393 >            unexpectedException();
394          }
395      }
396  
397 <    public void testTake(){
397 >    /**
398 >     * take retrieves elements in FIFO order
399 >     */
400 >    public void testTake() {
401          try {
402              LinkedBlockingQueue q = populatedQueue(SIZE);
403              for (int i = 0; i < SIZE; ++i) {
404                  assertEquals(i, ((Integer)q.take()).intValue());
405              }
406          } catch (InterruptedException e){
407 <            fail("Unexpected exception");
407 >            unexpectedException();
408          }  
409      }
410  
411 +    /**
412 +     * take blocks interruptibly when empty
413 +     */
414      public void testTakeFromEmpty() {
415          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
416          Thread t = new Thread(new Runnable() {
417 <                public void run(){
417 >                public void run() {
418                      try {
419                          q.take();
420 <                        threadFail("Should block");
420 >                        threadShouldThrow();
421                      } catch (InterruptedException success){ }                
422                  }
423              });
# Line 332 | Line 427 | public class LinkedBlockingQueueTest ext
427              t.interrupt();
428              t.join();
429          } catch (Exception e){
430 <            fail("Unexpected exception");
430 >            unexpectedException();
431          }
432      }
433  
434 <    public void testBlockingTake(){
434 >    /**
435 >     * Take removes existing elements until empty, then blocks interruptibly
436 >     */
437 >    public void testBlockingTake() {
438          Thread t = new Thread(new Runnable() {
439                  public void run() {
440                      try {
# Line 345 | Line 443 | public class LinkedBlockingQueueTest ext
443                              assertEquals(i, ((Integer)q.take()).intValue());
444                          }
445                          q.take();
446 <                        threadFail("take should block");
446 >                        threadShouldThrow();
447                      } catch (InterruptedException success){
448                      }  
449                  }});
# Line 356 | Line 454 | public class LinkedBlockingQueueTest ext
454             t.join();
455          }
456          catch (InterruptedException ie) {
457 <            fail("Unexpected exception");
457 >            unexpectedException();
458          }
459      }
460  
461  
462 <    public void testPoll(){
462 >    /**
463 >     * poll succeeds unless empty
464 >     */
465 >    public void testPoll() {
466          LinkedBlockingQueue q = populatedQueue(SIZE);
467          for (int i = 0; i < SIZE; ++i) {
468              assertEquals(i, ((Integer)q.poll()).intValue());
# Line 369 | Line 470 | public class LinkedBlockingQueueTest ext
470          assertNull(q.poll());
471      }
472  
473 +    /**
474 +     * timed pool with zero timeout succeeds when non-empty, else times out
475 +     */
476      public void testTimedPoll0() {
477          try {
478              LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 377 | Line 481 | public class LinkedBlockingQueueTest ext
481              }
482              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
483          } catch (InterruptedException e){
484 <            fail("Unexpected exception");
484 >            unexpectedException();
485          }  
486      }
487  
488 +    /**
489 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
490 +     */
491      public void testTimedPoll() {
492          try {
493              LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 389 | Line 496 | public class LinkedBlockingQueueTest ext
496              }
497              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
498          } catch (InterruptedException e){
499 <            fail("Unexpected exception");
499 >            unexpectedException();
500          }  
501      }
502  
503 <    public void testInterruptedTimedPoll(){
503 >    /**
504 >     * Interrupted timed poll throws InterruptedException instead of
505 >     * returning timeout status
506 >     */
507 >    public void testInterruptedTimedPoll() {
508          Thread t = new Thread(new Runnable() {
509                  public void run() {
510                      try {
# Line 412 | Line 523 | public class LinkedBlockingQueueTest ext
523             t.join();
524          }
525          catch (InterruptedException ie) {
526 <            fail("Unexpected exception");
526 >            unexpectedException();
527          }
528      }
529  
530 <    public void testTimedPollWithOffer(){
530 >    /**
531 >     *  timed poll before a delayed offer fails; after offer succeeds;
532 >     *  on interruption throws
533 >     */
534 >    public void testTimedPollWithOffer() {
535          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
536          Thread t = new Thread(new Runnable() {
537 <                public void run(){
537 >                public void run() {
538                      try {
539                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
540                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
541                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
542 <                        threadFail("Should block");
542 >                        threadShouldThrow();
543                      } catch (InterruptedException success) { }                
544                  }
545              });
# Line 435 | Line 550 | public class LinkedBlockingQueueTest ext
550              t.interrupt();
551              t.join();
552          } catch (Exception e){
553 <            fail("Unexpected exception");
553 >            unexpectedException();
554          }
555      }  
556  
557 <
558 <    public void testPeek(){
557 >    /**
558 >     * peek returns next element, or null if empty
559 >     */
560 >    public void testPeek() {
561          LinkedBlockingQueue q = populatedQueue(SIZE);
562          for (int i = 0; i < SIZE; ++i) {
563              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 451 | Line 568 | public class LinkedBlockingQueueTest ext
568          assertNull(q.peek());
569      }
570  
571 <    public void testElement(){
571 >    /**
572 >     * element returns next element, or throws NSEE if empty
573 >     */
574 >    public void testElement() {
575          LinkedBlockingQueue q = populatedQueue(SIZE);
576          for (int i = 0; i < SIZE; ++i) {
577              assertEquals(i, ((Integer)q.element()).intValue());
# Line 459 | Line 579 | public class LinkedBlockingQueueTest ext
579          }
580          try {
581              q.element();
582 <            fail("no such element");
582 >            shouldThrow();
583          }
584          catch (NoSuchElementException success) {}
585      }
586  
587 <    public void testRemove(){
587 >    /**
588 >     * remove removes next element, or throws NSEE if empty
589 >     */
590 >    public void testRemove() {
591          LinkedBlockingQueue q = populatedQueue(SIZE);
592          for (int i = 0; i < SIZE; ++i) {
593              assertEquals(i, ((Integer)q.remove()).intValue());
594          }
595          try {
596              q.remove();
597 <            fail("remove should throw");
597 >            shouldThrow();
598          } catch (NoSuchElementException success){
599          }  
600      }
601  
602 <    public void testRemoveElement(){
602 >    /**
603 >     * remove(x) removes x and returns true if present
604 >     */
605 >    public void testRemoveElement() {
606          LinkedBlockingQueue q = populatedQueue(SIZE);
607          for (int i = 1; i < SIZE; i+=2) {
608              assertTrue(q.remove(new Integer(i)));
# Line 488 | Line 614 | public class LinkedBlockingQueueTest ext
614          assertTrue(q.isEmpty());
615      }
616          
617 <    public void testContains(){
617 >    /**
618 >     * contains(x) reports true when elements added but not yet removed
619 >     */
620 >    public void testContains() {
621          LinkedBlockingQueue q = populatedQueue(SIZE);
622          for (int i = 0; i < SIZE; ++i) {
623              assertTrue(q.contains(new Integer(i)));
# Line 497 | Line 626 | public class LinkedBlockingQueueTest ext
626          }
627      }
628  
629 <    public void testClear(){
629 >    /**
630 >     * clear removes all elements
631 >     */
632 >    public void testClear() {
633          LinkedBlockingQueue q = populatedQueue(SIZE);
634          q.clear();
635          assertTrue(q.isEmpty());
# Line 509 | Line 641 | public class LinkedBlockingQueueTest ext
641          assertTrue(q.isEmpty());
642      }
643  
644 <    public void testContainsAll(){
644 >    /**
645 >     * containsAll(c) is true when c contains a subset of elements
646 >     */
647 >    public void testContainsAll() {
648          LinkedBlockingQueue q = populatedQueue(SIZE);
649          LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
650          for (int i = 0; i < SIZE; ++i) {
# Line 520 | Line 655 | public class LinkedBlockingQueueTest ext
655          assertTrue(p.containsAll(q));
656      }
657  
658 <    public void testRetainAll(){
658 >    /**
659 >     * retainAll(c) retains only those elements of c and reports true if changed
660 >     */
661 >    public void testRetainAll() {
662          LinkedBlockingQueue q = populatedQueue(SIZE);
663          LinkedBlockingQueue p = populatedQueue(SIZE);
664          for (int i = 0; i < SIZE; ++i) {
# Line 536 | Line 674 | public class LinkedBlockingQueueTest ext
674          }
675      }
676  
677 <    public void testRemoveAll(){
677 >    /**
678 >     * removeAll(c) removes only those elements of c and reports true if changed
679 >     */
680 >    public void testRemoveAll() {
681          for (int i = 1; i < SIZE; ++i) {
682              LinkedBlockingQueue q = populatedQueue(SIZE);
683              LinkedBlockingQueue p = populatedQueue(i);
# Line 549 | Line 690 | public class LinkedBlockingQueueTest ext
690          }
691      }
692  
693 <
694 <    public void testToArray(){
693 >    /**
694 >     * toArray contains all elements
695 >     */
696 >    public void testToArray() {
697          LinkedBlockingQueue q = populatedQueue(SIZE);
698          Object[] o = q.toArray();
699          try {
700          for(int i = 0; i < o.length; i++)
701              assertEquals(o[i], q.take());
702          } catch (InterruptedException e){
703 <            fail("Unexpected exception");
703 >            unexpectedException();
704          }    
705      }
706  
707 <    public void testToArray2(){
707 >    /**
708 >     * toArray(a) contains all elements
709 >     */
710 >    public void testToArray2() {
711          LinkedBlockingQueue q = populatedQueue(SIZE);
712          Integer[] ints = new Integer[SIZE];
713          ints = (Integer[])q.toArray(ints);
# Line 569 | Line 715 | public class LinkedBlockingQueueTest ext
715              for(int i = 0; i < ints.length; i++)
716                  assertEquals(ints[i], q.take());
717          } catch (InterruptedException e){
718 <            fail("Unexpected exception");
718 >            unexpectedException();
719          }    
720      }
721 +
722 +    /**
723 +     * toArray(null) throws NPE
724 +     */
725 +    public void testToArray_BadArg() {
726 +        try {
727 +            LinkedBlockingQueue q = populatedQueue(SIZE);
728 +            Object o[] = q.toArray(null);
729 +            shouldThrow();
730 +        } catch(NullPointerException success){}
731 +    }
732 +
733 +    /**
734 +     * toArray with incompatable array type throws CCE
735 +     */
736 +    public void testToArray1_BadArg() {
737 +        try {
738 +            LinkedBlockingQueue q = populatedQueue(SIZE);
739 +            Object o[] = q.toArray(new String[10] );
740 +            shouldThrow();
741 +        } catch(ArrayStoreException  success){}
742 +    }
743 +
744      
745 <    public void testIterator(){
745 >    /**
746 >     * iterator iterates through all elements
747 >     */
748 >    public void testIterator() {
749          LinkedBlockingQueue q = populatedQueue(SIZE);
750          Iterator it = q.iterator();
751          try {
# Line 581 | Line 753 | public class LinkedBlockingQueueTest ext
753                  assertEquals(it.next(), q.take());
754              }
755          } catch (InterruptedException e){
756 <            fail("Unexpected exception");
756 >            unexpectedException();
757          }    
758      }
759  
760 <    public void testIteratorOrdering() {
761 <
760 >    /**
761 >     * iterator.remove removes current element
762 >     */
763 >    public void testIteratorRemove () {
764          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
591
592        q.add(one);
765          q.add(two);
766 +        q.add(one);
767          q.add(three);
768  
769 <        assertEquals("queue should be full", 0, q.remainingCapacity());
769 >        Iterator it = q.iterator();
770 >        it.next();
771 >        it.remove();
772 >        
773 >        it = q.iterator();
774 >        assertEquals(it.next(), one);
775 >        assertEquals(it.next(), three);
776 >        assertFalse(it.hasNext());
777 >    }
778 >
779  
780 +    /**
781 +     * iterator ordering is FIFO
782 +     */
783 +    public void testIteratorOrdering() {
784 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
785 +        q.add(one);
786 +        q.add(two);
787 +        q.add(three);
788 +        assertEquals(0, q.remainingCapacity());
789          int k = 0;
790          for (Iterator it = q.iterator(); it.hasNext();) {
791              int i = ((Integer)(it.next())).intValue();
792 <            assertEquals("items should come out in order", ++k, i);
792 >            assertEquals(++k, i);
793          }
794 <
604 <        assertEquals("should go through 3 elements", 3, k);
794 >        assertEquals(3, k);
795      }
796  
797 +    /**
798 +     * Modifications do not cause iterators to fail
799 +     */
800      public void testWeaklyConsistentIteration () {
608
801          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
610
802          q.add(one);
803          q.add(two);
804          q.add(three);
614
805          try {
806              for (Iterator it = q.iterator(); it.hasNext();) {
807                  q.remove();
# Line 619 | Line 809 | public class LinkedBlockingQueueTest ext
809              }
810          }
811          catch (ConcurrentModificationException e) {
812 <            fail("weakly consistent iterator; should not get CME");
812 >            unexpectedException();
813          }
814 <
625 <        assertEquals("queue should be empty again", 0, q.size());
814 >        assertEquals(0, q.size());
815      }
816  
817  
818 <    public void testToString(){
818 >    /**
819 >     * toString contains toStrings of elements
820 >     */
821 >    public void testToString() {
822          LinkedBlockingQueue q = populatedQueue(SIZE);
823          String s = q.toString();
824          for (int i = 0; i < SIZE; ++i) {
# Line 635 | Line 827 | public class LinkedBlockingQueueTest ext
827      }        
828  
829  
830 +    /**
831 +     * offer transfers elements across Executor tasks
832 +     */
833      public void testOfferInExecutor() {
639
834          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
641
835          q.add(one);
836          q.add(two);
644
837          ExecutorService executor = Executors.newFixedThreadPool(2);
646
838          executor.execute(new Runnable() {
839              public void run() {
840                  threadAssertFalse(q.offer(three));
# Line 652 | Line 843 | public class LinkedBlockingQueueTest ext
843                      threadAssertEquals(0, q.remainingCapacity());
844                  }
845                  catch (InterruptedException e) {
846 <                    threadFail("should not be interrupted");
846 >                    threadUnexpectedException();
847                  }
848              }
849          });
# Line 664 | Line 855 | public class LinkedBlockingQueueTest ext
855                      threadAssertEquals(one, q.take());
856                  }
857                  catch (InterruptedException e) {
858 <                    threadFail("should not be interrupted");
858 >                    threadUnexpectedException();
859                  }
860              }
861          });
862          
863          joinPool(executor);
673
864      }
865  
866 +    /**
867 +     * poll retrieves elements across Executor threads
868 +     */
869      public void testPollInExecutor() {
677
870          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
679
871          ExecutorService executor = Executors.newFixedThreadPool(2);
681
872          executor.execute(new Runnable() {
873              public void run() {
874                  threadAssertNull(q.poll());
# Line 687 | Line 877 | public class LinkedBlockingQueueTest ext
877                      threadAssertTrue(q.isEmpty());
878                  }
879                  catch (InterruptedException e) {
880 <                    threadFail("should not be interrupted");
880 >                    threadUnexpectedException();
881                  }
882              }
883          });
# Line 699 | Line 889 | public class LinkedBlockingQueueTest ext
889                      q.put(one);
890                  }
891                  catch (InterruptedException e) {
892 <                    threadFail("should not be interrupted");
892 >                    threadUnexpectedException();
893                  }
894              }
895          });
# Line 707 | Line 897 | public class LinkedBlockingQueueTest ext
897          joinPool(executor);
898      }
899  
900 +    /**
901 +     * A deserialized serialized queue has same elements in same order
902 +     */
903      public void testSerialization() {
904          LinkedBlockingQueue q = populatedQueue(SIZE);
905  
# Line 723 | Line 916 | public class LinkedBlockingQueueTest ext
916              while (!q.isEmpty())
917                  assertEquals(q.remove(), r.remove());
918          } catch(Exception e){
919 <            e.printStackTrace();
920 <            fail("unexpected exception");
919 >            unexpectedException();
920 >        }
921 >    }
922 >
923 >    /**
924 >     * drainTo(null) throws NPE
925 >     */
926 >    public void testDrainToNull() {
927 >        LinkedBlockingQueue q = populatedQueue(SIZE);
928 >        try {
929 >            q.drainTo(null);
930 >            shouldThrow();
931 >        } catch(NullPointerException success) {
932 >        }
933 >    }
934 >
935 >    /**
936 >     * drainTo(this) throws IAE
937 >     */
938 >    public void testDrainToSelf() {
939 >        LinkedBlockingQueue q = populatedQueue(SIZE);
940 >        try {
941 >            q.drainTo(q);
942 >            shouldThrow();
943 >        } catch(IllegalArgumentException success) {
944 >        }
945 >    }
946 >
947 >    /**
948 >     * drainTo(c) empties queue into another collection c
949 >     */
950 >    public void testDrainTo() {
951 >        LinkedBlockingQueue q = populatedQueue(SIZE);
952 >        ArrayList l = new ArrayList();
953 >        q.drainTo(l);
954 >        assertEquals(q.size(), 0);
955 >        assertEquals(l.size(), SIZE);
956 >        for (int i = 0; i < SIZE; ++i)
957 >            assertEquals(l.get(i), new Integer(i));
958 >    }
959 >
960 >    /**
961 >     * drainTo empties full queue, unblocking a waiting put.
962 >     */
963 >    public void testDrainToWithActivePut() {
964 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
965 >        Thread t = new Thread(new Runnable() {
966 >                public void run() {
967 >                    try {
968 >                        q.put(new Integer(SIZE+1));
969 >                    } catch (InterruptedException ie){
970 >                        threadUnexpectedException();
971 >                    }
972 >                }
973 >            });
974 >        try {
975 >            t.start();
976 >            ArrayList l = new ArrayList();
977 >            q.drainTo(l);
978 >            assertTrue(l.size() >= SIZE);
979 >            for (int i = 0; i < SIZE; ++i)
980 >                assertEquals(l.get(i), new Integer(i));
981 >            t.join();
982 >            assertTrue(q.size() + l.size() == SIZE+1);
983 >        } catch(Exception e){
984 >            unexpectedException();
985 >        }
986 >    }
987 >
988 >    /**
989 >     * drainTo(null, n) throws NPE
990 >     */
991 >    public void testDrainToNullN() {
992 >        LinkedBlockingQueue q = populatedQueue(SIZE);
993 >        try {
994 >            q.drainTo(null, 0);
995 >            shouldThrow();
996 >        } catch(NullPointerException success) {
997 >        }
998 >    }
999 >
1000 >    /**
1001 >     * drainTo(this, n) throws IAE
1002 >     */
1003 >    public void testDrainToSelfN() {
1004 >        LinkedBlockingQueue q = populatedQueue(SIZE);
1005 >        try {
1006 >            q.drainTo(q, 0);
1007 >            shouldThrow();
1008 >        } catch(IllegalArgumentException success) {
1009 >        }
1010 >    }
1011 >
1012 >    /**
1013 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
1014 >     */
1015 >    public void testDrainToN() {
1016 >        for (int i = 0; i < SIZE + 2; ++i) {
1017 >            LinkedBlockingQueue q = populatedQueue(SIZE);
1018 >            ArrayList l = new ArrayList();
1019 >            q.drainTo(l, i);
1020 >            int k = (i < SIZE)? i : SIZE;
1021 >            assertEquals(q.size(), SIZE-k);
1022 >            assertEquals(l.size(), k);
1023 >            for (int j = 0; j < k; ++j)
1024 >                assertEquals(l.get(j), new Integer(j));
1025          }
1026      }
1027  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines