1515 */
1616package nl .talsmasoftware .context .mdc ;
1717
18+ import nl .talsmasoftware .context .Context ;
19+ import nl .talsmasoftware .context .ContextManagers ;
20+ import nl .talsmasoftware .context .ContextSnapshot ;
1821import nl .talsmasoftware .context .executors .ContextAwareExecutorService ;
1922import org .junit .After ;
2023import org .junit .Before ;
@@ -46,6 +49,12 @@ public void shutdownThreadpool() {
4649 threadpool = null ;
4750 }
4851
52+ @ Before
53+ @ After
54+ public void clearMDC () {
55+ MDC .clear ();
56+ }
57+
4958 private static final Callable <String > GET_MDC_ITEM = new Callable <String >() {
5059 public String call () {
5160 return MDC .get ("mdc-item" );
@@ -59,4 +68,21 @@ public void testMdcItemPropagation() throws ExecutionException, InterruptedExcep
5968 assertThat (itemValue .get (), is ("Item value" ));
6069 }
6170
71+ @ Test
72+ public void testMdcItemRestoration () throws Exception {
73+ MDC .put ("mdc-item" , "Value 1" );
74+
75+ ContextSnapshot snapshot = ContextManagers .createContextSnapshot ();
76+ assertThat ("New snapshot shouldn't manipulate MDC." , GET_MDC_ITEM .call (), is ("Value 1" ));
77+
78+ MDC .put ("mdc-item" , "Value 2" );
79+ assertThat ("Sanity check: MDC changed?" , GET_MDC_ITEM .call (), is ("Value 2" ));
80+
81+ Context <Void > reactivation = snapshot .reactivate ();
82+ assertThat ("MDC changed by reactivation" , GET_MDC_ITEM .call (), is ("Value 1" ));
83+
84+ reactivation .close ();
85+ assertThat ("MDC restored?" , GET_MDC_ITEM .call (), is ("Value 2" ));
86+ }
87+
6288}
0 commit comments