1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of OptionalLong
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng BasicLong
  30  */
  31 
  32 import java.util.NoSuchElementException;
  33 import java.util.OptionalLong;
  34 import java.util.concurrent.atomic.AtomicBoolean;
  35 
  36 import static org.testng.Assert.*;
  37 import org.testng.annotations.Test;
  38 
  39 public class BasicLong {
  40     static final long LONGVAL = 2_305_843_008_139_952_128L;
  41     static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;
  42 
  43     /**
  44      * Checks a block of assertions over an empty OptionalLong.
  45      */
  46     void checkEmpty(OptionalLong empty) {
  47         assertTrue(empty.equals(OptionalLong.empty()));
  48         assertTrue(OptionalLong.empty().equals(empty));
  49         assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));
  50         assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));
  51         assertFalse(empty.equals("unexpected"));
  52 
  53         assertFalse(empty.isPresent());
  54         assertEquals(empty.hashCode(), 0);
  55         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
  56         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
  57 
  58         assertThrows(NoSuchElementException.class, () -> empty.getAsLong());
  59         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  60         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  61 
  62         var b = new AtomicBoolean();
  63         empty.ifPresent(s -> b.set(true));
  64         assertFalse(b.get());
  65 
  66         var b1 = new AtomicBoolean(false);
  67         var b2 = new AtomicBoolean(false);
  68         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  69         assertFalse(b1.get());
  70         assertTrue(b2.get());
  71 
  72         assertEquals(empty.toString(), "OptionalLong.empty");
  73     }
  74 
  75     /**
  76      * Checks a block of assertions over an OptionalLong that is expected to
  77      * have a particular value present.
  78      */
  79     void checkPresent(OptionalLong opt, long expected) {
  80         assertFalse(opt.equals(OptionalLong.empty()));
  81         assertFalse(OptionalLong.empty().equals(opt));
  82         assertTrue(opt.equals(OptionalLong.of(expected)));
  83         assertTrue(OptionalLong.of(expected).equals(opt));
  84         assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));
  85         assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));
  86         assertFalse(opt.equals("unexpected"));
  87 
  88         assertTrue(opt.isPresent());
  89         assertEquals(opt.hashCode(), Long.hashCode(expected));
  90         assertEquals(opt.orElse(UNEXPECTED), expected);
  91         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
  92 
  93         assertEquals(opt.getAsLong(), expected);
  94         assertEquals(opt.orElseThrow(), expected);
  95         assertEquals(opt.orElseThrow(ObscureException::new), expected);
  96 
  97         var b = new AtomicBoolean(false);
  98         opt.ifPresent(s -> b.set(true));
  99         assertTrue(b.get());
 100 
 101         var b1 = new AtomicBoolean(false);
 102         var b2 = new AtomicBoolean(false);
 103         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
 104         assertTrue(b1.get());
 105         assertFalse(b2.get());
 106 
 107         assertEquals(opt.toString(), "OptionalLong[" + expected + "]");
 108     }
 109 
 110     @Test(groups = "unit")
 111     public void testEmpty() {
 112         checkEmpty(OptionalLong.empty());
 113     }
 114 
 115     @Test(groups = "unit")
 116     public void testPresent() {
 117         checkPresent(OptionalLong.of(LONGVAL), LONGVAL);
 118     }
 119 
 120     @Test(groups = "unit")
 121     public void testStreamEmpty() {
 122         assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });
 123     }
 124 
 125     @Test(groups = "unit")
 126     public void testStreamPresent() {
 127         assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });
 128     }
 129 }