1 /* 2 * Copyright (c) 2021, 2022, 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 /** 25 * @test 26 * @summary Basic test for ExtentLocal 27 * @modules jdk.incubator.concurrent 28 * @run testng Basic 29 */ 30 31 import jdk.incubator.concurrent.ExtentLocal; 32 import jdk.incubator.concurrent.StructureViolationException; 33 import java.util.NoSuchElementException; 34 import java.util.concurrent.Callable; 35 import java.util.concurrent.Executors; 36 import java.util.concurrent.ExecutionException; 37 import java.util.concurrent.Future; 38 39 import org.testng.annotations.Test; 40 41 import static org.testng.Assert.*; 42 43 public class Basic { 44 45 @Test 46 public void testUnbound1() { 47 ExtentLocal<String> v = ExtentLocal.newInstance(); 48 assertFalse(v.isBound()); 49 assertThrows(NoSuchElementException.class, () -> v.get()); 50 } 51 52 @Test 53 public void testOrElse() { 54 ExtentLocal<String> name = ExtentLocal.newInstance(); 55 assertFalse(name.isBound()); 56 assertTrue(name.orElse(null) == null); 57 assertEquals(name.orElse("default"), "default"); 58 ExtentLocal.where(name, "fred", () -> { 59 assertEquals(name.orElse(null), "fred"); 60 assertEquals(name.orElse("default"), "fred"); 61 }); 62 } 63 64 @Test 65 public void testOrElseThrow() { 66 ExtentLocal<String> name = ExtentLocal.newInstance(); 67 assertFalse(name.isBound()); 68 assertThrows(IllegalStateException.class, () -> name.orElseThrow(IllegalStateException::new)); 69 assertThrows(NullPointerException.class, () -> name.orElseThrow(null)); 70 ExtentLocal.where(name, "fred", () -> { 71 assertEquals(name.orElseThrow(IllegalStateException::new), "fred"); 72 assertThrows(NullPointerException.class, () -> name.orElseThrow(null)); 73 }); 74 } 75 76 @Test 77 public void testRunWithBinding1() { 78 ExtentLocal<String> name = ExtentLocal.newInstance(); 79 ExtentLocal.where(name, "fred", () -> { 80 assertTrue(name.isBound()); 81 assertTrue("fred".equals(name.get())); 82 }); 83 } 84 85 @Test 86 public void testRunWithBinding2() { 87 ExtentLocal<String> name = ExtentLocal.newInstance(); 88 ExtentLocal.where(name, "fred", () -> { 89 assertTrue(name.isBound()); 90 assertTrue("fred".equals(name.get())); 91 92 ExtentLocal.where(name, "joe", () -> { 93 assertTrue(name.isBound()); 94 assertTrue("joe".equals(name.get())); 95 }); 96 97 assertTrue(name.isBound()); 98 assertTrue("fred".equals(name.get())); 99 }); 100 } 101 102 @Test 103 public void testRunWithBinding3() { 104 ExtentLocal<String> name = ExtentLocal.newInstance(); 105 ExtentLocal.where(name, null, () -> { 106 assertTrue(name.isBound()); 107 assertTrue(name.get() == null); 108 }); 109 } 110 111 @Test 112 public void testRunWithBinding4() { 113 ExtentLocal<String> name = ExtentLocal.newInstance(); 114 ExtentLocal.where(name, "fred", () -> { 115 assertTrue(name.isBound()); 116 assertTrue("fred".equals(name.get())); 117 118 ExtentLocal.where(name, null, () -> { 119 assertTrue(name.isBound()); 120 assertTrue(name.get() == null); 121 }); 122 123 assertTrue(name.isBound()); 124 assertTrue("fred".equals(name.get())); 125 }); 126 } 127 128 @Test 129 public void testRunWithBinding9() { 130 ExtentLocal<String> name = ExtentLocal.newInstance(); 131 assertThrows(NullPointerException.class, 132 () -> ExtentLocal.where(name, "fred", (Runnable) null)); 133 } 134 135 @Test 136 public void testCallWithBinding1() throws Exception { 137 ExtentLocal<String> name = ExtentLocal.newInstance(); 138 int result = ExtentLocal.where(name, "fred", () -> { 139 assertTrue(name.isBound()); 140 String value = name.get(); 141 assertTrue("fred".equals(value)); 142 return 1; 143 }); 144 assertTrue(result == 1); 145 } 146 147 @Test 148 public void testCallWithBinding2() throws Exception { 149 ExtentLocal<String> name = ExtentLocal.newInstance(); 150 int result1 = ExtentLocal.where(name, "fred", () -> { 151 assertTrue(name.isBound()); 152 String value1 = name.get(); 153 assertTrue("fred".equals(value1)); 154 155 int result2 = ExtentLocal.where(name, "joe", () -> { 156 assertTrue(name.isBound()); 157 String value2 = name.get(); 158 assertTrue("joe".equals(value2)); 159 return 2; 160 }); 161 assertTrue(result2 == 2); 162 163 return 1; 164 }); 165 assertTrue(result1 == 1); 166 } 167 168 @Test 169 public void testCallWithBinding3() throws Exception { 170 ExtentLocal<String> name = ExtentLocal.newInstance(); 171 int result = ExtentLocal.where(name, null, () -> { 172 assertTrue(name.isBound()); 173 assertTrue(name.get() == null); 174 return 1; 175 }); 176 assertTrue(result == 1); 177 } 178 179 @Test 180 public void testCallWithBinding4() throws Exception { 181 ExtentLocal<String> name = ExtentLocal.newInstance(); 182 int result1 = ExtentLocal.where(name, "fred", () -> { 183 assertTrue(name.isBound()); 184 String value1 = name.get(); 185 assertTrue("fred".equals(value1)); 186 187 int result2 = ExtentLocal.where(name, null, () -> { 188 assertTrue(name.isBound()); 189 assertTrue(name.get() == null); 190 return 2; 191 }); 192 assertTrue(result2 == 2); 193 194 return 1; 195 }); 196 assertTrue(result1 == 1); 197 } 198 199 @Test 200 public void testCallWithBinding9() throws Exception { 201 ExtentLocal<String> name = ExtentLocal.newInstance(); 202 assertThrows(NullPointerException.class, 203 () -> ExtentLocal.where(name, "fred", (Callable) null)); 204 } 205 }