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 OptionalLong
  26  * @author Mike Duigou
  27  * @run testng BasicLong
  28  */
  29 
  30 import java.util.NoSuchElementException;
  31 import java.util.OptionalLong;
  32 import java.util.concurrent.atomic.AtomicBoolean;
  33 import java.util.stream.LongStream;
  34 
  35 import static org.testng.Assert.*;
  36 import org.testng.annotations.Test;
  37 
  38 
  39 public class BasicLong {
  40 
  41     @Test(groups = "unit")
  42     public void testEmpty() {
  43         OptionalLong empty = OptionalLong.empty();
  44         OptionalLong present = OptionalLong.of(1);
  45 
  46         // empty
  47         assertTrue(empty.equals(empty));
  48         assertTrue(empty.equals(OptionalLong.empty()));
  49         assertTrue(!empty.equals(present));
  50         assertTrue(0 == empty.hashCode());
  51         assertTrue(!empty.toString().isEmpty());
  52         assertTrue(!empty.isPresent());
  53 
  54         empty.ifPresent(v -> { fail(); });
  55 
  56         AtomicBoolean emptyCheck = new AtomicBoolean();
  57         empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
  58         assertTrue(emptyCheck.get());
  59 
  60         try {
  61             empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
  62             fail();
  63         } catch (ObscureException expected) {
  64         } catch (AssertionError e) {
  65             throw e;
  66         } catch (Throwable t) {
  67             fail();
  68         }
  69 
  70         assertEquals(2, empty.orElse(2));
  71         assertEquals(2, empty.orElseGet(()-> 2));
  72     }
  73 
  74     @Test(groups = "unit")
  75     public void testIfPresentAndOrElseAndNull() {
  76         OptionalLong empty = OptionalLong.empty();
  77         OptionalLong present = OptionalLong.of(1);
  78 
  79         // No NPE
  80         present.ifPresentOrElse(v -> {}, null);
  81         empty.ifPresent(null);
  82         empty.ifPresentOrElse(null, () -> {});
  83 
  84         // NPE
  85         try {
  86             present.ifPresent(null);
  87             fail();
  88         } catch (NullPointerException ex) {}
  89         try {
  90             present.ifPresentOrElse(null, () -> {});
  91             fail();
  92         } catch (NullPointerException ex) {}
  93         try {
  94             empty.ifPresentOrElse(v -> {}, null);
  95             fail();
  96         } catch (NullPointerException ex) {}
  97     }
  98 
  99     @Test(expectedExceptions=NoSuchElementException.class)
 100     public void testEmptyGet() {
 101         OptionalLong empty = OptionalLong.empty();
 102 
 103         long got = empty.getAsLong();
 104     }
 105 
 106     @Test(expectedExceptions=NullPointerException.class)
 107     public void testEmptyOrElseGetNull() {
 108         OptionalLong empty = OptionalLong.empty();
 109 
 110         long got = empty.orElseGet(null);
 111     }
 112 
 113     @Test(expectedExceptions=NullPointerException.class)
 114     public void testEmptyOrElseThrowNull() throws Throwable {
 115         OptionalLong empty = OptionalLong.empty();
 116 
 117         long got = empty.orElseThrow(null);
 118     }
 119 
 120     @Test(expectedExceptions=ObscureException.class)
 121     public void testEmptyOrElseThrow() throws Exception {
 122         OptionalLong empty = OptionalLong.empty();
 123 
 124         long got = empty.orElseThrow(ObscureException::new);
 125     }
 126 
 127     @Test(groups = "unit")
 128     public void testPresent() {
 129         OptionalLong empty = OptionalLong.empty();
 130         OptionalLong present = OptionalLong.of(1L);
 131 
 132         // present
 133         assertTrue(present.equals(present));
 134         assertFalse(present.equals(OptionalLong.of(0L)));
 135         assertTrue(present.equals(OptionalLong.of(1L)));
 136         assertFalse(present.equals(empty));
 137         assertTrue(Long.hashCode(1) == present.hashCode());
 138         assertFalse(present.toString().isEmpty());
 139         assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
 140         assertEquals(1L, present.getAsLong());
 141 
 142         AtomicBoolean presentCheck = new AtomicBoolean();
 143         present.ifPresent(v -> presentCheck.set(true));
 144         assertTrue(presentCheck.get());
 145         presentCheck.set(false);
 146         present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
 147         assertTrue(presentCheck.get());
 148 
 149         try {
 150             present.ifPresent(v -> { throw new ObscureException(); });
 151             fail();
 152         } catch (ObscureException expected) {
 153         } catch (AssertionError e) {
 154             throw e;
 155         } catch (Throwable t) {
 156             fail();
 157         }
 158         try {
 159             present.ifPresentOrElse(v -> {
 160                 throw new ObscureException();
 161             }, () -> fail());
 162             fail();
 163         } catch (ObscureException expected) {
 164         } catch (AssertionError e) {
 165             throw e;
 166         } catch (Throwable t) {
 167             fail();
 168         }
 169 
 170         assertEquals(1, present.orElse(2));
 171         assertEquals(1, present.orElseGet(null));
 172         assertEquals(1, present.orElseGet(()-> 2));
 173         assertEquals(1, present.orElseGet(()-> 3));
 174         assertEquals(1, present.<RuntimeException>orElseThrow(null));
 175         assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
 176     }
 177 
 178     @Test(groups = "unit")
 179     public void testStream() {
 180         {
 181             LongStream s = OptionalLong.empty().stream();
 182 
 183             long[] es = s.toArray();
 184             assertEquals(es.length, 0);
 185         }
 186 
 187         {
 188             LongStream s = OptionalLong.of(42L).stream();
 189 
 190             long[] es = s.toArray();
 191             assertEquals(es.length, 1);
 192             assertEquals(es[0], 42L);
 193         }
 194     }
 195 
 196     private static class ObscureException extends RuntimeException {
 197 
 198     }
 199 }