35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
< |
* |
38 |
> |
* A new queue has the indicated capacity |
39 |
|
*/ |
40 |
|
public void testConstructor1() { |
41 |
|
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* |
45 |
> |
* Constructor throws IAE if capacity argument nonpositive |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
|
try { |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* |
56 |
> |
* Initializing from null Collection throws NPE |
57 |
|
*/ |
58 |
|
public void testConstructor3() { |
59 |
– |
|
59 |
|
try { |
60 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
61 |
|
shouldThrow(); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* |
67 |
> |
* Initializing from Collection of null elements throws NPE |
68 |
|
*/ |
69 |
|
public void testConstructor4() { |
70 |
|
try { |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
< |
* |
79 |
> |
* Initializing from Collection with some null elements throws NPE |
80 |
|
*/ |
81 |
|
public void testConstructor5() { |
82 |
|
try { |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* |
93 |
> |
* Initializing from too large collection throws IAE |
94 |
|
*/ |
95 |
|
public void testConstructor6() { |
96 |
|
try { |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* |
107 |
> |
* Queue contains all elements of collection used to initialize |
108 |
|
*/ |
109 |
|
public void testConstructor7() { |
110 |
|
try { |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* |
122 |
> |
* Queue transitions from empty to full when elements added |
123 |
|
*/ |
124 |
|
public void testEmptyFull() { |
125 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* |
137 |
> |
* remainingCapacity decreases on add, increases on remove |
138 |
|
*/ |
139 |
|
public void testRemainingCapacity() { |
140 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* |
154 |
> |
* offer(null) throws NPE |
155 |
|
*/ |
156 |
|
public void testOfferNull() { |
157 |
|
try { |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* |
165 |
> |
* Offer succeeds if not full; fails if full |
166 |
|
*/ |
167 |
|
public void testOffer() { |
168 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* |
174 |
> |
* add succeeds if not full; throws ISE if full |
175 |
|
*/ |
176 |
|
public void testAdd() { |
177 |
|
try { |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* |
189 |
> |
* addAll(null) throws NPE |
190 |
|
*/ |
191 |
|
public void testAddAll1() { |
192 |
|
try { |
197 |
|
catch (NullPointerException success) {} |
198 |
|
} |
199 |
|
/** |
200 |
< |
* |
200 |
> |
* addAll of a collection with null elements throws NPE |
201 |
|
*/ |
202 |
|
public void testAddAll2() { |
203 |
|
try { |
209 |
|
catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
/** |
212 |
< |
* |
212 |
> |
* addAll of a collection with any null elements throws NPE after |
213 |
> |
* possibly adding some elements |
214 |
|
*/ |
215 |
|
public void testAddAll3() { |
216 |
|
try { |
224 |
|
catch (NullPointerException success) {} |
225 |
|
} |
226 |
|
/** |
227 |
< |
* |
227 |
> |
* addAll throws ISE if not enough room |
228 |
|
*/ |
229 |
|
public void testAddAll4() { |
230 |
|
try { |
238 |
|
catch (IllegalStateException success) {} |
239 |
|
} |
240 |
|
/** |
241 |
< |
* |
241 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
242 |
|
*/ |
243 |
|
public void testAddAll5() { |
244 |
|
try { |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* |
259 |
> |
* put(null) throws NPE |
260 |
|
*/ |
261 |
|
public void testPutNull() { |
262 |
|
try { |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
< |
* |
275 |
> |
* all elements successfully put are contained |
276 |
|
*/ |
277 |
|
public void testPut() { |
278 |
|
try { |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
< |
* |
293 |
> |
* put blocks interruptibly if full |
294 |
|
*/ |
295 |
|
public void testBlockingPut() { |
296 |
|
Thread t = new Thread(new Runnable() { |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* |
323 |
> |
* put blocks waiting for take when full |
324 |
|
*/ |
325 |
|
public void testPutWithTake() { |
326 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
< |
* |
357 |
> |
* timed offer times out if full and elements not taken |
358 |
|
*/ |
359 |
|
public void testTimedOffer() { |
360 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
< |
* |
384 |
> |
* take retrieves elements in FIFO order |
385 |
|
*/ |
386 |
|
public void testTake() { |
387 |
|
try { |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* |
398 |
> |
* take blocks interruptibly when empty |
399 |
|
*/ |
400 |
|
public void testTakeFromEmpty() { |
401 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* |
421 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
422 |
|
*/ |
423 |
|
public void testBlockingTake() { |
424 |
|
Thread t = new Thread(new Runnable() { |
446 |
|
|
447 |
|
|
448 |
|
/** |
449 |
< |
* |
449 |
> |
* poll succeeds unless empty |
450 |
|
*/ |
451 |
|
public void testPoll() { |
452 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* |
460 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
461 |
|
*/ |
462 |
|
public void testTimedPoll0() { |
463 |
|
try { |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
< |
* |
475 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
476 |
|
*/ |
477 |
|
public void testTimedPoll() { |
478 |
|
try { |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* |
490 |
> |
* Interrupted timed poll throws InterruptedException instead of |
491 |
> |
* returning timeout status |
492 |
|
*/ |
493 |
|
public void testInterruptedTimedPoll() { |
494 |
|
Thread t = new Thread(new Runnable() { |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
< |
* |
517 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
518 |
> |
* on interruption throws |
519 |
|
*/ |
520 |
|
public void testTimedPollWithOffer() { |
521 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
542 |
|
|
543 |
|
|
544 |
|
/** |
545 |
< |
* |
545 |
> |
* peek returns next element, or null if empty |
546 |
|
*/ |
547 |
|
public void testPeek() { |
548 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* |
559 |
> |
* element returns next element, or throws NSEE if empty |
560 |
|
*/ |
561 |
|
public void testElement() { |
562 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
< |
* |
575 |
> |
* remove removes next element, or throws NSEE if empty |
576 |
|
*/ |
577 |
|
public void testRemove() { |
578 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* |
590 |
> |
* remove(x) removes x and returns true if present |
591 |
|
*/ |
592 |
|
public void testRemoveElement() { |
593 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
< |
* |
605 |
> |
* contains(x) reports true when elements added but not yet removed |
606 |
|
*/ |
607 |
|
public void testContains() { |
608 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* |
617 |
> |
* clear removes all elements |
618 |
|
*/ |
619 |
|
public void testClear() { |
620 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
< |
* |
632 |
> |
* containsAll(c) is true when c contains a subset of elements |
633 |
|
*/ |
634 |
|
public void testContainsAll() { |
635 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* |
646 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
647 |
|
*/ |
648 |
|
public void testRetainAll() { |
649 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* |
665 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
666 |
|
*/ |
667 |
|
public void testRemoveAll() { |
668 |
|
for (int i = 1; i < SIZE; ++i) { |
677 |
|
} |
678 |
|
} |
679 |
|
|
678 |
– |
|
680 |
|
/** |
681 |
< |
* |
681 |
> |
* toArray contains all elements |
682 |
|
*/ |
683 |
|
public void testToArray() { |
684 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
< |
* |
695 |
> |
* toArray(a) contains all elements |
696 |
|
*/ |
697 |
|
public void testToArray2() { |
698 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* |
710 |
> |
* iterator iterates through all elements |
711 |
|
*/ |
712 |
|
public void testIterator() { |
713 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
722 |
|
} |
723 |
|
|
724 |
|
/** |
725 |
< |
* |
725 |
> |
* iterator.remove removes current element |
726 |
> |
*/ |
727 |
> |
public void testIteratorRemove () { |
728 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
729 |
> |
q.add(two); |
730 |
> |
q.add(one); |
731 |
> |
q.add(three); |
732 |
> |
|
733 |
> |
Iterator it = q.iterator(); |
734 |
> |
it.next(); |
735 |
> |
it.remove(); |
736 |
> |
|
737 |
> |
it = q.iterator(); |
738 |
> |
assertEquals(it.next(), one); |
739 |
> |
assertEquals(it.next(), three); |
740 |
> |
assertFalse(it.hasNext()); |
741 |
> |
} |
742 |
> |
|
743 |
> |
/** |
744 |
> |
* iterator ordering is FIFO |
745 |
|
*/ |
746 |
|
public void testIteratorOrdering() { |
747 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
760 |
|
} |
761 |
|
|
762 |
|
/** |
763 |
< |
* |
763 |
> |
* Modifications do not cause iterators to fail |
764 |
|
*/ |
765 |
|
public void testWeaklyConsistentIteration () { |
766 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
776 |
|
catch (ConcurrentModificationException e) { |
777 |
|
unexpectedException(); |
778 |
|
} |
759 |
– |
|
779 |
|
assertEquals(0, q.size()); |
780 |
|
} |
781 |
|
|
782 |
|
|
783 |
|
/** |
784 |
< |
* |
784 |
> |
* toString contains toStrings of elements |
785 |
|
*/ |
786 |
|
public void testToString() { |
787 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
793 |
|
|
794 |
|
|
795 |
|
/** |
796 |
< |
* |
796 |
> |
* offer transfers elements across Executor tasks |
797 |
|
*/ |
798 |
|
public void testOfferInExecutor() { |
780 |
– |
|
799 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
782 |
– |
|
800 |
|
q.add(one); |
801 |
|
q.add(two); |
785 |
– |
|
802 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
787 |
– |
|
803 |
|
executor.execute(new Runnable() { |
804 |
|
public void run() { |
805 |
|
threadAssertFalse(q.offer(three)); |
830 |
|
} |
831 |
|
|
832 |
|
/** |
833 |
< |
* |
833 |
> |
* poll retrieves elements across Executor threads |
834 |
|
*/ |
835 |
|
public void testPollInExecutor() { |
821 |
– |
|
836 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
823 |
– |
|
837 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
825 |
– |
|
838 |
|
executor.execute(new Runnable() { |
839 |
|
public void run() { |
840 |
|
threadAssertNull(q.poll()); |
861 |
|
}); |
862 |
|
|
863 |
|
joinPool(executor); |
852 |
– |
|
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
< |
* |
867 |
> |
* A deserialized serialized queue has same elements in same order |
868 |
|
*/ |
869 |
|
public void testSerialization() { |
870 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |