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.1 by dl, Sun Sep 8 23:00:36 2013 UTC vs.
Revision 1.7 by jsr166, Sat Sep 24 12:54:31 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
9 < import java.util.*;
10 < import java.util.concurrent.*;
11 < import java.util.concurrent.atomic.*;
8 > import java.util.concurrent.atomic.AtomicInteger;
9 > import java.util.concurrent.atomic.AtomicIntegerArray;
10 > import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
11 > import java.util.concurrent.atomic.AtomicLong;
12 > import java.util.concurrent.atomic.AtomicLongArray;
13 > import java.util.concurrent.atomic.AtomicLongFieldUpdater;
14 > import java.util.concurrent.atomic.AtomicReference;
15 > import java.util.concurrent.atomic.AtomicReferenceArray;
16 > import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17  
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  
22    /*
23     * Tests of atomic class methods accepting lambdas
24     * introduced in JDK8.
25     */
26
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() {
47 +        return AtomicLongFieldUpdater.newUpdater
48 +            (Atomic8Test.class, "aLongField");
49 +    }
50 +
51 +    AtomicIntegerFieldUpdater anIntFieldUpdater() {
52 +        return AtomicIntegerFieldUpdater.newUpdater
53 +            (Atomic8Test.class, "anIntField");
54 +    }
55 +
56 +    AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() {
57 +        return AtomicReferenceFieldUpdater.newUpdater
58 +            (Atomic8Test.class, Integer.class, "anIntegerField");
59 +    }
60 +
61      /**
62       * AtomicLong getAndUpdate returns previous value and updates
63       * result of supplied function
# Line 77 | Line 98 | public class Atomic8Test extends JSR166T
98          AtomicLong a = new AtomicLong(1L);
99          assertEquals(7L, a.accumulateAndGet(6L, Long::sum));
100          assertEquals(10L, a.accumulateAndGet(3L, Long::sum));
101 +        assertEquals(10L, a.get());
102      }
103  
104      /**
# Line 98 | Line 120 | public class Atomic8Test extends JSR166T
120          AtomicInteger a = new AtomicInteger(1);
121          assertEquals(18, a.updateAndGet(Atomic8Test::addInt17));
122          assertEquals(35, a.updateAndGet(Atomic8Test::addInt17));
123 +        assertEquals(35, a.get());
124      }
125  
126      /**
# Line 119 | Line 142 | public class Atomic8Test extends JSR166T
142          AtomicInteger a = new AtomicInteger(1);
143          assertEquals(7, a.accumulateAndGet(6, Integer::sum));
144          assertEquals(10, a.accumulateAndGet(3, Integer::sum));
145 +        assertEquals(10, a.get());
146      }
147  
148      /**
# Line 126 | Line 150 | public class Atomic8Test extends JSR166T
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      /**
# Line 137 | Line 161 | public class Atomic8Test extends JSR166T
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));
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      /**
# Line 147 | Line 172 | public class Atomic8Test extends JSR166T
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      /**
# Line 158 | Line 183 | public class Atomic8Test extends JSR166T
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));
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  
166
192      /**
193       * AtomicLongArray getAndUpdate returns previous value and updates
194       * result of supplied function
# Line 185 | Line 210 | public class Atomic8Test extends JSR166T
210          a.set(0, 1);
211          assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17));
212          assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17));
213 +        assertEquals(35L, a.get(0));
214      }
215  
216      /**
# Line 208 | Line 234 | public class Atomic8Test extends JSR166T
234          a.set(0, 1);
235          assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum));
236          assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum));
237 +        assertEquals(10L, a.get(0));
238      }
239  
240      /**
# Line 231 | Line 258 | public class Atomic8Test extends JSR166T
258          a.set(0, 1);
259          assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17));
260          assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17));
261 +        assertEquals(35, a.get(0));
262      }
263  
264      /**
# Line 263 | Line 291 | public class Atomic8Test extends JSR166T
291      public void testReferenceArrayGetAndUpdate() {
292          AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(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      /**
# Line 275 | Line 303 | public class Atomic8Test extends JSR166T
303      public void testReferenceArrayUpdateAndGet() {
304          AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(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      /**
# Line 286 | Line 314 | public class Atomic8Test extends JSR166T
314      public void testReferenceArrayGetAndAccumulate() {
315          AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(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      /**
# Line 298 | Line 326 | public class Atomic8Test extends JSR166T
326      public void testReferenceArrayAccumulateAndGet() {
327          AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(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      /**
# Line 307 | Line 335 | public class Atomic8Test extends JSR166T
335       * result of supplied function
336       */
337      public void testLongFieldUpdaterGetAndUpdate() {
338 <        AtomicLongFieldUpdater a = AtomicLongFieldUpdater.
311 <            newUpdater(Atomic8Test.class, "aLongField");
338 >        AtomicLongFieldUpdater a = aLongFieldUpdater();
339          a.set(this, 1);
340          assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17));
341          assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17));
342          assertEquals(35L, a.get(this));
343 +        assertEquals(35L, aLongField);
344      }
345  
346      /**
# Line 320 | Line 348 | public class Atomic8Test extends JSR166T
348       * returns result.
349       */
350      public void testLongFieldUpdaterUpdateAndGet() {
351 <        AtomicLongFieldUpdater a = AtomicLongFieldUpdater.
324 <            newUpdater(Atomic8Test.class, "aLongField");
351 >        AtomicLongFieldUpdater a = aLongFieldUpdater();
352          a.set(this, 1);
353          assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17));
354          assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17));
355 +        assertEquals(35L, a.get(this));
356 +        assertEquals(35L, aLongField);
357      }
358  
359      /**
# Line 332 | Line 361 | public class Atomic8Test extends JSR166T
361       * and updates with supplied function.
362       */
363      public void testLongFieldUpdaterGetAndAccumulate() {
364 <        AtomicLongFieldUpdater a = AtomicLongFieldUpdater.
336 <            newUpdater(Atomic8Test.class, "aLongField");
364 >        AtomicLongFieldUpdater a = aLongFieldUpdater();
365          a.set(this, 1);
366          assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum));
367          assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum));
368          assertEquals(6L, a.get(this));
369 +        assertEquals(6L, aLongField);
370      }
371  
372      /**
# Line 345 | Line 374 | public class Atomic8Test extends JSR166T
374       * function and returns result.
375       */
376      public void testLongFieldUpdaterAccumulateAndGet() {
377 <        AtomicLongFieldUpdater a = AtomicLongFieldUpdater.
349 <            newUpdater(Atomic8Test.class, "aLongField");
377 >        AtomicLongFieldUpdater a = aLongFieldUpdater();
378          a.set(this, 1);
379          assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum));
380          assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum));
381 +        assertEquals(10L, a.get(this));
382 +        assertEquals(10L, aLongField);
383      }
384  
385      /**
# Line 357 | Line 387 | public class Atomic8Test extends JSR166T
387       * result of supplied function
388       */
389      public void testIntegerFieldUpdaterGetAndUpdate() {
390 <        AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater.
361 <            newUpdater(Atomic8Test.class, "anIntField");
390 >        AtomicIntegerFieldUpdater a = anIntFieldUpdater();
391          a.set(this, 1);
392          assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17));
393          assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17));
394          assertEquals(35, a.get(this));
395 +        assertEquals(35, anIntField);
396      }
397  
398      /**
# Line 370 | Line 400 | public class Atomic8Test extends JSR166T
400       * returns result.
401       */
402      public void testIntegerFieldUpdaterUpdateAndGet() {
403 <        AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater.
374 <            newUpdater(Atomic8Test.class, "anIntField");
403 >        AtomicIntegerFieldUpdater a = anIntFieldUpdater();
404          a.set(this, 1);
405          assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17));
406          assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17));
407 +        assertEquals(35, a.get(this));
408 +        assertEquals(35, anIntField);
409      }
410  
411      /**
# Line 382 | Line 413 | public class Atomic8Test extends JSR166T
413       * and updates with supplied function.
414       */
415      public void testIntegerFieldUpdaterGetAndAccumulate() {
416 <        AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater.
386 <            newUpdater(Atomic8Test.class, "anIntField");
416 >        AtomicIntegerFieldUpdater a = anIntFieldUpdater();
417          a.set(this, 1);
418          assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum));
419          assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum));
420          assertEquals(6, a.get(this));
421 +        assertEquals(6, anIntField);
422      }
423  
424      /**
# Line 395 | Line 426 | public class Atomic8Test extends JSR166T
426       * function and returns result.
427       */
428      public void testIntegerFieldUpdaterAccumulateAndGet() {
429 <        AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater.
399 <            newUpdater(Atomic8Test.class, "anIntField");
429 >        AtomicIntegerFieldUpdater a = anIntFieldUpdater();
430          a.set(this, 1);
431          assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum));
432          assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum));
433 +        assertEquals(10, a.get(this));
434 +        assertEquals(10, anIntField);
435      }
436  
405
437      /**
438       * AtomicReferenceFieldUpdater getAndUpdate returns previous value
439       * and updates result of supplied function
440       */
441      public void testReferenceFieldUpdaterGetAndUpdate() {
442 <        AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = AtomicReferenceFieldUpdater.
412 <            newUpdater(Atomic8Test.class, Integer.class, "anIntegerField");
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));
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      /**
# Line 421 | Line 452 | public class Atomic8Test extends JSR166T
452       * function and returns result.
453       */
454      public void testReferenceFieldUpdaterUpdateAndGet() {
455 <        AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = AtomicReferenceFieldUpdater.
425 <            newUpdater(Atomic8Test.class, Integer.class, "anIntegerField");
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));
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      /**
# Line 433 | Line 465 | public class Atomic8Test extends JSR166T
465       * with supplied function.
466       */
467      public void testReferenceFieldUpdaterGetAndAccumulate() {
468 <        AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = AtomicReferenceFieldUpdater.
437 <            newUpdater(Atomic8Test.class, Integer.class, "anIntegerField");
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));
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      /**
# Line 446 | Line 478 | public class Atomic8Test extends JSR166T
478       * supplied function and returns result.
479       */
480      public void testReferenceFieldUpdaterAccumulateAndGet() {
481 <        AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = AtomicReferenceFieldUpdater.
450 <            newUpdater(Atomic8Test.class, Integer.class, "anIntegerField");
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));
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      /**
490 <     * All Atomic getAndUpdate methods throw npe on null function argument
490 >     * All Atomic getAndUpdate methods throw NullPointerException on
491 >     * null function argument
492       */
493      public void testGetAndUpdateNPE() {
494 <        try { new AtomicLong().getAndUpdate(null); shouldThrow();
495 <        } catch(NullPointerException ok) {}
496 <        try { new AtomicInteger().getAndUpdate(null); shouldThrow();
497 <        } catch(NullPointerException ok) {}
498 <        try { new AtomicReference().getAndUpdate(null); shouldThrow();
499 <        } catch(NullPointerException ok) {}
500 <        try { new AtomicLongArray(1).getAndUpdate(0, null); shouldThrow();
501 <        } catch(NullPointerException ok) {}
502 <        try { new AtomicIntegerArray(1).getAndUpdate(0, null); shouldThrow();
503 <        } catch(NullPointerException ok) {}
504 <        try { new AtomicReferenceArray(1).getAndUpdate(0, null); shouldThrow();
505 <        } catch(NullPointerException ok) {}
506 <        try { AtomicLongFieldUpdater.
507 <                newUpdater(Atomic8Test.class, "aLongField").
508 <                getAndUpdate(this, null);
475 <            shouldThrow();
476 <        } catch(NullPointerException ok) {}
477 <        try { AtomicIntegerFieldUpdater.
478 <                newUpdater(Atomic8Test.class, "anIntField").
479 <                getAndUpdate(this, null);
480 <            shouldThrow();
481 <        } catch(NullPointerException ok) {}
482 <        try {  AtomicReferenceFieldUpdater.
483 <                newUpdater(Atomic8Test.class, Integer.class, "anIntegerField").
484 <                getAndUpdate(this, null);
485 <            shouldThrow();
486 <        } catch(NullPointerException ok) {}
487 <    }
488 <
489 <    /**
490 <     * All Atomic updateAndGet methods throw npe on null function argument
491 <     */
492 <    public void testUpdateGetAndNPE() {
493 <        try { new AtomicLong().updateAndGet(null); shouldThrow();
494 <        } catch(NullPointerException ok) {}
495 <        try { new AtomicInteger().updateAndGet(null); shouldThrow();
496 <        } catch(NullPointerException ok) {}
497 <        try { new AtomicReference().updateAndGet(null); shouldThrow();
498 <        } catch(NullPointerException ok) {}
499 <        try { new AtomicLongArray(1).updateAndGet(0, null); shouldThrow();
500 <        } catch(NullPointerException ok) {}
501 <        try { new AtomicIntegerArray(1).updateAndGet(0, null); shouldThrow();
502 <        } catch(NullPointerException ok) {}
503 <        try { new AtomicReferenceArray(1).updateAndGet(0, null); shouldThrow();
504 <        } catch(NullPointerException ok) {}
505 <        try { AtomicLongFieldUpdater.
506 <                newUpdater(Atomic8Test.class, "aLongField").
507 <                updateAndGet(this, null);
508 <            shouldThrow();
509 <        } catch(NullPointerException ok) {}
510 <        try { AtomicIntegerFieldUpdater.
511 <                newUpdater(Atomic8Test.class, "anIntField").
512 <                updateAndGet(this, null);
513 <            shouldThrow();
514 <        } catch(NullPointerException ok) {}
515 <        try {  AtomicReferenceFieldUpdater.
516 <                newUpdater(Atomic8Test.class, Integer.class, "anIntegerField").
517 <                updateAndGet(this, null);
518 <            shouldThrow();
519 <        } catch(NullPointerException ok) {}
494 >        Runnable[] throwingActions = {
495 >            () -> new AtomicLong().getAndUpdate(null),
496 >            () -> new AtomicInteger().getAndUpdate(null),
497 >            () -> new AtomicReference().getAndUpdate(null),
498 >            () -> new AtomicLongArray(1).getAndUpdate(0, null),
499 >            () -> new AtomicIntegerArray(1).getAndUpdate(0, null),
500 >            () -> new AtomicReferenceArray(1).getAndUpdate(0, null),
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),
507 >        };
508 >        assertThrows(NullPointerException.class, throwingActions);
509      }
510  
511      /**
512 <     * All Atomic getAndAccumulate methods throw npe on null function argument
512 >     * All Atomic updateAndGet methods throw NullPointerException on null function argument
513 >     */
514 >    public void testUpdateAndGetNPE() {
515 >        Runnable[] throwingActions = {
516 >            () -> new AtomicLong().updateAndGet(null),
517 >            () -> new AtomicInteger().updateAndGet(null),
518 >            () -> new AtomicReference().updateAndGet(null),
519 >            () -> new AtomicLongArray(1).updateAndGet(0, null),
520 >            () -> new AtomicIntegerArray(1).updateAndGet(0, null),
521 >            () -> new AtomicReferenceArray(1).updateAndGet(0, null),
522 >            () -> aLongFieldUpdater().updateAndGet(this, null),
523 >            () -> anIntFieldUpdater().updateAndGet(this, null),
524 >            () -> anIntegerFieldUpdater().updateAndGet(this, null),
525 >        };
526 >        assertThrows(NullPointerException.class, throwingActions);
527 >    }
528 >
529 >    /**
530 >     * All Atomic getAndAccumulate methods throw NullPointerException
531 >     * on null function argument
532       */
533      public void testGetAndAccumulateNPE() {
534 <        try { new AtomicLong().getAndAccumulate(1L, null); shouldThrow();
535 <        } catch(NullPointerException ok) {}
536 <        try { new AtomicInteger().getAndAccumulate(1, null); shouldThrow();
537 <        } catch(NullPointerException ok) {}
538 <        try { new AtomicReference().getAndAccumulate(one, null); shouldThrow();
539 <        } catch(NullPointerException ok) {}
540 <        try { new AtomicLongArray(1).getAndAccumulate(0, 1L, null); shouldThrow();
541 <        } catch(NullPointerException ok) {}
542 <        try { new AtomicIntegerArray(1).getAndAccumulate(0, 1, null); shouldThrow();
543 <        } catch(NullPointerException ok) {}
544 <        try { new AtomicReferenceArray(1).getAndAccumulate(0, one, null); shouldThrow();
545 <        } catch(NullPointerException ok) {}
538 <        try { AtomicLongFieldUpdater.
539 <                newUpdater(Atomic8Test.class, "aLongField").
540 <                getAndAccumulate(this, 1L, null);
541 <            shouldThrow();
542 <        } catch(NullPointerException ok) {}
543 <        try { AtomicIntegerFieldUpdater.
544 <                newUpdater(Atomic8Test.class, "anIntField").
545 <                getAndAccumulate(this, 1, null);
546 <            shouldThrow();
547 <        } catch(NullPointerException ok) {}
548 <        try {  AtomicReferenceFieldUpdater.
549 <                newUpdater(Atomic8Test.class, Integer.class, "anIntegerField").
550 <                getAndAccumulate(this, one, null);
551 <            shouldThrow();
552 <        } catch(NullPointerException ok) {}
534 >        Runnable[] throwingActions = {
535 >            () -> new AtomicLong().getAndAccumulate(1L, null),
536 >            () -> new AtomicInteger().getAndAccumulate(1, null),
537 >            () -> new AtomicReference().getAndAccumulate(one, null),
538 >            () -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null),
539 >            () -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null),
540 >            () -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null),
541 >            () -> aLongFieldUpdater().getAndAccumulate(this, 1L, null),
542 >            () -> anIntFieldUpdater().getAndAccumulate(this, 1, null),
543 >            () -> anIntegerFieldUpdater().getAndAccumulate(this, one, null),
544 >        };
545 >        assertThrows(NullPointerException.class, throwingActions);
546      }
547  
548      /**
549 <     * All Atomic accumulateAndGet methods throw npe on null function argument
549 >     * All Atomic accumulateAndGet methods throw NullPointerException
550 >     * on null function argument
551       */
552      public void testAccumulateAndGetNPE() {
553 <        try { new AtomicLong().accumulateAndGet(1L, null); shouldThrow();
554 <        } catch(NullPointerException ok) {}
555 <        try { new AtomicInteger().accumulateAndGet(1, null); shouldThrow();
556 <        } catch(NullPointerException ok) {}
557 <        try { new AtomicReference().accumulateAndGet(one, null); shouldThrow();
558 <        } catch(NullPointerException ok) {}
559 <        try { new AtomicLongArray(1).accumulateAndGet(0, 1L, null); shouldThrow();
560 <        } catch(NullPointerException ok) {}
561 <        try { new AtomicIntegerArray(1).accumulateAndGet(0, 1, null); shouldThrow();
562 <        } catch(NullPointerException ok) {}
563 <        try { new AtomicReferenceArray(1).accumulateAndGet(0, one, null); shouldThrow();
564 <        } catch(NullPointerException ok) {}
571 <        try { AtomicLongFieldUpdater.
572 <                newUpdater(Atomic8Test.class, "aLongField").
573 <                accumulateAndGet(this, 1L, null);
574 <            shouldThrow();
575 <        } catch(NullPointerException ok) {}
576 <        try { AtomicIntegerFieldUpdater.
577 <                newUpdater(Atomic8Test.class, "anIntField").
578 <                accumulateAndGet(this, 1, null);
579 <            shouldThrow();
580 <        } catch(NullPointerException ok) {}
581 <        try {  AtomicReferenceFieldUpdater.
582 <                newUpdater(Atomic8Test.class, Integer.class, "anIntegerField").
583 <                accumulateAndGet(this, one, null);
584 <            shouldThrow();
585 <        } catch(NullPointerException ok) {}
553 >        Runnable[] throwingActions = {
554 >            () -> new AtomicLong().accumulateAndGet(1L, null),
555 >            () -> new AtomicInteger().accumulateAndGet(1, null),
556 >            () -> new AtomicReference().accumulateAndGet(one, null),
557 >            () -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null),
558 >            () -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null),
559 >            () -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null),
560 >            () -> aLongFieldUpdater().accumulateAndGet(this, 1L, null),
561 >            () -> anIntFieldUpdater().accumulateAndGet(this, 1, null),
562 >            () -> anIntegerFieldUpdater().accumulateAndGet(this, one, null),
563 >        };
564 >        assertThrows(NullPointerException.class, throwingActions);
565      }
566  
567   }
589

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines