rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs

   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.lang.AssertionError;
  31 import java.lang.NullPointerException;
  32 import java.lang.Throwable;
  33 import java.util.NoSuchElementException;
  34 import java.util.Optional;
  35 import java.util.concurrent.atomic.AtomicBoolean;
  36 import java.util.stream.Stream;
  37 
  38 import static org.testng.Assert.*;
  39 import org.testng.annotations.Test;
  40 
  41 
  42 public class Basic {
  43 
  44     @Test(groups = "unit")
  45     public void testEmpty() {
  46         Optional<Boolean> empty = Optional.empty();
  47         Optional<String> presentEmptyString = Optional.of("");
  48         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  49 
  50         // empty
  51         assertTrue(empty.equals(empty));
  52         assertTrue(empty.equals(Optional.empty()));
  53         assertTrue(!empty.equals(present));
  54         assertTrue(0 == empty.hashCode());
  55         assertTrue(!empty.toString().isEmpty());
  56         assertTrue(!empty.toString().equals(presentEmptyString.toString()));
  57         assertTrue(!empty.isPresent());
  58 
  59         empty.ifPresent(v -> fail());
  60 
  61         AtomicBoolean emptyCheck = new AtomicBoolean();
  62         empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
  63         assertTrue(emptyCheck.get());
  64 
  65         try {
  66             empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
  67             fail();
  68         } catch (ObscureException expected) {
  69         } catch (AssertionError e) {
  70             throw e;
  71         } catch (Throwable t) {
  72             fail();
  73         }
  74 
  75         assertSame(null, empty.orElse(null));
  76         RuntimeException orElse = new RuntimeException() { };
  77         assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
  78         assertSame(null, empty.orElseGet(() -> null));
  79         assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
  80     }
  81 
  82     @Test(groups = "unit")
  83     public void testIfPresentAndOrElseAndNull() {
  84         Optional<Boolean> empty = Optional.empty();
  85         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  86 
  87         // No NPE
  88         present.ifPresentOrElse(v -> {}, null);
  89         empty.ifPresent(null);
  90         empty.ifPresentOrElse(null, () -> {});
  91 
  92         // NPE
  93         try {
  94             present.ifPresent(null);
  95             fail();
  96         } catch (NullPointerException ex) {}
  97         try {
  98             present.ifPresentOrElse(null, () -> {});
  99             fail();
 100         } catch (NullPointerException ex) {}
 101         try {
 102             empty.ifPresentOrElse(v -> {}, null);
 103             fail();
 104         } catch (NullPointerException ex) {}
 105     }
 106 
 107     @Test(expectedExceptions=NoSuchElementException.class)
 108     public void testEmptyGet() {
 109         Optional<Boolean> empty = Optional.empty();
 110 
 111         Boolean got = empty.get();
 112     }
 113 
 114     @Test(expectedExceptions=NullPointerException.class)
 115     public void testEmptyOrElseGetNull() {
 116         Optional<Boolean> empty = Optional.empty();
 117 
 118         Boolean got = empty.orElseGet(null);
 119     }
 120 
 121     @Test(expectedExceptions=NullPointerException.class)
 122     public void testEmptyOrElseThrowNull() throws Throwable {
 123         Optional<Boolean> empty = Optional.empty();
 124 
 125         Boolean got = empty.orElseThrow(null);
 126     }
 127 
 128     @Test(expectedExceptions=ObscureException.class)
 129     public void testEmptyOrElseThrow() throws Exception {
 130         Optional<Boolean> empty = Optional.empty();
 131 
 132         Boolean got = empty.orElseThrow(ObscureException::new);
 133     }
 134 
 135     @Test(groups = "unit")
 136     public void testPresent() {
 137         Optional<Boolean> empty = Optional.empty();
 138         Optional<String> presentEmptyString = Optional.of("");
 139         Optional<Boolean> present = Optional.of(Boolean.TRUE);
 140 
 141         // present
 142         assertTrue(present.equals(present));
 143         assertTrue(present.equals(Optional.of(Boolean.TRUE)));
 144         assertTrue(!present.equals(empty));
 145         assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
 146         assertTrue(!present.toString().isEmpty());
 147         assertTrue(!present.toString().equals(presentEmptyString.toString()));
 148         assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
 149         assertSame(Boolean.TRUE, present.get());
 150 
 151         AtomicBoolean presentCheck = new AtomicBoolean();
 152         present.ifPresent(v -> presentCheck.set(true));
 153         assertTrue(presentCheck.get());
 154         presentCheck.set(false);
 155         present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
 156         assertTrue(presentCheck.get());
 157 
 158         try {
 159             present.ifPresent(v -> { throw new ObscureException(); });
 160             fail();
 161         } catch (ObscureException expected) {
 162         } catch (AssertionError e) {
 163             throw e;
 164         } catch (Throwable t) {
 165             fail();
 166         }
 167         try {
 168             present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
 169             fail();
 170         } catch (ObscureException expected) {
 171         } catch (AssertionError e) {
 172             throw e;
 173         } catch (Throwable t) {
 174             fail();
 175         }
 176 
 177         assertSame(Boolean.TRUE, present.orElse(null));
 178         assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
 179         assertSame(Boolean.TRUE, present.orElseGet(null));
 180         assertSame(Boolean.TRUE, present.orElseGet(() -> null));
 181         assertSame(Boolean.TRUE, present.orElseGet(() -> Boolean.FALSE));
 182         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(null));
 183         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
 184     }
 185 
 186     @Test(groups = "unit")
 187     public void testOfNullable() {
 188         Optional<String> instance = Optional.ofNullable(null);
 189         assertFalse(instance.isPresent());
 190 
 191         instance = Optional.ofNullable("Duke");
 192         assertTrue(instance.isPresent());
 193         assertEquals(instance.get(), "Duke");
 194     }
 195 
 196     @Test(groups = "unit")
 197     public void testFilter() {
 198         // Null mapper function
 199         Optional<String> empty = Optional.empty();
 200         Optional<String> duke = Optional.of("Duke");
 201 
 202         try {
 203             Optional<String> result = empty.filter(null);
 204             fail("Should throw NPE on null mapping function");
 205         } catch (NullPointerException npe) {
 206             // expected
 207         }
 208 
 209         Optional<String> result = empty.filter(String::isEmpty);
 210         assertFalse(result.isPresent());
 211 
 212         result = duke.filter(String::isEmpty);
 213         assertFalse(result.isPresent());
 214         result = duke.filter(s -> s.startsWith("D"));
 215         assertTrue(result.isPresent());
 216         assertEquals(result.get(), "Duke");
 217 
 218         Optional<String> emptyString = Optional.of("");
 219         result = emptyString.filter(String::isEmpty);
 220         assertTrue(result.isPresent());
 221         assertEquals(result.get(), "");
 222     }
 223 
 224     @Test(groups = "unit")
 225     public void testMap() {
 226         Optional<String> empty = Optional.empty();
 227         Optional<String> duke = Optional.of("Duke");
 228 
 229         // Null mapper function
 230         try {
 231             Optional<Boolean> b = empty.map(null);
 232             fail("Should throw NPE on null mapping function");
 233         } catch (NullPointerException npe) {
 234             // expected
 235         }
 236 
 237         // Map an empty value
 238         Optional<Boolean> b = empty.map(String::isEmpty);
 239         assertFalse(b.isPresent());
 240 
 241         // Map into null
 242         b = empty.map(n -> null);
 243         assertFalse(b.isPresent());
 244         b = duke.map(s -> null);
 245         assertFalse(b.isPresent());
 246 
 247         // Map to value
 248         Optional<Integer> l = duke.map(String::length);
 249         assertEquals(l.get().intValue(), 4);
 250     }
 251 
 252     @Test(groups = "unit")
 253     public void testFlatMap() {
 254         Optional<String> empty = Optional.empty();
 255         Optional<String> duke = Optional.of("Duke");
 256 
 257         // Null mapper function
 258         try {
 259             Optional<Boolean> b = empty.flatMap(null);
 260             fail("Should throw NPE on null mapping function");
 261         } catch (NullPointerException npe) {
 262             // expected
 263         }
 264 
 265         // Map into null
 266         try {
 267             Optional<Boolean> b = duke.flatMap(s -> null);
 268             fail("Should throw NPE when mapper return null");
 269         } catch (NullPointerException npe) {
 270             // expected
 271         }
 272 
 273         // Empty won't invoke mapper function
 274         try {
 275             Optional<Boolean> b = empty.flatMap(s -> null);
 276             assertFalse(b.isPresent());
 277         } catch (NullPointerException npe) {
 278             fail("Mapper function should not be invoked");
 279         }
 280 
 281         // Map an empty value
 282         Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
 283         assertFalse(l.isPresent());
 284 
 285         // Map to value
 286         Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
 287         l = duke.flatMap(s -> Optional.of(s.length()));
 288         assertTrue(l.isPresent());
 289         assertEquals(l.get().intValue(), 4);
 290 
 291         // Verify same instance
 292         l = duke.flatMap(s -> fixture);
 293         assertSame(l, fixture);
 294     }
 295 
 296     @Test(groups = "unit")
 297     public void testStream() {
 298         {
 299             Stream<String> s = Optional.<String>empty().stream();
 300             assertFalse(s.isParallel());
 301 
 302             Object[] es = s.toArray();
 303             assertEquals(es.length, 0);
 304         }
 305 
 306         {
 307             Stream<String> s = Optional.of("Duke").stream();
 308             assertFalse(s.isParallel());
 309 
 310             String[] es = s.toArray(String[]::new);
 311             assertEquals(es.length, 1);
 312             assertEquals(es[0], "Duke");
 313         }
 314     }
 315 
 316     private static class ObscureException extends RuntimeException {
 317 
 318     }
 319 }
--- EOF ---