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 assertTrue(empty.isEmpty()); 56 assertEquals(empty.hashCode(), 0); 57 assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); 58 assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); 59 60 assertThrows(NoSuchElementException.class, () -> empty.getAsInt()); 61 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); 62 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); 63 64 var b = new AtomicBoolean(); 65 empty.ifPresent(s -> b.set(true)); 66 assertFalse(b.get()); 67 68 var b1 = new AtomicBoolean(false); 69 var b2 = new AtomicBoolean(false); 70 empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 71 assertFalse(b1.get()); 72 assertTrue(b2.get()); 73 74 assertEquals(empty.toString(), "OptionalInt.empty"); 75 } 76 77 /** 78 * Checks a block of assertions over an OptionalInt that is expected to 79 * have a particular value present. 80 */ 81 void checkPresent(OptionalInt opt, int expected) { 82 assertFalse(opt.equals(OptionalInt.empty())); 83 assertFalse(OptionalInt.empty().equals(opt)); 84 assertTrue(opt.equals(OptionalInt.of(expected))); 85 assertTrue(OptionalInt.of(expected).equals(opt)); 86 assertFalse(opt.equals(OptionalInt.of(UNEXPECTED))); 87 assertFalse(OptionalInt.of(UNEXPECTED).equals(opt)); 88 assertFalse(opt.equals("unexpected")); 89 90 assertTrue(opt.isPresent()); 91 assertFalse(opt.isEmpty()); 92 assertEquals(opt.hashCode(), Integer.hashCode(expected)); 93 assertEquals(opt.orElse(UNEXPECTED), expected); 94 assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); 95 96 assertEquals(opt.getAsInt(), expected); 97 assertEquals(opt.orElseThrow(), expected); 98 assertEquals(opt.orElseThrow(ObscureException::new), expected); 99 100 var b = new AtomicBoolean(false); 101 opt.ifPresent(s -> b.set(true)); 102 assertTrue(b.get()); 103 104 var b1 = new AtomicBoolean(false); 105 var b2 = new AtomicBoolean(false); 106 opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); 107 assertTrue(b1.get()); 108 assertFalse(b2.get()); 109 110 assertEquals(opt.toString(), "OptionalInt[" + expected + "]"); 111 } 112 113 @Test(groups = "unit") 114 public void testEmpty() { 115 checkEmpty(OptionalInt.empty()); 116 } 117 118 @Test(groups = "unit") 119 public void testPresent() { 120 checkPresent(OptionalInt.of(INTVAL), INTVAL); 121 } 122 123 @Test(groups = "unit") 124 public void testStreamEmpty() { 125 assertEquals(OptionalInt.empty().stream().toArray(), new int[] { }); 126 } 127 128 @Test(groups = "unit") 129 public void testStreamPresent() { 130 assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL }); 131 } 132 }