ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Atomic8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Atomic8Test.java (file contents):
Revision 1.7 by jsr166, Sat Sep 24 12:54:31 2016 UTC vs.
Revision 1.9 by jsr166, Wed Jan 4 06:09:58 2017 UTC

# Line 43 | Line 43 | public class Atomic8Test extends JSR166T
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      }
# Line 289 | Line 289 | public class Atomic8Test extends JSR166T
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));
# Line 301 | Line 301 | public class Atomic8Test extends JSR166T
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));
# Line 312 | Line 312 | public class Atomic8Test extends JSR166T
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));
# Line 324 | Line 324 | public class Atomic8Test extends JSR166T
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));
# Line 501 | Line 501 | public class Atomic8Test extends JSR166T
501              () -> aLongFieldUpdater().getAndUpdate(this, null),
502              () -> anIntFieldUpdater().getAndUpdate(this, null),
503              () -> anIntegerFieldUpdater().getAndUpdate(this, null),
504            ////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17),
505            ////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17),
506            ////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17),
504          };
505          assertThrows(NullPointerException.class, throwingActions);
506      }
# Line 564 | Line 561 | public class Atomic8Test extends JSR166T
561          assertThrows(NullPointerException.class, throwingActions);
562      }
563  
564 +    /**
565 +     * Object arguments for parameters of type T that are not
566 +     * instances of the class passed to the newUpdater call will
567 +     * result in a ClassCastException being thrown.
568 +     */
569 +    public void testFieldUpdaters_ClassCastException() {
570 +        // Use raw types to allow passing wrong object type, provoking CCE
571 +        final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater();
572 +        final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater();
573 +        final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater();
574 +        final Object obj = new Object();
575 +        for (Object x : new Object[]{ new Object(), null }) {
576 +            Runnable[] throwingActions = {
577 +                () -> longUpdater.get(x),
578 +                () -> intUpdater.get(x),
579 +                () -> refUpdater.get(x),
580 +
581 +                () -> longUpdater.set(x, 17L),
582 +                () -> intUpdater.set(x, 17),
583 +                () -> refUpdater.set(x, (Integer) 17),
584 +
585 +                () -> longUpdater.addAndGet(x, 17L),
586 +                () -> intUpdater.addAndGet(x, 17),
587 +
588 +                () -> longUpdater.getAndUpdate(x, y -> y),
589 +                () -> intUpdater.getAndUpdate(x, y -> y),
590 +                () -> refUpdater.getAndUpdate(x, y -> y),
591 +
592 +                () -> longUpdater.compareAndSet(x, 17L, 42L),
593 +                () -> intUpdater.compareAndSet(x, 17, 42),
594 +                () -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42),
595 +            };
596 +            assertThrows(ClassCastException.class, throwingActions);
597 +        }
598 +    }
599 +
600   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines