1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35 import java.util.concurrent.atomic.AtomicInteger; 36 import java.util.concurrent.atomic.AtomicIntegerArray; 37 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; 38 import java.util.concurrent.atomic.AtomicLong; 39 import java.util.concurrent.atomic.AtomicLongArray; 40 import java.util.concurrent.atomic.AtomicLongFieldUpdater; 41 import java.util.concurrent.atomic.AtomicReference; 42 import java.util.concurrent.atomic.AtomicReferenceArray; 43 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; 44 45 import junit.framework.Test; 46 import junit.framework.TestSuite; 47 48 /** 49 * Tests of atomic class methods accepting lambdas introduced in JDK8. 50 */ 51 public class Atomic8Test extends JSR166TestCase { 52 main(String[] args)53 public static void main(String[] args) { 54 main(suite(), args); 55 } suite()56 public static Test suite() { 57 return new TestSuite(Atomic8Test.class); 58 } 59 addLong17(long x)60 static long addLong17(long x) { return x + 17; } addInt17(int x)61 static int addInt17(int x) { return x + 17; } addInteger17(Integer x)62 static Integer addInteger17(Integer x) { 63 return x.intValue() + 17; 64 } sumInteger(Integer x, Integer y)65 static Integer sumInteger(Integer x, Integer y) { 66 return x.intValue() + y.intValue(); 67 } 68 69 volatile long aLongField; 70 volatile int anIntField; 71 volatile Integer anIntegerField; 72 aLongFieldUpdater()73 AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { 74 return AtomicLongFieldUpdater.newUpdater 75 (Atomic8Test.class, "aLongField"); 76 } 77 anIntFieldUpdater()78 AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { 79 return AtomicIntegerFieldUpdater.newUpdater 80 (Atomic8Test.class, "anIntField"); 81 } 82 anIntegerFieldUpdater()83 AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() { 84 return AtomicReferenceFieldUpdater.newUpdater 85 (Atomic8Test.class, Integer.class, "anIntegerField"); 86 } 87 88 /** 89 * AtomicLong getAndUpdate returns previous value and updates 90 * result of supplied function 91 */ testLongGetAndUpdate()92 public void testLongGetAndUpdate() { 93 AtomicLong a = new AtomicLong(1L); 94 assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17)); 95 assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17)); 96 assertEquals(35L, a.get()); 97 } 98 99 /** 100 * AtomicLong updateAndGet updates with supplied function and 101 * returns result. 102 */ testLongUpdateAndGet()103 public void testLongUpdateAndGet() { 104 AtomicLong a = new AtomicLong(1L); 105 assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17)); 106 assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17)); 107 } 108 109 /** 110 * AtomicLong getAndAccumulate returns previous value and updates 111 * with supplied function. 112 */ testLongGetAndAccumulate()113 public void testLongGetAndAccumulate() { 114 AtomicLong a = new AtomicLong(1L); 115 assertEquals(1L, a.getAndAccumulate(2L, Long::sum)); 116 assertEquals(3L, a.getAndAccumulate(3L, Long::sum)); 117 assertEquals(6L, a.get()); 118 } 119 120 /** 121 * AtomicLong accumulateAndGet updates with supplied function and 122 * returns result. 123 */ testLongAccumulateAndGet()124 public void testLongAccumulateAndGet() { 125 AtomicLong a = new AtomicLong(1L); 126 assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); 127 assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); 128 assertEquals(10L, a.get()); 129 } 130 131 /** 132 * AtomicInteger getAndUpdate returns previous value and updates 133 * result of supplied function 134 */ testIntGetAndUpdate()135 public void testIntGetAndUpdate() { 136 AtomicInteger a = new AtomicInteger(1); 137 assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17)); 138 assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17)); 139 assertEquals(35, a.get()); 140 } 141 142 /** 143 * AtomicInteger updateAndGet updates with supplied function and 144 * returns result. 145 */ testIntUpdateAndGet()146 public void testIntUpdateAndGet() { 147 AtomicInteger a = new AtomicInteger(1); 148 assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); 149 assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); 150 assertEquals(35, a.get()); 151 } 152 153 /** 154 * AtomicInteger getAndAccumulate returns previous value and updates 155 * with supplied function. 156 */ testIntGetAndAccumulate()157 public void testIntGetAndAccumulate() { 158 AtomicInteger a = new AtomicInteger(1); 159 assertEquals(1, a.getAndAccumulate(2, Integer::sum)); 160 assertEquals(3, a.getAndAccumulate(3, Integer::sum)); 161 assertEquals(6, a.get()); 162 } 163 164 /** 165 * AtomicInteger accumulateAndGet updates with supplied function and 166 * returns result. 167 */ testIntAccumulateAndGet()168 public void testIntAccumulateAndGet() { 169 AtomicInteger a = new AtomicInteger(1); 170 assertEquals(7, a.accumulateAndGet(6, Integer::sum)); 171 assertEquals(10, a.accumulateAndGet(3, Integer::sum)); 172 assertEquals(10, a.get()); 173 } 174 175 /** 176 * AtomicReference getAndUpdate returns previous value and updates 177 * result of supplied function 178 */ testReferenceGetAndUpdate()179 public void testReferenceGetAndUpdate() { 180 AtomicReference<Integer> a = new AtomicReference<>(one); 181 assertEquals((Integer) 1, a.getAndUpdate(Atomic8Test::addInteger17)); 182 assertEquals((Integer) 18, a.getAndUpdate(Atomic8Test::addInteger17)); 183 assertEquals((Integer) 35, a.get()); 184 } 185 186 /** 187 * AtomicReference updateAndGet updates with supplied function and 188 * returns result. 189 */ testReferenceUpdateAndGet()190 public void testReferenceUpdateAndGet() { 191 AtomicReference<Integer> a = new AtomicReference<>(one); 192 assertEquals((Integer) 18, a.updateAndGet(Atomic8Test::addInteger17)); 193 assertEquals((Integer) 35, a.updateAndGet(Atomic8Test::addInteger17)); 194 assertEquals((Integer) 35, a.get()); 195 } 196 197 /** 198 * AtomicReference getAndAccumulate returns previous value and updates 199 * with supplied function. 200 */ testReferenceGetAndAccumulate()201 public void testReferenceGetAndAccumulate() { 202 AtomicReference<Integer> a = new AtomicReference<>(one); 203 assertEquals((Integer) 1, a.getAndAccumulate(2, Atomic8Test::sumInteger)); 204 assertEquals((Integer) 3, a.getAndAccumulate(3, Atomic8Test::sumInteger)); 205 assertEquals((Integer) 6, a.get()); 206 } 207 208 /** 209 * AtomicReference accumulateAndGet updates with supplied function and 210 * returns result. 211 */ testReferenceAccumulateAndGet()212 public void testReferenceAccumulateAndGet() { 213 AtomicReference<Integer> a = new AtomicReference<>(one); 214 assertEquals((Integer) 7, a.accumulateAndGet(6, Atomic8Test::sumInteger)); 215 assertEquals((Integer) 10, a.accumulateAndGet(3, Atomic8Test::sumInteger)); 216 assertEquals((Integer) 10, a.get()); 217 } 218 219 /** 220 * AtomicLongArray getAndUpdate returns previous value and updates 221 * result of supplied function 222 */ testLongArrayGetAndUpdate()223 public void testLongArrayGetAndUpdate() { 224 AtomicLongArray a = new AtomicLongArray(1); 225 a.set(0, 1); 226 assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); 227 assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); 228 assertEquals(35L, a.get(0)); 229 } 230 231 /** 232 * AtomicLongArray updateAndGet updates with supplied function and 233 * returns result. 234 */ testLongArrayUpdateAndGet()235 public void testLongArrayUpdateAndGet() { 236 AtomicLongArray a = new AtomicLongArray(1); 237 a.set(0, 1); 238 assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); 239 assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); 240 assertEquals(35L, a.get(0)); 241 } 242 243 /** 244 * AtomicLongArray getAndAccumulate returns previous value and updates 245 * with supplied function. 246 */ testLongArrayGetAndAccumulate()247 public void testLongArrayGetAndAccumulate() { 248 AtomicLongArray a = new AtomicLongArray(1); 249 a.set(0, 1); 250 assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum)); 251 assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum)); 252 assertEquals(6L, a.get(0)); 253 } 254 255 /** 256 * AtomicLongArray accumulateAndGet updates with supplied function and 257 * returns result. 258 */ testLongArrayAccumulateAndGet()259 public void testLongArrayAccumulateAndGet() { 260 AtomicLongArray a = new AtomicLongArray(1); 261 a.set(0, 1); 262 assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); 263 assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); 264 assertEquals(10L, a.get(0)); 265 } 266 267 /** 268 * AtomicIntegerArray getAndUpdate returns previous value and updates 269 * result of supplied function 270 */ testIntArrayGetAndUpdate()271 public void testIntArrayGetAndUpdate() { 272 AtomicIntegerArray a = new AtomicIntegerArray(1); 273 a.set(0, 1); 274 assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17)); 275 assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17)); 276 assertEquals(35, a.get(0)); 277 } 278 279 /** 280 * AtomicIntegerArray updateAndGet updates with supplied function and 281 * returns result. 282 */ testIntArrayUpdateAndGet()283 public void testIntArrayUpdateAndGet() { 284 AtomicIntegerArray a = new AtomicIntegerArray(1); 285 a.set(0, 1); 286 assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); 287 assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); 288 assertEquals(35, a.get(0)); 289 } 290 291 /** 292 * AtomicIntegerArray getAndAccumulate returns previous value and updates 293 * with supplied function. 294 */ testIntArrayGetAndAccumulate()295 public void testIntArrayGetAndAccumulate() { 296 AtomicIntegerArray a = new AtomicIntegerArray(1); 297 a.set(0, 1); 298 assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum)); 299 assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum)); 300 assertEquals(6, a.get(0)); 301 } 302 303 /** 304 * AtomicIntegerArray accumulateAndGet updates with supplied function and 305 * returns result. 306 */ testIntArrayAccumulateAndGet()307 public void testIntArrayAccumulateAndGet() { 308 AtomicIntegerArray a = new AtomicIntegerArray(1); 309 a.set(0, 1); 310 assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum)); 311 assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum)); 312 } 313 314 /** 315 * AtomicReferenceArray getAndUpdate returns previous value and updates 316 * result of supplied function 317 */ testReferenceArrayGetAndUpdate()318 public void testReferenceArrayGetAndUpdate() { 319 AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); 320 a.set(0, one); 321 assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); 322 assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); 323 assertEquals((Integer) 35, a.get(0)); 324 } 325 326 /** 327 * AtomicReferenceArray updateAndGet updates with supplied function and 328 * returns result. 329 */ testReferenceArrayUpdateAndGet()330 public void testReferenceArrayUpdateAndGet() { 331 AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); 332 a.set(0, one); 333 assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); 334 assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); 335 } 336 337 /** 338 * AtomicReferenceArray getAndAccumulate returns previous value and updates 339 * with supplied function. 340 */ testReferenceArrayGetAndAccumulate()341 public void testReferenceArrayGetAndAccumulate() { 342 AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); 343 a.set(0, one); 344 assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); 345 assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); 346 assertEquals((Integer) 6, a.get(0)); 347 } 348 349 /** 350 * AtomicReferenceArray accumulateAndGet updates with supplied function and 351 * returns result. 352 */ testReferenceArrayAccumulateAndGet()353 public void testReferenceArrayAccumulateAndGet() { 354 AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); 355 a.set(0, one); 356 assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); 357 assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); 358 } 359 360 /** 361 * AtomicLongFieldUpdater getAndUpdate returns previous value and updates 362 * result of supplied function 363 */ testLongFieldUpdaterGetAndUpdate()364 public void testLongFieldUpdaterGetAndUpdate() { 365 AtomicLongFieldUpdater a = aLongFieldUpdater(); 366 a.set(this, 1); 367 assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); 368 assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); 369 assertEquals(35L, a.get(this)); 370 assertEquals(35L, aLongField); 371 } 372 373 /** 374 * AtomicLongFieldUpdater updateAndGet updates with supplied function and 375 * returns result. 376 */ testLongFieldUpdaterUpdateAndGet()377 public void testLongFieldUpdaterUpdateAndGet() { 378 AtomicLongFieldUpdater a = aLongFieldUpdater(); 379 a.set(this, 1); 380 assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); 381 assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); 382 assertEquals(35L, a.get(this)); 383 assertEquals(35L, aLongField); 384 } 385 386 /** 387 * AtomicLongFieldUpdater getAndAccumulate returns previous value 388 * and updates with supplied function. 389 */ testLongFieldUpdaterGetAndAccumulate()390 public void testLongFieldUpdaterGetAndAccumulate() { 391 AtomicLongFieldUpdater a = aLongFieldUpdater(); 392 a.set(this, 1); 393 assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); 394 assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); 395 assertEquals(6L, a.get(this)); 396 assertEquals(6L, aLongField); 397 } 398 399 /** 400 * AtomicLongFieldUpdater accumulateAndGet updates with supplied 401 * function and returns result. 402 */ testLongFieldUpdaterAccumulateAndGet()403 public void testLongFieldUpdaterAccumulateAndGet() { 404 AtomicLongFieldUpdater a = aLongFieldUpdater(); 405 a.set(this, 1); 406 assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); 407 assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); 408 assertEquals(10L, a.get(this)); 409 assertEquals(10L, aLongField); 410 } 411 412 /** 413 * AtomicIntegerFieldUpdater getAndUpdate returns previous value and updates 414 * result of supplied function 415 */ testIntegerFieldUpdaterGetAndUpdate()416 public void testIntegerFieldUpdaterGetAndUpdate() { 417 AtomicIntegerFieldUpdater a = anIntFieldUpdater(); 418 a.set(this, 1); 419 assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); 420 assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); 421 assertEquals(35, a.get(this)); 422 assertEquals(35, anIntField); 423 } 424 425 /** 426 * AtomicIntegerFieldUpdater updateAndGet updates with supplied function and 427 * returns result. 428 */ testIntegerFieldUpdaterUpdateAndGet()429 public void testIntegerFieldUpdaterUpdateAndGet() { 430 AtomicIntegerFieldUpdater a = anIntFieldUpdater(); 431 a.set(this, 1); 432 assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); 433 assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); 434 assertEquals(35, a.get(this)); 435 assertEquals(35, anIntField); 436 } 437 438 /** 439 * AtomicIntegerFieldUpdater getAndAccumulate returns previous value 440 * and updates with supplied function. 441 */ testIntegerFieldUpdaterGetAndAccumulate()442 public void testIntegerFieldUpdaterGetAndAccumulate() { 443 AtomicIntegerFieldUpdater a = anIntFieldUpdater(); 444 a.set(this, 1); 445 assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); 446 assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); 447 assertEquals(6, a.get(this)); 448 assertEquals(6, anIntField); 449 } 450 451 /** 452 * AtomicIntegerFieldUpdater accumulateAndGet updates with supplied 453 * function and returns result. 454 */ testIntegerFieldUpdaterAccumulateAndGet()455 public void testIntegerFieldUpdaterAccumulateAndGet() { 456 AtomicIntegerFieldUpdater a = anIntFieldUpdater(); 457 a.set(this, 1); 458 assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); 459 assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); 460 assertEquals(10, a.get(this)); 461 assertEquals(10, anIntField); 462 } 463 464 /** 465 * AtomicReferenceFieldUpdater getAndUpdate returns previous value 466 * and updates result of supplied function 467 */ testReferenceFieldUpdaterGetAndUpdate()468 public void testReferenceFieldUpdaterGetAndUpdate() { 469 AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); 470 a.set(this, one); 471 assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17)); 472 assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17)); 473 assertEquals((Integer) 35, a.get(this)); 474 assertEquals((Integer) 35, anIntegerField); 475 } 476 477 /** 478 * AtomicReferenceFieldUpdater updateAndGet updates with supplied 479 * function and returns result. 480 */ testReferenceFieldUpdaterUpdateAndGet()481 public void testReferenceFieldUpdaterUpdateAndGet() { 482 AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); 483 a.set(this, one); 484 assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17)); 485 assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17)); 486 assertEquals((Integer) 35, a.get(this)); 487 assertEquals((Integer) 35, anIntegerField); 488 } 489 490 /** 491 * AtomicReferenceFieldUpdater returns previous value and updates 492 * with supplied function. 493 */ testReferenceFieldUpdaterGetAndAccumulate()494 public void testReferenceFieldUpdaterGetAndAccumulate() { 495 AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); 496 a.set(this, one); 497 assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); 498 assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); 499 assertEquals((Integer) 6, a.get(this)); 500 assertEquals((Integer) 6, anIntegerField); 501 } 502 503 /** 504 * AtomicReferenceFieldUpdater accumulateAndGet updates with 505 * supplied function and returns result. 506 */ testReferenceFieldUpdaterAccumulateAndGet()507 public void testReferenceFieldUpdaterAccumulateAndGet() { 508 AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); 509 a.set(this, one); 510 assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); 511 assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); 512 assertEquals((Integer) 10, a.get(this)); 513 assertEquals((Integer) 10, anIntegerField); 514 } 515 516 /** 517 * All Atomic getAndUpdate methods throw NullPointerException on 518 * null function argument 519 */ testGetAndUpdateNPE()520 public void testGetAndUpdateNPE() { 521 assertThrows( 522 NullPointerException.class, 523 () -> new AtomicLong().getAndUpdate(null), 524 () -> new AtomicInteger().getAndUpdate(null), 525 () -> new AtomicReference().getAndUpdate(null), 526 () -> new AtomicLongArray(1).getAndUpdate(0, null), 527 () -> new AtomicIntegerArray(1).getAndUpdate(0, null), 528 () -> new AtomicReferenceArray(1).getAndUpdate(0, null), 529 () -> aLongFieldUpdater().getAndUpdate(this, null), 530 () -> anIntFieldUpdater().getAndUpdate(this, null), 531 () -> anIntegerFieldUpdater().getAndUpdate(this, null)); 532 } 533 534 /** 535 * All Atomic updateAndGet methods throw NullPointerException on null function argument 536 */ testUpdateAndGetNPE()537 public void testUpdateAndGetNPE() { 538 assertThrows( 539 NullPointerException.class, 540 () -> new AtomicLong().updateAndGet(null), 541 () -> new AtomicInteger().updateAndGet(null), 542 () -> new AtomicReference().updateAndGet(null), 543 () -> new AtomicLongArray(1).updateAndGet(0, null), 544 () -> new AtomicIntegerArray(1).updateAndGet(0, null), 545 () -> new AtomicReferenceArray(1).updateAndGet(0, null), 546 () -> aLongFieldUpdater().updateAndGet(this, null), 547 () -> anIntFieldUpdater().updateAndGet(this, null), 548 () -> anIntegerFieldUpdater().updateAndGet(this, null)); 549 } 550 551 /** 552 * All Atomic getAndAccumulate methods throw NullPointerException 553 * on null function argument 554 */ testGetAndAccumulateNPE()555 public void testGetAndAccumulateNPE() { 556 assertThrows( 557 NullPointerException.class, 558 () -> new AtomicLong().getAndAccumulate(1L, null), 559 () -> new AtomicInteger().getAndAccumulate(1, null), 560 () -> new AtomicReference().getAndAccumulate(one, null), 561 () -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null), 562 () -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null), 563 () -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), 564 () -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), 565 () -> anIntFieldUpdater().getAndAccumulate(this, 1, null), 566 () -> anIntegerFieldUpdater().getAndAccumulate(this, one, null)); 567 } 568 569 /** 570 * All Atomic accumulateAndGet methods throw NullPointerException 571 * on null function argument 572 */ testAccumulateAndGetNPE()573 public void testAccumulateAndGetNPE() { 574 assertThrows( 575 NullPointerException.class, 576 () -> new AtomicLong().accumulateAndGet(1L, null), 577 () -> new AtomicInteger().accumulateAndGet(1, null), 578 () -> new AtomicReference().accumulateAndGet(one, null), 579 () -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null), 580 () -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null), 581 () -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), 582 () -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), 583 () -> anIntFieldUpdater().accumulateAndGet(this, 1, null), 584 () -> anIntegerFieldUpdater().accumulateAndGet(this, one, null)); 585 } 586 587 /** 588 * Object arguments for parameters of type T that are not 589 * instances of the class passed to the newUpdater call will 590 * result in a ClassCastException being thrown. 591 */ testFieldUpdaters_ClassCastException()592 public void testFieldUpdaters_ClassCastException() { 593 // Use raw types to allow passing wrong object type, provoking CCE 594 final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); 595 final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); 596 final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); 597 for (Object x : new Object[]{ new Object(), null }) { 598 assertThrows( 599 ClassCastException.class, 600 () -> longUpdater.get(x), 601 () -> intUpdater.get(x), 602 () -> refUpdater.get(x), 603 604 () -> longUpdater.set(x, 17L), 605 () -> intUpdater.set(x, 17), 606 () -> refUpdater.set(x, (Integer) 17), 607 608 () -> longUpdater.addAndGet(x, 17L), 609 () -> intUpdater.addAndGet(x, 17), 610 611 () -> longUpdater.getAndUpdate(x, y -> y), 612 () -> intUpdater.getAndUpdate(x, y -> y), 613 () -> refUpdater.getAndUpdate(x, y -> y), 614 615 () -> longUpdater.compareAndSet(x, 17L, 42L), 616 () -> intUpdater.compareAndSet(x, 17, 42), 617 () -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42)); 618 } 619 } 620 621 } 622