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     private static class ObscureException extends RuntimeException {
 120 
 121     }
 122 }