< prev index next > test/jdk/java/lang/ScopedValue/ScopedValueAPI.java
Print this page
private static Stream<ThreadFactory> factories() {
return Stream.of(Thread.ofPlatform().factory(), Thread.ofVirtual().factory());
}
/**
- * Test that runWhere invokes the Runnable's run method.
+ * Test that where invokes the Runnable's run method.
*/
@ParameterizedTest
@MethodSource("factories")
void testRunWhere(ThreadFactory factory) throws Exception {
test(factory, () -> {
class Box { static boolean executed; }
ScopedValue<String> name = ScopedValue.newInstance();
- ScopedValue.runWhere(name, "duke", () -> { Box.executed = true; });
+ ScopedValue.where(name, "duke").run(() -> { Box.executed = true; });
assertTrue(Box.executed);
});
}
/**
- * Test runWhere when the run method throws an exception.
+ * Test where when the run method throws an exception.
*/
@ParameterizedTest
@MethodSource("factories")
void testRunWhereThrows(ThreadFactory factory) throws Exception {
test(factory, () -> {
class FooException extends RuntimeException { }
ScopedValue<String> name = ScopedValue.newInstance();
Runnable op = () -> { throw new FooException(); };
- assertThrows(FooException.class, () -> ScopedValue.runWhere(name, "duke", op));
+ assertThrows(FooException.class, () -> ScopedValue.where(name, "duke").run(op));
assertFalse(name.isBound());
});
}
/**
@ParameterizedTest
@MethodSource("factories")
void testCallWhere(ThreadFactory factory) throws Exception {
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
- String result = ScopedValue.callWhere(name, "duke", name::get);
+ String result = ScopedValue.where(name, "duke").call(name::get);
assertEquals("duke", result);
});
}
/**
void testCallWhereThrows(ThreadFactory factory) throws Exception {
test(factory, () -> {
class FooException extends RuntimeException { }
ScopedValue<String> name = ScopedValue.newInstance();
CallableOp<Void, RuntimeException> op = () -> { throw new FooException(); };
- assertThrows(FooException.class, () -> ScopedValue.callWhere(name, "duke", op));
+ assertThrows(FooException.class, () -> ScopedValue.where(name, "duke").call(op));
assertFalse(name.isBound());
});
}
/**
ScopedValue<String> name1 = ScopedValue.newInstance();
ScopedValue<String> name2 = ScopedValue.newInstance();
assertThrows(NoSuchElementException.class, name1::get);
assertThrows(NoSuchElementException.class, name2::get);
- // runWhere
- ScopedValue.runWhere(name1, "duke", () -> {
+ // where
+ ScopedValue.where(name1, "duke").run(() -> {
assertEquals("duke", name1.get());
assertThrows(NoSuchElementException.class, name2::get);
});
assertThrows(NoSuchElementException.class, name1::get);
assertThrows(NoSuchElementException.class, name2::get);
// callWhere
- ScopedValue.callWhere(name1, "duke", () -> {
+ ScopedValue.where(name1, "duke").call(() -> {
assertEquals("duke", name1.get());
assertThrows(NoSuchElementException.class, name2::get);
return null;
});
assertThrows(NoSuchElementException.class, name1::get);
ScopedValue<String> name1 = ScopedValue.newInstance();
ScopedValue<String> name2 = ScopedValue.newInstance();
assertFalse(name1.isBound());
assertFalse(name2.isBound());
- // runWhere
- ScopedValue.runWhere(name1, "duke", () -> {
+ // where
+ ScopedValue.where(name1, "duke").run(() -> {
assertTrue(name1.isBound());
assertFalse(name2.isBound());
});
assertFalse(name1.isBound());
assertFalse(name2.isBound());
// callWhere
- ScopedValue.callWhere(name1, "duke", () -> {
+ ScopedValue.where(name1, "duke").call(() -> {
assertTrue(name1.isBound());
assertFalse(name2.isBound());
return null;
});
assertFalse(name1.isBound());
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
assertNull(name.orElse(null));
assertEquals("default", name.orElse("default"));
- // runWhere
- ScopedValue.runWhere(name, "duke", () -> {
+ // where
+ ScopedValue.where(name, "duke").run(() -> {
assertEquals("duke", name.orElse(null));
assertEquals("duke", name.orElse("default"));
});
// callWhere
- ScopedValue.callWhere(name, "duke", () -> {
+ ScopedValue.where(name, "duke").call(() -> {
assertEquals("duke", name.orElse(null));
assertEquals("duke", name.orElse("default"));
return null;
});
});
test(factory, () -> {
class FooException extends RuntimeException { }
ScopedValue<String> name = ScopedValue.newInstance();
assertThrows(FooException.class, () -> name.orElseThrow(FooException::new));
- // runWhere
- ScopedValue.runWhere(name, "duke", () -> {
+ // where
+ ScopedValue.where(name, "duke").run(() -> {
assertEquals("duke", name.orElseThrow(FooException::new));
});
// callWhere
- ScopedValue.callWhere(name, "duke", () -> {
+ ScopedValue.where(name, "duke").call(() -> {
assertEquals("duke", name.orElseThrow(FooException::new));
return null;
});
});
}
@MethodSource("factories")
void testRebinding(ThreadFactory factory) throws Exception {
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
- // runWhere
- ScopedValue.runWhere(name, "duke", () -> {
+ // where
+ ScopedValue.where(name, "duke").run(() -> {
assertTrue(name.isBound());
assertEquals("duke", name.get());
- ScopedValue.runWhere(name, "duchess", () -> {
+ ScopedValue.where(name, "duchess").run(() -> {
assertTrue(name.isBound());
assertEquals("duchess", name.get());
});
assertTrue(name.isBound());
assertEquals("duke", name.get());
});
assertFalse(name.isBound());
// callWhere
- ScopedValue.callWhere(name, "duke", () -> {
+ ScopedValue.where(name, "duke").call(() -> {
assertTrue(name.isBound());
assertEquals("duke", name.get());
- ScopedValue.callWhere(name, "duchess", () -> {
+ ScopedValue.where(name, "duchess").call(() -> {
assertTrue(name.isBound());
assertEquals("duchess", name.get());
return null;
});
@MethodSource("factories")
void testRebindingFromNull(ThreadFactory factory) throws Exception {
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
- // runWhere
- ScopedValue.runWhere(name, null, () -> {
+ // where
+ ScopedValue.where(name, null).run(() -> {
assertTrue(name.isBound());
assertNull(name.get());
- ScopedValue.runWhere(name, "duchess", () -> {
+ ScopedValue.where(name, "duchess").run(() -> {
assertTrue(name.isBound());
assertTrue("duchess".equals(name.get()));
});
assertTrue(name.isBound());
assertNull(name.get());
});
assertFalse(name.isBound());
// callWhere
- ScopedValue.callWhere(name, null, () -> {
+ ScopedValue.where(name, null).call(() -> {
assertTrue(name.isBound());
assertNull(name.get());
- ScopedValue.callWhere(name, "duchess", () -> {
+ ScopedValue.where(name, "duchess").call(() -> {
assertTrue(name.isBound());
assertTrue("duchess".equals(name.get()));
return null;
});
@MethodSource("factories")
void testRebindingToNull(ThreadFactory factory) throws Exception {
test(factory, () -> {
ScopedValue<String> name = ScopedValue.newInstance();
- // runWhere
- ScopedValue.runWhere(name, "duke", () -> {
+ // where
+ ScopedValue.where(name, "duke").run(() -> {
assertTrue(name.isBound());
assertEquals("duke", name.get());
- ScopedValue.runWhere(name, null, () -> {
+ ScopedValue.where(name, null).run(() -> {
assertTrue(name.isBound());
assertNull(name.get());
});
assertTrue(name.isBound());
assertEquals("duke", name.get());
});
assertFalse(name.isBound());
// callWhere
- ScopedValue.callWhere(name, "duke", () -> {
+ ScopedValue.where(name, "duke").call(() -> {
assertTrue(name.isBound());
assertEquals("duke", name.get());
- ScopedValue.callWhere(name, null, () -> {
+ ScopedValue.where(name, null).call(() -> {
assertTrue(name.isBound());
assertNull(name.get());
return null;
});
void testNullPointerException() {
ScopedValue<String> name = ScopedValue.newInstance();
assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke"));
- assertThrows(NullPointerException.class, () -> ScopedValue.runWhere(null, "duke", () -> { }));
- assertThrows(NullPointerException.class, () -> ScopedValue.runWhere(name, "duke", null));
+ assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").run(() -> { }));
+ assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").run(null));
- assertThrows(NullPointerException.class, () -> ScopedValue.callWhere(null, "duke", () -> ""));
- assertThrows(NullPointerException.class, () -> ScopedValue.callWhere(name, "duke", null));
+ assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").call(() -> ""));
+ assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").call(null));
assertThrows(NullPointerException.class, () -> name.orElseThrow(null));
var carrier = ScopedValue.where(name, "duke");
assertThrows(NullPointerException.class, () -> carrier.where(null, "duke"));
< prev index next >