--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/16 05:30:07 1.14 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/21 02:33:20 1.16 @@ -10,14 +10,15 @@ 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,11 +28,11 @@ 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; } @@ -155,7 +156,7 @@ public class ArrayBlockingQueueTest exte * offer(null) throws NPE */ public void testOfferNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); @@ -166,7 +167,7 @@ public class ArrayBlockingQueueTest exte * add(null) throws NPE */ public void testAddNull() { - try { + try { ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); @@ -186,7 +187,7 @@ 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))); @@ -194,7 +195,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } 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 (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -311,7 +312,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.remainingCapacity()); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -341,7 +342,7 @@ public class ArrayBlockingQueueTest exte t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -362,7 +363,7 @@ public class ArrayBlockingQueueTest exte ++added; q.put(new Object()); ++added; - threadShouldThrow(); + threadShouldThrow(); } catch (InterruptedException e) { threadAssertTrue(added >= 2); } @@ -389,9 +390,9 @@ 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(); + threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); } catch (InterruptedException success) {} } }); @@ -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(); - } + unexpectedException(); + } } /** @@ -429,7 +430,7 @@ public class ArrayBlockingQueueTest exte public void run() { try { q.take(); - threadShouldThrow(); + threadShouldThrow(); } catch (InterruptedException success) { } } }); @@ -466,7 +467,7 @@ public class ArrayBlockingQueueTest exte 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)); + assertNull(q.poll(0, MILLISECONDS)); } catch (InterruptedException e) { - unexpectedException(); - } + 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)); + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } catch (InterruptedException e) { - unexpectedException(); - } + unexpectedException(); + } } /** @@ -522,9 +523,9 @@ 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)); + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } catch (InterruptedException success) { } }}); @@ -535,7 +536,7 @@ public class ArrayBlockingQueueTest exte t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -548,17 +549,17 @@ 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(); + 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) { @@ -578,7 +579,7 @@ public class ArrayBlockingQueueTest exte assertTrue(q.peek() == null || i != ((Integer)q.peek()).intValue()); } - assertNull(q.peek()); + assertNull(q.peek()); } /** @@ -609,7 +610,7 @@ public class ArrayBlockingQueueTest exte q.remove(); shouldThrow(); } catch (NoSuchElementException success) { - } + } } /** @@ -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,36 +724,36 @@ 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) {} } @@ -761,14 +762,14 @@ public class ArrayBlockingQueueTest exte */ 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(); + } } /** @@ -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) { @@ -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) {