--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2009/11/21 02:07:27 1.16 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2010/09/29 12:33:48 1.28 @@ -9,11 +9,12 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; public class PriorityBlockingQueueTest 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(PriorityBlockingQueueTest.class); @@ -24,11 +25,7 @@ public class PriorityBlockingQueueTest e /** Sample Comparator */ static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { - int i = ((Integer)x).intValue(); - int j = ((Integer)y).intValue(); - if (i < j) return 1; - if (i > j) return -1; - return 0; + return ((Comparable)y).compareTo(x); } } @@ -57,7 +54,7 @@ public class PriorityBlockingQueueTest e } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -248,6 +245,7 @@ public class PriorityBlockingQueueTest e shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -327,16 +325,13 @@ public class PriorityBlockingQueueTest e */ public void testTimedOffer() throws InterruptedException { final PriorityBlockingQueue q = new PriorityBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Integer(0)); - q.put(new Integer(0)); - threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - } finally { } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + q.put(new Integer(0)); + q.put(new Integer(0)); + assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -350,7 +345,7 @@ public class PriorityBlockingQueueTest e public void testTake() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } } @@ -374,13 +369,16 @@ public class PriorityBlockingQueueTest e * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { + final PriorityBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } - q.take(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); @@ -396,7 +394,7 @@ public class PriorityBlockingQueueTest e public void testPoll() { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } @@ -407,9 +405,9 @@ public class PriorityBlockingQueueTest e public void testTimedPoll0() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, q.poll(0, MILLISECONDS)); } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); + assertNull(q.poll(0, MILLISECONDS)); } /** @@ -418,9 +416,9 @@ public class PriorityBlockingQueueTest e public void testTimedPoll() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } /** @@ -432,11 +430,11 @@ public class PriorityBlockingQueueTest e public void realRun() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } try { - q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (InterruptedException success) {} }}); @@ -454,17 +452,17 @@ public class PriorityBlockingQueueTest e final PriorityBlockingQueue q = new PriorityBlockingQueue(2); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); try { - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); t.interrupt(); t.join(); } @@ -476,10 +474,10 @@ public class PriorityBlockingQueueTest e public void testPeek() { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peek()).intValue()); - q.poll(); + assertEquals(i, q.peek()); + assertEquals(i, q.poll()); assertTrue(q.peek() == null || - i != ((Integer)q.peek()).intValue()); + !q.peek().equals(i)); } assertNull(q.peek()); } @@ -490,8 +488,8 @@ public class PriorityBlockingQueueTest e public void testElement() { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.element()).intValue()); - q.poll(); + assertEquals(i, q.element()); + assertEquals(i, q.poll()); } try { q.element(); @@ -505,7 +503,7 @@ public class PriorityBlockingQueueTest e public void testRemove() { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); @@ -631,8 +629,8 @@ public class PriorityBlockingQueueTest e * toArray(null) throws NPE */ public void testToArray_BadArg() { + PriorityBlockingQueue q = populatedQueue(SIZE); try { - PriorityBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} @@ -642,11 +640,11 @@ public class PriorityBlockingQueueTest e * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { + PriorityBlockingQueue q = populatedQueue(SIZE); try { - PriorityBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); + Object o[] = q.toArray(new String[10]); shouldThrow(); - } catch (ArrayStoreException success) {} + } catch (ArrayStoreException success) {} } /** @@ -666,7 +664,7 @@ public class PriorityBlockingQueueTest e /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final PriorityBlockingQueue q = new PriorityBlockingQueue(3); q.add(new Integer(2)); q.add(new Integer(1)); @@ -702,15 +700,15 @@ public class PriorityBlockingQueueTest e ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + assertNull(q.poll()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - q.put(new Integer(1)); + q.put(one); }}); joinPool(executor);