--- jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2011/06/10 20:17:11 1.23 +++ jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2014/12/31 19:21:20 1.28 @@ -6,9 +6,11 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicLongFieldUpdaterTest extends JSR166TestCase { volatile long x = 0; int z; @@ -21,40 +23,41 @@ public class AtomicLongFieldUpdaterTest return new TestSuite(AtomicLongFieldUpdaterTest.class); } + AtomicLongFieldUpdater updaterFor(String fieldName) { + return AtomicLongFieldUpdater.newUpdater + (AtomicLongFieldUpdaterTest.class, fieldName); + } + /** * Construction with non-existent field throws RuntimeException */ public void testConstructor() { try { - AtomicLongFieldUpdater - a = AtomicLongFieldUpdater.newUpdater - (AtomicLongFieldUpdaterTest.class, "y"); + updaterFor("y"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } } /** - * construction with field not of given type throws RuntimeException + * construction with field not of given type throws IllegalArgumentException */ public void testConstructor2() { try { - AtomicLongFieldUpdater - a = AtomicLongFieldUpdater.newUpdater - (AtomicLongFieldUpdaterTest.class, "z"); + updaterFor("z"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} } /** - * construction with non-volatile field throws RuntimeException + * construction with non-volatile field throws IllegalArgumentException */ public void testConstructor3() { try { - AtomicLongFieldUpdater - a = AtomicLongFieldUpdater.newUpdater - (AtomicLongFieldUpdaterTest.class, "w"); + updaterFor("w"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -62,11 +65,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetSet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.get(this)); a.set(this, 2); @@ -80,11 +79,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetLazySet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.get(this)); a.lazySet(this, 2); @@ -98,11 +93,7 @@ public class AtomicLongFieldUpdaterTest */ public void testCompareAndSet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertTrue(a.compareAndSet(this, 1, 2)); assertTrue(a.compareAndSet(this, 2, -4)); @@ -119,12 +110,8 @@ public class AtomicLongFieldUpdaterTest */ public void testCompareAndSetInMultipleThreads() throws Exception { x = 1; - final AtomicLongFieldUpdatera; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + final AtomicLongFieldUpdater a; + a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { @@ -145,16 +132,12 @@ public class AtomicLongFieldUpdaterTest */ public void testWeakCompareAndSet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - while (!a.weakCompareAndSet(this, 1, 2)); - while (!a.weakCompareAndSet(this, 2, -4)); + do {} while (!a.weakCompareAndSet(this, 1, 2)); + do {} while (!a.weakCompareAndSet(this, 2, -4)); assertEquals(-4, a.get(this)); - while (!a.weakCompareAndSet(this, -4, 7)); + do {} while (!a.weakCompareAndSet(this, -4, 7)); assertEquals(7, a.get(this)); } @@ -163,11 +146,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetAndSet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.getAndSet(this, 0)); assertEquals(0, a.getAndSet(this, -10)); @@ -179,11 +158,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetAndAdd() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.getAndAdd(this, 2)); assertEquals(3, a.get(this)); @@ -196,11 +171,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetAndDecrement() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.getAndDecrement(this)); assertEquals(0, a.getAndDecrement(this)); @@ -212,11 +183,7 @@ public class AtomicLongFieldUpdaterTest */ public void testGetAndIncrement() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(1, a.getAndIncrement(this)); assertEquals(2, a.get(this)); @@ -232,11 +199,7 @@ public class AtomicLongFieldUpdaterTest */ public void testAddAndGet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(3, a.addAndGet(this, 2)); assertEquals(3, a.get(this)); @@ -249,11 +212,7 @@ public class AtomicLongFieldUpdaterTest */ public void testDecrementAndGet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(0, a.decrementAndGet(this)); assertEquals(-1, a.decrementAndGet(this)); @@ -266,11 +225,7 @@ public class AtomicLongFieldUpdaterTest */ public void testIncrementAndGet() { AtomicLongFieldUpdater a; - try { - a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; assertEquals(2, a.incrementAndGet(this)); assertEquals(2, a.get(this));