Path: blob/master/test/jdk/java/util/Optional/BasicLong.java
41152 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 819564925* @summary Basic functional test of OptionalLong26* @author Mike Duigou27* @build ObscureException28* @run testng BasicLong29*/3031import java.util.NoSuchElementException;32import java.util.OptionalLong;33import java.util.concurrent.atomic.AtomicBoolean;3435import static org.testng.Assert.*;36import org.testng.annotations.Test;3738public class BasicLong {39static final long LONGVAL = 2_305_843_008_139_952_128L;40static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL;4142/**43* Checks a block of assertions over an empty OptionalLong.44*/45void checkEmpty(OptionalLong empty) {46assertTrue(empty.equals(OptionalLong.empty()));47assertTrue(OptionalLong.empty().equals(empty));48assertFalse(empty.equals(OptionalLong.of(UNEXPECTED)));49assertFalse(OptionalLong.of(UNEXPECTED).equals(empty));50assertFalse(empty.equals("unexpected"));5152assertFalse(empty.isPresent());53assertTrue(empty.isEmpty());54assertEquals(empty.hashCode(), 0);55assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);56assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);5758assertThrows(NoSuchElementException.class, () -> empty.getAsLong());59assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());60assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));6162var b = new AtomicBoolean();63empty.ifPresent(s -> b.set(true));64assertFalse(b.get());6566var b1 = new AtomicBoolean(false);67var b2 = new AtomicBoolean(false);68empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));69assertFalse(b1.get());70assertTrue(b2.get());7172assertEquals(empty.toString(), "OptionalLong.empty");73}7475/**76* Checks a block of assertions over an OptionalLong that is expected to77* have a particular value present.78*/79void checkPresent(OptionalLong opt, long expected) {80assertFalse(opt.equals(OptionalLong.empty()));81assertFalse(OptionalLong.empty().equals(opt));82assertTrue(opt.equals(OptionalLong.of(expected)));83assertTrue(OptionalLong.of(expected).equals(opt));84assertFalse(opt.equals(OptionalLong.of(UNEXPECTED)));85assertFalse(OptionalLong.of(UNEXPECTED).equals(opt));86assertFalse(opt.equals("unexpected"));8788assertTrue(opt.isPresent());89assertFalse(opt.isEmpty());90assertEquals(opt.hashCode(), Long.hashCode(expected));91assertEquals(opt.orElse(UNEXPECTED), expected);92assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);9394assertEquals(opt.getAsLong(), expected);95assertEquals(opt.orElseThrow(), expected);96assertEquals(opt.orElseThrow(ObscureException::new), expected);9798var b = new AtomicBoolean(false);99opt.ifPresent(s -> b.set(true));100assertTrue(b.get());101102var b1 = new AtomicBoolean(false);103var b2 = new AtomicBoolean(false);104opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));105assertTrue(b1.get());106assertFalse(b2.get());107108assertEquals(opt.toString(), "OptionalLong[" + expected + "]");109}110111@Test(groups = "unit")112public void testEmpty() {113checkEmpty(OptionalLong.empty());114}115116@Test(groups = "unit")117public void testPresent() {118checkPresent(OptionalLong.of(LONGVAL), LONGVAL);119}120121@Test(groups = "unit")122public void testStreamEmpty() {123assertEquals(OptionalLong.empty().stream().toArray(), new long[] { });124}125126@Test(groups = "unit")127public void testStreamPresent() {128assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL });129}130}131132133