--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2004/10/31 14:55:14 1.11 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/16 05:30:07 1.14 @@ -2,8 +2,8 @@ * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -14,7 +14,7 @@ import java.io.*; public class ArrayBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(ArrayBlockingQueueTest.class); @@ -27,14 +27,14 @@ public class ArrayBlockingQueueTest exte private ArrayBlockingQueue populatedQueue(int n) { ArrayBlockingQueue q = new ArrayBlockingQueue(n); assertTrue(q.isEmpty()); - for(int i = 0; i < n; i++) + for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity */ @@ -159,7 +159,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -170,7 +170,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -193,8 +193,8 @@ public class ArrayBlockingQueueTest exte } assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + } catch (IllegalStateException success) { + } } /** @@ -289,9 +289,9 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); q.put(null); shouldThrow(); - } - catch (NullPointerException success){ - } + } + catch (NullPointerException success) { + } catch (InterruptedException ie) { unexpectedException(); } @@ -330,13 +330,13 @@ public class ArrayBlockingQueueTest exte } q.put(new Integer(SIZE)); threadShouldThrow(); - } catch (InterruptedException ie){ + } catch (InterruptedException ie) { threadAssertEquals(added, SIZE); - } + } }}); - try { + try { t.start(); - Thread.sleep(MEDIUM_DELAY_MS); + Thread.sleep(MEDIUM_DELAY_MS); t.interrupt(); t.join(); } @@ -363,7 +363,7 @@ public class ArrayBlockingQueueTest exte q.put(new Object()); ++added; threadShouldThrow(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { threadAssertTrue(added >= 2); } } @@ -374,7 +374,7 @@ public class ArrayBlockingQueueTest exte q.take(); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -392,16 +392,16 @@ public class ArrayBlockingQueueTest exte threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success){} + } catch (InterruptedException success) {} } }); - + try { t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -415,9 +415,9 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.take()).intValue()); } - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -430,7 +430,7 @@ public class ArrayBlockingQueueTest exte try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success) { } } }); try { @@ -438,7 +438,7 @@ public class ArrayBlockingQueueTest exte Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -456,12 +456,12 @@ public class ArrayBlockingQueueTest exte } q.take(); threadShouldThrow(); - } catch (InterruptedException success){ - } + } catch (InterruptedException success) { + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -492,9 +492,9 @@ public class ArrayBlockingQueueTest exte assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); } assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -507,9 +507,9 @@ public class ArrayBlockingQueueTest exte assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); } assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -525,12 +525,12 @@ public class ArrayBlockingQueueTest exte threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } + } catch (InterruptedException success) { + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -552,7 +552,7 @@ public class ArrayBlockingQueueTest exte q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -561,10 +561,10 @@ public class ArrayBlockingQueueTest exte assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } - } + } /** @@ -608,8 +608,8 @@ public class ArrayBlockingQueueTest exte try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) { + } } /** @@ -626,7 +626,7 @@ public class ArrayBlockingQueueTest exte } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -711,11 +711,11 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); try { - for(int i = 0; i < o.length; i++) + for (int i = 0; i < o.length; i++) assertEquals(o[i], q.take()); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -726,11 +726,11 @@ public class ArrayBlockingQueueTest exte Integer[] ints = new Integer[SIZE]; ints = (Integer[])q.toArray(ints); try { - for(int i = 0; i < ints.length; i++) + for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -741,7 +741,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch(NullPointerException success){} + } catch (NullPointerException success) {} } /** @@ -752,10 +752,10 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(new String[10] ); shouldThrow(); - } catch(ArrayStoreException success){} + } catch (ArrayStoreException success) {} } - + /** * iterator iterates through all elements */ @@ -763,12 +763,12 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); try { - while(it.hasNext()){ + while (it.hasNext()) { assertEquals(it.next(), q.take()); } - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } } /** @@ -783,7 +783,7 @@ public class ArrayBlockingQueueTest exte Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -839,7 +839,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -874,7 +874,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -909,7 +909,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -929,47 +929,47 @@ public class ArrayBlockingQueueTest exte ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); assertEquals(q.size(), r.size()); - while (!q.isEmpty()) + while (!q.isEmpty()) assertEquals(q.remove(), r.remove()); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); } } /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null); shouldThrow(); - } catch(NullPointerException success) { + } catch (NullPointerException success) { } } /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q); shouldThrow(); - } catch(IllegalArgumentException success) { + } catch (IllegalArgumentException success) { } } /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { ArrayBlockingQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(q.size(), 0); assertEquals(l.size(), SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); q.add(zero); q.add(one); @@ -980,20 +980,20 @@ public class ArrayBlockingQueueTest exte q.drainTo(l); assertEquals(q.size(), 0); assertEquals(l.size(), 2); - for (int i = 0; i < 2; ++i) + for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } /** * drainTo empties full queue, unblocking a waiting put. - */ + */ public void testDrainToWithActivePut() { final ArrayBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new Runnable() { public void run() { try { q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie){ + } catch (InterruptedException ie) { threadUnexpectedException(); } } @@ -1003,53 +1003,53 @@ public class ArrayBlockingQueueTest exte ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); t.join(); assertTrue(q.size() + l.size() >= SIZE); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); } } /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null, 0); shouldThrow(); - } catch(NullPointerException success) { + } catch (NullPointerException success) { } } /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q, 0); shouldThrow(); - } catch(IllegalArgumentException success) { + } catch (IllegalArgumentException success) { } } /** * drainTo(c, n) empties first max {n, size} elements of queue into c - */ + */ public void testDrainToN() { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); for (int i = 0; i < SIZE + 2; ++i) { - for(int j = 0; j < SIZE; j++) + for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE)? i : SIZE; assertEquals(l.size(), k); assertEquals(q.size(), SIZE-k); - for (int j = 0; j < k; ++j) + for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); while (q.poll() != null) ; }