test/jdk/java/util/Optional/BasicLong.java
branchJDK-8195649-branch
changeset 56376 718b9df3e302
parent 56375 e0530ee8af48
child 56383 4872fc3de1b0
equal deleted inserted replaced
56375:e0530ee8af48 56376:718b9df3e302
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2018, 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
    25  * @summary Basic functional test of OptionalLong
    26  * @summary Basic functional test of OptionalLong
    26  * @author Mike Duigou
    27  * @author Mike Duigou
    27  * @build ObscureException
    28  * @build ObscureException
    28  * @run testng BasicLong
    29  * @run testng BasicLong
    29  */
    30  */
    34 import java.util.stream.LongStream;
    35 import java.util.stream.LongStream;
    35 
    36 
    36 import static org.testng.Assert.*;
    37 import static org.testng.Assert.*;
    37 import org.testng.annotations.Test;
    38 import org.testng.annotations.Test;
    38 
    39 
       
    40 public class BasicLong {
       
    41     static final long LONGVAL = 2_305_843_008_139_952_128L;
       
    42     static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;
    39 
    43 
    40 public class BasicLong {
    44     /**
       
    45      * Checks a block of assertions over an empty OptionalLong.
       
    46      */
       
    47     void checkEmpty(OptionalLong empty) {
       
    48         assertTrue(empty.equals(OptionalLong.empty()));
       
    49         assertTrue(OptionalLong.empty().equals(empty));
       
    50         assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));
       
    51         assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));
       
    52         assertFalse(empty.equals("unexpected"));
       
    53 
       
    54         assertFalse(empty.isPresent());
       
    55         assertEquals(empty.hashCode(), 0);
       
    56         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
       
    57         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
       
    58 
       
    59         assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
       
    60         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
       
    61         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
       
    62 
       
    63         var b = new AtomicBoolean();
       
    64         empty.ifPresent(s -> b.set(true));
       
    65         assertFalse(b.get());
       
    66 
       
    67         var b1 = new AtomicBoolean(false);
       
    68         var b2 = new AtomicBoolean(false);
       
    69         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
       
    70         assertFalse(b1.get());
       
    71         assertTrue(b2.get());
       
    72 
       
    73         assertEquals(empty.toString(), "OptionalLong.empty");
       
    74     }
       
    75 
       
    76     /**
       
    77      * Checks a block of assertions over an OptionalLong that is expected to
       
    78      * have a particular value present.
       
    79      */
       
    80     void checkPresent(OptionalLong opt, long expected) {
       
    81         assertFalse(opt.equals(OptionalLong.empty()));
       
    82         assertFalse(OptionalLong.empty().equals(opt));
       
    83         assertTrue(opt.equals(OptionalLong.of(expected)));
       
    84         assertTrue(OptionalLong.of(expected).equals(opt));
       
    85         assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));
       
    86         assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));
       
    87         assertFalse(opt.equals("unexpected"));
       
    88 
       
    89         assertTrue(opt.isPresent());
       
    90         assertEquals(opt.hashCode(), Long.hashCode(expected));
       
    91         assertEquals(opt.orElse(UNEXPECTED), expected);
       
    92         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
       
    93 
       
    94         assertEquals(opt.getAsLong(), expected);
       
    95         assertEquals(opt.orElseThrow(), expected);
       
    96         assertEquals(opt.orElseThrow(ObscureException::new), expected);
       
    97 
       
    98         var b = new AtomicBoolean(false);
       
    99         opt.ifPresent(s -> b.set(true));
       
   100         assertTrue(b.get());
       
   101 
       
   102         var b1 = new AtomicBoolean(false);
       
   103         var b2 = new AtomicBoolean(false);
       
   104         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
       
   105         assertTrue(b1.get());
       
   106         assertFalse(b2.get());
       
   107 
       
   108         assertEquals(opt.toString(), "OptionalLong[" + expected + "]");
       
   109     }
    41 
   110 
    42     @Test(groups = "unit")
   111     @Test(groups = "unit")
    43     public void testEmpty() {
   112     public void testEmpty() {
    44         OptionalLong empty = OptionalLong.empty();
   113         checkEmpty(OptionalLong.empty());
    45         OptionalLong present = OptionalLong.of(1);
       
    46 
       
    47         // empty
       
    48         assertTrue(empty.equals(empty));
       
    49         assertTrue(empty.equals(OptionalLong.empty()));
       
    50         assertTrue(!empty.equals(present));
       
    51         assertTrue(0 == empty.hashCode());
       
    52         assertTrue(!empty.toString().isEmpty());
       
    53         assertTrue(!empty.isPresent());
       
    54 
       
    55         empty.ifPresent(v -> { fail(); });
       
    56 
       
    57         AtomicBoolean emptyCheck = new AtomicBoolean();
       
    58         empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
       
    59         assertTrue(emptyCheck.get());
       
    60 
       
    61         try {
       
    62             empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
       
    63             fail();
       
    64         } catch (ObscureException expected) {
       
    65         } catch (AssertionError e) {
       
    66             throw e;
       
    67         } catch (Throwable t) {
       
    68             fail();
       
    69         }
       
    70 
       
    71         assertEquals(2, empty.orElse(2));
       
    72         assertEquals(2, empty.orElseGet(()-> 2));
       
    73     }
       
    74 
       
    75     @Test(groups = "unit")
       
    76     public void testIfPresentAndOrElseAndNull() {
       
    77         OptionalLong empty = OptionalLong.empty();
       
    78         OptionalLong present = OptionalLong.of(1);
       
    79 
       
    80         // No NPE
       
    81         present.ifPresentOrElse(v -> {}, null);
       
    82         empty.ifPresent(null);
       
    83         empty.ifPresentOrElse(null, () -> {});
       
    84 
       
    85         // NPE
       
    86         try {
       
    87             present.ifPresent(null);
       
    88             fail();
       
    89         } catch (NullPointerException ex) {}
       
    90         try {
       
    91             present.ifPresentOrElse(null, () -> {});
       
    92             fail();
       
    93         } catch (NullPointerException ex) {}
       
    94         try {
       
    95             empty.ifPresentOrElse(v -> {}, null);
       
    96             fail();
       
    97         } catch (NullPointerException ex) {}
       
    98     }
       
    99 
       
   100     @Test(expectedExceptions=NoSuchElementException.class)
       
   101     public void testEmptyGet() {
       
   102         OptionalLong empty = OptionalLong.empty();
       
   103 
       
   104         long got = empty.getAsLong();
       
   105     }
       
   106 
       
   107     @Test(expectedExceptions=NullPointerException.class)
       
   108     public void testEmptyOrElseGetNull() {
       
   109         OptionalLong empty = OptionalLong.empty();
       
   110 
       
   111         long got = empty.orElseGet(null);
       
   112     }
       
   113 
       
   114     @Test(expectedExceptions=NullPointerException.class)
       
   115     public void testEmptyOrElseThrowNull() throws Throwable {
       
   116         OptionalLong empty = OptionalLong.empty();
       
   117 
       
   118         long got = empty.orElseThrow(null);
       
   119     }
       
   120 
       
   121     @Test(expectedExceptions=ObscureException.class)
       
   122     public void testEmptyOrElseThrow() throws Exception {
       
   123         OptionalLong empty = OptionalLong.empty();
       
   124 
       
   125         long got = empty.orElseThrow(ObscureException::new);
       
   126     }
       
   127 
       
   128     @Test(expectedExceptions=NoSuchElementException.class)
       
   129     public void testEmptyOrElseThrowNoArg() throws Exception {
       
   130         OptionalLong empty = OptionalLong.empty();
       
   131 
       
   132         long got = empty.orElseThrow();
       
   133     }
   114     }
   134 
   115 
   135     @Test(groups = "unit")
   116     @Test(groups = "unit")
   136     public void testPresent() {
   117     public void testPresent() {
   137         OptionalLong empty = OptionalLong.empty();
   118         checkPresent(OptionalLong.of(LONGVAL), LONGVAL);
   138         OptionalLong present = OptionalLong.of(1L);
       
   139 
       
   140         // present
       
   141         assertTrue(present.equals(present));
       
   142         assertFalse(present.equals(OptionalLong.of(0L)));
       
   143         assertTrue(present.equals(OptionalLong.of(1L)));
       
   144         assertFalse(present.equals(empty));
       
   145         assertTrue(Long.hashCode(1) == present.hashCode());
       
   146         assertFalse(present.toString().isEmpty());
       
   147         assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
       
   148         assertTrue(-1 != present.toString().indexOf(Long.toString(present.orElseThrow()).toString()));
       
   149         assertEquals(1L, present.getAsLong());
       
   150         assertEquals(1L, present.orElseThrow());
       
   151 
       
   152         AtomicBoolean presentCheck = new AtomicBoolean();
       
   153         present.ifPresent(v -> presentCheck.set(true));
       
   154         assertTrue(presentCheck.get());
       
   155         presentCheck.set(false);
       
   156         present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
       
   157         assertTrue(presentCheck.get());
       
   158 
       
   159         try {
       
   160             present.ifPresent(v -> { throw new ObscureException(); });
       
   161             fail();
       
   162         } catch (ObscureException expected) {
       
   163         } catch (AssertionError e) {
       
   164             throw e;
       
   165         } catch (Throwable t) {
       
   166             fail();
       
   167         }
       
   168         try {
       
   169             present.ifPresentOrElse(v -> {
       
   170                 throw new ObscureException();
       
   171             }, () -> fail());
       
   172             fail();
       
   173         } catch (ObscureException expected) {
       
   174         } catch (AssertionError e) {
       
   175             throw e;
       
   176         } catch (Throwable t) {
       
   177             fail();
       
   178         }
       
   179 
       
   180         assertEquals(1, present.orElse(2));
       
   181         assertEquals(1, present.orElseGet(null));
       
   182         assertEquals(1, present.orElseGet(()-> 2));
       
   183         assertEquals(1, present.orElseGet(()-> 3));
       
   184         assertEquals(1, present.<RuntimeException>orElseThrow(null));
       
   185         assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
       
   186     }
   119     }
   187 
   120 
   188     @Test(groups = "unit")
   121     @Test(groups = "unit")
   189     public void testStream() {
   122     public void testStreamEmpty() {
   190         {
   123         assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });
   191             LongStream s = OptionalLong.empty().stream();
   124     }
   192 
   125 
   193             long[] es = s.toArray();
   126     @Test(groups = "unit")
   194             assertEquals(es.length, 0);
   127     public void testStreamPresent() {
   195         }
   128         assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });
   196 
       
   197         {
       
   198             LongStream s = OptionalLong.of(42L).stream();
       
   199 
       
   200             long[] es = s.toArray();
       
   201             assertEquals(es.length, 1);
       
   202             assertEquals(es[0], 42L);
       
   203         }
       
   204     }
   129     }
   205 }
   130 }