--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2004/10/31 14:55:14 1.11 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/21 02:33:20 1.16 @@ -2,22 +2,23 @@ * 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. */ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; 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); + return new TestSuite(ArrayBlockingQueueTest.class); } /** @@ -27,14 +28,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++) - assertTrue(q.offer(new Integer(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()); + assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity */ @@ -155,22 +156,22 @@ public class ArrayBlockingQueueTest exte * offer(null) throws NPE */ public void testOfferNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** * add(null) throws NPE */ public void testAddNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -186,15 +187,15 @@ public class ArrayBlockingQueueTest exte * add succeeds if not full; throws ISE if full */ public void testAdd() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.add(new Integer(i))); } assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + } catch (IllegalStateException success) { + } } /** @@ -285,15 +286,15 @@ public class ArrayBlockingQueueTest exte * put(null) throws NPE */ public void testPutNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); q.put(null); shouldThrow(); - } - catch (NullPointerException success){ - } + } + catch (NullPointerException success) { + } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -311,7 +312,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.remainingCapacity()); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -330,18 +331,18 @@ 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(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -362,8 +363,8 @@ public class ArrayBlockingQueueTest exte ++added; q.put(new Object()); ++added; - threadShouldThrow(); - } catch (InterruptedException e){ + threadShouldThrow(); + } catch (InterruptedException e) { threadAssertTrue(added >= 2); } } @@ -374,7 +375,7 @@ public class ArrayBlockingQueueTest exte q.take(); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -389,19 +390,19 @@ public class ArrayBlockingQueueTest exte try { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} + threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); + } catch (InterruptedException success) {} } }); - + try { t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -410,14 +411,14 @@ public class ArrayBlockingQueueTest exte * take retrieves elements in FIFO order */ public void testTake() { - try { + try { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.take()).intValue()); } - } catch (InterruptedException e){ - unexpectedException(); - } + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -429,8 +430,8 @@ public class ArrayBlockingQueueTest exte public void run() { try { q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ } + threadShouldThrow(); + } catch (InterruptedException success) { } } }); try { @@ -438,7 +439,7 @@ public class ArrayBlockingQueueTest exte Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -456,17 +457,17 @@ 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(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -479,7 +480,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll()).intValue()); } - assertNull(q.poll()); + assertNull(q.poll()); } /** @@ -489,12 +490,12 @@ public class ArrayBlockingQueueTest exte try { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + assertNull(q.poll(0, MILLISECONDS)); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -504,12 +505,12 @@ public class ArrayBlockingQueueTest exte try { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -522,20 +523,20 @@ public class ArrayBlockingQueueTest exte try { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + } catch (InterruptedException success) { + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -548,23 +549,23 @@ public class ArrayBlockingQueueTest exte Thread t = new Thread(new Runnable() { public void run() { try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); + } catch (InterruptedException success) { } } }); try { t.start(); Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } - } + } /** @@ -578,7 +579,7 @@ public class ArrayBlockingQueueTest exte assertTrue(q.peek() == null || i != ((Integer)q.peek()).intValue()); } - assertNull(q.peek()); + assertNull(q.peek()); } /** @@ -608,8 +609,8 @@ public class ArrayBlockingQueueTest exte try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) { + } } /** @@ -626,7 +627,7 @@ public class ArrayBlockingQueueTest exte } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -709,13 +710,13 @@ public class ArrayBlockingQueueTest exte */ public void testToArray() { ArrayBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - try { - for(int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Object[] o = q.toArray(); + try { + for (int i = 0; i < o.length; i++) + assertEquals(o[i], q.take()); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -723,52 +724,52 @@ public class ArrayBlockingQueueTest exte */ public void testToArray2() { ArrayBlockingQueue q = populatedQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); - try { - for(int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Integer[] ints = new Integer[SIZE]; + ints = (Integer[])q.toArray(ints); + try { + for (int i = 0; i < ints.length; i++) + assertEquals(ints[i], q.take()); + } catch (InterruptedException e) { + unexpectedException(); + } } /** * toArray(null) throws NPE */ public void testToArray_BadArg() { - try { + try { ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch(NullPointerException success){} + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { - try { + try { ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); - shouldThrow(); - } catch(ArrayStoreException success){} + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch (ArrayStoreException success) {} } - + /** * iterator iterates through all elements */ public void testIterator() { ArrayBlockingQueue q = populatedQueue(SIZE); - Iterator it = q.iterator(); - try { - while(it.hasNext()){ - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + Iterator it = q.iterator(); + try { + while (it.hasNext()) { + assertEquals(it.next(), q.take()); + } + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -783,7 +784,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 +840,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -854,7 +855,7 @@ public class ArrayBlockingQueueTest exte public void run() { threadAssertFalse(q.offer(three)); try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); threadAssertEquals(0, q.remainingCapacity()); } catch (InterruptedException e) { @@ -874,7 +875,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -889,7 +890,7 @@ public class ArrayBlockingQueueTest exte public void run() { threadAssertNull(q.poll()); try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); threadAssertTrue(q.isEmpty()); } catch (InterruptedException e) { @@ -909,7 +910,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -929,47 +930,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 +981,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 +1004,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) ; }