Path: blob/master/test/jdk/java/util/Optional/Basic.java
41149 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 Optional26* @author Mike Duigou27* @build ObscureException28* @run testng Basic29*/3031import java.util.List;32import java.util.NoSuchElementException;33import java.util.Optional;34import java.util.concurrent.atomic.AtomicBoolean;3536import static java.util.stream.Collectors.toList;3738import static org.testng.Assert.*;39import org.testng.annotations.Test;4041public class Basic {4243/**44* Checks a block of assertions over an empty Optional.45*/46void checkEmpty(Optional<String> empty) {47assertTrue(empty.equals(Optional.empty()));48assertTrue(Optional.empty().equals(empty));49assertFalse(empty.equals(Optional.of("unexpected")));50assertFalse(Optional.of("unexpected").equals(empty));51assertFalse(empty.equals("unexpected"));5253assertFalse(empty.isPresent());54assertTrue(empty.isEmpty());55assertEquals(empty.hashCode(), 0);56assertEquals(empty.orElse("x"), "x");57assertEquals(empty.orElseGet(() -> "y"), "y");5859assertThrows(NoSuchElementException.class, () -> empty.get());60assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());61assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new));6263var b = new AtomicBoolean();64empty.ifPresent(s -> b.set(true));65assertFalse(b.get());6667var b1 = new AtomicBoolean(false);68var b2 = new AtomicBoolean(false);69empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));70assertFalse(b1.get());71assertTrue(b2.get());7273assertEquals(empty.toString(), "Optional.empty");74}7576/**77* Checks a block of assertions over an Optional that is expected to78* have a particular value present.79*/80void checkPresent(Optional<String> opt, String expected) {81assertFalse(opt.equals(Optional.empty()));82assertFalse(Optional.empty().equals(opt));83assertTrue(opt.equals(Optional.of(expected)));84assertTrue(Optional.of(expected).equals(opt));85assertFalse(opt.equals(Optional.of("unexpected")));86assertFalse(Optional.of("unexpected").equals(opt));87assertFalse(opt.equals("unexpected"));8889assertTrue(opt.isPresent());90assertFalse(opt.isEmpty());91assertEquals(opt.hashCode(), expected.hashCode());92assertEquals(opt.orElse("unexpected"), expected);93assertEquals(opt.orElseGet(() -> "unexpected"), expected);9495assertEquals(opt.get(), expected);96assertEquals(opt.orElseThrow(), expected);97assertEquals(opt.orElseThrow(ObscureException::new), expected);9899var b = new AtomicBoolean(false);100opt.ifPresent(s -> b.set(true));101assertTrue(b.get());102103var b1 = new AtomicBoolean(false);104var b2 = new AtomicBoolean(false);105opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));106assertTrue(b1.get());107assertFalse(b2.get());108109assertEquals(opt.toString(), "Optional[" + expected + "]");110}111112@Test(groups = "unit")113public void testEmpty() {114checkEmpty(Optional.empty());115}116117@Test(groups = "unit")118public void testOfNull() {119assertThrows(NullPointerException.class, () -> Optional.of(null));120}121122@Test(groups = "unit")123public void testOfPresent() {124checkPresent(Optional.of("xyzzy"), "xyzzy");125}126127@Test(groups = "unit")128public void testOfNullableNull() {129checkEmpty(Optional.ofNullable(null));130}131132@Test(groups = "unit")133public void testOfNullablePresent() {134checkPresent(Optional.ofNullable("xyzzy"), "xyzzy");135}136137@Test(groups = "unit")138public void testFilterEmpty() {139checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; }));140}141142@Test(groups = "unit")143public void testFilterFalse() {144checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh")));145}146147@Test(groups = "unit")148public void testFilterTrue() {149checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy");150}151152@Test(groups = "unit")153public void testMapEmpty() {154checkEmpty(Optional.empty().map(s -> { fail(); return ""; }));155}156157@Test(groups = "unit")158public void testMapPresent() {159checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh");160}161162@Test(groups = "unit")163public void testFlatMapEmpty() {164checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); }));165}166167@Test(groups = "unit")168public void testFlatMapPresentReturnEmpty() {169checkEmpty(Optional.of("xyzzy")170.flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); }));171}172173@Test(groups = "unit")174public void testFlatMapPresentReturnPresent() {175checkPresent(Optional.of("xyzzy")176.flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }),177"plugh");178}179180@Test(groups = "unit")181public void testOrEmptyEmpty() {182checkEmpty(Optional.<String>empty().or(() -> Optional.empty()));183}184185@Test(groups = "unit")186public void testOrEmptyPresent() {187checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");188}189190@Test(groups = "unit")191public void testOrPresentDontCare() {192checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");193}194195@Test(groups = "unit")196public void testStreamEmpty() {197assertEquals(Optional.empty().stream().collect(toList()), List.of());198}199200@Test(groups = "unit")201public void testStreamPresent() {202assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));203}204}205206207