--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/21 10:25:05 1.18 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2010/09/29 12:33:48 1.31 @@ -15,7 +15,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); @@ -104,15 +104,12 @@ public class ArrayBlockingQueueTest exte * Queue contains all elements of collection used to initialize */ public void testConstructor7() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -227,6 +224,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -241,6 +239,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if not enough room */ @@ -254,6 +253,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (IllegalStateException success) {} } + /** * Queue contains all elements, in traversal order, of successful addAll */ @@ -299,53 +299,49 @@ public class ArrayBlockingQueueTest exte public void testBlockingPut() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); try { - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ie) { - threadAssertEquals(added, SIZE); - }}}); + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); - Thread.sleep(MEDIUM_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * put blocks waiting for take when full */ public void testPutWithTake() throws InterruptedException { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); + final int capacity = 2; + final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.put(i); try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException e) { - threadAssertTrue(added >= 2); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -353,13 +349,16 @@ public class ArrayBlockingQueueTest exte */ public void testTimedOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -373,7 +372,7 @@ public class ArrayBlockingQueueTest exte public void testTake() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } } @@ -397,19 +396,22 @@ public class ArrayBlockingQueueTest exte * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final ArrayBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - ArrayBlockingQueue 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(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } @@ -419,7 +421,7 @@ public class ArrayBlockingQueueTest exte public void testPoll() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } @@ -430,7 +432,7 @@ public class ArrayBlockingQueueTest exte public void testTimedPoll0() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -441,7 +443,7 @@ public class ArrayBlockingQueueTest exte public void testTimedPoll() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } @@ -451,17 +453,20 @@ public class ArrayBlockingQueueTest exte * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; } - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); - }}; + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); - Thread.sleep(SMALL_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -472,12 +477,15 @@ public class ArrayBlockingQueueTest exte */ public void testTimedPollWithOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}; + try { + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -493,10 +501,10 @@ public class ArrayBlockingQueueTest exte public void testPeek() { ArrayBlockingQueue 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()); } @@ -507,8 +515,8 @@ public class ArrayBlockingQueueTest exte public void testElement() { ArrayBlockingQueue 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(); @@ -522,7 +530,7 @@ public class ArrayBlockingQueueTest exte public void testRemove() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); @@ -552,7 +560,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.contains(new Integer(i))); - q.poll(); + assertEquals(i, q.poll()); assertFalse(q.contains(new Integer(i))); } } @@ -647,8 +655,8 @@ public class ArrayBlockingQueueTest exte * toArray(null) throws NPE */ public void testToArray_BadArg() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} @@ -658,9 +666,9 @@ public class ArrayBlockingQueueTest exte * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); + Object o[] = q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -680,7 +688,7 @@ public class ArrayBlockingQueueTest exte /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); q.add(two); q.add(one); @@ -691,8 +699,8 @@ public class ArrayBlockingQueueTest exte it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } @@ -709,8 +717,7 @@ public class ArrayBlockingQueueTest exte int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); } @@ -718,7 +725,7 @@ public class ArrayBlockingQueueTest exte /** * Modifications do not cause iterators to fail */ - public void testWeaklyConsistentIteration () { + public void testWeaklyConsistentIteration() { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); q.add(one); q.add(two); @@ -753,19 +760,18 @@ public class ArrayBlockingQueueTest exte ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(q.offer(three)); - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); + assertFalse(q.offer(three)); + assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); - } /** @@ -776,9 +782,9 @@ public class ArrayBlockingQueueTest exte ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + assertNull(q.poll()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() {