< prev index next > test/jdk/java/lang/ScopedValue/ScopedValueAPI.java
Print this page
/*
- * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@ParameterizedTest
@MethodSource("factories")
void testOrElse(ThreadFactory factory) throws Exception {
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
- assertNull(name.orElse(null));
assertEquals("default", name.orElse("default"));
// where
ScopedValue.where(name, "duke").run(() -> {
- assertEquals("duke", name.orElse(null));
assertEquals("duke", name.orElse("default"));
});
// callWhere
ScopedValue.where(name, "duke").call(() -> {
- assertEquals("duke", name.orElse(null));
assertEquals("duke", name.orElse("default"));
return null;
});
});
}
assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").run(null));
assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").call(() -> ""));
assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").call(null));
+ assertThrows(NullPointerException.class, () -> name.orElse(null));
assertThrows(NullPointerException.class, () -> name.orElseThrow(null));
var carrier = ScopedValue.where(name, "duke");
assertThrows(NullPointerException.class, () -> carrier.where(null, "duke"));
assertThrows(NullPointerException.class, () -> carrier.get((ScopedValue<?>)null));
assertThrows(NullPointerException.class, () -> carrier.run(null));
assertThrows(NullPointerException.class, () -> carrier.call(null));
+ assertThrows(NullPointerException.class, () -> carrier.run(() -> name.orElse(null)));
}
@FunctionalInterface
private interface ThrowingRunnable {
void run() throws Exception;
< prev index next >