18 |
|
import junit.framework.Test; |
19 |
|
import junit.framework.TestSuite; |
20 |
|
|
21 |
+ |
/** |
22 |
+ |
* Tests of atomic class methods accepting lambdas introduced in JDK8. |
23 |
+ |
*/ |
24 |
|
public class Atomic8Test extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(Atomic8Test.class); |
31 |
|
} |
32 |
|
|
30 |
– |
/* |
31 |
– |
* Tests of atomic class methods accepting lambdas |
32 |
– |
* introduced in JDK8. |
33 |
– |
*/ |
34 |
– |
|
33 |
|
static long addLong17(long x) { return x + 17; } |
34 |
|
static int addInt17(int x) { return x + 17; } |
35 |
|
static Integer addInteger17(Integer x) { |
36 |
< |
return new Integer(x.intValue() + 17); |
36 |
> |
return x.intValue() + 17; |
37 |
|
} |
38 |
|
static Integer sumInteger(Integer x, Integer y) { |
39 |
< |
return new Integer(x.intValue() + y.intValue()); |
39 |
> |
return x.intValue() + y.intValue(); |
40 |
|
} |
41 |
|
|
42 |
|
volatile long aLongField; |
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 |
|
} |
150 |
|
* result of supplied function |
151 |
|
*/ |
152 |
|
public void testReferenceGetAndUpdate() { |
153 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
154 |
< |
assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17)); |
155 |
< |
assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17)); |
156 |
< |
assertEquals(new Integer(35), a.get()); |
153 |
> |
AtomicReference<Integer> a = new AtomicReference<>(one); |
154 |
> |
assertEquals((Integer) 1, a.getAndUpdate(Atomic8Test::addInteger17)); |
155 |
> |
assertEquals((Integer) 18, a.getAndUpdate(Atomic8Test::addInteger17)); |
156 |
> |
assertEquals((Integer) 35, a.get()); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
161 |
|
* returns result. |
162 |
|
*/ |
163 |
|
public void testReferenceUpdateAndGet() { |
164 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
165 |
< |
assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); |
166 |
< |
assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); |
167 |
< |
assertEquals(new Integer(35), a.get()); |
164 |
> |
AtomicReference<Integer> a = new AtomicReference<>(one); |
165 |
> |
assertEquals((Integer) 18, a.updateAndGet(Atomic8Test::addInteger17)); |
166 |
> |
assertEquals((Integer) 35, a.updateAndGet(Atomic8Test::addInteger17)); |
167 |
> |
assertEquals((Integer) 35, a.get()); |
168 |
|
} |
169 |
|
|
170 |
|
/** |
172 |
|
* with supplied function. |
173 |
|
*/ |
174 |
|
public void testReferenceGetAndAccumulate() { |
175 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
176 |
< |
assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger)); |
177 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger)); |
178 |
< |
assertEquals(new Integer(6), a.get()); |
175 |
> |
AtomicReference<Integer> a = new AtomicReference<>(one); |
176 |
> |
assertEquals((Integer) 1, a.getAndAccumulate(2, Atomic8Test::sumInteger)); |
177 |
> |
assertEquals((Integer) 3, a.getAndAccumulate(3, Atomic8Test::sumInteger)); |
178 |
> |
assertEquals((Integer) 6, a.get()); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
183 |
|
* returns result. |
184 |
|
*/ |
185 |
|
public void testReferenceAccumulateAndGet() { |
186 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
187 |
< |
assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); |
188 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); |
189 |
< |
assertEquals(new Integer(10), a.get()); |
186 |
> |
AtomicReference<Integer> a = new AtomicReference<>(one); |
187 |
> |
assertEquals((Integer) 7, a.accumulateAndGet(6, Atomic8Test::sumInteger)); |
188 |
> |
assertEquals((Integer) 10, a.accumulateAndGet(3, Atomic8Test::sumInteger)); |
189 |
> |
assertEquals((Integer) 10, a.get()); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
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(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
295 |
< |
assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
296 |
< |
assertEquals(new Integer(35), a.get(0)); |
294 |
> |
assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
295 |
> |
assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
296 |
> |
assertEquals((Integer) 35, a.get(0)); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
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(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17)); |
307 |
< |
assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17)); |
306 |
> |
assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); |
307 |
> |
assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
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(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
318 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
319 |
< |
assertEquals(new Integer(6), a.get(0)); |
317 |
> |
assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
318 |
> |
assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
319 |
> |
assertEquals((Integer) 6, a.get(0)); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
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(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
330 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
329 |
> |
assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
330 |
> |
assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
441 |
|
public void testReferenceFieldUpdaterGetAndUpdate() { |
442 |
|
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
443 |
|
a.set(this, one); |
444 |
< |
assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
445 |
< |
assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
446 |
< |
assertEquals(new Integer(35), a.get(this)); |
447 |
< |
assertEquals(new Integer(35), anIntegerField); |
444 |
> |
assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17)); |
445 |
> |
assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17)); |
446 |
> |
assertEquals((Integer) 35, a.get(this)); |
447 |
> |
assertEquals((Integer) 35, anIntegerField); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
454 |
|
public void testReferenceFieldUpdaterUpdateAndGet() { |
455 |
|
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
456 |
|
a.set(this, one); |
457 |
< |
assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); |
458 |
< |
assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); |
459 |
< |
assertEquals(new Integer(35), a.get(this)); |
460 |
< |
assertEquals(new Integer(35), anIntegerField); |
457 |
> |
assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17)); |
458 |
> |
assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17)); |
459 |
> |
assertEquals((Integer) 35, a.get(this)); |
460 |
> |
assertEquals((Integer) 35, anIntegerField); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
467 |
|
public void testReferenceFieldUpdaterGetAndAccumulate() { |
468 |
|
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
469 |
|
a.set(this, one); |
470 |
< |
assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); |
471 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); |
472 |
< |
assertEquals(new Integer(6), a.get(this)); |
473 |
< |
assertEquals(new Integer(6), anIntegerField); |
470 |
> |
assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); |
471 |
> |
assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); |
472 |
> |
assertEquals((Integer) 6, a.get(this)); |
473 |
> |
assertEquals((Integer) 6, anIntegerField); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
480 |
|
public void testReferenceFieldUpdaterAccumulateAndGet() { |
481 |
|
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
482 |
|
a.set(this, one); |
483 |
< |
assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); |
484 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); |
485 |
< |
assertEquals(new Integer(10), a.get(this)); |
486 |
< |
assertEquals(new Integer(10), anIntegerField); |
483 |
> |
assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); |
484 |
> |
assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); |
485 |
> |
assertEquals((Integer) 10, a.get(this)); |
486 |
> |
assertEquals((Integer) 10, anIntegerField); |
487 |
|
} |
488 |
|
|
489 |
|
/** |
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), |
506 |
< |
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), |
507 |
< |
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), |
508 |
< |
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), |
509 |
< |
}; |
510 |
< |
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), |
527 |
< |
}; |
528 |
< |
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), |
546 |
< |
}; |
547 |
< |
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 |
|
} |