RandomRegistry { public static Random random() { return CONTEXT.get().get(); } private  RandomRegistry(); static Random random(); static void random(final Random random); static void random(final ThreadLocal<? extends Random> random); static void reset(); static void using(
		final R random,
		final Consumer<? super R> consumer
	); static void using(
		final ThreadLocal<R> random,
		final Consumer<? super R> consumer
	); static T with(
		final R random,
		final Function<? super R, ? extends T> function
	); static T with(
		final ThreadLocal<R> random,
		final Function<? super R, ? extends T> function
	);  }
@Test public void setRandom() { final Random random = new Random(); RandomRegistry.random(random); assertSame(RandomRegistry.random(), random); }
@Test public void setRandomThreading() throws ExecutionException, InterruptedException { final Random random = new Random(); RandomRegistry.random(random); final ExecutorService executor = Executors.newFixedThreadPool(10); try { final List<Future<?>> futures = IntStream.range(0, 500) .mapToObj(i -> executor .submit(() -> assertSame(RandomRegistry.random(), random))) .collect(Collectors.toList()); for (Future<?> future : futures) { future.get(); } } finally { executor.shutdown(); } }
@Test public void setThreadLocalRandom() { final Random random = new Random(); RandomRegistry.random(random); Assert.assertSame(RandomRegistry.random(), random); }
@Test(expectedExceptions = NullPointerException.class) public void setNullRandom() { RandomRegistry.random((Random)null); }
@Test(expectedExceptions = NullPointerException.class) public void setNullTLRandom() { RandomRegistry.random((ThreadLocal<Random>)null); }