--- jsr166/src/test/tck/CountDownLatchTest.java 2003/09/25 11:02:41 1.4 +++ jsr166/src/test/tck/CountDownLatchTest.java 2009/11/16 05:30:07 1.10 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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. */ import junit.framework.*; @@ -11,7 +12,7 @@ import java.util.concurrent.*; public class CountDownLatchTest 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(CountDownLatchTest.class); @@ -24,7 +25,7 @@ public class CountDownLatchTest extends try { new CountDownLatch(-1); shouldThrow(); - } catch(IllegalArgumentException success){} + } catch (IllegalArgumentException success) {} } /** @@ -38,9 +39,9 @@ public class CountDownLatchTest extends } /** - * countDown has no effect when count is zero + * countDown decrements count when positive and has no effect when zero */ - public void testcountDown() { + public void testCountDown() { final CountDownLatch l = new CountDownLatch(1); assertEquals(1, l.getCount()); l.countDown(); @@ -61,7 +62,7 @@ public class CountDownLatchTest extends threadAssertTrue(l.getCount() > 0); l.await(); threadAssertTrue(l.getCount() == 0); - } catch(InterruptedException e){ + } catch (InterruptedException e) { threadUnexpectedException(); } } @@ -75,11 +76,11 @@ public class CountDownLatchTest extends l.countDown(); assertEquals(l.getCount(), 0); t.join(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } } - + /** * timed await returns after countDown to zero @@ -92,7 +93,7 @@ public class CountDownLatchTest extends try { threadAssertTrue(l.getCount() > 0); threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch(InterruptedException e){ + } catch (InterruptedException e) { threadUnexpectedException(); } } @@ -106,13 +107,13 @@ public class CountDownLatchTest extends l.countDown(); assertEquals(l.getCount(), 0); t.join(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } } - + /** - * await throws IE ig interrupted before counted down + * await throws IE if interrupted before counted down */ public void testAwait_InterruptedException() { final CountDownLatch l = new CountDownLatch(1); @@ -122,7 +123,7 @@ public class CountDownLatchTest extends threadAssertTrue(l.getCount() > 0); l.await(); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success) {} } }); t.start(); @@ -130,13 +131,13 @@ public class CountDownLatchTest extends assertEquals(l.getCount(), 1); t.interrupt(); t.join(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } } /** - * timed await throws IE ig interrupted before counted down + * timed await throws IE if interrupted before counted down */ public void testTimedAwait_InterruptedException() { final CountDownLatch l = new CountDownLatch(1); @@ -145,8 +146,8 @@ public class CountDownLatchTest extends try { threadAssertTrue(l.getCount() > 0); l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch(InterruptedException success){} + threadShouldThrow(); + } catch (InterruptedException success) {} } }); t.start(); @@ -155,7 +156,7 @@ public class CountDownLatchTest extends assertEquals(l.getCount(), 1); t.interrupt(); t.join(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } } @@ -171,7 +172,7 @@ public class CountDownLatchTest extends threadAssertTrue(l.getCount() > 0); threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); threadAssertTrue(l.getCount() > 0); - } catch(InterruptedException ie){ + } catch (InterruptedException ie) { threadUnexpectedException(); } } @@ -180,9 +181,24 @@ public class CountDownLatchTest extends try { assertEquals(l.getCount(), 1); t.join(); - } catch (InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); } } + /** + * toString indicates current count + */ + public void testToString() { + CountDownLatch s = new CountDownLatch(2); + String us = s.toString(); + assertTrue(us.indexOf("Count = 2") >= 0); + s.countDown(); + String s1 = s.toString(); + assertTrue(s1.indexOf("Count = 1") >= 0); + s.countDown(); + String s2 = s.toString(); + assertTrue(s2.indexOf("Count = 0") >= 0); + } + }