--- jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2013/04/01 20:58:58 1.24 +++ jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2015/11/09 07:54:28 1.31 @@ -6,21 +6,74 @@ * 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; + protected volatile long protectedField; + private volatile long privateField; long w; - + float z; public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(AtomicLongFieldUpdaterTest.class); } + // for testing subclass access + static class AtomicLongFieldUpdaterTestSubclass extends AtomicLongFieldUpdaterTest { + public void checkPrivateAccess() { + try { + AtomicLongFieldUpdater a = + AtomicLongFieldUpdater.newUpdater + (AtomicLongFieldUpdaterTest.class, "privateField"); + shouldThrow(); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + + public void checkCompareAndSetProtectedSub() { + AtomicLongFieldUpdater 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 checkPackageAccess(AtomicLongFieldUpdaterTest obj) { + obj.x = 72L; + AtomicLongFieldUpdater a = + AtomicLongFieldUpdater.newUpdater + (AtomicLongFieldUpdaterTest.class, "x"); + assertEquals(72L, a.get(obj)); + assertTrue(a.compareAndSet(obj, 72L, 73L)); + assertEquals(73L, a.get(obj)); + } + + public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) { + try { + AtomicLongFieldUpdater a = + AtomicLongFieldUpdater.newUpdater + (AtomicLongFieldUpdaterTest.class, "privateField"); + throw new AssertionError("should throw"); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + } + AtomicLongFieldUpdater updaterFor(String fieldName) { return AtomicLongFieldUpdater.newUpdater (AtomicLongFieldUpdaterTest.class, fieldName); @@ -33,27 +86,47 @@ public class AtomicLongFieldUpdaterTest try { 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 { 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 { updaterFor("w"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} + } + + /** + * construction using private field from subclass throws RuntimeException + */ + public void testPrivateFieldInSubclass() { + AtomicLongFieldUpdaterTestSubclass s = + new AtomicLongFieldUpdaterTestSubclass(); + 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); } /** @@ -101,6 +174,33 @@ public class AtomicLongFieldUpdaterTest } /** + * compareAndSet succeeds in changing protected field value if + * equal to expected else fails + */ + public void testCompareAndSetProtected() { + AtomicLongFieldUpdater 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() { + AtomicLongFieldUpdaterTestSubclass s = + new AtomicLongFieldUpdaterTestSubclass(); + s.checkCompareAndSetProtectedSub(); + } + + /** * compareAndSet in one thread enables another waiting for value * to succeed */ @@ -130,10 +230,10 @@ public class AtomicLongFieldUpdaterTest AtomicLongFieldUpdater a; 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)); }