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.stream.IntStream; 33 34 import static org.testng.Assert.*; 35 import org.testng.annotations.Test; 36 37 38 public class BasicInt { 39 40 @Test(groups = "unit") 41 public void testEmpty() { 42 OptionalInt empty = OptionalInt.empty(); 43 OptionalInt present = OptionalInt.of(1); 44 45 // empty 46 assertTrue(empty.equals(empty)); 47 assertTrue(empty.equals(OptionalInt.empty())); 48 assertTrue(!empty.equals(present)); 49 assertTrue(0 == empty.hashCode()); 50 assertTrue(!empty.toString().isEmpty()); 51 assertTrue(!empty.isPresent()); 52 empty.ifPresent(v -> { fail(); }); 53 assertEquals(2, empty.orElse(2)); 54 assertEquals(2, empty.orElseGet(()-> 2)); 55 } 56 57 @Test(expectedExceptions=NoSuchElementException.class) 58 public void testEmptyGet() { 59 OptionalInt empty = OptionalInt.empty(); 60 61 int got = empty.getAsInt(); 62 } 63 64 @Test(expectedExceptions=NullPointerException.class) 65 public void testEmptyOrElseGetNull() { 66 OptionalInt empty = OptionalInt.empty(); 67 68 int got = empty.orElseGet(null); 69 } 70 71 @Test(expectedExceptions=NullPointerException.class) 72 public void testEmptyOrElseThrowNull() throws Throwable { 73 OptionalInt empty = OptionalInt.empty(); 74 75 int got = empty.orElseThrow(null); 76 } 77 78 @Test(expectedExceptions=ObscureException.class) 79 public void testEmptyOrElseThrow() throws Exception { 80 OptionalInt empty = OptionalInt.empty(); 81 82 int got = empty.orElseThrow(ObscureException::new); 83 } 84 85 @Test(groups = "unit") 86 public void testPresent() { 87 OptionalInt empty = OptionalInt.empty(); 88 OptionalInt present = OptionalInt.of(1); 89 90 // present 91 assertTrue(present.equals(present)); 92 assertFalse(present.equals(OptionalInt.of(0))); 93 assertTrue(present.equals(OptionalInt.of(1))); 94 assertFalse(present.equals(empty)); 95 assertTrue(Integer.hashCode(1) == present.hashCode()); 96 assertFalse(present.toString().isEmpty()); 97 assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString())); 98 assertEquals(1, present.getAsInt()); 99 try { 100 present.ifPresent(v -> { throw new ObscureException(); }); 101 fail(); 102 } catch(ObscureException expected) { 103 104 } 105 assertEquals(1, present.orElse(2)); 106 assertEquals(1, present.orElseGet(null)); 107 assertEquals(1, present.orElseGet(()-> 2)); 108 assertEquals(1, present.orElseGet(()-> 3)); 109 assertEquals(1, present.<RuntimeException>orElseThrow(null)); 110 assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new)); 111 } 112 113 @Test(groups = "unit") 114 public void testStream() { 115 { 116 IntStream s = OptionalInt.empty().stream(); 117 assertFalse(s.isParallel()); 118 119 int[] es = s.toArray(); 120 assertEquals(es.length, 0); 121 } 122 123 { 124 IntStream s = OptionalInt.of(42).stream(); 125 assertFalse(s.isParallel()); 126 127 int[] es = OptionalInt.of(42).stream().toArray(); 128 assertEquals(es.length, 1); 129 assertEquals(es[0], 42); 130 } 131 } 132 133 private static class ObscureException extends RuntimeException { 134 135 } 136 }