--- jsr166/src/test/tck/Atomic8Test.java 2013/09/08 23:00:36 1.1 +++ jsr166/src/test/tck/Atomic8Test.java 2019/02/22 19:27:47 1.10 @@ -5,38 +5,59 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerArray; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongArray; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.AtomicReferenceArray; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests of atomic class methods accepting lambdas introduced in JDK8. + */ public class Atomic8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(Atomic8Test.class); } - /* - * Tests of atomic class methods accepting lambdas - * introduced in JDK8. - */ - static long addLong17(long x) { return x + 17; } static int addInt17(int x) { return x + 17; } static Integer addInteger17(Integer x) { - return new Integer(x.intValue() + 17); + return x.intValue() + 17; } static Integer sumInteger(Integer x, Integer y) { - return new Integer(x.intValue() + y.intValue()); + return x.intValue() + y.intValue(); } volatile long aLongField; volatile int anIntField; volatile Integer anIntegerField; + AtomicLongFieldUpdater aLongFieldUpdater() { + return AtomicLongFieldUpdater.newUpdater + (Atomic8Test.class, "aLongField"); + } + + AtomicIntegerFieldUpdater anIntFieldUpdater() { + return AtomicIntegerFieldUpdater.newUpdater + (Atomic8Test.class, "anIntField"); + } + + AtomicReferenceFieldUpdater anIntegerFieldUpdater() { + return AtomicReferenceFieldUpdater.newUpdater + (Atomic8Test.class, Integer.class, "anIntegerField"); + } + /** * AtomicLong getAndUpdate returns previous value and updates * result of supplied function @@ -77,6 +98,7 @@ public class Atomic8Test extends JSR166T AtomicLong a = new AtomicLong(1L); assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); + assertEquals(10L, a.get()); } /** @@ -98,6 +120,7 @@ public class Atomic8Test extends JSR166T AtomicInteger a = new AtomicInteger(1); assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); + assertEquals(35, a.get()); } /** @@ -119,6 +142,7 @@ public class Atomic8Test extends JSR166T AtomicInteger a = new AtomicInteger(1); assertEquals(7, a.accumulateAndGet(6, Integer::sum)); assertEquals(10, a.accumulateAndGet(3, Integer::sum)); + assertEquals(10, a.get()); } /** @@ -126,10 +150,10 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testReferenceGetAndUpdate() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17)); - assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.get()); + AtomicReference a = new AtomicReference<>(one); + assertEquals((Integer) 1, a.getAndUpdate(Atomic8Test::addInteger17)); + assertEquals((Integer) 18, a.getAndUpdate(Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.get()); } /** @@ -137,9 +161,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceUpdateAndGet() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); + AtomicReference a = new AtomicReference<>(one); + assertEquals((Integer) 18, a.updateAndGet(Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.updateAndGet(Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.get()); } /** @@ -147,10 +172,10 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceGetAndAccumulate() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger)); - assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger)); - assertEquals(new Integer(6), a.get()); + AtomicReference a = new AtomicReference<>(one); + assertEquals((Integer) 1, a.getAndAccumulate(2, Atomic8Test::sumInteger)); + assertEquals((Integer) 3, a.getAndAccumulate(3, Atomic8Test::sumInteger)); + assertEquals((Integer) 6, a.get()); } /** @@ -158,12 +183,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceAccumulateAndGet() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); + AtomicReference a = new AtomicReference<>(one); + assertEquals((Integer) 7, a.accumulateAndGet(6, Atomic8Test::sumInteger)); + assertEquals((Integer) 10, a.accumulateAndGet(3, Atomic8Test::sumInteger)); + assertEquals((Integer) 10, a.get()); } - /** * AtomicLongArray getAndUpdate returns previous value and updates * result of supplied function @@ -185,6 +210,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); + assertEquals(35L, a.get(0)); } /** @@ -208,6 +234,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); + assertEquals(10L, a.get(0)); } /** @@ -231,6 +258,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); + assertEquals(35, a.get(0)); } /** @@ -261,11 +289,11 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testReferenceArrayGetAndUpdate() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.get(0)); + assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); + assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.get(0)); } /** @@ -273,10 +301,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceArrayUpdateAndGet() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17)); + assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); } /** @@ -284,11 +312,11 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceArrayGetAndAccumulate() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); - assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); - assertEquals(new Integer(6), a.get(0)); + assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); + assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); + assertEquals((Integer) 6, a.get(0)); } /** @@ -296,10 +324,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceArrayAccumulateAndGet() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); + assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); + assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); } /** @@ -307,12 +335,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testLongFieldUpdaterGetAndUpdate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); assertEquals(35L, a.get(this)); + assertEquals(35L, aLongField); } /** @@ -320,11 +348,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testLongFieldUpdaterUpdateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); + assertEquals(35L, a.get(this)); + assertEquals(35L, aLongField); } /** @@ -332,12 +361,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testLongFieldUpdaterGetAndAccumulate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); assertEquals(6L, a.get(this)); + assertEquals(6L, aLongField); } /** @@ -345,11 +374,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testLongFieldUpdaterAccumulateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); + assertEquals(10L, a.get(this)); + assertEquals(10L, aLongField); } /** @@ -357,12 +387,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testIntegerFieldUpdaterGetAndUpdate() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); assertEquals(35, a.get(this)); + assertEquals(35, anIntField); } /** @@ -370,11 +400,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testIntegerFieldUpdaterUpdateAndGet() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); + assertEquals(35, a.get(this)); + assertEquals(35, anIntField); } /** @@ -382,12 +413,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testIntegerFieldUpdaterGetAndAccumulate() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); assertEquals(6, a.get(this)); + assertEquals(6, anIntField); } /** @@ -395,25 +426,25 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testIntegerFieldUpdaterAccumulateAndGet() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); + assertEquals(10, a.get(this)); + assertEquals(10, anIntField); } - /** * AtomicReferenceFieldUpdater getAndUpdate returns previous value * and updates result of supplied function */ public void testReferenceFieldUpdaterGetAndUpdate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); - assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17)); - assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.get(this)); + assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17)); + assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.get(this)); + assertEquals((Integer) 35, anIntegerField); } /** @@ -421,11 +452,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testReferenceFieldUpdaterUpdateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); - assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); + assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17)); + assertEquals((Integer) 35, a.get(this)); + assertEquals((Integer) 35, anIntegerField); } /** @@ -433,12 +465,12 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceFieldUpdaterGetAndAccumulate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); - assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); - assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); - assertEquals(new Integer(6), a.get(this)); + assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); + assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); + assertEquals((Integer) 6, a.get(this)); + assertEquals((Integer) 6, anIntegerField); } /** @@ -446,144 +478,117 @@ public class Atomic8Test extends JSR166T * supplied function and returns result. */ public void testReferenceFieldUpdaterAccumulateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); - assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); + assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); + assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); + assertEquals((Integer) 10, a.get(this)); + assertEquals((Integer) 10, anIntegerField); } /** - * All Atomic getAndUpdate methods throw npe on null function argument + * All Atomic getAndUpdate methods throw NullPointerException on + * null function argument */ public void testGetAndUpdateNPE() { - try { new AtomicLong().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - } - - /** - * All Atomic updateAndGet methods throw npe on null function argument - */ - public void testUpdateGetAndNPE() { - try { new AtomicLong().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} + assertThrows( + NullPointerException.class, + () -> new AtomicLong().getAndUpdate(null), + () -> new AtomicInteger().getAndUpdate(null), + () -> new AtomicReference().getAndUpdate(null), + () -> new AtomicLongArray(1).getAndUpdate(0, null), + () -> new AtomicIntegerArray(1).getAndUpdate(0, null), + () -> new AtomicReferenceArray(1).getAndUpdate(0, null), + () -> aLongFieldUpdater().getAndUpdate(this, null), + () -> anIntFieldUpdater().getAndUpdate(this, null), + () -> anIntegerFieldUpdater().getAndUpdate(this, null)); + } + + /** + * All Atomic updateAndGet methods throw NullPointerException on null function argument + */ + public void testUpdateAndGetNPE() { + assertThrows( + NullPointerException.class, + () -> new AtomicLong().updateAndGet(null), + () -> new AtomicInteger().updateAndGet(null), + () -> new AtomicReference().updateAndGet(null), + () -> new AtomicLongArray(1).updateAndGet(0, null), + () -> new AtomicIntegerArray(1).updateAndGet(0, null), + () -> new AtomicReferenceArray(1).updateAndGet(0, null), + () -> aLongFieldUpdater().updateAndGet(this, null), + () -> anIntFieldUpdater().updateAndGet(this, null), + () -> anIntegerFieldUpdater().updateAndGet(this, null)); } /** - * All Atomic getAndAccumulate methods throw npe on null function argument + * All Atomic getAndAccumulate methods throw NullPointerException + * on null function argument */ public void testGetAndAccumulateNPE() { - try { new AtomicLong().getAndAccumulate(1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().getAndAccumulate(1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().getAndAccumulate(one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).getAndAccumulate(0, 1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).getAndAccumulate(0, 1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).getAndAccumulate(0, one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - getAndAccumulate(this, 1L, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - getAndAccumulate(this, 1, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - getAndAccumulate(this, one, null); - shouldThrow(); - } catch(NullPointerException ok) {} + assertThrows( + NullPointerException.class, + () -> new AtomicLong().getAndAccumulate(1L, null), + () -> new AtomicInteger().getAndAccumulate(1, null), + () -> new AtomicReference().getAndAccumulate(one, null), + () -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null), + () -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null), + () -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), + () -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), + () -> anIntFieldUpdater().getAndAccumulate(this, 1, null), + () -> anIntegerFieldUpdater().getAndAccumulate(this, one, null)); } /** - * All Atomic accumulateAndGet methods throw npe on null function argument + * All Atomic accumulateAndGet methods throw NullPointerException + * on null function argument */ public void testAccumulateAndGetNPE() { - try { new AtomicLong().accumulateAndGet(1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().accumulateAndGet(1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().accumulateAndGet(one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).accumulateAndGet(0, 1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).accumulateAndGet(0, 1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).accumulateAndGet(0, one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - accumulateAndGet(this, 1L, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - accumulateAndGet(this, 1, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - accumulateAndGet(this, one, null); - shouldThrow(); - } catch(NullPointerException ok) {} + assertThrows( + NullPointerException.class, + () -> new AtomicLong().accumulateAndGet(1L, null), + () -> new AtomicInteger().accumulateAndGet(1, null), + () -> new AtomicReference().accumulateAndGet(one, null), + () -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null), + () -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null), + () -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), + () -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), + () -> anIntFieldUpdater().accumulateAndGet(this, 1, null), + () -> anIntegerFieldUpdater().accumulateAndGet(this, one, null)); } -} + /** + * Object arguments for parameters of type T that are not + * instances of the class passed to the newUpdater call will + * result in a ClassCastException being thrown. + */ + public void testFieldUpdaters_ClassCastException() { + // Use raw types to allow passing wrong object type, provoking CCE + final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); + final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); + final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); + for (Object x : new Object[]{ new Object(), null }) { + assertThrows( + ClassCastException.class, + () -> longUpdater.get(x), + () -> intUpdater.get(x), + () -> refUpdater.get(x), + + () -> longUpdater.set(x, 17L), + () -> intUpdater.set(x, 17), + () -> refUpdater.set(x, (Integer) 17), + + () -> longUpdater.addAndGet(x, 17L), + () -> intUpdater.addAndGet(x, 17), + + () -> longUpdater.getAndUpdate(x, y -> y), + () -> intUpdater.getAndUpdate(x, y -> y), + () -> refUpdater.getAndUpdate(x, y -> y), + + () -> longUpdater.compareAndSet(x, 17L, 42L), + () -> intUpdater.compareAndSet(x, 17, 42), + () -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42)); + } + } +}