--- jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2011/06/10 20:17:11 1.25 +++ jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2013/05/30 03:28:55 1.29 @@ -13,6 +13,7 @@ public class AtomicReferenceFieldUpdater volatile Integer x = null; Object z; Integer w; + volatile int i; public static void main(String[] args) { junit.textui.TestRunner.run(suite()); @@ -21,40 +22,51 @@ public class AtomicReferenceFieldUpdater return new TestSuite(AtomicReferenceFieldUpdaterTest.class); } + AtomicReferenceFieldUpdater updaterFor(String fieldName) { + return AtomicReferenceFieldUpdater.newUpdater + (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); + } + /** * Construction with non-existent field throws RuntimeException */ public void testConstructor() { try { - AtomicReferenceFieldUpdater - a = AtomicReferenceFieldUpdater.newUpdater - (AtomicReferenceFieldUpdaterTest.class, Integer.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 ClassCastException */ public void testConstructor2() { try { - AtomicReferenceFieldUpdater - a = AtomicReferenceFieldUpdater.newUpdater - (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); + updaterFor("z"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (ClassCastException success) {} } /** - * Constructor with non-volatile field throws exception + * Constructor with non-volatile field throws IllegalArgumentException */ public void testConstructor3() { try { - AtomicReferenceFieldUpdater - a = AtomicReferenceFieldUpdater.newUpdater - (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); + updaterFor("w"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} + } + + /** + * Constructor with non-reference field throws ClassCastException + */ + public void testConstructor4() { + try { + updaterFor("i"); + shouldThrow(); + } catch (ClassCastException success) {} } /** @@ -62,11 +74,7 @@ public class AtomicReferenceFieldUpdater */ public void testGetSet() { AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = one; assertSame(one, a.get(this)); a.set(this, two); @@ -80,11 +88,7 @@ public class AtomicReferenceFieldUpdater */ public void testGetLazySet() { AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = one; assertSame(one, a.get(this)); a.lazySet(this, two); @@ -98,11 +102,7 @@ public class AtomicReferenceFieldUpdater */ public void testCompareAndSet() { AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = one; assertTrue(a.compareAndSet(this, one, two)); assertTrue(a.compareAndSet(this, two, m4)); @@ -120,11 +120,7 @@ public class AtomicReferenceFieldUpdater public void testCompareAndSetInMultipleThreads() throws Exception { x = one; final AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { @@ -145,11 +141,7 @@ public class AtomicReferenceFieldUpdater */ public void testWeakCompareAndSet() { AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = one; while (!a.weakCompareAndSet(this, one, two)); while (!a.weakCompareAndSet(this, two, m4)); @@ -163,11 +155,7 @@ public class AtomicReferenceFieldUpdater */ public void testGetAndSet() { AtomicReferenceFieldUpdatera; - try { - a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = one; assertSame(one, a.getAndSet(this, zero)); assertSame(zero, a.getAndSet(this, m10));