test/jdk/java/util/Optional/BasicInt.java
branchJDK-8195649-branch
changeset 56383 4872fc3de1b0
parent 56376 718b9df3e302
equal deleted inserted replaced
56376:718b9df3e302 56383:4872fc3de1b0
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 8195649
       
    26  * @summary Basic functional test of OptionalInt
    25  * @summary Basic functional test of OptionalInt
    27  * @author Mike Duigou
    26  * @author Mike Duigou
    28  * @build ObscureException
       
    29  * @run testng BasicInt
    27  * @run testng BasicInt
    30  */
    28  */
    31 
    29 
    32 import java.util.NoSuchElementException;
    30 import java.util.NoSuchElementException;
    33 import java.util.OptionalInt;
    31 import java.util.OptionalInt;
    35 import java.util.stream.IntStream;
    33 import java.util.stream.IntStream;
    36 
    34 
    37 import static org.testng.Assert.*;
    35 import static org.testng.Assert.*;
    38 import org.testng.annotations.Test;
    36 import org.testng.annotations.Test;
    39 
    37 
       
    38 
    40 public class BasicInt {
    39 public class BasicInt {
    41 
    40 
    42     static final int INTVAL = 33_550_336;
    41     @Test(groups = "unit")
    43     static final int UNEXPECTED = 0xCAFEBABE;
    42     public void testEmpty() {
    44 
    43         OptionalInt empty = OptionalInt.empty();
    45     /**
    44         OptionalInt present = OptionalInt.of(1);
    46      * Checks a block of assertions over an empty OptionalInt.
    45 
    47      */
    46         // empty
    48     void checkEmpty(OptionalInt empty) {
    47         assertTrue(empty.equals(empty));
    49         assertTrue(empty.equals(OptionalInt.empty()));
    48         assertTrue(empty.equals(OptionalInt.empty()));
    50         assertTrue(OptionalInt.empty().equals(empty));
    49         assertTrue(!empty.equals(present));
    51         assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
    50         assertTrue(0 == empty.hashCode());
    52         assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
    51         assertTrue(!empty.toString().isEmpty());
    53         assertFalse(empty.equals("unexpected"));
    52         assertTrue(!empty.isPresent());
    54 
    53 
    55         assertFalse(empty.isPresent());
    54         empty.ifPresent(v -> { fail(); });
    56         assertEquals(empty.hashCode(), 0);
    55 
    57         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
    56         AtomicBoolean emptyCheck = new AtomicBoolean();
    58         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
    57         empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
    59 
    58         assertTrue(emptyCheck.get());
    60         assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
    59 
    61         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
    60         try {
    62         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
    61             empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
    63 
    62             fail();
    64         var b = new AtomicBoolean();
    63         } catch (ObscureException expected) {
    65         empty.ifPresent(s -> b.set(true));
    64         } catch (AssertionError e) {
    66         assertFalse(b.get());
    65             throw e;
    67 
    66         } catch (Throwable t) {
    68         var b1 = new AtomicBoolean(false);
    67             fail();
    69         var b2 = new AtomicBoolean(false);
    68         }
    70         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
    69 
    71         assertFalse(b1.get());
    70         assertEquals(2, empty.orElse(2));
    72         assertTrue(b2.get());
    71         assertEquals(2, empty.orElseGet(()-> 2));
    73 
    72     }
    74         assertEquals(empty.toString(), "OptionalInt.empty");
    73 
    75     }
    74     @Test(groups = "unit")
    76 
    75     public void testIfPresentAndOrElseAndNull() {
    77     /**
    76         OptionalInt empty = OptionalInt.empty();
    78      * Checks a block of assertions over an OptionalInt that is expected to
    77         OptionalInt present = OptionalInt.of(1);
    79      * have a particular value present.
    78 
    80      */
    79         // No NPE
    81     void checkPresent(OptionalInt opt, int expected) {
    80         present.ifPresentOrElse(v -> {}, null);
    82         assertFalse(opt.equals(OptionalInt.empty()));
    81         empty.ifPresent(null);
    83         assertFalse(OptionalInt.empty().equals(opt));
    82         empty.ifPresentOrElse(null, () -> {});
    84         assertTrue(opt.equals(OptionalInt.of(expected)));
    83 
    85         assertTrue(OptionalInt.of(expected).equals(opt));
    84         // NPE
    86         assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
    85         try {
    87         assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
    86             present.ifPresent(null);
    88         assertFalse(opt.equals("unexpected"));
    87             fail();
    89 
    88         } catch (NullPointerException ex) {}
    90         assertTrue(opt.isPresent());
    89         try {
    91         assertEquals(opt.hashCode(), Integer.hashCode(expected));
    90             present.ifPresentOrElse(null, () -> {});
    92         assertEquals(opt.orElse(UNEXPECTED), expected);
    91             fail();
    93         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
    92         } catch (NullPointerException ex) {}
    94 
    93         try {
    95         assertEquals(opt.getAsInt(), expected);
    94             empty.ifPresentOrElse(v -> {}, null);
    96         assertEquals(opt.orElseThrow(), expected);
    95             fail();
    97         assertEquals(opt.orElseThrow(ObscureException::new), expected);
    96         } catch (NullPointerException ex) {}
    98 
    97     }
    99         var b = new AtomicBoolean(false);
    98 
   100         opt.ifPresent(s -> b.set(true));
    99     @Test(expectedExceptions=NoSuchElementException.class)
   101         assertTrue(b.get());
   100     public void testEmptyGet() {
   102 
   101         OptionalInt empty = OptionalInt.empty();
   103         var b1 = new AtomicBoolean(false);
   102 
   104         var b2 = new AtomicBoolean(false);
   103         int got = empty.getAsInt();
   105         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
   104     }
   106         assertTrue(b1.get());
   105 
   107         assertFalse(b2.get());
   106     @Test(expectedExceptions=NullPointerException.class)
   108 
   107     public void testEmptyOrElseGetNull() {
   109         assertEquals(opt.toString(), "OptionalInt[" + expected + "]");
   108         OptionalInt empty = OptionalInt.empty();
   110     }
   109 
   111 
   110         int got = empty.orElseGet(null);
   112     @Test(groups = "unit")
   111     }
   113     public void testEmpty() {
   112 
   114         checkEmpty(OptionalInt.empty());
   113     @Test(expectedExceptions=NullPointerException.class)
       
   114     public void testEmptyOrElseThrowNull() throws Throwable {
       
   115         OptionalInt empty = OptionalInt.empty();
       
   116 
       
   117         int got = empty.orElseThrow(null);
       
   118     }
       
   119 
       
   120     @Test(expectedExceptions=ObscureException.class)
       
   121     public void testEmptyOrElseThrow() throws Exception {
       
   122         OptionalInt empty = OptionalInt.empty();
       
   123 
       
   124         int got = empty.orElseThrow(ObscureException::new);
       
   125     }
       
   126 
       
   127     @Test(expectedExceptions=NoSuchElementException.class)
       
   128     public void testEmptyOrElseThrowNoArg() throws Exception {
       
   129         OptionalInt empty = OptionalInt.empty();
       
   130 
       
   131         int got = empty.orElseThrow();
   115     }
   132     }
   116 
   133 
   117     @Test(groups = "unit")
   134     @Test(groups = "unit")
   118     public void testPresent() {
   135     public void testPresent() {
   119         checkPresent(OptionalInt.of(INTVAL), INTVAL);
   136         OptionalInt empty = OptionalInt.empty();
   120     }
   137         OptionalInt present = OptionalInt.of(1);
   121 
   138 
   122     @Test(groups = "unit")
   139         // present
   123     public void testStreamEmpty() {
   140         assertTrue(present.equals(present));
   124         assertEquals(OptionalInt.empty().stream().toArray(), new int[] { });
   141         assertFalse(present.equals(OptionalInt.of(0)));
   125     }
   142         assertTrue(present.equals(OptionalInt.of(1)));
   126 
   143         assertFalse(present.equals(empty));
   127     @Test(groups = "unit")
   144         assertTrue(Integer.hashCode(1) == present.hashCode());
   128     public void testStreamPresent() {
   145         assertFalse(present.toString().isEmpty());
   129         assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL });
   146         assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
       
   147         assertTrue(-1 != present.toString().indexOf(Integer.toString(present.orElseThrow()).toString()));
       
   148         assertEquals(1, present.getAsInt());
       
   149         assertEquals(1, present.orElseThrow());
       
   150 
       
   151         AtomicBoolean presentCheck = new AtomicBoolean();
       
   152         present.ifPresent(v -> presentCheck.set(true));
       
   153         assertTrue(presentCheck.get());
       
   154         presentCheck.set(false);
       
   155         present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
       
   156         assertTrue(presentCheck.get());
       
   157 
       
   158         try {
       
   159             present.ifPresent(v -> { throw new ObscureException(); });
       
   160             fail();
       
   161         } catch (ObscureException expected) {
       
   162         } catch (AssertionError e) {
       
   163             throw e;
       
   164         } catch (Throwable t) {
       
   165             fail();
       
   166         }
       
   167         try {
       
   168             present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
       
   169             fail();
       
   170         } catch (ObscureException expected) {
       
   171         } catch (AssertionError e) {
       
   172             throw e;
       
   173         } catch (Throwable t) {
       
   174             fail();
       
   175         }
       
   176 
       
   177         assertEquals(1, present.orElse(2));
       
   178         assertEquals(1, present.orElseGet(null));
       
   179         assertEquals(1, present.orElseGet(()-> 2));
       
   180         assertEquals(1, present.orElseGet(()-> 3));
       
   181         assertEquals(1, present.<RuntimeException>orElseThrow(null));
       
   182         assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
       
   183     }
       
   184 
       
   185     @Test(groups = "unit")
       
   186     public void testStream() {
       
   187         {
       
   188             IntStream s = OptionalInt.empty().stream();
       
   189             assertFalse(s.isParallel());
       
   190 
       
   191             int[] es = s.toArray();
       
   192             assertEquals(es.length, 0);
       
   193         }
       
   194 
       
   195         {
       
   196             IntStream s = OptionalInt.of(42).stream();
       
   197             assertFalse(s.isParallel());
       
   198 
       
   199             int[] es = OptionalInt.of(42).stream().toArray();
       
   200             assertEquals(es.length, 1);
       
   201             assertEquals(es[0], 42);
       
   202         }
       
   203     }
       
   204 
       
   205     private static class ObscureException extends RuntimeException {
       
   206 
   130     }
   207     }
   131 }
   208 }