1 /*
   2  * Copyright (c) 2013, 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  * @summary Basic functional test of Optional
  26  * @author Mike Duigou
  27  * @run testng Basic
  28  */
  29 
  30 import java.util.NoSuchElementException;
  31 import java.util.Optional;
  32 
  33 import static org.testng.Assert.*;
  34 import org.testng.annotations.Test;
  35 
  36 
  37 public class Basic {
  38 
  39     @Test(groups = "unit")
  40     public void testEmpty() {
  41         Optional<Boolean> empty = Optional.empty();
  42         Optional<String> presentEmptyString = Optional.of("");
  43         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  44 
  45         // empty
  46         assertTrue(empty.equals(empty));
  47         assertTrue(empty.equals(Optional.empty()));
  48         assertTrue(!empty.equals(present));
  49         assertTrue(0 == empty.hashCode());
  50         assertTrue(!empty.toString().isEmpty());
  51         assertTrue(!empty.toString().equals(presentEmptyString.toString()));
  52         assertTrue(!empty.isPresent());
  53         empty.ifPresent(v -> { fail(); });
  54         assertSame(null, empty.orElse(null));
  55         RuntimeException orElse = new RuntimeException() { };
  56         assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
  57         assertSame(null, empty.orElseGet(()-> null));
  58         assertSame(Boolean.FALSE, empty.orElseGet(()-> Boolean.FALSE));
  59     }
  60 
  61     @Test(expectedExceptions=NoSuchElementException.class)
  62     public void testEmptyGet() {
  63         Optional<Boolean> empty = Optional.empty();
  64 
  65         Boolean got = empty.get();
  66     }
  67 
  68     @Test(expectedExceptions=NullPointerException.class)
  69     public void testEmptyOrElseGetNull() {
  70         Optional<Boolean> empty = Optional.empty();
  71 
  72         Boolean got = empty.orElseGet(null);
  73     }
  74 
  75     @Test(expectedExceptions=NullPointerException.class)
  76     public void testEmptyOrElseThrowNull() throws Throwable {
  77         Optional<Boolean> empty = Optional.empty();
  78 
  79         Boolean got = empty.orElseThrow(null);
  80     }
  81 
  82     @Test(expectedExceptions=ObscureException.class)
  83     public void testEmptyOrElseThrow() throws Exception {
  84         Optional<Boolean> empty = Optional.empty();
  85 
  86         Boolean got = empty.orElseThrow(ObscureException::new);
  87     }
  88 
  89     @Test(groups = "unit")
  90     public void testPresent() {
  91         Optional<Boolean> empty = Optional.empty();
  92         Optional<String> presentEmptyString = Optional.of("");
  93         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  94 
  95         // present
  96         assertTrue(present.equals(present));
  97         assertTrue(present.equals(Optional.of(Boolean.TRUE)));
  98         assertTrue(!present.equals(empty));
  99         assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
 100         assertTrue(!present.toString().isEmpty());
 101         assertTrue(!present.toString().equals(presentEmptyString.toString()));
 102         assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
 103         assertSame(Boolean.TRUE, present.get());
 104         try {
 105             present.ifPresent(v -> { throw new ObscureException(); });
 106             fail();
 107         } catch(ObscureException expected) {
 108 
 109         }
 110         assertSame(Boolean.TRUE, present.orElse(null));
 111         assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
 112         assertSame(Boolean.TRUE, present.orElseGet(null));
 113         assertSame(Boolean.TRUE, present.orElseGet(()-> null));
 114         assertSame(Boolean.TRUE, present.orElseGet(()-> Boolean.FALSE));
 115         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));
 116         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
 117     }
 118 
 119     @Test(groups = "unit")
 120     public void testOfNullable() {
 121         Optional<String> instance = Optional.ofNullable(null);
 122         assertFalse(instance.isPresent());
 123 
 124         instance = Optional.ofNullable("Duke");
 125         assertTrue(instance.isPresent());
 126         assertEquals(instance.get(), "Duke");
 127     }
 128 
 129     @Test(groups = "unit")
 130     public void testFilter() {
 131         // Null mapper function
 132         Optional<String> empty = Optional.empty();
 133         Optional<String> duke = Optional.of("Duke");
 134 
 135         try {
 136             Optional<String> result = empty.filter(null);
 137             fail("Should throw NPE on null mapping function");
 138         } catch (NullPointerException npe) {
 139             // expected
 140         }
 141 
 142         Optional<String> result = empty.filter(String::isEmpty);
 143         assertFalse(result.isPresent());
 144 
 145         result = duke.filter(String::isEmpty);
 146         assertFalse(result.isPresent());
 147         result = duke.filter(s -> s.startsWith("D"));
 148         assertTrue(result.isPresent());
 149         assertEquals(result.get(), "Duke");
 150 
 151         Optional<String> emptyString = Optional.of("");
 152         result = emptyString.filter(String::isEmpty);
 153         assertTrue(result.isPresent());
 154         assertEquals(result.get(), "");
 155     }
 156 
 157     @Test(groups = "unit")
 158     public void testMap() {
 159         Optional<String> empty = Optional.empty();
 160         Optional<String> duke = Optional.of("Duke");
 161 
 162         // Null mapper function
 163         try {
 164             Optional<Boolean> b = empty.map(null);
 165             fail("Should throw NPE on null mapping function");
 166         } catch (NullPointerException npe) {
 167             // expected
 168         }
 169 
 170         // Map an empty value
 171         Optional<Boolean> b = empty.map(String::isEmpty);
 172         assertFalse(b.isPresent());
 173 
 174         // Map into null
 175         b = empty.map(n -> null);
 176         assertFalse(b.isPresent());
 177         b = duke.map(s -> null);
 178         assertFalse(b.isPresent());
 179 
 180         // Map to value
 181         Optional<Integer> l = duke.map(String::length);
 182         assertEquals(l.get().intValue(), 4);
 183     }
 184 
 185     @Test(groups = "unit")
 186     public void testFlatMap() {
 187         Optional<String> empty = Optional.empty();
 188         Optional<String> duke = Optional.of("Duke");
 189 
 190         // Null mapper function
 191         try {
 192             Optional<Boolean> b = empty.flatMap(null);
 193             fail("Should throw NPE on null mapping function");
 194         } catch (NullPointerException npe) {
 195             // expected
 196         }
 197 
 198         // Map into null
 199         try {
 200             Optional<Boolean> b = duke.flatMap(s -> null);
 201             fail("Should throw NPE when mapper return null");
 202         } catch (NullPointerException npe) {
 203             // expected
 204         }
 205 
 206         // Empty won't invoke mapper function
 207         try {
 208             Optional<Boolean> b = empty.flatMap(s -> null);
 209             assertFalse(b.isPresent());
 210         } catch (NullPointerException npe) {
 211             fail("Mapper function should not be invoked");
 212         }
 213 
 214         // Map an empty value
 215         Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
 216         assertFalse(l.isPresent());
 217 
 218         // Map to value
 219         Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
 220         l = duke.flatMap(s -> Optional.of(s.length()));
 221         assertTrue(l.isPresent());
 222         assertEquals(l.get().intValue(), 4);
 223 
 224         // Verify same instance
 225         l = duke.flatMap(s -> fixture);
 226         assertSame(l, fixture);
 227     }
 228 
 229     private static class ObscureException extends RuntimeException {
 230 
 231     }
 232 }