1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of Optional
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng Basic
  30  */
  31 
  32 import java.util.List;
  33 import java.util.NoSuchElementException;
  34 import java.util.Optional;
  35 import java.util.concurrent.atomic.AtomicBoolean;
  36 
  37 import static java.util.stream.Collectors.toList;
  38 
  39 import static org.testng.Assert.*;
  40 import org.testng.annotations.Test;
  41 
  42 public class Basic {
  43 
  44     /**
  45      * Checks a block of assertions over an empty Optional.
  46      */
  47     void checkEmpty(Optional<String> empty) {
  48         assertTrue(empty.equals(Optional.empty()));
  49         assertTrue(Optional.empty().equals(empty));
  50         assertFalse(empty.equals(Optional.of("unexpected")));
  51         assertFalse(Optional.of("unexpected").equals(empty));
  52         assertFalse(empty.equals("unexpected"));
  53 
  54         assertFalse(empty.isPresent());
  55         assertEquals(empty.hashCode(), 0);
  56         assertEquals(empty.orElse("x"), "x");
  57         assertEquals(empty.orElseGet(() -> "y"), "y");
  58 
  59         assertThrows(NoSuchElementException.class, () -> empty.get());
  60         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  61         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  62 
  63         var b = new AtomicBoolean();
  64         empty.ifPresent(s -> b.set(true));
  65         assertFalse(b.get());
  66 
  67         var b1 = new AtomicBoolean(false);
  68         var b2 = new AtomicBoolean(false);
  69         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  70         assertFalse(b1.get());
  71         assertTrue(b2.get());
  72 
  73         assertEquals(empty.toString(), "Optional.empty");
  74     }
  75 
  76     /**
  77      * Checks a block of assertions over an Optional that is expected to
  78      * have a particular value present.
  79      */
  80     void checkPresent(Optional<String> opt, String expected) {
  81         assertFalse(opt.equals(Optional.empty()));
  82         assertFalse(Optional.empty().equals(opt));
  83         assertTrue(opt.equals(Optional.of(expected)));
  84         assertTrue(Optional.of(expected).equals(opt));
  85         assertFalse(opt.equals(Optional.of("unexpected")));
  86         assertFalse(Optional.of("unexpected").equals(opt));
  87         assertFalse(opt.equals("unexpected"));
  88 
  89         assertTrue(opt.isPresent());
  90         assertEquals(opt.hashCode(), expected.hashCode());
  91         assertEquals(opt.orElse("unexpected"), expected);
  92         assertEquals(opt.orElseGet(() -> "unexpected"), expected);
  93 
  94         assertEquals(opt.get(), expected);
  95         assertEquals(opt.orElseThrow(), expected);
  96         assertEquals(opt.orElseThrow(ObscureException::new), expected);
  97 
  98         var b = new AtomicBoolean(false);
  99         opt.ifPresent(s -> b.set(true));
 100         assertTrue(b.get());
 101 
 102         var b1 = new AtomicBoolean(false);
 103         var b2 = new AtomicBoolean(false);
 104         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
 105         assertTrue(b1.get());
 106         assertFalse(b2.get());
 107 
 108         assertEquals(opt.toString(), "Optional[" + expected + "]");
 109     }
 110 
 111     @Test(groups = "unit")
 112     public void testEmpty() {
 113         checkEmpty(Optional.empty());
 114     }
 115 
 116     @Test(groups = "unit")
 117     public void testOfNull() {
 118         assertThrows(NullPointerException.class, () -> Optional.of(null));
 119     }
 120 
 121     @Test(groups = "unit")
 122     public void testOfPresent() {
 123         checkPresent(Optional.of("xyzzy"), "xyzzy");
 124     }
 125 
 126     @Test(groups = "unit")
 127     public void testOfNullableNull() {
 128         checkEmpty(Optional.ofNullable(null));
 129     }
 130 
 131     @Test(groups = "unit")
 132     public void testOfNullablePresent() {
 133         checkPresent(Optional.ofNullable("xyzzy"), "xyzzy");
 134     }
 135 
 136     @Test(groups = "unit")
 137     public void testFilterEmpty() {
 138         checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; }));
 139     }
 140 
 141     @Test(groups = "unit")
 142     public void testFilterFalse() {
 143         checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh")));
 144     }
 145 
 146     @Test(groups = "unit")
 147     public void testFilterTrue() {
 148         checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy");
 149     }
 150 
 151     @Test(groups = "unit")
 152     public void testMapEmpty() {
 153         checkEmpty(Optional.empty().map(s -> { fail(); return ""; }));
 154     }
 155 
 156     @Test(groups = "unit")
 157     public void testMapPresent() {
 158         checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh");
 159     }
 160 
 161     @Test(groups = "unit")
 162     public void testFlatMapEmpty() {
 163         checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); }));
 164     }
 165 
 166     @Test(groups = "unit")
 167     public void testFlatMapPresentReturnEmpty() {
 168         checkEmpty(Optional.of("xyzzy")
 169                            .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); }));
 170     }
 171 
 172     @Test(groups = "unit")
 173     public void testFlatMapPresentReturnPresent() {
 174         checkPresent(Optional.of("xyzzy")
 175                              .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }),
 176                      "plugh");
 177     }
 178 
 179     @Test(groups = "unit")
 180     public void testOrEmptyEmpty() {
 181         checkEmpty(Optional.<String>empty().or(() -> Optional.empty()));
 182     }
 183 
 184     @Test(groups = "unit")
 185     public void testOrEmptyPresent() {
 186         checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh");
 187     }
 188 
 189     @Test(groups = "unit")
 190     public void testOrPresentDontCare() {
 191         checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy");
 192     }
 193 
 194     @Test(groups = "unit")
 195     public void testStreamEmpty() {
 196         assertEquals(Optional.empty().stream().collect(toList()), List.of());
 197     }
 198 
 199     @Test(groups = "unit")
 200     public void testStreamPresent() {
 201         assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy"));
 202     }
 203 }