--- jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2015/04/25 04:55:30 1.33 +++ jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2015/11/09 18:42:41 1.34 @@ -13,6 +13,8 @@ import junit.framework.TestSuite; public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { volatile Integer x = null; + protected volatile Integer protectedField; + private volatile Integer privateField; Object z; Integer w; volatile int i; @@ -24,7 +26,58 @@ public class AtomicReferenceFieldUpdater return new TestSuite(AtomicReferenceFieldUpdaterTest.class); } - AtomicReferenceFieldUpdater updaterFor(String fieldName) { + // for testing subclass access + static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest { + public void checkPrivateAccess() { + try { + AtomicReferenceFieldUpdater a = + AtomicReferenceFieldUpdater.newUpdater + (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); + shouldThrow(); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + + public void checkCompareAndSetProtectedSub() { + AtomicReferenceFieldUpdater a = + AtomicReferenceFieldUpdater.newUpdater + (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField"); + this.protectedField = one; + assertTrue(a.compareAndSet(this, one, two)); + assertTrue(a.compareAndSet(this, two, m4)); + assertSame(m4, a.get(this)); + assertFalse(a.compareAndSet(this, m5, seven)); + assertFalse(seven == a.get(this)); + assertTrue(a.compareAndSet(this, m4, seven)); + assertSame(seven, a.get(this)); + } + } + + static class UnrelatedClass { + public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) { + obj.x = one; + AtomicReferenceFieldUpdater a = + AtomicReferenceFieldUpdater.newUpdater + (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); + assertSame(one, a.get(obj)); + assertTrue(a.compareAndSet(obj, one, two)); + assertSame(two, a.get(obj)); + } + + public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) { + try { + AtomicReferenceFieldUpdater a = + AtomicReferenceFieldUpdater.newUpdater + (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); + throw new AssertionError("should throw"); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + } + + static AtomicReferenceFieldUpdater updaterFor(String fieldName) { return AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); } @@ -72,6 +125,24 @@ public class AtomicReferenceFieldUpdater } /** + * construction using private field from subclass throws RuntimeException + */ + public void testPrivateFieldInSubclass() { + AtomicReferenceFieldUpdaterTestSubclass s = + new AtomicReferenceFieldUpdaterTestSubclass(); + s.checkPrivateAccess(); + } + + /** + * construction from unrelated class; package access is allowed, + * private access is not + */ + public void testUnrelatedClassAccess() { + new UnrelatedClass().checkPackageAccess(this); + new UnrelatedClass().checkPrivateAccess(this); + } + + /** * get returns the last value set or assigned */ public void testGetSet() {