Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Optional/BasicInt.java
47019 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 OptionalInt25* @author Mike Duigou26* @run testng BasicInt27*/2829import java.util.NoSuchElementException;30import java.util.OptionalInt;3132import static org.testng.Assert.*;33import org.testng.annotations.Test;343536public class BasicInt {3738@Test(groups = "unit")39public void testEmpty() {40OptionalInt empty = OptionalInt.empty();41OptionalInt present = OptionalInt.of(1);4243// empty44assertTrue(empty.equals(empty));45assertTrue(empty.equals(OptionalInt.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() {57OptionalInt empty = OptionalInt.empty();5859int got = empty.getAsInt();60}6162@Test(expectedExceptions=NullPointerException.class)63public void testEmptyOrElseGetNull() {64OptionalInt empty = OptionalInt.empty();6566int got = empty.orElseGet(null);67}6869@Test(expectedExceptions=NullPointerException.class)70public void testEmptyOrElseThrowNull() throws Throwable {71OptionalInt empty = OptionalInt.empty();7273int got = empty.orElseThrow(null);74}7576@Test(expectedExceptions=ObscureException.class)77public void testEmptyOrElseThrow() throws Exception {78OptionalInt empty = OptionalInt.empty();7980int got = empty.orElseThrow(ObscureException::new);81}8283@Test(groups = "unit")84public void testPresent() {85OptionalInt empty = OptionalInt.empty();86OptionalInt present = OptionalInt.of(1);8788// present89assertTrue(present.equals(present));90assertFalse(present.equals(OptionalInt.of(0)));91assertTrue(present.equals(OptionalInt.of(1)));92assertFalse(present.equals(empty));93assertTrue(Integer.hashCode(1) == present.hashCode());94assertFalse(present.toString().isEmpty());95assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));96assertEquals(1, present.getAsInt());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