< prev index next >

test/jdk/java/lang/ScopedValue/ScopedValueAPI.java

Print this page

  1 /*
  2  * Copyright (c) 2021, 2024, 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  */

158 
159             // callWhere
160             ScopedValue.where(name1, "duke").call(() -> {
161                 assertTrue(name1.isBound());
162                 assertFalse(name2.isBound());
163                 return null;
164             });
165             assertFalse(name1.isBound());
166             assertFalse(name2.isBound());
167         });
168     }
169 
170     /**
171      * Test orElse method.
172      */
173     @ParameterizedTest
174     @MethodSource("factories")
175     void testOrElse(ThreadFactory factory) throws Exception {
176         test(factory, () -> {
177             ScopedValue<String> name = ScopedValue.newInstance();
178             assertNull(name.orElse(null));
179             assertEquals("default", name.orElse("default"));
180 
181             // where
182             ScopedValue.where(name, "duke").run(() -> {
183                 assertEquals("duke", name.orElse(null));
184                 assertEquals("duke", name.orElse("default"));
185             });
186 
187             // callWhere
188             ScopedValue.where(name, "duke").call(() -> {
189                 assertEquals("duke", name.orElse(null));
190                 assertEquals("duke", name.orElse("default"));
191                 return null;
192             });
193         });
194     }
195 
196     /**
197      * Test orElseThrow method.
198      */
199     @ParameterizedTest
200     @MethodSource("factories")
201     void testOrElseThrow(ThreadFactory factory) throws Exception {
202         test(factory, () -> {
203             class FooException extends RuntimeException { }
204             ScopedValue<String> name = ScopedValue.newInstance();
205             assertThrows(FooException.class, () -> name.orElseThrow(FooException::new));
206 
207             // where
208             ScopedValue.where(name, "duke").run(() -> {
209                 assertEquals("duke", name.orElseThrow(FooException::new));

399             assertEquals("duke", carrier2.get(name));
400             assertEquals(20, (int) carrier2.get(age));
401         });
402     }
403 
404     /**
405      * Test NullPointerException.
406      */
407     @Test
408     void testNullPointerException() {
409         ScopedValue<String> name = ScopedValue.newInstance();
410 
411         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke"));
412 
413         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").run(() -> { }));
414         assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").run(null));
415 
416         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").call(() -> ""));
417         assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").call(null));
418 

419         assertThrows(NullPointerException.class, () -> name.orElseThrow(null));
420 
421         var carrier = ScopedValue.where(name, "duke");
422         assertThrows(NullPointerException.class, () -> carrier.where(null, "duke"));
423         assertThrows(NullPointerException.class, () -> carrier.get((ScopedValue<?>)null));
424         assertThrows(NullPointerException.class, () -> carrier.run(null));
425         assertThrows(NullPointerException.class, () -> carrier.call(null));

426     }
427 
428     @FunctionalInterface
429     private interface ThrowingRunnable {
430         void run() throws Exception;
431     }
432 
433     /**
434      * Run the given task in a thread created with the given thread factory.
435      * @throws Exception if the task throws an exception
436      */
437     private static void test(ThreadFactory factory, ThrowingRunnable task) throws Exception {
438         try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
439             var future = executor.submit(() -> {
440                 task.run();
441                 return null;
442             });
443             try {
444                 future.get();
445             } catch (ExecutionException ee) {

  1 /*
  2  * Copyright (c) 2021, 2025, 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  */

158 
159             // callWhere
160             ScopedValue.where(name1, "duke").call(() -> {
161                 assertTrue(name1.isBound());
162                 assertFalse(name2.isBound());
163                 return null;
164             });
165             assertFalse(name1.isBound());
166             assertFalse(name2.isBound());
167         });
168     }
169 
170     /**
171      * Test orElse method.
172      */
173     @ParameterizedTest
174     @MethodSource("factories")
175     void testOrElse(ThreadFactory factory) throws Exception {
176         test(factory, () -> {
177             ScopedValue<String> name = ScopedValue.newInstance();

178             assertEquals("default", name.orElse("default"));
179 
180             // where
181             ScopedValue.where(name, "duke").run(() -> {

182                 assertEquals("duke", name.orElse("default"));
183             });
184 
185             // callWhere
186             ScopedValue.where(name, "duke").call(() -> {

187                 assertEquals("duke", name.orElse("default"));
188                 return null;
189             });
190         });
191     }
192 
193     /**
194      * Test orElseThrow method.
195      */
196     @ParameterizedTest
197     @MethodSource("factories")
198     void testOrElseThrow(ThreadFactory factory) throws Exception {
199         test(factory, () -> {
200             class FooException extends RuntimeException { }
201             ScopedValue<String> name = ScopedValue.newInstance();
202             assertThrows(FooException.class, () -> name.orElseThrow(FooException::new));
203 
204             // where
205             ScopedValue.where(name, "duke").run(() -> {
206                 assertEquals("duke", name.orElseThrow(FooException::new));

396             assertEquals("duke", carrier2.get(name));
397             assertEquals(20, (int) carrier2.get(age));
398         });
399     }
400 
401     /**
402      * Test NullPointerException.
403      */
404     @Test
405     void testNullPointerException() {
406         ScopedValue<String> name = ScopedValue.newInstance();
407 
408         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke"));
409 
410         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").run(() -> { }));
411         assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").run(null));
412 
413         assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").call(() -> ""));
414         assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").call(null));
415 
416         assertThrows(NullPointerException.class, () -> name.orElse(null));
417         assertThrows(NullPointerException.class, () -> name.orElseThrow(null));
418 
419         var carrier = ScopedValue.where(name, "duke");
420         assertThrows(NullPointerException.class, () -> carrier.where(null, "duke"));
421         assertThrows(NullPointerException.class, () -> carrier.get((ScopedValue<?>)null));
422         assertThrows(NullPointerException.class, () -> carrier.run(null));
423         assertThrows(NullPointerException.class, () -> carrier.call(null));
424         assertThrows(NullPointerException.class, () -> carrier.run(() -> name.orElse(null)));
425     }
426 
427     @FunctionalInterface
428     private interface ThrowingRunnable {
429         void run() throws Exception;
430     }
431 
432     /**
433      * Run the given task in a thread created with the given thread factory.
434      * @throws Exception if the task throws an exception
435      */
436     private static void test(ThreadFactory factory, ThrowingRunnable task) throws Exception {
437         try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
438             var future = executor.submit(() -> {
439                 task.run();
440                 return null;
441             });
442             try {
443                 future.get();
444             } catch (ExecutionException ee) {
< prev index next >