### Question:
ThreadContextClassLoaderTaskExecutor { public static <V> V doWithTccl(ClassLoader classLoader, Callable<V> task) throws Exception { ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); try { return task.call(); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); } } private  ThreadContextClassLoaderTaskExecutor(); static V doWithTccl(ClassLoader classLoader, Callable<V> task);  }### Answer:
@Test public void test_with_normal_execution() throws Exception { final String expectedResult = RandomStringUtils.randomAlphanumeric(10); final ClassLoader parent = getClass().getClassLoader(); final ClassLoader innerLoader = new DummyClassLoader(parent); ClassLoader outerLoader = new DummyClassLoader(parent); Thread.currentThread().setContextClassLoader(outerLoader); String actual = ThreadContextClassLoaderTaskExecutor.doWithTccl(innerLoader, new Callable<String>() { @Override public String call() throws Exception { assertEquals(innerLoader, Thread.currentThread().getContextClassLoader()); return expectedResult; } }); assertEquals(expectedResult, actual); assertEquals(outerLoader, Thread.currentThread().getContextClassLoader()); }
@Test public void test_with_exception_in_task() throws Exception { final ClassLoader parent = getClass().getClassLoader(); final ClassLoader innerLoader = new DummyClassLoader(parent); ClassLoader outerLoader = new DummyClassLoader(parent); Thread.currentThread().setContextClassLoader(outerLoader); boolean thrown = false; try { ThreadContextClassLoaderTaskExecutor.doWithTccl(innerLoader, new Callable<String>() { @Override public String call() throws Exception { assertEquals(innerLoader, Thread.currentThread().getContextClassLoader()); throw new Exception(); } }); } catch (Exception e) { thrown = true; } assertTrue(thrown); assertEquals(outerLoader, Thread.currentThread().getContextClassLoader()); }