Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Optional/BasicLong.java
47182 views
/*1* Copyright (c) 2013, 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* @summary Basic functional test of OptionalLong25* @author Mike Duigou26* @run testng BasicLong27*/2829import java.util.NoSuchElementException;30import java.util.OptionalLong;3132import static org.testng.Assert.*;33import org.testng.annotations.Test;343536public class BasicLong {3738@Test(groups = "unit")39public void testEmpty() {40OptionalLong empty = OptionalLong.empty();41OptionalLong present = OptionalLong.of(1);4243// empty44assertTrue(empty.equals(empty));45assertTrue(empty.equals(OptionalLong.empty()));46assertTrue(!empty.equals(present));47assertTrue(0 == empty.hashCode());48assertTrue(!empty.toString().isEmpty());49assertTrue(!empty.isPresent());50empty.ifPresent(v -> { fail(); });51assertEquals(2, empty.orElse(2));52assertEquals(2, empty.orElseGet(()-> 2));53}5455@Test(expectedExceptions=NoSuchElementException.class)56public void testEmptyGet() {57OptionalLong empty = OptionalLong.empty();5859long got = empty.getAsLong();60}6162@Test(expectedExceptions=NullPointerException.class)63public void testEmptyOrElseGetNull() {64OptionalLong empty = OptionalLong.empty();6566long got = empty.orElseGet(null);67}6869@Test(expectedExceptions=NullPointerException.class)70public void testEmptyOrElseThrowNull() throws Throwable {71OptionalLong empty = OptionalLong.empty();7273long got = empty.orElseThrow(null);74}7576@Test(expectedExceptions=ObscureException.class)77public void testEmptyOrElseThrow() throws Exception {78OptionalLong empty = OptionalLong.empty();7980long got = empty.orElseThrow(ObscureException::new);81}8283@Test(groups = "unit")84public void testPresent() {85OptionalLong empty = OptionalLong.empty();86OptionalLong present = OptionalLong.of(1L);8788// present89assertTrue(present.equals(present));90assertFalse(present.equals(OptionalLong.of(0L)));91assertTrue(present.equals(OptionalLong.of(1L)));92assertFalse(present.equals(empty));93assertTrue(Long.hashCode(1) == present.hashCode());94assertFalse(present.toString().isEmpty());95assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));96assertEquals(1L, present.getAsLong());97try {98present.ifPresent(v -> { throw new ObscureException(); });99fail();100} catch(ObscureException expected) {101102}103assertEquals(1, present.orElse(2));104assertEquals(1, present.orElseGet(null));105assertEquals(1, present.orElseGet(()-> 2));106assertEquals(1, present.orElseGet(()-> 3));107assertEquals(1, present.<RuntimeException>orElseThrow(null));108assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));109}110111private static class ObscureException extends RuntimeException {112113}114}115116117