RequestScopedMdc { public static void copyAll(RequestContext ctx) { requireNonNull(ctx, "ctx"); checkState(delegate != null, ERROR_MESSAGE); final Map<String, String> map = getDelegateContextMap(); if (map != null) { putAll(ctx, map); } } private  RequestScopedMdc(); @Nullable static String get(RequestContext ctx, String key); static Map<String, String> getAll(RequestContext ctx); static void put(RequestContext ctx, String key, @Nullable String value); static void putAll(RequestContext ctx, Map<String, String> map); static void copy(RequestContext ctx, String key); static void copyAll(RequestContext ctx); static void remove(RequestContext ctx, String key); static void clear(RequestContext ctx);  }
@Test void copyAll() { final ServiceRequestContext ctx = newContext(); RequestScopedMdc.copyAll(ctx); assertThat(RequestScopedMdc.getAll(ctx)).isEmpty(); MDC.put("foo", "1"); MDC.put("bar", "2"); RequestScopedMdc.copyAll(ctx); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly( Maps.immutableEntry("foo", "1"), Maps.immutableEntry("bar", "2")); MDC.remove("foo"); MDC.put("bar", "3"); MDC.put("baz", "4"); RequestScopedMdc.copyAll(ctx); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly( Maps.immutableEntry("foo", "1"), Maps.immutableEntry("bar", "3"), Maps.immutableEntry("baz", "4")); }