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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.8 by dl, Mon Dec 29 19:05:40 2003 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 +
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13   import java.io.*;
14  
15   public class ArrayBlockingQueueTest extends JSR166TestCase {
14
16      public static void main(String[] args) {
17          junit.textui.TestRunner.run (suite());  
18      }
18
19      public static Test suite() {
20          return new TestSuite(ArrayBlockingQueueTest.class);
21      }
# Line 35 | Line 35 | public class ArrayBlockingQueueTest exte
35          return q;
36      }
37  
38 <    public void testConstructor1(){
38 >    /**
39 >     * A new queue has the indicated capacity
40 >     */
41 >    public void testConstructor1() {
42          assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
43      }
44  
45 <    public void testConstructor2(){
45 >    /**
46 >     * Constructor throws IAE if  capacity argument nonpositive
47 >     */
48 >    public void testConstructor2() {
49          try {
50              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
51 <            fail("Cannot make zero-sized");
51 >            shouldThrow();
52          }
53          catch (IllegalArgumentException success) {}
54      }
55  
56 <    public void testConstructor3(){
57 <
56 >    /**
57 >     * Initializing from null Collection throws NPE
58 >     */
59 >    public void testConstructor3() {
60          try {
61              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
62 <            fail("Cannot make from null collection");
62 >            shouldThrow();
63          }
64          catch (NullPointerException success) {}
65      }
66  
67 <    public void testConstructor4(){
67 >    /**
68 >     * Initializing from Collection of null elements throws NPE
69 >     */
70 >    public void testConstructor4() {
71          try {
72              Integer[] ints = new Integer[SIZE];
73              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
74 <            fail("Cannot make with null elements");
74 >            shouldThrow();
75          }
76          catch (NullPointerException success) {}
77      }
78  
79 <    public void testConstructor5(){
79 >    /**
80 >     * Initializing from Collection with some null elements throws NPE
81 >     */
82 >    public void testConstructor5() {
83          try {
84              Integer[] ints = new Integer[SIZE];
85              for (int i = 0; i < SIZE-1; ++i)
86                  ints[i] = new Integer(i);
87              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
88 <            fail("Cannot make with null elements");
88 >            shouldThrow();
89          }
90          catch (NullPointerException success) {}
91      }
92  
93 <    public void testConstructor6(){
93 >    /**
94 >     * Initializing from too large collection throws IAE
95 >     */
96 >    public void testConstructor6() {
97          try {
98              Integer[] ints = new Integer[SIZE];
99              for (int i = 0; i < SIZE; ++i)
100                  ints[i] = new Integer(i);
101              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
102 <            fail("Cannot make with insufficient capacity");
102 >            shouldThrow();
103          }
104          catch (IllegalArgumentException success) {}
105      }
106  
107 <    public void testConstructor7(){
107 >    /**
108 >     * Queue contains all elements of collection used to initialize
109 >     */
110 >    public void testConstructor7() {
111          try {
112              Integer[] ints = new Integer[SIZE];
113              for (int i = 0; i < SIZE; ++i)
# Line 99 | Line 119 | public class ArrayBlockingQueueTest exte
119          finally {}
120      }
121  
122 +    /**
123 +     * Queue transitions from empty to full when elements added
124 +     */
125      public void testEmptyFull() {
126          ArrayBlockingQueue q = new ArrayBlockingQueue(2);
127          assertTrue(q.isEmpty());
128 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
128 >        assertEquals(2, q.remainingCapacity());
129          q.add(one);
130          assertFalse(q.isEmpty());
131          q.add(two);
132          assertFalse(q.isEmpty());
133 <        assertEquals("queue should be full", 0, q.remainingCapacity());
134 <        assertFalse("offer should be rejected", q.offer(three));
133 >        assertEquals(0, q.remainingCapacity());
134 >        assertFalse(q.offer(three));
135      }
136  
137 <    public void testRemainingCapacity(){
137 >    /**
138 >     * remainingCapacity decreases on add, increases on remove
139 >     */
140 >    public void testRemainingCapacity() {
141          ArrayBlockingQueue q = populatedQueue(SIZE);
142          for (int i = 0; i < SIZE; ++i) {
143              assertEquals(i, q.remainingCapacity());
# Line 125 | Line 151 | public class ArrayBlockingQueueTest exte
151          }
152      }
153  
154 <    public void testOfferNull(){
154 >    /**
155 >     *  offer(null) throws NPE
156 >     */
157 >    public void testOfferNull() {
158          try {
159              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160              q.offer(null);
161 <            fail("should throw NPE");
161 >            shouldThrow();
162          } catch (NullPointerException success) { }  
163      }
164  
165 <    public void testOffer(){
165 >    /**
166 >     *  add(null) throws NPE
167 >     */
168 >    public void testAddNull() {
169 >        try {
170 >            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
171 >            q.add(null);
172 >            shouldThrow();
173 >        } catch (NullPointerException success) { }  
174 >    }
175 >
176 >    /**
177 >     * Offer succeeds if not full; fails if full
178 >     */
179 >    public void testOffer() {
180          ArrayBlockingQueue q = new ArrayBlockingQueue(1);
181          assertTrue(q.offer(zero));
182          assertFalse(q.offer(one));
183      }
184  
185 <    public void testAdd(){
185 >    /**
186 >     * add succeeds if not full; throws ISE if full
187 >     */
188 >    public void testAdd() {
189          try {
190              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
191              for (int i = 0; i < SIZE; ++i) {
# Line 151 | Line 197 | public class ArrayBlockingQueueTest exte
197          }  
198      }
199  
200 <    public void testAddAll1(){
200 >    /**
201 >     *  addAll(null) throws NPE
202 >     */
203 >    public void testAddAll1() {
204          try {
205              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
206              q.addAll(null);
207 <            fail("Cannot add null collection");
207 >            shouldThrow();
208          }
209          catch (NullPointerException success) {}
210      }
211 <    public void testAddAll2(){
211 >
212 >    /**
213 >     * addAll(this) throws IAE
214 >     */
215 >    public void testAddAllSelf() {
216 >        try {
217 >            ArrayBlockingQueue q = populatedQueue(SIZE);
218 >            q.addAll(q);
219 >            shouldThrow();
220 >        }
221 >        catch (IllegalArgumentException success) {}
222 >    }
223 >
224 >
225 >    /**
226 >     *  addAll of a collection with null elements throws NPE
227 >     */
228 >    public void testAddAll2() {
229          try {
230              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
231              Integer[] ints = new Integer[SIZE];
232              q.addAll(Arrays.asList(ints));
233 <            fail("Cannot add null elements");
233 >            shouldThrow();
234          }
235          catch (NullPointerException success) {}
236      }
237 <    public void testAddAll3(){
237 >    /**
238 >     * addAll of a collection with any null elements throws NPE after
239 >     * possibly adding some elements
240 >     */
241 >    public void testAddAll3() {
242          try {
243              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
244              Integer[] ints = new Integer[SIZE];
245              for (int i = 0; i < SIZE-1; ++i)
246                  ints[i] = new Integer(i);
247              q.addAll(Arrays.asList(ints));
248 <            fail("Cannot add null elements");
248 >            shouldThrow();
249          }
250          catch (NullPointerException success) {}
251      }
252 <    public void testAddAll4(){
252 >    /**
253 >     * addAll throws ISE if not enough room
254 >     */
255 >    public void testAddAll4() {
256          try {
257              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
258              Integer[] ints = new Integer[SIZE];
259              for (int i = 0; i < SIZE; ++i)
260                  ints[i] = new Integer(i);
261              q.addAll(Arrays.asList(ints));
262 <            fail("Cannot add with insufficient capacity");
262 >            shouldThrow();
263          }
264          catch (IllegalStateException success) {}
265      }
266 <    public void testAddAll5(){
266 >    /**
267 >     * Queue contains all elements, in traversal order, of successful addAll
268 >     */
269 >    public void testAddAll5() {
270          try {
271              Integer[] empty = new Integer[0];
272              Integer[] ints = new Integer[SIZE];
# Line 205 | Line 281 | public class ArrayBlockingQueueTest exte
281          finally {}
282      }
283  
284 +    /**
285 +     *  put(null) throws NPE
286 +     */
287       public void testPutNull() {
288          try {
289              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
290              q.put(null);
291 <            fail("put should throw NPE");
291 >            shouldThrow();
292          }
293          catch (NullPointerException success){
294          }  
295          catch (InterruptedException ie) {
296 <            fail("Unexpected exception");
296 >            unexpectedException();
297          }
298       }
299  
300 +    /**
301 +     * all elements successfully put are contained
302 +     */
303       public void testPut() {
304           try {
305               ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
# Line 229 | Line 311 | public class ArrayBlockingQueueTest exte
311               assertEquals(0, q.remainingCapacity());
312           }
313          catch (InterruptedException ie) {
314 <            fail("Unexpected exception");
314 >            unexpectedException();
315          }
316      }
317  
318 <    public void testBlockingPut(){
318 >    /**
319 >     * put blocks interruptibly if full
320 >     */
321 >    public void testBlockingPut() {
322          Thread t = new Thread(new Runnable() {
323                  public void run() {
324                      int added = 0;
# Line 244 | Line 329 | public class ArrayBlockingQueueTest exte
329                              ++added;
330                          }
331                          q.put(new Integer(SIZE));
332 <                        threadFail("put should block");
332 >                        threadShouldThrow();
333                      } catch (InterruptedException ie){
334                          threadAssertEquals(added, SIZE);
335                      }  
# Line 256 | Line 341 | public class ArrayBlockingQueueTest exte
341             t.join();
342          }
343          catch (InterruptedException ie) {
344 <            fail("Unexpected exception");
344 >            unexpectedException();
345          }
346      }
347  
348 +    /**
349 +     * put blocks waiting for take when full
350 +     */
351      public void testPutWithTake() {
352          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
353          Thread t = new Thread(new Runnable() {
354 <                public void run(){
354 >                public void run() {
355                      int added = 0;
356                      try {
357                          q.put(new Object());
# Line 274 | Line 362 | public class ArrayBlockingQueueTest exte
362                          ++added;
363                          q.put(new Object());
364                          ++added;
365 <                        threadFail("Should block");
365 >                        threadShouldThrow();
366                      } catch (InterruptedException e){
367                          threadAssertTrue(added >= 2);
368                      }
# Line 287 | Line 375 | public class ArrayBlockingQueueTest exte
375              t.interrupt();
376              t.join();
377          } catch (Exception e){
378 <            fail("Unexpected exception");
378 >            unexpectedException();
379          }
380      }
381  
382 +    /**
383 +     * timed offer times out if full and elements not taken
384 +     */
385      public void testTimedOffer() {
386          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
387          Thread t = new Thread(new Runnable() {
388 <                public void run(){
388 >                public void run() {
389                      try {
390                          q.put(new Object());
391                          q.put(new Object());
392                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
393                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
394 <                        threadFail("Should block");
394 >                        threadShouldThrow();
395                      } catch (InterruptedException success){}
396                  }
397              });
# Line 311 | Line 402 | public class ArrayBlockingQueueTest exte
402              t.interrupt();
403              t.join();
404          } catch (Exception e){
405 <            fail("Unexpected exception");
405 >            unexpectedException();
406          }
407      }
408  
409 <    public void testTake(){
409 >    /**
410 >     * take retrieves elements in FIFO order
411 >     */
412 >    public void testTake() {
413          try {
414              ArrayBlockingQueue q = populatedQueue(SIZE);
415              for (int i = 0; i < SIZE; ++i) {
416                  assertEquals(i, ((Integer)q.take()).intValue());
417              }
418          } catch (InterruptedException e){
419 <            fail("Unexpected exception");
419 >            unexpectedException();
420          }  
421      }
422  
423 +    /**
424 +     * take blocks interruptibly when empty
425 +     */
426      public void testTakeFromEmpty() {
427          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
428          Thread t = new Thread(new Runnable() {
429 <                public void run(){
429 >                public void run() {
430                      try {
431                          q.take();
432 <                        threadFail("Should block");
432 >                        threadShouldThrow();
433                      } catch (InterruptedException success){ }                
434                  }
435              });
# Line 342 | Line 439 | public class ArrayBlockingQueueTest exte
439              t.interrupt();
440              t.join();
441          } catch (Exception e){
442 <            fail("Unexpected exception");
442 >            unexpectedException();
443          }
444      }
445  
446 <    public void testBlockingTake(){
446 >    /**
447 >     * Take removes existing elements until empty, then blocks interruptibly
448 >     */
449 >    public void testBlockingTake() {
450          Thread t = new Thread(new Runnable() {
451                  public void run() {
452                      try {
# Line 355 | Line 455 | public class ArrayBlockingQueueTest exte
455                              threadAssertEquals(i, ((Integer)q.take()).intValue());
456                          }
457                          q.take();
458 <                        threadFail("take should block");
458 >                        threadShouldThrow();
459                      } catch (InterruptedException success){
460                      }  
461                  }});
# Line 366 | Line 466 | public class ArrayBlockingQueueTest exte
466              t.join();
467          }
468          catch (InterruptedException ie) {
469 <            fail("Unexpected exception");
469 >            unexpectedException();
470          }
471      }
472  
473  
474 <    public void testPoll(){
474 >    /**
475 >     * poll succeeds unless empty
476 >     */
477 >    public void testPoll() {
478          ArrayBlockingQueue q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480              assertEquals(i, ((Integer)q.poll()).intValue());
# Line 379 | Line 482 | public class ArrayBlockingQueueTest exte
482          assertNull(q.poll());
483      }
484  
485 +    /**
486 +     * timed pool with zero timeout succeeds when non-empty, else times out
487 +     */
488      public void testTimedPoll0() {
489          try {
490              ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 387 | Line 493 | public class ArrayBlockingQueueTest exte
493              }
494              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495          } catch (InterruptedException e){
496 <            fail("Unexpected exception");
496 >            unexpectedException();
497          }  
498      }
499  
500 +    /**
501 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
502 +     */
503      public void testTimedPoll() {
504          try {
505              ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 399 | Line 508 | public class ArrayBlockingQueueTest exte
508              }
509              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510          } catch (InterruptedException e){
511 <            fail("Unexpected exception");
511 >            unexpectedException();
512          }  
513      }
514  
515 <    public void testInterruptedTimedPoll(){
515 >    /**
516 >     * Interrupted timed poll throws InterruptedException instead of
517 >     * returning timeout status
518 >     */
519 >    public void testInterruptedTimedPoll() {
520          Thread t = new Thread(new Runnable() {
521                  public void run() {
522                      try {
# Line 422 | Line 535 | public class ArrayBlockingQueueTest exte
535              t.join();
536          }
537          catch (InterruptedException ie) {
538 <            fail("Unexpected exception");
538 >            unexpectedException();
539          }
540      }
541  
542 <    public void testTimedPollWithOffer(){
542 >    /**
543 >     *  timed poll before a delayed offer fails; after offer succeeds;
544 >     *  on interruption throws
545 >     */
546 >    public void testTimedPollWithOffer() {
547          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
548          Thread t = new Thread(new Runnable() {
549 <                public void run(){
549 >                public void run() {
550                      try {
551                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        threadFail("Should block");
554 >                        threadShouldThrow();
555                      } catch (InterruptedException success) { }                
556                  }
557              });
# Line 445 | Line 562 | public class ArrayBlockingQueueTest exte
562              t.interrupt();
563              t.join();
564          } catch (Exception e){
565 <            fail("Unexpected exception");
565 >            unexpectedException();
566          }
567      }  
568  
569  
570 <    public void testPeek(){
570 >    /**
571 >     * peek returns next element, or null if empty
572 >     */
573 >    public void testPeek() {
574          ArrayBlockingQueue q = populatedQueue(SIZE);
575          for (int i = 0; i < SIZE; ++i) {
576              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 461 | Line 581 | public class ArrayBlockingQueueTest exte
581          assertNull(q.peek());
582      }
583  
584 <    public void testElement(){
584 >    /**
585 >     * element returns next element, or throws NSEE if empty
586 >     */
587 >    public void testElement() {
588          ArrayBlockingQueue q = populatedQueue(SIZE);
589          for (int i = 0; i < SIZE; ++i) {
590              assertEquals(i, ((Integer)q.element()).intValue());
# Line 469 | Line 592 | public class ArrayBlockingQueueTest exte
592          }
593          try {
594              q.element();
595 <            fail("no such element");
595 >            shouldThrow();
596          }
597          catch (NoSuchElementException success) {}
598      }
599  
600 <    public void testRemove(){
600 >    /**
601 >     * remove removes next element, or throws NSEE if empty
602 >     */
603 >    public void testRemove() {
604          ArrayBlockingQueue q = populatedQueue(SIZE);
605          for (int i = 0; i < SIZE; ++i) {
606              assertEquals(i, ((Integer)q.remove()).intValue());
607          }
608          try {
609              q.remove();
610 <            fail("remove should throw");
610 >            shouldThrow();
611          } catch (NoSuchElementException success){
612          }  
613      }
614  
615 <    public void testRemoveElement(){
615 >    /**
616 >     * remove(x) removes x and returns true if present
617 >     */
618 >    public void testRemoveElement() {
619          ArrayBlockingQueue q = populatedQueue(SIZE);
620          for (int i = 1; i < SIZE; i+=2) {
621              assertTrue(q.remove(new Integer(i)));
# Line 498 | Line 627 | public class ArrayBlockingQueueTest exte
627          assertTrue(q.isEmpty());
628      }
629          
630 <    public void testContains(){
630 >    /**
631 >     * contains(x) reports true when elements added but not yet removed
632 >     */
633 >    public void testContains() {
634          ArrayBlockingQueue q = populatedQueue(SIZE);
635          for (int i = 0; i < SIZE; ++i) {
636              assertTrue(q.contains(new Integer(i)));
# Line 507 | Line 639 | public class ArrayBlockingQueueTest exte
639          }
640      }
641  
642 <    public void testClear(){
642 >    /**
643 >     * clear removes all elements
644 >     */
645 >    public void testClear() {
646          ArrayBlockingQueue q = populatedQueue(SIZE);
647          q.clear();
648          assertTrue(q.isEmpty());
# Line 519 | Line 654 | public class ArrayBlockingQueueTest exte
654          assertTrue(q.isEmpty());
655      }
656  
657 <    public void testContainsAll(){
657 >    /**
658 >     * containsAll(c) is true when c contains a subset of elements
659 >     */
660 >    public void testContainsAll() {
661          ArrayBlockingQueue q = populatedQueue(SIZE);
662          ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
663          for (int i = 0; i < SIZE; ++i) {
# Line 530 | Line 668 | public class ArrayBlockingQueueTest exte
668          assertTrue(p.containsAll(q));
669      }
670  
671 <    public void testRetainAll(){
671 >    /**
672 >     * retainAll(c) retains only those elements of c and reports true if changed
673 >     */
674 >    public void testRetainAll() {
675          ArrayBlockingQueue q = populatedQueue(SIZE);
676          ArrayBlockingQueue p = populatedQueue(SIZE);
677          for (int i = 0; i < SIZE; ++i) {
# Line 546 | Line 687 | public class ArrayBlockingQueueTest exte
687          }
688      }
689  
690 <    public void testRemoveAll(){
690 >    /**
691 >     * removeAll(c) removes only those elements of c and reports true if changed
692 >     */
693 >    public void testRemoveAll() {
694          for (int i = 1; i < SIZE; ++i) {
695              ArrayBlockingQueue q = populatedQueue(SIZE);
696              ArrayBlockingQueue p = populatedQueue(i);
# Line 559 | Line 703 | public class ArrayBlockingQueueTest exte
703          }
704      }
705  
706 <
707 <    public void testToArray(){
706 >    /**
707 >     *  toArray contains all elements
708 >     */
709 >    public void testToArray() {
710          ArrayBlockingQueue q = populatedQueue(SIZE);
711          Object[] o = q.toArray();
712          try {
713          for(int i = 0; i < o.length; i++)
714              assertEquals(o[i], q.take());
715          } catch (InterruptedException e){
716 <            fail("Unexpected exception");
716 >            unexpectedException();
717          }    
718      }
719  
720 <    public void testToArray2(){
720 >    /**
721 >     * toArray(a) contains all elements
722 >     */
723 >    public void testToArray2() {
724          ArrayBlockingQueue q = populatedQueue(SIZE);
725          Integer[] ints = new Integer[SIZE];
726          ints = (Integer[])q.toArray(ints);
# Line 579 | Line 728 | public class ArrayBlockingQueueTest exte
728              for(int i = 0; i < ints.length; i++)
729                  assertEquals(ints[i], q.take());
730          } catch (InterruptedException e){
731 <            fail("Unexpected exception");
731 >            unexpectedException();
732          }    
733      }
734 +
735 +    /**
736 +     * toArray(null) throws NPE
737 +     */
738 +    public void testToArray_BadArg() {
739 +        try {
740 +            ArrayBlockingQueue q = populatedQueue(SIZE);
741 +            Object o[] = q.toArray(null);
742 +            shouldThrow();
743 +        } catch(NullPointerException success){}
744 +    }
745 +
746 +    /**
747 +     * toArray with incompatible array type throws CCE
748 +     */
749 +    public void testToArray1_BadArg() {
750 +        try {
751 +            ArrayBlockingQueue q = populatedQueue(SIZE);
752 +            Object o[] = q.toArray(new String[10] );
753 +            shouldThrow();
754 +        } catch(ArrayStoreException  success){}
755 +    }
756 +
757      
758 <    public void testIterator(){
758 >    /**
759 >     * iterator iterates through all elements
760 >     */
761 >    public void testIterator() {
762          ArrayBlockingQueue q = populatedQueue(SIZE);
763          Iterator it = q.iterator();
764          try {
# Line 591 | Line 766 | public class ArrayBlockingQueueTest exte
766                  assertEquals(it.next(), q.take());
767              }
768          } catch (InterruptedException e){
769 <            fail("Unexpected exception");
769 >            unexpectedException();
770          }    
771      }
772  
773 <    public void testIteratorOrdering() {
774 <
773 >    /**
774 >     * iterator.remove removes current element
775 >     */
776 >    public void testIteratorRemove () {
777          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
778 +        q.add(two);
779 +        q.add(one);
780 +        q.add(three);
781  
782 +        Iterator it = q.iterator();
783 +        it.next();
784 +        it.remove();
785 +        
786 +        it = q.iterator();
787 +        assertEquals(it.next(), one);
788 +        assertEquals(it.next(), three);
789 +        assertFalse(it.hasNext());
790 +    }
791 +
792 +    /**
793 +     * iterator ordering is FIFO
794 +     */
795 +    public void testIteratorOrdering() {
796 +        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
797          q.add(one);
798          q.add(two);
799          q.add(three);
# Line 608 | Line 803 | public class ArrayBlockingQueueTest exte
803          int k = 0;
804          for (Iterator it = q.iterator(); it.hasNext();) {
805              int i = ((Integer)(it.next())).intValue();
806 <            assertEquals("items should come out in order", ++k, i);
806 >            assertEquals(++k, i);
807          }
808 <
614 <        assertEquals("should go through 3 elements", 3, k);
808 >        assertEquals(3, k);
809      }
810  
811 +    /**
812 +     * Modifications do not cause iterators to fail
813 +     */
814      public void testWeaklyConsistentIteration () {
618
815          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
620
816          q.add(one);
817          q.add(two);
818          q.add(three);
624
819          try {
820              for (Iterator it = q.iterator(); it.hasNext();) {
821                  q.remove();
# Line 629 | Line 823 | public class ArrayBlockingQueueTest exte
823              }
824          }
825          catch (ConcurrentModificationException e) {
826 <            fail("weakly consistent iterator; should not get CME");
826 >            unexpectedException();
827          }
828 <
635 <        assertEquals("queue should be empty again", 0, q.size());
828 >        assertEquals(0, q.size());
829      }
830  
831  
832 <    public void testToString(){
832 >    /**
833 >     * toString contains toStrings of elements
834 >     */
835 >    public void testToString() {
836          ArrayBlockingQueue q = populatedQueue(SIZE);
837          String s = q.toString();
838          for (int i = 0; i < SIZE; ++i) {
# Line 645 | Line 841 | public class ArrayBlockingQueueTest exte
841      }        
842  
843  
844 +    /**
845 +     * offer transfers elements across Executor tasks
846 +     */
847      public void testOfferInExecutor() {
649
848          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
651
849          q.add(one);
850          q.add(two);
654
851          ExecutorService executor = Executors.newFixedThreadPool(2);
656
852          executor.execute(new Runnable() {
853              public void run() {
854                  threadAssertFalse(q.offer(three));
# Line 662 | Line 857 | public class ArrayBlockingQueueTest exte
857                      threadAssertEquals(0, q.remainingCapacity());
858                  }
859                  catch (InterruptedException e) {
860 <                    threadFail("should not be interrupted");
860 >                    threadUnexpectedException();
861                  }
862              }
863          });
# Line 674 | Line 869 | public class ArrayBlockingQueueTest exte
869                      threadAssertEquals(one, q.take());
870                  }
871                  catch (InterruptedException e) {
872 <                    threadFail("should not be interrupted");
872 >                    threadUnexpectedException();
873                  }
874              }
875          });
# Line 683 | Line 878 | public class ArrayBlockingQueueTest exte
878  
879      }
880  
881 +    /**
882 +     * poll retrieves elements across Executor threads
883 +     */
884      public void testPollInExecutor() {
687
885          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
689
886          ExecutorService executor = Executors.newFixedThreadPool(2);
691
887          executor.execute(new Runnable() {
888              public void run() {
889                  threadAssertNull(q.poll());
# Line 697 | Line 892 | public class ArrayBlockingQueueTest exte
892                      threadAssertTrue(q.isEmpty());
893                  }
894                  catch (InterruptedException e) {
895 <                    threadFail("should not be interrupted");
895 >                    threadUnexpectedException();
896                  }
897              }
898          });
# Line 709 | Line 904 | public class ArrayBlockingQueueTest exte
904                      q.put(one);
905                  }
906                  catch (InterruptedException e) {
907 <                    threadFail("should not be interrupted");
907 >                    threadUnexpectedException();
908                  }
909              }
910          });
911          
912          joinPool(executor);
718
913      }
914  
915 +    /**
916 +     * A deserialized serialized queue has same elements in same order
917 +     */
918      public void testSerialization() {
919          ArrayBlockingQueue q = populatedQueue(SIZE);
920  
# Line 734 | Line 931 | public class ArrayBlockingQueueTest exte
931              while (!q.isEmpty())
932                  assertEquals(q.remove(), r.remove());
933          } catch(Exception e){
934 <            fail("unexpected exception");
934 >            unexpectedException();
935 >        }
936 >    }
937 >
938 >    /**
939 >     * drainTo(null) throws NPE
940 >     */
941 >    public void testDrainToNull() {
942 >        ArrayBlockingQueue q = populatedQueue(SIZE);
943 >        try {
944 >            q.drainTo(null);
945 >            shouldThrow();
946 >        } catch(NullPointerException success) {
947 >        }
948 >    }
949 >
950 >    /**
951 >     * drainTo(this) throws IAE
952 >     */
953 >    public void testDrainToSelf() {
954 >        ArrayBlockingQueue q = populatedQueue(SIZE);
955 >        try {
956 >            q.drainTo(q);
957 >            shouldThrow();
958 >        } catch(IllegalArgumentException success) {
959 >        }
960 >    }
961 >
962 >    /**
963 >     * drainTo(c) empties queue into another collection c
964 >     */
965 >    public void testDrainTo() {
966 >        ArrayBlockingQueue q = populatedQueue(SIZE);
967 >        ArrayList l = new ArrayList();
968 >        q.drainTo(l);
969 >        assertEquals(q.size(), 0);
970 >        assertEquals(l.size(), SIZE);
971 >        for (int i = 0; i < SIZE; ++i)
972 >            assertEquals(l.get(i), new Integer(i));
973 >    }
974 >
975 >    /**
976 >     * drainTo empties full queue, unblocking a waiting put.
977 >     */
978 >    public void testDrainToWithActivePut() {
979 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
980 >        Thread t = new Thread(new Runnable() {
981 >                public void run() {
982 >                    try {
983 >                        q.put(new Integer(SIZE+1));
984 >                    } catch (InterruptedException ie){
985 >                        threadUnexpectedException();
986 >                    }
987 >                }
988 >            });
989 >        try {
990 >            t.start();
991 >            ArrayList l = new ArrayList();
992 >            q.drainTo(l);
993 >            assertTrue(l.size() >= SIZE);
994 >            for (int i = 0; i < SIZE; ++i)
995 >                assertEquals(l.get(i), new Integer(i));
996 >            t.join();
997 >            assertTrue(q.size() + l.size() == SIZE+1);
998 >        } catch(Exception e){
999 >            unexpectedException();
1000 >        }
1001 >    }
1002 >
1003 >    /**
1004 >     * drainTo(null, n) throws NPE
1005 >     */
1006 >    public void testDrainToNullN() {
1007 >        ArrayBlockingQueue q = populatedQueue(SIZE);
1008 >        try {
1009 >            q.drainTo(null, 0);
1010 >            shouldThrow();
1011 >        } catch(NullPointerException success) {
1012 >        }
1013 >    }
1014 >
1015 >    /**
1016 >     * drainTo(this, n) throws IAE
1017 >     */
1018 >    public void testDrainToSelfN() {
1019 >        ArrayBlockingQueue q = populatedQueue(SIZE);
1020 >        try {
1021 >            q.drainTo(q, 0);
1022 >            shouldThrow();
1023 >        } catch(IllegalArgumentException success) {
1024 >        }
1025 >    }
1026 >
1027 >    /**
1028 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
1029 >     */
1030 >    public void testDrainToN() {
1031 >        for (int i = 0; i < SIZE + 2; ++i) {
1032 >            ArrayBlockingQueue q = populatedQueue(SIZE);
1033 >            ArrayList l = new ArrayList();
1034 >            q.drainTo(l, i);
1035 >            int k = (i < SIZE)? i : SIZE;
1036 >            assertEquals(q.size(), SIZE-k);
1037 >            assertEquals(l.size(), k);
1038 >            for (int j = 0; j < k; ++j)
1039 >                assertEquals(l.get(j), new Integer(j));
1040          }
1041      }
1042  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines