LoggingAsyncContextImpl implements AsyncContext { @Override public void start(Runnable run) { try { Map<String, String> contextMap = getContextMap(); asyncContext.start(new Runnable() { @Override public void run() { Map<String, String> currentContextMap = MDC.getCopyOfContextMap(); try { MDC.setContextMap(contextMap); run.run(); } finally { if (currentContextMap != null) { MDC.setContextMap(currentContextMap); } } } }); } catch (ClassCastException ignored) { asyncContext.start(run); } } LoggingAsyncContextImpl(AsyncContext asyncContext, final RequestLogger requestLogger); @Override void start(Runnable run); @Override ServletRequest getRequest(); @Override ServletResponse getResponse(); @Override boolean hasOriginalRequestAndResponse(); @Override void dispatch(); @Override void dispatch(String path); @Override void dispatch(ServletContext context, String path); @Override void complete(); @Override void addListener(AsyncListener listener); @Override void addListener(AsyncListener listener, ServletRequest servletRequest, ServletResponse servletResponse); @Override T createListener(Class<T> clazz); @Override void setTimeout(long timeout); @Override long getTimeout();  }
@Test public void hasEmptyMDCWhenNoMapInRequest() throws Exception { Map<String, String> contextMap = new HashMap<>(); testedContext.start(putAllContextMap(contextMap)); assertThat(contextMap.entrySet(), is(empty())); }
@Test public void importsMDCEntriesFromRequest() throws Exception { Map<String, String> mdcAttributes = new HashMap<>(); mdcAttributes.put("this-key", "this-value"); mdcAttributes.put("that-key", "that-value"); when(request.getAttribute(MDC.class.getName())).thenReturn(mdcAttributes); Map<String, String> contextMap = new HashMap<>(); testedContext.start(putAllContextMap(contextMap)); assertThat(contextMap, both(hasEntry("that-key", "that-value")).and(hasEntry("this-key", "this-value"))); }
@Test public void resetsMDCEntriesBetweenConsequtiveRuns() throws Exception { Map<String, String> mdcAttributes = new HashMap<>(); mdcAttributes.put("this-key", "this-value"); mdcAttributes.put("that-key", "that-value"); when(request.getAttribute(MDC.class.getName())).thenReturn(mdcAttributes); Map<String, String> firstContextMap = new HashMap<>(); testedContext.start(putAllContextMap(firstContextMap)); reset(request); when(request.getAttribute(MDC.class.getName())).thenReturn(emptyMap()); Map<String, String> secondContextMap = new HashMap<>(); testedContext.start(putAllContextMap(secondContextMap)); assertThat(firstContextMap.entrySet(), is(not(empty()))); assertThat(secondContextMap.entrySet(), is(empty())); }
@Test public void savesAndRestoresThreadMDC() throws Exception { executor.submit(new Runnable() { @Override public void run() { MDC.put("initial-key", "initial-value"); } }).get(); Map<String, String> requestContextMap = new HashMap<>(); testedContext.start(putAllContextMap(requestContextMap)); Map<String, String> finalContextMap = new HashMap<>(); executor.submit(new Runnable() { @Override public void run() { finalContextMap.putAll(MDC.getCopyOfContextMap()); } }).get(); assertThat(requestContextMap.entrySet(), is(empty())); assertThat(finalContextMap, hasEntry("initial-key", "initial-value")); }