HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (migrate HEM tests - first attempt)

This commit is contained in:
Steve Ebersole 2016-05-04 11:10:27 -05:00
parent 2e1d1a6f16
commit 11fc090557
1 changed files with 13 additions and 3 deletions

View File

@ -10,13 +10,17 @@ import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.jboss.logging.Logger;
/**
* @author Steve Ebersole
*/
public class ClassLoadingIsolater implements MethodRule {
private static final Logger log = Logger.getLogger( ClassLoadingIsolater.class );
public static interface IsolatedClassLoaderProvider {
public ClassLoader buildIsolatedClassLoader();
public void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader);
ClassLoader buildIsolatedClassLoader();
void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader);
}
private final IsolatedClassLoaderProvider provider;
@ -30,14 +34,20 @@ public class ClassLoadingIsolater implements MethodRule {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final ClassLoader isolatedClassLoader = provider.buildIsolatedClassLoader();
final ClassLoader originalTCCL = Thread.currentThread().getContextClassLoader();
final ClassLoader isolatedClassLoader = provider.buildIsolatedClassLoader();
log.infof( "Overriding TCCL [%s] -> [%s]", originalTCCL, isolatedClassLoader );
Thread.currentThread().setContextClassLoader( isolatedClassLoader );
try {
base.evaluate();
}
finally {
assert Thread.currentThread().getContextClassLoader() == isolatedClassLoader;
log.infof( "Reverting TCCL [%s] -> [%s]", isolatedClassLoader, originalTCCL );
Thread.currentThread().setContextClassLoader( originalTCCL );
provider.releaseIsolatedClassLoader( isolatedClassLoader );
}