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.12 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.28 by jsr166, Tue Dec 1 09:56:28 2009 UTC

# Line 10 | Line 10
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.io.*;
15  
16   public class ArrayBlockingQueueTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run (suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ArrayBlockingQueueTest.class);
21 >        return new TestSuite(ArrayBlockingQueueTest.class);
22      }
23  
24      /**
# Line 27 | Line 28 | public class ArrayBlockingQueueTest exte
28      private ArrayBlockingQueue populatedQueue(int n) {
29          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
30          assertTrue(q.isEmpty());
31 <        for(int i = 0; i < n; i++)
32 <            assertTrue(q.offer(new Integer(i)));
31 >        for (int i = 0; i < n; i++)
32 >            assertTrue(q.offer(new Integer(i)));
33          assertFalse(q.isEmpty());
34          assertEquals(0, q.remainingCapacity());
35 <        assertEquals(n, q.size());
35 >        assertEquals(n, q.size());
36          return q;
37      }
38  
# Line 43 | Line 44 | public class ArrayBlockingQueueTest exte
44      }
45  
46      /**
47 <     * Constructor throws IAE if  capacity argument nonpositive
47 >     * Constructor throws IAE if capacity argument nonpositive
48       */
49      public void testConstructor2() {
50          try {
51              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
52              shouldThrow();
53 <        }
53 <        catch (IllegalArgumentException success) {}
53 >        } catch (IllegalArgumentException success) {}
54      }
55  
56      /**
# Line 60 | Line 60 | public class ArrayBlockingQueueTest exte
60          try {
61              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
62              shouldThrow();
63 <        }
64 <        catch (NullPointerException success) {}
63 >        } catch (NullPointerException success) {}
64      }
65  
66      /**
# Line 72 | Line 71 | public class ArrayBlockingQueueTest exte
71              Integer[] ints = new Integer[SIZE];
72              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
73              shouldThrow();
74 <        }
76 <        catch (NullPointerException success) {}
74 >        } catch (NullPointerException success) {}
75      }
76  
77      /**
# Line 86 | Line 84 | public class ArrayBlockingQueueTest exte
84                  ints[i] = new Integer(i);
85              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
86              shouldThrow();
87 <        }
90 <        catch (NullPointerException success) {}
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 100 | Line 97 | public class ArrayBlockingQueueTest exte
97                  ints[i] = new Integer(i);
98              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
99              shouldThrow();
100 <        }
104 <        catch (IllegalArgumentException success) {}
100 >        } catch (IllegalArgumentException success) {}
101      }
102  
103      /**
104       * Queue contains all elements of collection used to initialize
105       */
106      public void testConstructor7() {
107 <        try {
108 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE; ++i)
110 <                ints[i] = new Integer(i);
111 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
112 <            for (int i = 0; i < SIZE; ++i)
117 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
107 >        Integer[] ints = new Integer[SIZE];
108 >        for (int i = 0; i < SIZE; ++i)
109 >            ints[i] = new Integer(i);
110 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
111 >        for (int i = 0; i < SIZE; ++i)
112 >            assertEquals(ints[i], q.poll());
113      }
114  
115      /**
# Line 155 | Line 148 | public class ArrayBlockingQueueTest exte
148       *  offer(null) throws NPE
149       */
150      public void testOfferNull() {
151 <        try {
151 >        try {
152              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
153              q.offer(null);
154              shouldThrow();
155 <        } catch (NullPointerException success) { }
155 >        } catch (NullPointerException success) {}
156      }
157  
158      /**
159       *  add(null) throws NPE
160       */
161      public void testAddNull() {
162 <        try {
162 >        try {
163              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
164              q.add(null);
165              shouldThrow();
166 <        } catch (NullPointerException success) { }
166 >        } catch (NullPointerException success) {}
167      }
168  
169      /**
# Line 186 | Line 179 | public class ArrayBlockingQueueTest exte
179       * add succeeds if not full; throws ISE if full
180       */
181      public void testAdd() {
182 <        try {
182 >        try {
183              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
184              for (int i = 0; i < SIZE; ++i) {
185                  assertTrue(q.add(new Integer(i)));
186              }
187              assertEquals(0, q.remainingCapacity());
188              q.add(new Integer(SIZE));
189 <        } catch (IllegalStateException success){
190 <        }
189 >            shouldThrow();
190 >        } catch (IllegalStateException success) {}
191      }
192  
193      /**
# Line 205 | Line 198 | public class ArrayBlockingQueueTest exte
198              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
199              q.addAll(null);
200              shouldThrow();
201 <        }
209 <        catch (NullPointerException success) {}
201 >        } catch (NullPointerException success) {}
202      }
203  
204      /**
# Line 217 | Line 209 | public class ArrayBlockingQueueTest exte
209              ArrayBlockingQueue q = populatedQueue(SIZE);
210              q.addAll(q);
211              shouldThrow();
212 <        }
221 <        catch (IllegalArgumentException success) {}
212 >        } catch (IllegalArgumentException success) {}
213      }
214  
215  
# Line 231 | Line 222 | public class ArrayBlockingQueueTest exte
222              Integer[] ints = new Integer[SIZE];
223              q.addAll(Arrays.asList(ints));
224              shouldThrow();
225 <        }
235 <        catch (NullPointerException success) {}
225 >        } catch (NullPointerException success) {}
226      }
227      /**
228       * addAll of a collection with any null elements throws NPE after
# Line 246 | Line 236 | public class ArrayBlockingQueueTest exte
236                  ints[i] = new Integer(i);
237              q.addAll(Arrays.asList(ints));
238              shouldThrow();
239 <        }
250 <        catch (NullPointerException success) {}
239 >        } catch (NullPointerException success) {}
240      }
241      /**
242       * addAll throws ISE if not enough room
# Line 260 | Line 249 | public class ArrayBlockingQueueTest exte
249                  ints[i] = new Integer(i);
250              q.addAll(Arrays.asList(ints));
251              shouldThrow();
252 <        }
264 <        catch (IllegalStateException success) {}
252 >        } catch (IllegalStateException success) {}
253      }
254      /**
255       * Queue contains all elements, in traversal order, of successful addAll
256       */
257      public void testAddAll5() {
258 <        try {
259 <            Integer[] empty = new Integer[0];
260 <            Integer[] ints = new Integer[SIZE];
261 <            for (int i = 0; i < SIZE; ++i)
262 <                ints[i] = new Integer(i);
263 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
264 <            assertFalse(q.addAll(Arrays.asList(empty)));
265 <            assertTrue(q.addAll(Arrays.asList(ints)));
266 <            for (int i = 0; i < SIZE; ++i)
279 <                assertEquals(ints[i], q.poll());
280 <        }
281 <        finally {}
258 >        Integer[] empty = new Integer[0];
259 >        Integer[] ints = new Integer[SIZE];
260 >        for (int i = 0; i < SIZE; ++i)
261 >            ints[i] = new Integer(i);
262 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
263 >        assertFalse(q.addAll(Arrays.asList(empty)));
264 >        assertTrue(q.addAll(Arrays.asList(ints)));
265 >        for (int i = 0; i < SIZE; ++i)
266 >            assertEquals(ints[i], q.poll());
267      }
268  
269      /**
270       *  put(null) throws NPE
271       */
272 <     public void testPutNull() {
273 <        try {
272 >    public void testPutNull() throws InterruptedException {
273 >        try {
274              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
275              q.put(null);
276              shouldThrow();
277 <        }
293 <        catch (NullPointerException success){
294 <        }
295 <        catch (InterruptedException ie) {
296 <            unexpectedException();
297 <        }
277 >        } catch (NullPointerException success) {}
278       }
279  
280      /**
281       * all elements successfully put are contained
282       */
283 <     public void testPut() {
284 <         try {
285 <             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
286 <             for (int i = 0; i < SIZE; ++i) {
287 <                 Integer I = new Integer(i);
288 <                 q.put(I);
309 <                 assertTrue(q.contains(I));
310 <             }
311 <             assertEquals(0, q.remainingCapacity());
312 <         }
313 <        catch (InterruptedException ie) {
314 <            unexpectedException();
283 >    public void testPut() throws InterruptedException {
284 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
285 >        for (int i = 0; i < SIZE; ++i) {
286 >            Integer I = new Integer(i);
287 >            q.put(I);
288 >            assertTrue(q.contains(I));
289          }
290 +        assertEquals(0, q.remainingCapacity());
291      }
292  
293      /**
294       * put blocks interruptibly if full
295       */
296 <    public void testBlockingPut() {
296 >    public void testBlockingPut() throws InterruptedException {
297          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
298 <        Thread t = new Thread(new Runnable() {
299 <                public void run() {
300 <                    int added = 0;
301 <                    try {
302 <                        for (int i = 0; i < SIZE; ++i) {
303 <                            q.put(new Integer(i));
304 <                            ++added;
305 <                        }
306 <                        q.put(new Integer(SIZE));
307 <                        threadShouldThrow();
308 <                    } catch (InterruptedException ie){
309 <                        threadAssertEquals(added, SIZE);
310 <                    }
311 <                }});
312 <        try {
313 <            t.start();
314 <           Thread.sleep(MEDIUM_DELAY_MS);
315 <           t.interrupt();
341 <           t.join();
342 <        }
343 <        catch (InterruptedException ie) {
344 <            unexpectedException();
345 <        }
298 >        Thread t = new Thread(new CheckedRunnable() {
299 >            public void realRun() throws InterruptedException {
300 >                for (int i = 0; i < SIZE; ++i)
301 >                    q.put(i);
302 >                assertEquals(SIZE, q.size());
303 >                assertEquals(0, q.remainingCapacity());
304 >                try {
305 >                    q.put(99);
306 >                    shouldThrow();
307 >                } catch (InterruptedException success) {}
308 >            }});
309 >
310 >        t.start();
311 >        Thread.sleep(SHORT_DELAY_MS);
312 >        t.interrupt();
313 >        t.join();
314 >        assertEquals(SIZE, q.size());
315 >        assertEquals(0, q.remainingCapacity());
316      }
317  
318      /**
319       * put blocks waiting for take when full
320       */
321 <    public void testPutWithTake() {
322 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
323 <        Thread t = new Thread(new Runnable() {
324 <                public void run() {
325 <                    int added = 0;
326 <                    try {
327 <                        q.put(new Object());
328 <                        ++added;
329 <                        q.put(new Object());
330 <                        ++added;
331 <                        q.put(new Object());
332 <                        ++added;
333 <                        q.put(new Object());
334 <                        ++added;
335 <                        threadShouldThrow();
336 <                    } catch (InterruptedException e){
337 <                        threadAssertTrue(added >= 2);
338 <                    }
339 <                }
340 <            });
341 <        try {
372 <            t.start();
373 <            Thread.sleep(SHORT_DELAY_MS);
374 <            q.take();
375 <            t.interrupt();
376 <            t.join();
377 <        } catch (Exception e){
378 <            unexpectedException();
379 <        }
321 >    public void testPutWithTake() throws InterruptedException {
322 >        final int capacity = 2;
323 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
324 >        Thread t = new Thread(new CheckedRunnable() {
325 >            public void realRun() throws InterruptedException {
326 >                for (int i = 0; i < capacity + 1; i++)
327 >                    q.put(i);
328 >                try {
329 >                    q.put(99);
330 >                    shouldThrow();
331 >                } catch (InterruptedException success) {}
332 >            }});
333 >
334 >        t.start();
335 >        Thread.sleep(SHORT_DELAY_MS);
336 >        assertEquals(q.remainingCapacity(), 0);
337 >        assertEquals(0, q.take());
338 >        Thread.sleep(SHORT_DELAY_MS);
339 >        t.interrupt();
340 >        t.join();
341 >        assertEquals(q.remainingCapacity(), 0);
342      }
343  
344      /**
345       * timed offer times out if full and elements not taken
346       */
347 <    public void testTimedOffer() {
347 >    public void testTimedOffer() throws InterruptedException {
348          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
349 <        Thread t = new Thread(new Runnable() {
350 <                public void run() {
351 <                    try {
352 <                        q.put(new Object());
353 <                        q.put(new Object());
354 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
355 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                        threadShouldThrow();
357 <                    } catch (InterruptedException success){}
358 <                }
359 <            });
360 <
361 <        try {
362 <            t.start();
363 <            Thread.sleep(SHORT_DELAY_MS);
402 <            t.interrupt();
403 <            t.join();
404 <        } catch (Exception e){
405 <            unexpectedException();
406 <        }
349 >        Thread t = new Thread(new CheckedRunnable() {
350 >            public void realRun() throws InterruptedException {
351 >                q.put(new Object());
352 >                q.put(new Object());
353 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
354 >                try {
355 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
356 >                    shouldThrow();
357 >                } catch (InterruptedException success) {}
358 >            }});
359 >
360 >        t.start();
361 >        Thread.sleep(SHORT_DELAY_MS);
362 >        t.interrupt();
363 >        t.join();
364      }
365  
366      /**
367       * take retrieves elements in FIFO order
368       */
369 <    public void testTake() {
370 <        try {
371 <            ArrayBlockingQueue q = populatedQueue(SIZE);
372 <            for (int i = 0; i < SIZE; ++i) {
373 <                assertEquals(i, ((Integer)q.take()).intValue());
417 <            }
418 <        } catch (InterruptedException e){
419 <            unexpectedException();
420 <        }
369 >    public void testTake() throws InterruptedException {
370 >        ArrayBlockingQueue q = populatedQueue(SIZE);
371 >        for (int i = 0; i < SIZE; ++i) {
372 >            assertEquals(i, q.take());
373 >        }
374      }
375  
376      /**
377       * take blocks interruptibly when empty
378       */
379 <    public void testTakeFromEmpty() {
379 >    public void testTakeFromEmpty() throws InterruptedException {
380          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
381 <        Thread t = new Thread(new Runnable() {
382 <                public void run() {
383 <                    try {
384 <                        q.take();
385 <                        threadShouldThrow();
386 <                    } catch (InterruptedException success){ }
387 <                }
388 <            });
389 <        try {
437 <            t.start();
438 <            Thread.sleep(SHORT_DELAY_MS);
439 <            t.interrupt();
440 <            t.join();
441 <        } catch (Exception e){
442 <            unexpectedException();
443 <        }
381 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
382 >            public void realRun() throws InterruptedException {
383 >                q.take();
384 >            }};
385 >
386 >        t.start();
387 >        Thread.sleep(SHORT_DELAY_MS);
388 >        t.interrupt();
389 >        t.join();
390      }
391  
392      /**
393       * Take removes existing elements until empty, then blocks interruptibly
394       */
395 <    public void testBlockingTake() {
396 <        Thread t = new Thread(new Runnable() {
397 <                public void run() {
398 <                    try {
399 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
400 <                        for (int i = 0; i < SIZE; ++i) {
401 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
402 <                        }
403 <                        q.take();
404 <                        threadShouldThrow();
405 <                    } catch (InterruptedException success){
406 <                    }
407 <                }});
408 <        try {
409 <            t.start();
410 <            Thread.sleep(SHORT_DELAY_MS);
411 <            t.interrupt();
466 <            t.join();
467 <        }
468 <        catch (InterruptedException ie) {
469 <            unexpectedException();
470 <        }
395 >    public void testBlockingTake() throws InterruptedException {
396 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
397 >        Thread t = new Thread(new CheckedRunnable() {
398 >            public void realRun() throws InterruptedException {
399 >                for (int i = 0; i < SIZE; ++i) {
400 >                    assertEquals(i, q.take());
401 >                }
402 >                try {
403 >                    q.take();
404 >                    shouldThrow();
405 >                } catch (InterruptedException success) {}
406 >            }});
407 >
408 >        t.start();
409 >        Thread.sleep(SHORT_DELAY_MS);
410 >        t.interrupt();
411 >        t.join();
412      }
413  
414  
# Line 477 | Line 418 | public class ArrayBlockingQueueTest exte
418      public void testPoll() {
419          ArrayBlockingQueue q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer)q.poll()).intValue());
421 >            assertEquals(i, q.poll());
422          }
423 <        assertNull(q.poll());
423 >        assertNull(q.poll());
424      }
425  
426      /**
427       * timed pool with zero timeout succeeds when non-empty, else times out
428       */
429 <    public void testTimedPoll0() {
430 <        try {
431 <            ArrayBlockingQueue q = populatedQueue(SIZE);
432 <            for (int i = 0; i < SIZE; ++i) {
433 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
434 <            }
494 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495 <        } catch (InterruptedException e){
496 <            unexpectedException();
497 <        }
429 >    public void testTimedPoll0() throws InterruptedException {
430 >        ArrayBlockingQueue q = populatedQueue(SIZE);
431 >        for (int i = 0; i < SIZE; ++i) {
432 >            assertEquals(i, q.poll(0, MILLISECONDS));
433 >        }
434 >        assertNull(q.poll(0, MILLISECONDS));
435      }
436  
437      /**
438       * timed pool with nonzero timeout succeeds when non-empty, else times out
439       */
440 <    public void testTimedPoll() {
441 <        try {
442 <            ArrayBlockingQueue q = populatedQueue(SIZE);
443 <            for (int i = 0; i < SIZE; ++i) {
444 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
445 <            }
509 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510 <        } catch (InterruptedException e){
511 <            unexpectedException();
512 <        }
440 >    public void testTimedPoll() throws InterruptedException {
441 >        ArrayBlockingQueue q = populatedQueue(SIZE);
442 >        for (int i = 0; i < SIZE; ++i) {
443 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
444 >        }
445 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
446      }
447  
448      /**
449       * Interrupted timed poll throws InterruptedException instead of
450       * returning timeout status
451       */
452 <    public void testInterruptedTimedPoll() {
453 <        Thread t = new Thread(new Runnable() {
454 <                public void run() {
455 <                    try {
456 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
457 <                        for (int i = 0; i < SIZE; ++i) {
458 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
459 <                        }
460 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
461 <                    } catch (InterruptedException success){
462 <                    }
463 <                }});
464 <        try {
465 <            t.start();
466 <            Thread.sleep(SHORT_DELAY_MS);
467 <            t.interrupt();
468 <            t.join();
536 <        }
537 <        catch (InterruptedException ie) {
538 <            unexpectedException();
539 <        }
452 >    public void testInterruptedTimedPoll() throws InterruptedException {
453 >        Thread t = new Thread(new CheckedRunnable() {
454 >            public void realRun() throws InterruptedException {
455 >                ArrayBlockingQueue q = populatedQueue(SIZE);
456 >                for (int i = 0; i < SIZE; ++i) {
457 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
458 >                }
459 >                try {
460 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
461 >                    shouldThrow();
462 >                } catch (InterruptedException success) {}
463 >            }});
464 >
465 >        t.start();
466 >        Thread.sleep(SHORT_DELAY_MS);
467 >        t.interrupt();
468 >        t.join();
469      }
470  
471      /**
472       *  timed poll before a delayed offer fails; after offer succeeds;
473       *  on interruption throws
474       */
475 <    public void testTimedPollWithOffer() {
475 >    public void testTimedPollWithOffer() throws InterruptedException {
476          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
477 <        Thread t = new Thread(new Runnable() {
478 <                public void run() {
479 <                    try {
480 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
481 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
482 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
483 <                        threadShouldThrow();
484 <                    } catch (InterruptedException success) { }
485 <                }
486 <            });
487 <        try {
488 <            t.start();
489 <            Thread.sleep(SMALL_DELAY_MS);
490 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
491 <            t.interrupt();
563 <            t.join();
564 <        } catch (Exception e){
565 <            unexpectedException();
566 <        }
477 >        Thread t = new Thread(new CheckedRunnable() {
478 >            public void realRun() throws InterruptedException {
479 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
480 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
481 >                try {
482 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
483 >                    shouldThrow();
484 >                } catch (InterruptedException success) {}
485 >            }});
486 >
487 >        t.start();
488 >        Thread.sleep(SMALL_DELAY_MS);
489 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
490 >        t.interrupt();
491 >        t.join();
492      }
493  
494  
# Line 573 | Line 498 | public class ArrayBlockingQueueTest exte
498      public void testPeek() {
499          ArrayBlockingQueue q = populatedQueue(SIZE);
500          for (int i = 0; i < SIZE; ++i) {
501 <            assertEquals(i, ((Integer)q.peek()).intValue());
502 <            q.poll();
501 >            assertEquals(i, q.peek());
502 >            assertEquals(i, q.poll());
503              assertTrue(q.peek() == null ||
504 <                       i != ((Integer)q.peek()).intValue());
504 >                       !q.peek().equals(i));
505          }
506 <        assertNull(q.peek());
506 >        assertNull(q.peek());
507      }
508  
509      /**
# Line 587 | Line 512 | public class ArrayBlockingQueueTest exte
512      public void testElement() {
513          ArrayBlockingQueue q = populatedQueue(SIZE);
514          for (int i = 0; i < SIZE; ++i) {
515 <            assertEquals(i, ((Integer)q.element()).intValue());
516 <            q.poll();
515 >            assertEquals(i, q.element());
516 >            assertEquals(i, q.poll());
517          }
518          try {
519              q.element();
520              shouldThrow();
521 <        }
597 <        catch (NoSuchElementException success) {}
521 >        } catch (NoSuchElementException success) {}
522      }
523  
524      /**
# Line 603 | Line 527 | public class ArrayBlockingQueueTest exte
527      public void testRemove() {
528          ArrayBlockingQueue q = populatedQueue(SIZE);
529          for (int i = 0; i < SIZE; ++i) {
530 <            assertEquals(i, ((Integer)q.remove()).intValue());
530 >            assertEquals(i, q.remove());
531          }
532          try {
533              q.remove();
534              shouldThrow();
535 <        } catch (NoSuchElementException success){
612 <        }
535 >        } catch (NoSuchElementException success) {}
536      }
537  
538      /**
# Line 634 | Line 557 | public class ArrayBlockingQueueTest exte
557          ArrayBlockingQueue q = populatedQueue(SIZE);
558          for (int i = 0; i < SIZE; ++i) {
559              assertTrue(q.contains(new Integer(i)));
560 <            q.poll();
560 >            assertEquals(i, q.poll());
561              assertFalse(q.contains(new Integer(i)));
562          }
563      }
# Line 707 | Line 630 | public class ArrayBlockingQueueTest exte
630      /**
631       *  toArray contains all elements
632       */
633 <    public void testToArray() {
633 >    public void testToArray() throws InterruptedException {
634          ArrayBlockingQueue q = populatedQueue(SIZE);
635 <        Object[] o = q.toArray();
636 <        try {
637 <        for(int i = 0; i < o.length; i++)
715 <            assertEquals(o[i], q.take());
716 <        } catch (InterruptedException e){
717 <            unexpectedException();
718 <        }
635 >        Object[] o = q.toArray();
636 >        for (int i = 0; i < o.length; i++)
637 >            assertEquals(o[i], q.take());
638      }
639  
640      /**
641       * toArray(a) contains all elements
642       */
643 <    public void testToArray2() {
643 >    public void testToArray2() throws InterruptedException {
644          ArrayBlockingQueue q = populatedQueue(SIZE);
645 <        Integer[] ints = new Integer[SIZE];
646 <        ints = (Integer[])q.toArray(ints);
647 <        try {
648 <            for(int i = 0; i < ints.length; i++)
730 <                assertEquals(ints[i], q.take());
731 <        } catch (InterruptedException e){
732 <            unexpectedException();
733 <        }
645 >        Integer[] ints = new Integer[SIZE];
646 >        ints = (Integer[])q.toArray(ints);
647 >        for (int i = 0; i < ints.length; i++)
648 >            assertEquals(ints[i], q.take());
649      }
650  
651      /**
652       * toArray(null) throws NPE
653       */
654      public void testToArray_BadArg() {
655 <        try {
656 <            ArrayBlockingQueue q = populatedQueue(SIZE);
657 <            Object o[] = q.toArray(null);
658 <            shouldThrow();
659 <        } catch(NullPointerException success){}
655 >        ArrayBlockingQueue q = populatedQueue(SIZE);
656 >        try {
657 >            Object o[] = q.toArray(null);
658 >            shouldThrow();
659 >        } catch (NullPointerException success) {}
660      }
661  
662      /**
663       * toArray with incompatible array type throws CCE
664       */
665      public void testToArray1_BadArg() {
666 <        try {
667 <            ArrayBlockingQueue q = populatedQueue(SIZE);
668 <            Object o[] = q.toArray(new String[10] );
669 <            shouldThrow();
670 <        } catch(ArrayStoreException  success){}
666 >        ArrayBlockingQueue q = populatedQueue(SIZE);
667 >        try {
668 >            Object o[] = q.toArray(new String[10]);
669 >            shouldThrow();
670 >        } catch (ArrayStoreException success) {}
671      }
672  
673  
674      /**
675       * iterator iterates through all elements
676       */
677 <    public void testIterator() {
677 >    public void testIterator() throws InterruptedException {
678          ArrayBlockingQueue q = populatedQueue(SIZE);
679 <        Iterator it = q.iterator();
680 <        try {
681 <            while(it.hasNext()){
682 <                assertEquals(it.next(), q.take());
768 <            }
769 <        } catch (InterruptedException e){
770 <            unexpectedException();
771 <        }
679 >        Iterator it = q.iterator();
680 >        while (it.hasNext()) {
681 >            assertEquals(it.next(), q.take());
682 >        }
683      }
684  
685      /**
# Line 785 | Line 696 | public class ArrayBlockingQueueTest exte
696          it.remove();
697  
698          it = q.iterator();
699 <        assertEquals(it.next(), one);
700 <        assertEquals(it.next(), three);
699 >        assertSame(it.next(), one);
700 >        assertSame(it.next(), three);
701          assertFalse(it.hasNext());
702      }
703  
# Line 803 | Line 714 | public class ArrayBlockingQueueTest exte
714  
715          int k = 0;
716          for (Iterator it = q.iterator(); it.hasNext();) {
717 <            int i = ((Integer)(it.next())).intValue();
807 <            assertEquals(++k, i);
717 >            assertEquals(++k, it.next());
718          }
719          assertEquals(3, k);
720      }
# Line 817 | Line 727 | public class ArrayBlockingQueueTest exte
727          q.add(one);
728          q.add(two);
729          q.add(three);
730 <        try {
731 <            for (Iterator it = q.iterator(); it.hasNext();) {
732 <                q.remove();
823 <                it.next();
824 <            }
825 <        }
826 <        catch (ConcurrentModificationException e) {
827 <            unexpectedException();
730 >        for (Iterator it = q.iterator(); it.hasNext();) {
731 >            q.remove();
732 >            it.next();
733          }
734          assertEquals(0, q.size());
735      }
# Line 850 | Line 755 | public class ArrayBlockingQueueTest exte
755          q.add(one);
756          q.add(two);
757          ExecutorService executor = Executors.newFixedThreadPool(2);
758 <        executor.execute(new Runnable() {
759 <            public void run() {
760 <                threadAssertFalse(q.offer(three));
761 <                try {
762 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
763 <                    threadAssertEquals(0, q.remainingCapacity());
764 <                }
765 <                catch (InterruptedException e) {
766 <                    threadUnexpectedException();
767 <                }
768 <            }
769 <        });
865 <
866 <        executor.execute(new Runnable() {
867 <            public void run() {
868 <                try {
869 <                    Thread.sleep(SMALL_DELAY_MS);
870 <                    threadAssertEquals(one, q.take());
871 <                }
872 <                catch (InterruptedException e) {
873 <                    threadUnexpectedException();
874 <                }
875 <            }
876 <        });
758 >        executor.execute(new CheckedRunnable() {
759 >            public void realRun() throws InterruptedException {
760 >                assertFalse(q.offer(three));
761 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
762 >                assertEquals(0, q.remainingCapacity());
763 >            }});
764 >
765 >        executor.execute(new CheckedRunnable() {
766 >            public void realRun() throws InterruptedException {
767 >                Thread.sleep(SMALL_DELAY_MS);
768 >                assertSame(one, q.take());
769 >            }});
770  
771          joinPool(executor);
879
772      }
773  
774      /**
# Line 885 | Line 777 | public class ArrayBlockingQueueTest exte
777      public void testPollInExecutor() {
778          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
779          ExecutorService executor = Executors.newFixedThreadPool(2);
780 <        executor.execute(new Runnable() {
781 <            public void run() {
782 <                threadAssertNull(q.poll());
783 <                try {
784 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
785 <                    threadAssertTrue(q.isEmpty());
786 <                }
787 <                catch (InterruptedException e) {
788 <                    threadUnexpectedException();
789 <                }
790 <            }
791 <        });
900 <
901 <        executor.execute(new Runnable() {
902 <            public void run() {
903 <                try {
904 <                    Thread.sleep(SMALL_DELAY_MS);
905 <                    q.put(one);
906 <                }
907 <                catch (InterruptedException e) {
908 <                    threadUnexpectedException();
909 <                }
910 <            }
911 <        });
780 >        executor.execute(new CheckedRunnable() {
781 >            public void realRun() throws InterruptedException {
782 >                assertNull(q.poll());
783 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
784 >                assertTrue(q.isEmpty());
785 >            }});
786 >
787 >        executor.execute(new CheckedRunnable() {
788 >            public void realRun() throws InterruptedException {
789 >                Thread.sleep(SMALL_DELAY_MS);
790 >                q.put(one);
791 >            }});
792  
793          joinPool(executor);
794      }
# Line 916 | Line 796 | public class ArrayBlockingQueueTest exte
796      /**
797       * A deserialized serialized queue has same elements in same order
798       */
799 <    public void testSerialization() {
799 >    public void testSerialization() throws Exception {
800          ArrayBlockingQueue q = populatedQueue(SIZE);
801  
802 <        try {
803 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
804 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
805 <            out.writeObject(q);
806 <            out.close();
807 <
808 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
809 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
810 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
811 <            assertEquals(q.size(), r.size());
812 <            while (!q.isEmpty())
933 <                assertEquals(q.remove(), r.remove());
934 <        } catch(Exception e){
935 <            unexpectedException();
936 <        }
802 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
803 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
804 >        out.writeObject(q);
805 >        out.close();
806 >
807 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
808 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
809 >        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
810 >        assertEquals(q.size(), r.size());
811 >        while (!q.isEmpty())
812 >            assertEquals(q.remove(), r.remove());
813      }
814  
815      /**
# Line 944 | Line 820 | public class ArrayBlockingQueueTest exte
820          try {
821              q.drainTo(null);
822              shouldThrow();
823 <        } catch(NullPointerException success) {
948 <        }
823 >        } catch (NullPointerException success) {}
824      }
825  
826      /**
# Line 956 | Line 831 | public class ArrayBlockingQueueTest exte
831          try {
832              q.drainTo(q);
833              shouldThrow();
834 <        } catch(IllegalArgumentException success) {
960 <        }
834 >        } catch (IllegalArgumentException success) {}
835      }
836  
837      /**
# Line 987 | Line 861 | public class ArrayBlockingQueueTest exte
861      /**
862       * drainTo empties full queue, unblocking a waiting put.
863       */
864 <    public void testDrainToWithActivePut() {
864 >    public void testDrainToWithActivePut() throws InterruptedException {
865          final ArrayBlockingQueue q = populatedQueue(SIZE);
866 <        Thread t = new Thread(new Runnable() {
867 <                public void run() {
868 <                    try {
869 <                        q.put(new Integer(SIZE+1));
870 <                    } catch (InterruptedException ie){
871 <                        threadUnexpectedException();
872 <                    }
873 <                }
874 <            });
875 <        try {
876 <            t.start();
877 <            ArrayList l = new ArrayList();
878 <            q.drainTo(l);
1005 <            assertTrue(l.size() >= SIZE);
1006 <            for (int i = 0; i < SIZE; ++i)
1007 <                assertEquals(l.get(i), new Integer(i));
1008 <            t.join();
1009 <            assertTrue(q.size() + l.size() >= SIZE);
1010 <        } catch(Exception e){
1011 <            unexpectedException();
1012 <        }
866 >        Thread t = new Thread(new CheckedRunnable() {
867 >            public void realRun() throws InterruptedException {
868 >                q.put(new Integer(SIZE+1));
869 >            }});
870 >
871 >        t.start();
872 >        ArrayList l = new ArrayList();
873 >        q.drainTo(l);
874 >        assertTrue(l.size() >= SIZE);
875 >        for (int i = 0; i < SIZE; ++i)
876 >            assertEquals(l.get(i), new Integer(i));
877 >        t.join();
878 >        assertTrue(q.size() + l.size() >= SIZE);
879      }
880  
881      /**
# Line 1020 | Line 886 | public class ArrayBlockingQueueTest exte
886          try {
887              q.drainTo(null, 0);
888              shouldThrow();
889 <        } catch(NullPointerException success) {
1024 <        }
889 >        } catch (NullPointerException success) {}
890      }
891  
892      /**
# Line 1032 | Line 897 | public class ArrayBlockingQueueTest exte
897          try {
898              q.drainTo(q, 0);
899              shouldThrow();
900 <        } catch(IllegalArgumentException success) {
1036 <        }
900 >        } catch (IllegalArgumentException success) {}
901      }
902  
903      /**
# Line 1042 | Line 906 | public class ArrayBlockingQueueTest exte
906      public void testDrainToN() {
907          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
908          for (int i = 0; i < SIZE + 2; ++i) {
909 <            for(int j = 0; j < SIZE; j++)
909 >            for (int j = 0; j < SIZE; j++)
910                  assertTrue(q.offer(new Integer(j)));
911              ArrayList l = new ArrayList();
912              q.drainTo(l, i);
# Line 1055 | Line 919 | public class ArrayBlockingQueueTest exte
919          }
920      }
921  
1058
922   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines