--- jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2015/04/25 04:55:30 1.29 +++ jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2015/11/08 15:34:00 1.30 @@ -13,8 +13,10 @@ import junit.framework.TestSuite; public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { volatile int x = 0; + protected volatile int protectedField; + private volatile int privateField; int w; - long z; + float z; public static void main(String[] args) { main(suite(), args); } @@ -22,6 +24,47 @@ public class AtomicIntegerFieldUpdaterTe return new TestSuite(AtomicIntegerFieldUpdaterTest.class); } + // for testing subclass access + static class AtomicIntegerFieldUpdaterTestSubclass extends AtomicIntegerFieldUpdaterTest { + public void checkPrivateAccess() { + try { + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "privateField"); + shouldThrow(); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + + public void checkCompareAndSetProtectedSub() { + AtomicIntegerFieldUpdater a; + a = updaterFor("protectedField"); + protectedField = 1; + assertTrue(a.compareAndSet(this, 1, 2)); + assertTrue(a.compareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + assertFalse(a.compareAndSet(this, -5, 7)); + assertEquals(-4, a.get(this)); + assertTrue(a.compareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); + } + } + + static class UnrelatedClass { + public void checkPrivateAccess() { + Exception ex = null; + try { + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "x"); + } catch (RuntimeException rex) { + ex = rex; + } + if (ex != null) throw new Error(); + } + } + AtomicIntegerFieldUpdater updaterFor(String fieldName) { return AtomicIntegerFieldUpdater.newUpdater (AtomicIntegerFieldUpdaterTest.class, fieldName); @@ -60,6 +103,23 @@ public class AtomicIntegerFieldUpdaterTe } /** + * construction using private field from subclass throws RuntimeException + */ + public void testPrivateFieldInSubclass() { + AtomicIntegerFieldUpdaterTestSubclass s = + new AtomicIntegerFieldUpdaterTestSubclass(); + s.checkPrivateAccess(); + } + + /** + * construction from unrelated class throws RuntimeException + */ + public void testUnrelatedClassAccess() { + UnrelatedClass s = new UnrelatedClass(); + s.checkPrivateAccess(); + } + + /** * get returns the last value set or assigned */ public void testGetSet() { @@ -104,6 +164,33 @@ public class AtomicIntegerFieldUpdaterTe } /** + * compareAndSet succeeds in changing protected field value if + * equal to expected else fails + */ + public void testCompareAndSetProtected() { + AtomicIntegerFieldUpdater a; + a = updaterFor("protectedField"); + protectedField = 1; + assertTrue(a.compareAndSet(this, 1, 2)); + assertTrue(a.compareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + assertFalse(a.compareAndSet(this, -5, 7)); + assertEquals(-4, a.get(this)); + assertTrue(a.compareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); + } + + /** + * compareAndSet succeeds in changing protected field value if + * equal to expected else fails + */ + public void testCompareAndSetProtectedInSubclass() { + AtomicIntegerFieldUpdaterTestSubclass s = + new AtomicIntegerFieldUpdaterTestSubclass(); + s.checkCompareAndSetProtectedSub(); + } + + /** * compareAndSet in one thread enables another waiting for value * to succeed */