43 |
|
volatile int anIntField; |
44 |
|
volatile Integer anIntegerField; |
45 |
|
|
46 |
< |
AtomicLongFieldUpdater aLongFieldUpdater() { |
46 |
> |
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { |
47 |
|
return AtomicLongFieldUpdater.newUpdater |
48 |
|
(Atomic8Test.class, "aLongField"); |
49 |
|
} |
50 |
|
|
51 |
< |
AtomicIntegerFieldUpdater anIntFieldUpdater() { |
51 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { |
52 |
|
return AtomicIntegerFieldUpdater.newUpdater |
53 |
|
(Atomic8Test.class, "anIntField"); |
54 |
|
} |
289 |
|
* result of supplied function |
290 |
|
*/ |
291 |
|
public void testReferenceArrayGetAndUpdate() { |
292 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
292 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
293 |
|
a.set(0, one); |
294 |
|
assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
295 |
|
assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
301 |
|
* returns result. |
302 |
|
*/ |
303 |
|
public void testReferenceArrayUpdateAndGet() { |
304 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
304 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
305 |
|
a.set(0, one); |
306 |
|
assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); |
307 |
|
assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); |
312 |
|
* with supplied function. |
313 |
|
*/ |
314 |
|
public void testReferenceArrayGetAndAccumulate() { |
315 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
315 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
316 |
|
a.set(0, one); |
317 |
|
assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
318 |
|
assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
324 |
|
* returns result. |
325 |
|
*/ |
326 |
|
public void testReferenceArrayAccumulateAndGet() { |
327 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
327 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
328 |
|
a.set(0, one); |
329 |
|
assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
330 |
|
assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
491 |
|
* null function argument |
492 |
|
*/ |
493 |
|
public void testGetAndUpdateNPE() { |
494 |
< |
Runnable[] throwingActions = { |
494 |
> |
assertThrows( |
495 |
> |
NullPointerException.class, |
496 |
|
() -> new AtomicLong().getAndUpdate(null), |
497 |
|
() -> new AtomicInteger().getAndUpdate(null), |
498 |
|
() -> new AtomicReference().getAndUpdate(null), |
501 |
|
() -> new AtomicReferenceArray(1).getAndUpdate(0, null), |
502 |
|
() -> aLongFieldUpdater().getAndUpdate(this, null), |
503 |
|
() -> anIntFieldUpdater().getAndUpdate(this, null), |
504 |
< |
() -> anIntegerFieldUpdater().getAndUpdate(this, null), |
504 |
< |
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), |
505 |
< |
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), |
506 |
< |
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), |
507 |
< |
}; |
508 |
< |
assertThrows(NullPointerException.class, throwingActions); |
504 |
> |
() -> anIntegerFieldUpdater().getAndUpdate(this, null)); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* All Atomic updateAndGet methods throw NullPointerException on null function argument |
509 |
|
*/ |
510 |
|
public void testUpdateAndGetNPE() { |
511 |
< |
Runnable[] throwingActions = { |
511 |
> |
assertThrows( |
512 |
> |
NullPointerException.class, |
513 |
|
() -> new AtomicLong().updateAndGet(null), |
514 |
|
() -> new AtomicInteger().updateAndGet(null), |
515 |
|
() -> new AtomicReference().updateAndGet(null), |
518 |
|
() -> new AtomicReferenceArray(1).updateAndGet(0, null), |
519 |
|
() -> aLongFieldUpdater().updateAndGet(this, null), |
520 |
|
() -> anIntFieldUpdater().updateAndGet(this, null), |
521 |
< |
() -> anIntegerFieldUpdater().updateAndGet(this, null), |
525 |
< |
}; |
526 |
< |
assertThrows(NullPointerException.class, throwingActions); |
521 |
> |
() -> anIntegerFieldUpdater().updateAndGet(this, null)); |
522 |
|
} |
523 |
|
|
524 |
|
/** |
526 |
|
* on null function argument |
527 |
|
*/ |
528 |
|
public void testGetAndAccumulateNPE() { |
529 |
< |
Runnable[] throwingActions = { |
529 |
> |
assertThrows( |
530 |
> |
NullPointerException.class, |
531 |
|
() -> new AtomicLong().getAndAccumulate(1L, null), |
532 |
|
() -> new AtomicInteger().getAndAccumulate(1, null), |
533 |
|
() -> new AtomicReference().getAndAccumulate(one, null), |
536 |
|
() -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), |
537 |
|
() -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), |
538 |
|
() -> anIntFieldUpdater().getAndAccumulate(this, 1, null), |
539 |
< |
() -> anIntegerFieldUpdater().getAndAccumulate(this, one, null), |
544 |
< |
}; |
545 |
< |
assertThrows(NullPointerException.class, throwingActions); |
539 |
> |
() -> anIntegerFieldUpdater().getAndAccumulate(this, one, null)); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
544 |
|
* on null function argument |
545 |
|
*/ |
546 |
|
public void testAccumulateAndGetNPE() { |
547 |
< |
Runnable[] throwingActions = { |
547 |
> |
assertThrows( |
548 |
> |
NullPointerException.class, |
549 |
|
() -> new AtomicLong().accumulateAndGet(1L, null), |
550 |
|
() -> new AtomicInteger().accumulateAndGet(1, null), |
551 |
|
() -> new AtomicReference().accumulateAndGet(one, null), |
554 |
|
() -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), |
555 |
|
() -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), |
556 |
|
() -> anIntFieldUpdater().accumulateAndGet(this, 1, null), |
557 |
< |
() -> anIntegerFieldUpdater().accumulateAndGet(this, one, null), |
558 |
< |
}; |
559 |
< |
assertThrows(NullPointerException.class, throwingActions); |
557 |
> |
() -> anIntegerFieldUpdater().accumulateAndGet(this, one, null)); |
558 |
> |
} |
559 |
> |
|
560 |
> |
/** |
561 |
> |
* Object arguments for parameters of type T that are not |
562 |
> |
* instances of the class passed to the newUpdater call will |
563 |
> |
* result in a ClassCastException being thrown. |
564 |
> |
*/ |
565 |
> |
public void testFieldUpdaters_ClassCastException() { |
566 |
> |
// Use raw types to allow passing wrong object type, provoking CCE |
567 |
> |
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); |
568 |
> |
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); |
569 |
> |
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); |
570 |
> |
for (Object x : new Object[]{ new Object(), null }) { |
571 |
> |
assertThrows( |
572 |
> |
ClassCastException.class, |
573 |
> |
() -> longUpdater.get(x), |
574 |
> |
() -> intUpdater.get(x), |
575 |
> |
() -> refUpdater.get(x), |
576 |
> |
|
577 |
> |
() -> longUpdater.set(x, 17L), |
578 |
> |
() -> intUpdater.set(x, 17), |
579 |
> |
() -> refUpdater.set(x, (Integer) 17), |
580 |
> |
|
581 |
> |
() -> longUpdater.addAndGet(x, 17L), |
582 |
> |
() -> intUpdater.addAndGet(x, 17), |
583 |
> |
|
584 |
> |
() -> longUpdater.getAndUpdate(x, y -> y), |
585 |
> |
() -> intUpdater.getAndUpdate(x, y -> y), |
586 |
> |
() -> refUpdater.getAndUpdate(x, y -> y), |
587 |
> |
|
588 |
> |
() -> longUpdater.compareAndSet(x, 17L, 42L), |
589 |
> |
() -> intUpdater.compareAndSet(x, 17, 42), |
590 |
> |
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42)); |
591 |
> |
} |
592 |
|
} |
593 |
|
|
594 |
|
} |