--- jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2009/11/17 06:58:50 1.14 +++ jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2015/11/09 18:42:41 1.31 @@ -1,101 +1,161 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ -import java.util.concurrent.atomic.*; -import junit.framework.*; -import java.util.*; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +import junit.framework.Test; +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) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { 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 = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "protectedField"); + this.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(AtomicIntegerFieldUpdaterTest obj) { + obj.x = 72; + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "x"); + assertEquals(72, a.get(obj)); + assertTrue(a.compareAndSet(obj, 72, 73)); + assertEquals(73, a.get(obj)); + } + + public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) { + try { + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "privateField"); + throw new AssertionError("should throw"); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + } + + AtomicIntegerFieldUpdater updaterFor(String fieldName) { + return AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, fieldName); + } + /** * Construction with non-existent field throws RuntimeException */ public void testConstructor() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "y"); + updaterFor("y"); shouldThrow(); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); } - catch (RuntimeException rt) {} } /** - * construction with field not of given type throws RuntimeException + * construction with field not of given type throws IllegalArgumentException */ public void testConstructor2() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "z"); + updaterFor("z"); shouldThrow(); - } - catch (RuntimeException rt) {} + } catch (IllegalArgumentException success) {} } /** - * construction with non-volatile field throws RuntimeException + * construction with non-volatile field throws IllegalArgumentException */ public void testConstructor3() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "w"); + updaterFor("w"); shouldThrow(); - } - catch (RuntimeException rt) {} + } catch (IllegalArgumentException success) {} } /** - * get returns the last value set or assigned + * construction using private field from subclass throws RuntimeException + */ + public void testPrivateFieldInSubclass() { + AtomicIntegerFieldUpdaterTestSubclass s = + new AtomicIntegerFieldUpdaterTestSubclass(); + 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() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.get(this)); - a.set(this,2); - assertEquals(2,a.get(this)); - a.set(this,-3); - assertEquals(-3,a.get(this)); - + assertEquals(1, a.get(this)); + a.set(this, 2); + assertEquals(2, a.get(this)); + a.set(this, -3); + assertEquals(-3, a.get(this)); } /** - * get returns the last value lazySet by same thread + * get returns the last value lazySet by same thread */ public void testGetLazySet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.get(this)); - a.lazySet(this,2); - assertEquals(2,a.get(this)); - a.lazySet(this,-3); - assertEquals(-3,a.get(this)); - + assertEquals(1, a.get(this)); + a.lazySet(this, 2); + assertEquals(2, a.get(this)); + a.lazySet(this, -3); + assertEquals(-3, a.get(this)); } /** @@ -103,21 +163,43 @@ public class AtomicIntegerFieldUpdaterTe */ public void testCompareAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertTrue(a.compareAndSet(this,1,2)); - assertTrue(a.compareAndSet(this,2,-4)); - assertEquals(-4,a.get(this)); - assertFalse(a.compareAndSet(this,-5,7)); - assertFalse((7 == a.get(this))); - assertTrue(a.compareAndSet(this,-4,7)); - assertEquals(7,a.get(this)); + 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 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 @@ -125,12 +207,8 @@ public class AtomicIntegerFieldUpdaterTe */ public void testCompareAndSetInMultipleThreads() throws Exception { x = 1; - final AtomicIntegerFieldUpdatera; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + final AtomicIntegerFieldUpdater a; + a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { @@ -142,7 +220,7 @@ public class AtomicIntegerFieldUpdaterTe assertTrue(a.compareAndSet(this, 1, 2)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); - assertEquals(a.get(this), 3); + assertEquals(3, a.get(this)); } /** @@ -151,33 +229,25 @@ public class AtomicIntegerFieldUpdaterTe */ public void testWeakCompareAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - while (!a.weakCompareAndSet(this,1,2)); - while (!a.weakCompareAndSet(this,2,-4)); - assertEquals(-4,a.get(this)); - while (!a.weakCompareAndSet(this,-4,7)); - assertEquals(7,a.get(this)); + do {} while (!a.weakCompareAndSet(this, 1, 2)); + do {} while (!a.weakCompareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + do {} while (!a.weakCompareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); } /** - * getAndSet returns previous value and sets to given value + * getAndSet returns previous value and sets to given value */ public void testGetAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndSet(this, 0)); - assertEquals(0,a.getAndSet(this,-10)); - assertEquals(-10,a.getAndSet(this,1)); + assertEquals(1, a.getAndSet(this, 0)); + assertEquals(0, a.getAndSet(this, -10)); + assertEquals(-10, a.getAndSet(this, 1)); } /** @@ -185,16 +255,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndAdd() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndAdd(this,2)); - assertEquals(3,a.get(this)); - assertEquals(3,a.getAndAdd(this,-4)); - assertEquals(-1,a.get(this)); + assertEquals(1, a.getAndAdd(this, 2)); + assertEquals(3, a.get(this)); + assertEquals(3, a.getAndAdd(this, -4)); + assertEquals(-1, a.get(this)); } /** @@ -202,15 +268,11 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndDecrement() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndDecrement(this)); - assertEquals(0,a.getAndDecrement(this)); - assertEquals(-1,a.getAndDecrement(this)); + assertEquals(1, a.getAndDecrement(this)); + assertEquals(0, a.getAndDecrement(this)); + assertEquals(-1, a.getAndDecrement(this)); } /** @@ -218,19 +280,15 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndIncrement() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndIncrement(this)); - assertEquals(2,a.get(this)); - a.set(this,-2); - assertEquals(-2,a.getAndIncrement(this)); - assertEquals(-1,a.getAndIncrement(this)); - assertEquals(0,a.getAndIncrement(this)); - assertEquals(1,a.get(this)); + assertEquals(1, a.getAndIncrement(this)); + assertEquals(2, a.get(this)); + a.set(this, -2); + assertEquals(-2, a.getAndIncrement(this)); + assertEquals(-1, a.getAndIncrement(this)); + assertEquals(0, a.getAndIncrement(this)); + assertEquals(1, a.get(this)); } /** @@ -238,16 +296,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testAddAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(3,a.addAndGet(this,2)); - assertEquals(3,a.get(this)); - assertEquals(-1,a.addAndGet(this,-4)); - assertEquals(-1,a.get(this)); + assertEquals(3, a.addAndGet(this, 2)); + assertEquals(3, a.get(this)); + assertEquals(-1, a.addAndGet(this, -4)); + assertEquals(-1, a.get(this)); } /** @@ -255,16 +309,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testDecrementAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(0,a.decrementAndGet(this)); - assertEquals(-1,a.decrementAndGet(this)); - assertEquals(-2,a.decrementAndGet(this)); - assertEquals(-2,a.get(this)); + assertEquals(0, a.decrementAndGet(this)); + assertEquals(-1, a.decrementAndGet(this)); + assertEquals(-2, a.decrementAndGet(this)); + assertEquals(-2, a.get(this)); } /** @@ -272,19 +322,15 @@ public class AtomicIntegerFieldUpdaterTe */ public void testIncrementAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(2,a.incrementAndGet(this)); - assertEquals(2,a.get(this)); - a.set(this,-2); - assertEquals(-1,a.incrementAndGet(this)); - assertEquals(0,a.incrementAndGet(this)); - assertEquals(1,a.incrementAndGet(this)); - assertEquals(1,a.get(this)); + assertEquals(2, a.incrementAndGet(this)); + assertEquals(2, a.get(this)); + a.set(this, -2); + assertEquals(-1, a.incrementAndGet(this)); + assertEquals(0, a.incrementAndGet(this)); + assertEquals(1, a.incrementAndGet(this)); + assertEquals(1, a.get(this)); } }