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 import java.util.function.IntPredicate; 36 import java.util.List; 37 38 import static org.testng.Assert.*; 39 import org.testng.annotations.Test; 40 41 public class BasicInt { 42 43 static final int INTVAL = 33_550_336; 44 static final int UNEXPECTED = 0xCAFEBABE; 45 46 /** 47 * Checks a block of assertions over an empty OptionalInt. 48 */ 49 void checkEmpty(OptionalInt empty) { 50 assertTrue(empty.equals(OptionalInt.empty())); 51 assertTrue(OptionalInt.empty().equals(empty)); 52 assertFalse(empty.equals(OptionalInt.of(UNEXPECTED))); 53 assertFalse(OptionalInt.of(UNEXPECTED).equals(empty)); 54 assertFalse(empty.equals("unexpected")); 55 56 assertFalse(empty.isPresent()); 57 assertTrue(empty.isEmpty()); 58 59 assertEquals(empty.hashCode(), 0); 60 assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); 61 assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); 62 63 assertThrows(NoSuchElementException.class, () -> empty.getAsInt()); 64 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); 65 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); 66 67 var b = new AtomicBoolean(); 68 empty.ifPresent(s -> b.set(true)); 69 assertFalse(b.get()); 70 71 var b1 = new AtomicBoolean(false); 72 var b2 = new AtomicBoolean(false); 73 empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 74 assertFalse(b1.get()); 75 assertTrue(b2.get()); 76 77 assertEquals(empty.toString(), "OptionalInt.empty"); 78 } 79 80 /** 81 * Checks a block of assertions over an OptionalInt that is expected to 82 * have a particular value present. 83 */ 84 void checkPresent(OptionalInt opt, int expected) { 85 assertFalse(opt.equals(OptionalInt.empty())); 86 assertFalse(OptionalInt.empty().equals(opt)); 87 assertTrue(opt.equals(OptionalInt.of(expected))); 88 assertTrue(OptionalInt.of(expected).equals(opt)); 89 assertFalse(opt.equals(OptionalInt.of(UNEXPECTED))); 90 assertFalse(OptionalInt.of(UNEXPECTED).equals(opt)); 91 assertFalse(opt.equals("unexpected")); 92 93 assertTrue(opt.isPresent()); 94 assertEquals(opt.hashCode(), Integer.hashCode(expected)); 95 assertEquals(opt.orElse(UNEXPECTED), expected); 96 assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); 97 98 assertEquals(opt.getAsInt(), expected); 99 assertEquals(opt.orElseThrow(), expected); 100 assertEquals(opt.orElseThrow(ObscureException::new), expected); 101 102 var b = new AtomicBoolean(false); 103 opt.ifPresent(s -> b.set(true)); 104 assertTrue(b.get()); 105 106 var b1 = new AtomicBoolean(false); 107 var b2 = new AtomicBoolean(false); 108 opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 109 assertTrue(b1.get()); 110 assertFalse(b2.get()); 111 112 assertEquals(opt.toString(), "OptionalInt[" + expected + "]"); 113 } 114 115 @Test(groups = "unit") 116 public void testEmpty() { 117 checkEmpty(OptionalInt.empty()); 118 } 119 120 @Test(groups = "unit") 121 public void testPresent() { 122 checkPresent(OptionalInt.of(INTVAL), INTVAL); 123 } 124 125 @Test(groups = "unit") 126 public void testStreamEmpty() { 127 assertEquals(OptionalInt.empty().stream().toArray(), new int[] { }); 128 } 129 130 @Test(groups = "unit") 131 public void testStreamPresent() { 132 assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL }); 133 } 134 135 @Test(groups = "unit") 136 public void testIsEmpty() { 137 OptionalInt empty = OptionalInt.empty(); 138 assertTrue(empty.isEmpty()); 139 OptionalInt present = OptionalInt.of(1); 140 assertFalse(present.isEmpty()); 141 var integerList = List.of(1, 2, 3, 4, 5); 142 IntPredicate isPositiveNumber = x -> x > 0; 143 IntPredicate isNegativeNumber = x -> x < 0; 144 OptionalInt positiveNumber = integerList.stream().mapToInt(Integer::intValue).filter(isPositiveNumber).findAny(); 145 OptionalInt negativeNumber = integerList.stream().mapToInt(Integer::intValue).filter(isNegativeNumber).findAny(); 146 assertFalse(positiveNumber.isEmpty()); 147 assertTrue(negativeNumber.isEmpty()); 148 } 149 150 }