Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Optional/Basic.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 Optional25* @author Mike Duigou26* @run testng Basic27*/2829import java.util.NoSuchElementException;30import java.util.Optional;3132import static org.testng.Assert.*;33import org.testng.annotations.Test;343536public class Basic {3738@Test(groups = "unit")39public void testEmpty() {40Optional<Boolean> empty = Optional.empty();41Optional<String> presentEmptyString = Optional.of("");42Optional<Boolean> present = Optional.of(Boolean.TRUE);4344// empty45assertTrue(empty.equals(empty));46assertTrue(empty.equals(Optional.empty()));47assertTrue(!empty.equals(present));48assertTrue(0 == empty.hashCode());49assertTrue(!empty.toString().isEmpty());50assertTrue(!empty.toString().equals(presentEmptyString.toString()));51assertTrue(!empty.isPresent());52empty.ifPresent(v -> { fail(); });53assertSame(null, empty.orElse(null));54RuntimeException orElse = new RuntimeException() { };55assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));56assertSame(null, empty.orElseGet(()-> null));57assertSame(Boolean.FALSE, empty.orElseGet(()-> Boolean.FALSE));58}5960@Test(expectedExceptions=NoSuchElementException.class)61public void testEmptyGet() {62Optional<Boolean> empty = Optional.empty();6364Boolean got = empty.get();65}6667@Test(expectedExceptions=NullPointerException.class)68public void testEmptyOrElseGetNull() {69Optional<Boolean> empty = Optional.empty();7071Boolean got = empty.orElseGet(null);72}7374@Test(expectedExceptions=NullPointerException.class)75public void testEmptyOrElseThrowNull() throws Throwable {76Optional<Boolean> empty = Optional.empty();7778Boolean got = empty.orElseThrow(null);79}8081@Test(expectedExceptions=ObscureException.class)82public void testEmptyOrElseThrow() throws Exception {83Optional<Boolean> empty = Optional.empty();8485Boolean got = empty.orElseThrow(ObscureException::new);86}8788@Test(groups = "unit")89public void testPresent() {90Optional<Boolean> empty = Optional.empty();91Optional<String> presentEmptyString = Optional.of("");92Optional<Boolean> present = Optional.of(Boolean.TRUE);9394// present95assertTrue(present.equals(present));96assertTrue(present.equals(Optional.of(Boolean.TRUE)));97assertTrue(!present.equals(empty));98assertTrue(Boolean.TRUE.hashCode() == present.hashCode());99assertTrue(!present.toString().isEmpty());100assertTrue(!present.toString().equals(presentEmptyString.toString()));101assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));102assertSame(Boolean.TRUE, present.get());103try {104present.ifPresent(v -> { throw new ObscureException(); });105fail();106} catch(ObscureException expected) {107108}109assertSame(Boolean.TRUE, present.orElse(null));110assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));111assertSame(Boolean.TRUE, present.orElseGet(null));112assertSame(Boolean.TRUE, present.orElseGet(()-> null));113assertSame(Boolean.TRUE, present.orElseGet(()-> Boolean.FALSE));114assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));115assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));116}117118@Test(groups = "unit")119public void testOfNullable() {120Optional<String> instance = Optional.ofNullable(null);121assertFalse(instance.isPresent());122123instance = Optional.ofNullable("Duke");124assertTrue(instance.isPresent());125assertEquals(instance.get(), "Duke");126}127128@Test(groups = "unit")129public void testFilter() {130// Null mapper function131Optional<String> empty = Optional.empty();132Optional<String> duke = Optional.of("Duke");133134try {135Optional<String> result = empty.filter(null);136fail("Should throw NPE on null mapping function");137} catch (NullPointerException npe) {138// expected139}140141Optional<String> result = empty.filter(String::isEmpty);142assertFalse(result.isPresent());143144result = duke.filter(String::isEmpty);145assertFalse(result.isPresent());146result = duke.filter(s -> s.startsWith("D"));147assertTrue(result.isPresent());148assertEquals(result.get(), "Duke");149150Optional<String> emptyString = Optional.of("");151result = emptyString.filter(String::isEmpty);152assertTrue(result.isPresent());153assertEquals(result.get(), "");154}155156@Test(groups = "unit")157public void testMap() {158Optional<String> empty = Optional.empty();159Optional<String> duke = Optional.of("Duke");160161// Null mapper function162try {163Optional<Boolean> b = empty.map(null);164fail("Should throw NPE on null mapping function");165} catch (NullPointerException npe) {166// expected167}168169// Map an empty value170Optional<Boolean> b = empty.map(String::isEmpty);171assertFalse(b.isPresent());172173// Map into null174b = empty.map(n -> null);175assertFalse(b.isPresent());176b = duke.map(s -> null);177assertFalse(b.isPresent());178179// Map to value180Optional<Integer> l = duke.map(String::length);181assertEquals(l.get().intValue(), 4);182}183184@Test(groups = "unit")185public void testFlatMap() {186Optional<String> empty = Optional.empty();187Optional<String> duke = Optional.of("Duke");188189// Null mapper function190try {191Optional<Boolean> b = empty.flatMap(null);192fail("Should throw NPE on null mapping function");193} catch (NullPointerException npe) {194// expected195}196197// Map into null198try {199Optional<Boolean> b = duke.flatMap(s -> null);200fail("Should throw NPE when mapper return null");201} catch (NullPointerException npe) {202// expected203}204205// Empty won't invoke mapper function206try {207Optional<Boolean> b = empty.flatMap(s -> null);208assertFalse(b.isPresent());209} catch (NullPointerException npe) {210fail("Mapper function should not be invoked");211}212213// Map an empty value214Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));215assertFalse(l.isPresent());216217// Map to value218Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);219l = duke.flatMap(s -> Optional.of(s.length()));220assertTrue(l.isPresent());221assertEquals(l.get().intValue(), 4);222223// Verify same instance224l = duke.flatMap(s -> fixture);225assertSame(l, fixture);226}227228private static class ObscureException extends RuntimeException {229230}231}232233234