< prev index next >

test/java/util/Optional/Basic.java

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


  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     }


  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")




  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     }


 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")


< prev index next >