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.10 by dl, Sun Oct 31 14:55:14 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines