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.util.NoSuchElementException;
  31 import java.util.Optional;

  32 import java.util.stream.Stream;
  33 
  34 import static org.testng.Assert.*;
  35 import org.testng.annotations.Test;
  36 
  37 
  38 public class Basic {
  39 
  40     @Test(groups = "unit")
  41     public void testEmpty() {
  42         Optional<Boolean> empty = Optional.empty();
  43         Optional<String> presentEmptyString = Optional.of("");
  44         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  45 
  46         // empty
  47         assertTrue(empty.equals(empty));
  48         assertTrue(empty.equals(Optional.empty()));
  49         assertTrue(!empty.equals(present));
  50         assertTrue(0 == empty.hashCode());
  51         assertTrue(!empty.toString().isEmpty());
  52         assertTrue(!empty.toString().equals(presentEmptyString.toString()));
  53         assertTrue(!empty.isPresent());
  54         empty.ifPresent(v -> { fail(); });
















  55         assertSame(null, empty.orElse(null));
  56         RuntimeException orElse = new RuntimeException() { };
  57         assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
  58         assertSame(null, empty.orElseGet(() -> null));
  59         assertSame(Boolean.FALSE, empty.orElseGet(() -> Boolean.FALSE));
  60     }
  61 

























  62     @Test(expectedExceptions=NoSuchElementException.class)
  63     public void testEmptyGet() {
  64         Optional<Boolean> empty = Optional.empty();
  65 
  66         Boolean got = empty.get();
  67     }
  68 
  69     @Test(expectedExceptions=NullPointerException.class)
  70     public void testEmptyOrElseGetNull() {
  71         Optional<Boolean> empty = Optional.empty();
  72 
  73         Boolean got = empty.orElseGet(null);
  74     }
  75 
  76     @Test(expectedExceptions=NullPointerException.class)
  77     public void testEmptyOrElseThrowNull() throws Throwable {
  78         Optional<Boolean> empty = Optional.empty();
  79 
  80         Boolean got = empty.orElseThrow(null);
  81     }
  82 
  83     @Test(expectedExceptions=ObscureException.class)
  84     public void testEmptyOrElseThrow() throws Exception {
  85         Optional<Boolean> empty = Optional.empty();
  86 
  87         Boolean got = empty.orElseThrow(ObscureException::new);
  88     }
  89 
  90     @Test(groups = "unit")
  91     public void testPresent() {
  92         Optional<Boolean> empty = Optional.empty();
  93         Optional<String> presentEmptyString = Optional.of("");
  94         Optional<Boolean> present = Optional.of(Boolean.TRUE);
  95 
  96         // present
  97         assertTrue(present.equals(present));
  98         assertTrue(present.equals(Optional.of(Boolean.TRUE)));
  99         assertTrue(!present.equals(empty));
 100         assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
 101         assertTrue(!present.toString().isEmpty());
 102         assertTrue(!present.toString().equals(presentEmptyString.toString()));
 103         assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
 104         assertSame(Boolean.TRUE, present.get());








 105         try {
 106             present.ifPresent(v -> { throw new ObscureException(); });
 107             fail();
 108         } catch (ObscureException expected) {
 109 












 110         }

 111         assertSame(Boolean.TRUE, present.orElse(null));
 112         assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
 113         assertSame(Boolean.TRUE, present.orElseGet(null));
 114         assertSame(Boolean.TRUE, present.orElseGet(() -> null));
 115         assertSame(Boolean.TRUE, present.orElseGet(() -> Boolean.FALSE));
 116         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(null));
 117         assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
 118     }
 119 
 120     @Test(groups = "unit")
 121     public void testOfNullable() {
 122         Optional<String> instance = Optional.ofNullable(null);
 123         assertFalse(instance.isPresent());
 124 
 125         instance = Optional.ofNullable("Duke");
 126         assertTrue(instance.isPresent());
 127         assertEquals(instance.get(), "Duke");
 128     }
 129 
 130     @Test(groups = "unit")
 131     public void testFilter() {
 132         // Null mapper function
 133         Optional<String> empty = Optional.empty();
 134         Optional<String> duke = Optional.of("Duke");
 135 
 136         try {
 137             Optional<String> result = empty.filter(null);
 138             fail("Should throw NPE on null mapping function");
 139         } catch (NullPointerException npe) {
 140             // expected
 141         }
 142 
 143         Optional<String> result = empty.filter(String::isEmpty);
 144         assertFalse(result.isPresent());
 145 
 146         result = duke.filter(String::isEmpty);
 147         assertFalse(result.isPresent());
 148         result = duke.filter(s -> s.startsWith("D"));
 149         assertTrue(result.isPresent());
 150         assertEquals(result.get(), "Duke");
 151 
 152         Optional<String> emptyString = Optional.of("");
 153         result = emptyString.filter(String::isEmpty);
 154         assertTrue(result.isPresent());
 155         assertEquals(result.get(), "");
 156     }
 157 
 158     @Test(groups = "unit")
 159     public void testMap() {
 160         Optional<String> empty = Optional.empty();
 161         Optional<String> duke = Optional.of("Duke");
 162 
 163         // Null mapper function
 164         try {
 165             Optional<Boolean> b = empty.map(null);
 166             fail("Should throw NPE on null mapping function");
 167         } catch (NullPointerException npe) {
 168             // expected
 169         }
 170 
 171         // Map an empty value
 172         Optional<Boolean> b = empty.map(String::isEmpty);
 173         assertFalse(b.isPresent());
 174 
 175         // Map into null
 176         b = empty.map(n -> null);
 177         assertFalse(b.isPresent());
 178         b = duke.map(s -> null);
 179         assertFalse(b.isPresent());
 180 
 181         // Map to value
 182         Optional<Integer> l = duke.map(String::length);
 183         assertEquals(l.get().intValue(), 4);
 184     }
 185 
 186     @Test(groups = "unit")
 187     public void testFlatMap() {
 188         Optional<String> empty = Optional.empty();
 189         Optional<String> duke = Optional.of("Duke");
 190 
 191         // Null mapper function
 192         try {
 193             Optional<Boolean> b = empty.flatMap(null);
 194             fail("Should throw NPE on null mapping function");
 195         } catch (NullPointerException npe) {
 196             // expected
 197         }
 198 
 199         // Map into null
 200         try {
 201             Optional<Boolean> b = duke.flatMap(s -> null);
 202             fail("Should throw NPE when mapper return null");
 203         } catch (NullPointerException npe) {
 204             // expected
 205         }
 206 
 207         // Empty won't invoke mapper function
 208         try {
 209             Optional<Boolean> b = empty.flatMap(s -> null);
 210             assertFalse(b.isPresent());
 211         } catch (NullPointerException npe) {
 212             fail("Mapper function should not be invoked");
 213         }
 214 
 215         // Map an empty value
 216         Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
 217         assertFalse(l.isPresent());
 218 
 219         // Map to value
 220         Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
 221         l = duke.flatMap(s -> Optional.of(s.length()));
 222         assertTrue(l.isPresent());
 223         assertEquals(l.get().intValue(), 4);
 224 
 225         // Verify same instance
 226         l = duke.flatMap(s -> fixture);
 227         assertSame(l, fixture);
 228     }
 229 
 230     @Test(groups = "unit")
 231     public void testStream() {
 232         {
 233             Stream<String> s = Optional.<String>empty().stream();
 234             assertFalse(s.isParallel());
 235 
 236             Object[] es = s.toArray();
 237             assertEquals(es.length, 0);
 238         }
 239 
 240         {
 241             Stream<String> s = Optional.of("Duke").stream();
 242             assertFalse(s.isParallel());
 243 
 244             String[] es = s.toArray(String[]::new);
 245             assertEquals(es.length, 1);
 246             assertEquals(es[0], "Duke");
 247         }
 248     }
 249 
 250     private static class ObscureException extends RuntimeException {
 251 
 252     }
 253 }
--- EOF ---