ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +230 -93 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 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.
6     */
7    
8 dl 1.4
9 dl 1.1 import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 dl 1.2 import java.io.*;
13 dl 1.1
14 dl 1.3 public class ArrayBlockingQueueTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(ArrayBlockingQueueTest.class);
20     }
21    
22     /**
23     * Create a queue of given size containing consecutive
24     * Integers 0 ... n.
25     */
26 dl 1.3 private ArrayBlockingQueue populatedQueue(int n) {
27 dl 1.1 ArrayBlockingQueue q = new ArrayBlockingQueue(n);
28     assertTrue(q.isEmpty());
29     for(int i = 0; i < n; i++)
30     assertTrue(q.offer(new Integer(i)));
31     assertFalse(q.isEmpty());
32     assertEquals(0, q.remainingCapacity());
33     assertEquals(n, q.size());
34     return q;
35     }
36    
37 dl 1.4 /**
38     *
39     */
40     public void testConstructor1() {
41 dl 1.3 assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
42 dl 1.1 }
43    
44 dl 1.4 /**
45     *
46     */
47     public void testConstructor2() {
48 dl 1.1 try {
49     ArrayBlockingQueue q = new ArrayBlockingQueue(0);
50 dl 1.4 shouldThrow();
51 dl 1.1 }
52     catch (IllegalArgumentException success) {}
53     }
54    
55 dl 1.4 /**
56     *
57     */
58     public void testConstructor3() {
59 dl 1.1
60     try {
61     ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
62 dl 1.4 shouldThrow();
63 dl 1.1 }
64     catch (NullPointerException success) {}
65     }
66    
67 dl 1.4 /**
68     *
69     */
70     public void testConstructor4() {
71 dl 1.1 try {
72 dl 1.3 Integer[] ints = new Integer[SIZE];
73     ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
74 dl 1.4 shouldThrow();
75 dl 1.1 }
76     catch (NullPointerException success) {}
77     }
78    
79 dl 1.4 /**
80     *
81     */
82     public void testConstructor5() {
83 dl 1.1 try {
84 dl 1.3 Integer[] ints = new Integer[SIZE];
85     for (int i = 0; i < SIZE-1; ++i)
86 dl 1.1 ints[i] = new Integer(i);
87 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
88 dl 1.4 shouldThrow();
89 dl 1.1 }
90     catch (NullPointerException success) {}
91     }
92    
93 dl 1.4 /**
94     *
95     */
96     public void testConstructor6() {
97 dl 1.1 try {
98 dl 1.3 Integer[] ints = new Integer[SIZE];
99     for (int i = 0; i < SIZE; ++i)
100 dl 1.1 ints[i] = new Integer(i);
101     ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
102 dl 1.4 shouldThrow();
103 dl 1.1 }
104     catch (IllegalArgumentException success) {}
105     }
106    
107 dl 1.4 /**
108     *
109     */
110     public void testConstructor7() {
111 dl 1.1 try {
112 dl 1.3 Integer[] ints = new Integer[SIZE];
113     for (int i = 0; i < SIZE; ++i)
114 dl 1.1 ints[i] = new Integer(i);
115 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
116     for (int i = 0; i < SIZE; ++i)
117 dl 1.1 assertEquals(ints[i], q.poll());
118     }
119     finally {}
120     }
121    
122 dl 1.4 /**
123     *
124     */
125 dl 1.1 public void testEmptyFull() {
126     ArrayBlockingQueue q = new ArrayBlockingQueue(2);
127     assertTrue(q.isEmpty());
128 dl 1.4 assertEquals(2, q.remainingCapacity());
129 dl 1.3 q.add(one);
130 dl 1.1 assertFalse(q.isEmpty());
131 dl 1.3 q.add(two);
132 dl 1.1 assertFalse(q.isEmpty());
133 dl 1.4 assertEquals(0, q.remainingCapacity());
134     assertFalse(q.offer(three));
135 dl 1.1 }
136    
137 dl 1.4 /**
138     *
139     */
140     public void testRemainingCapacity() {
141 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
142     for (int i = 0; i < SIZE; ++i) {
143 dl 1.1 assertEquals(i, q.remainingCapacity());
144 dl 1.3 assertEquals(SIZE-i, q.size());
145 dl 1.1 q.remove();
146     }
147 dl 1.3 for (int i = 0; i < SIZE; ++i) {
148     assertEquals(SIZE-i, q.remainingCapacity());
149 dl 1.1 assertEquals(i, q.size());
150     q.add(new Integer(i));
151     }
152     }
153    
154 dl 1.4 /**
155     *
156     */
157     public void testOfferNull() {
158 dl 1.1 try {
159     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160     q.offer(null);
161 dl 1.4 shouldThrow();
162 dl 1.1 } catch (NullPointerException success) { }
163     }
164    
165 dl 1.4 /**
166     *
167     */
168     public void testOffer() {
169 dl 1.1 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
170 dl 1.3 assertTrue(q.offer(zero));
171     assertFalse(q.offer(one));
172 dl 1.1 }
173    
174 dl 1.4 /**
175     *
176     */
177     public void testAdd() {
178 dl 1.1 try {
179 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
180     for (int i = 0; i < SIZE; ++i) {
181 dl 1.1 assertTrue(q.add(new Integer(i)));
182     }
183     assertEquals(0, q.remainingCapacity());
184 dl 1.3 q.add(new Integer(SIZE));
185 dl 1.1 } catch (IllegalStateException success){
186     }
187     }
188    
189 dl 1.4 /**
190     *
191     */
192     public void testAddAll1() {
193 dl 1.1 try {
194     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
195     q.addAll(null);
196 dl 1.4 shouldThrow();
197 dl 1.1 }
198     catch (NullPointerException success) {}
199     }
200 dl 1.4 /**
201     *
202     */
203     public void testAddAll2() {
204 dl 1.1 try {
205 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
206     Integer[] ints = new Integer[SIZE];
207 dl 1.1 q.addAll(Arrays.asList(ints));
208 dl 1.4 shouldThrow();
209 dl 1.1 }
210     catch (NullPointerException success) {}
211     }
212 dl 1.4 /**
213     *
214     */
215     public void testAddAll3() {
216 dl 1.1 try {
217 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
218     Integer[] ints = new Integer[SIZE];
219     for (int i = 0; i < SIZE-1; ++i)
220 dl 1.1 ints[i] = new Integer(i);
221     q.addAll(Arrays.asList(ints));
222 dl 1.4 shouldThrow();
223 dl 1.1 }
224     catch (NullPointerException success) {}
225     }
226 dl 1.4 /**
227     *
228     */
229     public void testAddAll4() {
230 dl 1.1 try {
231     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
232 dl 1.3 Integer[] ints = new Integer[SIZE];
233     for (int i = 0; i < SIZE; ++i)
234 dl 1.1 ints[i] = new Integer(i);
235     q.addAll(Arrays.asList(ints));
236 dl 1.4 shouldThrow();
237 dl 1.1 }
238     catch (IllegalStateException success) {}
239     }
240 dl 1.4 /**
241     *
242     */
243     public void testAddAll5() {
244 dl 1.1 try {
245     Integer[] empty = new Integer[0];
246 dl 1.3 Integer[] ints = new Integer[SIZE];
247     for (int i = 0; i < SIZE; ++i)
248 dl 1.1 ints[i] = new Integer(i);
249 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
250 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
251     assertTrue(q.addAll(Arrays.asList(ints)));
252 dl 1.3 for (int i = 0; i < SIZE; ++i)
253 dl 1.1 assertEquals(ints[i], q.poll());
254     }
255     finally {}
256     }
257    
258 dl 1.4 /**
259     *
260     */
261 dl 1.1 public void testPutNull() {
262     try {
263 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
264 dl 1.1 q.put(null);
265 dl 1.4 shouldThrow();
266 dl 1.1 }
267     catch (NullPointerException success){
268     }
269     catch (InterruptedException ie) {
270 dl 1.4 unexpectedException();
271 dl 1.1 }
272     }
273    
274 dl 1.4 /**
275     *
276     */
277 dl 1.1 public void testPut() {
278     try {
279 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
280     for (int i = 0; i < SIZE; ++i) {
281 dl 1.1 Integer I = new Integer(i);
282     q.put(I);
283     assertTrue(q.contains(I));
284     }
285     assertEquals(0, q.remainingCapacity());
286     }
287     catch (InterruptedException ie) {
288 dl 1.4 unexpectedException();
289 dl 1.1 }
290     }
291    
292 dl 1.4 /**
293     *
294     */
295     public void testBlockingPut() {
296 dl 1.1 Thread t = new Thread(new Runnable() {
297     public void run() {
298     int added = 0;
299     try {
300 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
301     for (int i = 0; i < SIZE; ++i) {
302 dl 1.1 q.put(new Integer(i));
303     ++added;
304     }
305 dl 1.3 q.put(new Integer(SIZE));
306 dl 1.4 threadShouldThrow();
307 dl 1.1 } catch (InterruptedException ie){
308 dl 1.3 threadAssertEquals(added, SIZE);
309 dl 1.1 }
310     }});
311     try {
312 dl 1.3 t.start();
313 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
314     t.interrupt();
315     t.join();
316     }
317     catch (InterruptedException ie) {
318 dl 1.4 unexpectedException();
319 dl 1.1 }
320     }
321    
322 dl 1.4 /**
323     *
324     */
325 dl 1.1 public void testPutWithTake() {
326     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
327     Thread t = new Thread(new Runnable() {
328 dl 1.4 public void run() {
329 dl 1.1 int added = 0;
330     try {
331     q.put(new Object());
332     ++added;
333     q.put(new Object());
334     ++added;
335     q.put(new Object());
336     ++added;
337     q.put(new Object());
338     ++added;
339 dl 1.4 threadShouldThrow();
340 dl 1.1 } catch (InterruptedException e){
341 dl 1.3 threadAssertTrue(added >= 2);
342 dl 1.1 }
343     }
344     });
345     try {
346     t.start();
347     Thread.sleep(SHORT_DELAY_MS);
348     q.take();
349     t.interrupt();
350     t.join();
351     } catch (Exception e){
352 dl 1.4 unexpectedException();
353 dl 1.1 }
354     }
355    
356 dl 1.4 /**
357     *
358     */
359 dl 1.1 public void testTimedOffer() {
360     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
361     Thread t = new Thread(new Runnable() {
362 dl 1.4 public void run() {
363 dl 1.1 try {
364     q.put(new Object());
365     q.put(new Object());
366 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
367 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
368 dl 1.4 threadShouldThrow();
369 dl 1.1 } catch (InterruptedException success){}
370     }
371     });
372    
373     try {
374     t.start();
375     Thread.sleep(SHORT_DELAY_MS);
376     t.interrupt();
377     t.join();
378     } catch (Exception e){
379 dl 1.4 unexpectedException();
380 dl 1.1 }
381     }
382    
383 dl 1.4 /**
384     *
385     */
386     public void testTake() {
387 dl 1.1 try {
388 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
389     for (int i = 0; i < SIZE; ++i) {
390 dl 1.1 assertEquals(i, ((Integer)q.take()).intValue());
391     }
392     } catch (InterruptedException e){
393 dl 1.4 unexpectedException();
394 dl 1.1 }
395     }
396    
397 dl 1.4 /**
398     *
399     */
400 dl 1.1 public void testTakeFromEmpty() {
401     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
402     Thread t = new Thread(new Runnable() {
403 dl 1.4 public void run() {
404 dl 1.1 try {
405     q.take();
406 dl 1.4 threadShouldThrow();
407 dl 1.1 } catch (InterruptedException success){ }
408     }
409     });
410     try {
411     t.start();
412     Thread.sleep(SHORT_DELAY_MS);
413     t.interrupt();
414     t.join();
415     } catch (Exception e){
416 dl 1.4 unexpectedException();
417 dl 1.1 }
418     }
419    
420 dl 1.4 /**
421     *
422     */
423     public void testBlockingTake() {
424 dl 1.1 Thread t = new Thread(new Runnable() {
425     public void run() {
426     try {
427 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
428     for (int i = 0; i < SIZE; ++i) {
429     threadAssertEquals(i, ((Integer)q.take()).intValue());
430 dl 1.1 }
431     q.take();
432 dl 1.4 threadShouldThrow();
433 dl 1.1 } catch (InterruptedException success){
434     }
435     }});
436     try {
437 dl 1.3 t.start();
438     Thread.sleep(SHORT_DELAY_MS);
439     t.interrupt();
440     t.join();
441 dl 1.1 }
442     catch (InterruptedException ie) {
443 dl 1.4 unexpectedException();
444 dl 1.1 }
445     }
446    
447    
448 dl 1.4 /**
449     *
450     */
451     public void testPoll() {
452 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
453     for (int i = 0; i < SIZE; ++i) {
454 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
455     }
456     assertNull(q.poll());
457     }
458    
459 dl 1.4 /**
460     *
461     */
462 dl 1.1 public void testTimedPoll0() {
463     try {
464 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
465     for (int i = 0; i < SIZE; ++i) {
466 dl 1.1 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
467     }
468     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
469     } catch (InterruptedException e){
470 dl 1.4 unexpectedException();
471 dl 1.1 }
472     }
473    
474 dl 1.4 /**
475     *
476     */
477 dl 1.1 public void testTimedPoll() {
478     try {
479 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
480     for (int i = 0; i < SIZE; ++i) {
481 dl 1.1 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
482     }
483     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
484     } catch (InterruptedException e){
485 dl 1.4 unexpectedException();
486 dl 1.1 }
487     }
488    
489 dl 1.4 /**
490     *
491     */
492     public void testInterruptedTimedPoll() {
493 dl 1.1 Thread t = new Thread(new Runnable() {
494     public void run() {
495     try {
496 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
497     for (int i = 0; i < SIZE; ++i) {
498     threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
499 dl 1.1 }
500 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
501 dl 1.1 } catch (InterruptedException success){
502     }
503     }});
504     try {
505 dl 1.3 t.start();
506     Thread.sleep(SHORT_DELAY_MS);
507     t.interrupt();
508     t.join();
509 dl 1.1 }
510     catch (InterruptedException ie) {
511 dl 1.4 unexpectedException();
512 dl 1.1 }
513     }
514    
515 dl 1.4 /**
516     *
517     */
518     public void testTimedPollWithOffer() {
519 dl 1.1 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
520     Thread t = new Thread(new Runnable() {
521 dl 1.4 public void run() {
522 dl 1.1 try {
523 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
524 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
525     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
526 dl 1.4 threadShouldThrow();
527 dl 1.1 } catch (InterruptedException success) { }
528     }
529     });
530     try {
531     t.start();
532 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
533     assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
534 dl 1.1 t.interrupt();
535     t.join();
536     } catch (Exception e){
537 dl 1.4 unexpectedException();
538 dl 1.1 }
539     }
540    
541    
542 dl 1.4 /**
543     *
544     */
545     public void testPeek() {
546 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
547     for (int i = 0; i < SIZE; ++i) {
548 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
549     q.poll();
550     assertTrue(q.peek() == null ||
551     i != ((Integer)q.peek()).intValue());
552     }
553     assertNull(q.peek());
554     }
555    
556 dl 1.4 /**
557     *
558     */
559     public void testElement() {
560 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
561     for (int i = 0; i < SIZE; ++i) {
562 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
563     q.poll();
564     }
565     try {
566     q.element();
567 dl 1.4 shouldThrow();
568 dl 1.1 }
569     catch (NoSuchElementException success) {}
570     }
571    
572 dl 1.4 /**
573     *
574     */
575     public void testRemove() {
576 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
577     for (int i = 0; i < SIZE; ++i) {
578 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
579     }
580     try {
581     q.remove();
582 dl 1.4 shouldThrow();
583 dl 1.1 } catch (NoSuchElementException success){
584     }
585     }
586    
587 dl 1.4 /**
588     *
589     */
590     public void testRemoveElement() {
591 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
592     for (int i = 1; i < SIZE; i+=2) {
593 dl 1.1 assertTrue(q.remove(new Integer(i)));
594     }
595 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
596 dl 1.1 assertTrue(q.remove(new Integer(i)));
597     assertFalse(q.remove(new Integer(i+1)));
598     }
599 dl 1.2 assertTrue(q.isEmpty());
600 dl 1.1 }
601    
602 dl 1.4 /**
603     *
604     */
605     public void testContains() {
606 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
607     for (int i = 0; i < SIZE; ++i) {
608 dl 1.1 assertTrue(q.contains(new Integer(i)));
609     q.poll();
610     assertFalse(q.contains(new Integer(i)));
611     }
612     }
613    
614 dl 1.4 /**
615     *
616     */
617     public void testClear() {
618 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
619 dl 1.1 q.clear();
620     assertTrue(q.isEmpty());
621     assertEquals(0, q.size());
622 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
623     q.add(one);
624 dl 1.1 assertFalse(q.isEmpty());
625     q.clear();
626     assertTrue(q.isEmpty());
627     }
628    
629 dl 1.4 /**
630     *
631     */
632     public void testContainsAll() {
633 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
634     ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
635     for (int i = 0; i < SIZE; ++i) {
636 dl 1.1 assertTrue(q.containsAll(p));
637     assertFalse(p.containsAll(q));
638     p.add(new Integer(i));
639     }
640     assertTrue(p.containsAll(q));
641     }
642    
643 dl 1.4 /**
644     *
645     */
646     public void testRetainAll() {
647 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
648     ArrayBlockingQueue p = populatedQueue(SIZE);
649     for (int i = 0; i < SIZE; ++i) {
650 dl 1.1 boolean changed = q.retainAll(p);
651     if (i == 0)
652     assertFalse(changed);
653     else
654     assertTrue(changed);
655    
656     assertTrue(q.containsAll(p));
657 dl 1.3 assertEquals(SIZE-i, q.size());
658 dl 1.1 p.remove();
659     }
660     }
661    
662 dl 1.4 /**
663     *
664     */
665     public void testRemoveAll() {
666 dl 1.3 for (int i = 1; i < SIZE; ++i) {
667     ArrayBlockingQueue q = populatedQueue(SIZE);
668     ArrayBlockingQueue p = populatedQueue(i);
669 dl 1.1 assertTrue(q.removeAll(p));
670 dl 1.3 assertEquals(SIZE-i, q.size());
671 dl 1.1 for (int j = 0; j < i; ++j) {
672     Integer I = (Integer)(p.remove());
673     assertFalse(q.contains(I));
674     }
675     }
676     }
677    
678    
679 dl 1.4 /**
680     *
681     */
682     public void testToArray() {
683 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
684 dl 1.1 Object[] o = q.toArray();
685     try {
686     for(int i = 0; i < o.length; i++)
687     assertEquals(o[i], q.take());
688     } catch (InterruptedException e){
689 dl 1.4 unexpectedException();
690 dl 1.1 }
691     }
692    
693 dl 1.4 /**
694     *
695     */
696     public void testToArray2() {
697 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
698     Integer[] ints = new Integer[SIZE];
699 dl 1.1 ints = (Integer[])q.toArray(ints);
700     try {
701     for(int i = 0; i < ints.length; i++)
702     assertEquals(ints[i], q.take());
703     } catch (InterruptedException e){
704 dl 1.4 unexpectedException();
705 dl 1.1 }
706     }
707    
708 dl 1.4 /**
709     *
710     */
711     public void testIterator() {
712 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
713 dl 1.1 Iterator it = q.iterator();
714     try {
715     while(it.hasNext()){
716     assertEquals(it.next(), q.take());
717     }
718     } catch (InterruptedException e){
719 dl 1.4 unexpectedException();
720 dl 1.1 }
721     }
722    
723 dl 1.4 /**
724     *
725     */
726 dl 1.1 public void testIteratorOrdering() {
727     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
728 dl 1.3 q.add(one);
729     q.add(two);
730     q.add(three);
731 dl 1.1
732     assertEquals("queue should be full", 0, q.remainingCapacity());
733    
734     int k = 0;
735     for (Iterator it = q.iterator(); it.hasNext();) {
736     int i = ((Integer)(it.next())).intValue();
737 dl 1.4 assertEquals(++k, i);
738 dl 1.1 }
739 dl 1.4 assertEquals(3, k);
740 dl 1.1 }
741    
742 dl 1.4 /**
743     *
744     */
745 dl 1.1 public void testWeaklyConsistentIteration () {
746     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
747 dl 1.3 q.add(one);
748     q.add(two);
749     q.add(three);
750 dl 1.1 try {
751     for (Iterator it = q.iterator(); it.hasNext();) {
752     q.remove();
753     it.next();
754     }
755     }
756     catch (ConcurrentModificationException e) {
757 dl 1.4 unexpectedException();
758 dl 1.1 }
759    
760 dl 1.4 assertEquals(0, q.size());
761 dl 1.1 }
762    
763    
764 dl 1.4 /**
765     *
766     */
767     public void testToString() {
768 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
769 dl 1.1 String s = q.toString();
770 dl 1.3 for (int i = 0; i < SIZE; ++i) {
771 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
772     }
773     }
774    
775    
776 dl 1.4 /**
777     *
778     */
779 dl 1.1 public void testOfferInExecutor() {
780    
781     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
782    
783 dl 1.3 q.add(one);
784     q.add(two);
785 dl 1.1
786     ExecutorService executor = Executors.newFixedThreadPool(2);
787    
788     executor.execute(new Runnable() {
789     public void run() {
790 dl 1.3 threadAssertFalse(q.offer(three));
791 dl 1.1 try {
792 dl 1.3 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
793     threadAssertEquals(0, q.remainingCapacity());
794 dl 1.1 }
795     catch (InterruptedException e) {
796 dl 1.4 threadUnexpectedException();
797 dl 1.1 }
798     }
799     });
800    
801     executor.execute(new Runnable() {
802     public void run() {
803     try {
804 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
805     threadAssertEquals(one, q.take());
806 dl 1.1 }
807     catch (InterruptedException e) {
808 dl 1.4 threadUnexpectedException();
809 dl 1.1 }
810     }
811     });
812    
813 dl 1.3 joinPool(executor);
814 dl 1.1
815     }
816    
817 dl 1.4 /**
818     *
819     */
820 dl 1.1 public void testPollInExecutor() {
821    
822     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
823    
824     ExecutorService executor = Executors.newFixedThreadPool(2);
825    
826     executor.execute(new Runnable() {
827     public void run() {
828 dl 1.3 threadAssertNull(q.poll());
829 dl 1.1 try {
830 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
831     threadAssertTrue(q.isEmpty());
832 dl 1.1 }
833     catch (InterruptedException e) {
834 dl 1.4 threadUnexpectedException();
835 dl 1.1 }
836     }
837     });
838    
839     executor.execute(new Runnable() {
840     public void run() {
841     try {
842 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
843     q.put(one);
844 dl 1.1 }
845     catch (InterruptedException e) {
846 dl 1.4 threadUnexpectedException();
847 dl 1.1 }
848     }
849     });
850    
851 dl 1.3 joinPool(executor);
852 dl 1.1
853     }
854 dl 1.2
855 dl 1.4 /**
856     *
857     */
858 dl 1.2 public void testSerialization() {
859 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
860 dl 1.2
861     try {
862     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
863     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
864     out.writeObject(q);
865     out.close();
866    
867     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
868     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
869     ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
870     assertEquals(q.size(), r.size());
871     while (!q.isEmpty())
872     assertEquals(q.remove(), r.remove());
873     } catch(Exception e){
874 dl 1.4 unexpectedException();
875 dl 1.2 }
876     }
877    
878 dl 1.1
879     }