< prev index next >

test/jdk/java/util/Optional/BasicInt.java

Print this page
rev 49492 : 8195649: reorganize tests for java.util.Optional
Reviewed-by: XXX
   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 OptionalInt
  26  * @author Mike Duigou

  27  * @run testng BasicInt
  28  */
  29 
  30 import java.util.NoSuchElementException;
  31 import java.util.OptionalInt;
  32 import java.util.concurrent.atomic.AtomicBoolean;
  33 import java.util.stream.IntStream;
  34 
  35 import static org.testng.Assert.*;
  36 import org.testng.annotations.Test;
  37 
  38 
  39 public class BasicInt {
  40 
  41     @Test(groups = "unit")
  42     public void testEmpty() {
  43         OptionalInt empty = OptionalInt.empty();
  44         OptionalInt present = OptionalInt.of(1);
  45 
  46         // empty
  47         assertTrue(empty.equals(empty));


  48         assertTrue(empty.equals(OptionalInt.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         OptionalInt empty = OptionalInt.empty();
  77         OptionalInt present = OptionalInt.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         OptionalInt empty = OptionalInt.empty();
 102 
 103         int got = empty.getAsInt();
 104     }
 105 
 106     @Test(expectedExceptions=NullPointerException.class)
 107     public void testEmptyOrElseGetNull() {
 108         OptionalInt empty = OptionalInt.empty();
 109 
 110         int got = empty.orElseGet(null);
 111     }
 112 
 113     @Test(expectedExceptions=NullPointerException.class)
 114     public void testEmptyOrElseThrowNull() throws Throwable {
 115         OptionalInt empty = OptionalInt.empty();
 116 
 117         int got = empty.orElseThrow(null);
 118     }
 119 
 120     @Test(expectedExceptions=ObscureException.class)
 121     public void testEmptyOrElseThrow() throws Exception {
 122         OptionalInt empty = OptionalInt.empty();
 123 
 124         int got = empty.orElseThrow(ObscureException::new);
 125     }
 126 
 127     @Test(expectedExceptions=NoSuchElementException.class)
 128     public void testEmptyOrElseThrowNoArg() throws Exception {
 129         OptionalInt empty = OptionalInt.empty();
 130 
 131         int got = empty.orElseThrow();
 132     }
 133 
 134     @Test(groups = "unit")
 135     public void testPresent() {
 136         OptionalInt empty = OptionalInt.empty();
 137         OptionalInt present = OptionalInt.of(1);
 138 
 139         // present
 140         assertTrue(present.equals(present));
 141         assertFalse(present.equals(OptionalInt.of(0)));
 142         assertTrue(present.equals(OptionalInt.of(1)));
 143         assertFalse(present.equals(empty));
 144         assertTrue(Integer.hashCode(1) == present.hashCode());
 145         assertFalse(present.toString().isEmpty());
 146         assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
 147         assertTrue(-1 != present.toString().indexOf(Integer.toString(present.orElseThrow()).toString()));
 148         assertEquals(1, present.getAsInt());
 149         assertEquals(1, present.orElseThrow());
 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         assertEquals(1, present.orElse(2));
 178         assertEquals(1, present.orElseGet(null));
 179         assertEquals(1, present.orElseGet(()-> 2));
 180         assertEquals(1, present.orElseGet(()-> 3));
 181         assertEquals(1, present.<RuntimeException>orElseThrow(null));
 182         assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
 183     }
 184 
 185     @Test(groups = "unit")
 186     public void testStream() {
 187         {
 188             IntStream s = OptionalInt.empty().stream();
 189             assertFalse(s.isParallel());
 190 
 191             int[] es = s.toArray();
 192             assertEquals(es.length, 0);
 193         }
 194 
 195         {
 196             IntStream s = OptionalInt.of(42).stream();
 197             assertFalse(s.isParallel());
 198 
 199             int[] es = OptionalInt.of(42).stream().toArray();
 200             assertEquals(es.length, 1);
 201             assertEquals(es[0], 42);
 202         }
 203     }
 204 
 205     private static class ObscureException extends RuntimeException {
 206 

 207     }
 208 }
   1 /*
   2  * Copyright (c) 2013, 2018, 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  * @bug 8195649
  26  * @summary Basic functional test of OptionalInt
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng BasicInt
  30  */
  31 
  32 import java.util.NoSuchElementException;
  33 import java.util.OptionalInt;
  34 import java.util.concurrent.atomic.AtomicBoolean;

  35 
  36 import static org.testng.Assert.*;
  37 import org.testng.annotations.Test;
  38 

  39 public class BasicInt {
  40 
  41     static final int INTVAL = 33_550_336;
  42     static final int UNEXPECTED = 0xCAFEBABE;


  43 
  44     /**
  45      * Checks a block of assertions over an empty OptionalInt.
  46      */
  47     void checkEmpty(OptionalInt empty) {
  48         assertTrue(empty.equals(OptionalInt.empty()));
  49         assertTrue(OptionalInt.empty().equals(empty));
  50         assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
  51         assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
  52         assertFalse(empty.equals("unexpected"));
  53 
  54         assertFalse(empty.isPresent());
  55         assertEquals(empty.hashCode(), 0);
  56         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
  57         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
  58 
  59         assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
  60         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  61         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  62 
  63         var b = new AtomicBoolean();
  64         empty.ifPresent(s -> b.set(true));
  65         assertFalse(b.get());
  66 
  67         var b1 = new AtomicBoolean(false);
  68         var b2 = new AtomicBoolean(false);
  69         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  70         assertFalse(b1.get());
  71         assertTrue(b2.get());
  72 
  73         assertEquals(empty.toString(), "OptionalInt.empty");
  74     }
  75 
  76     /**
  77      * Checks a block of assertions over an OptionalInt that is expected to
  78      * have a particular value present.
  79      */
  80     void checkPresent(OptionalInt opt, int expected) {
  81         assertFalse(opt.equals(OptionalInt.empty()));
  82         assertFalse(OptionalInt.empty().equals(opt));
  83         assertTrue(opt.equals(OptionalInt.of(expected)));
  84         assertTrue(OptionalInt.of(expected).equals(opt));
  85         assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
  86         assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
  87         assertFalse(opt.equals("unexpected"));
  88 
  89         assertTrue(opt.isPresent());
  90         assertEquals(opt.hashCode(), Integer.hashCode(expected));
  91         assertEquals(opt.orElse(UNEXPECTED), expected);
  92         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
  93 
  94         assertEquals(opt.getAsInt(), expected);
  95         assertEquals(opt.orElseThrow(), expected);
  96         assertEquals(opt.orElseThrow(ObscureException::new), expected);
  97 
  98         var b = new AtomicBoolean(false);
  99         opt.ifPresent(s -> b.set(true));
 100         assertTrue(b.get());
 101 
 102         var b1 = new AtomicBoolean(false);
 103         var b2 = new AtomicBoolean(false);
 104         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
 105         assertTrue(b1.get());
 106         assertFalse(b2.get());
 107 
 108         assertEquals(opt.toString(), "OptionalInt[" + expected + "]");

 109     }
 110 
 111     @Test(groups = "unit")
 112     public void testEmpty() {
 113         checkEmpty(OptionalInt.empty());























































 114     }
 115 
 116     @Test(groups = "unit")
 117     public void testPresent() {
 118         checkPresent(OptionalInt.of(INTVAL), INTVAL);














































 119     }
 120 
 121     @Test(groups = "unit")
 122     public void testStreamEmpty() {
 123         assertEquals(OptionalInt.empty().stream().toArray(), new int[] { });















 124     }
 125 
 126     @Test(groups = "unit")
 127     public void testStreamPresent() {
 128         assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL });
 129     }
 130 }
< prev index next >