Created a ClassLoadingIsolaterExtension to remove the need of @Rule in tests using ClassLoadingIsolater

This commit is contained in:
Andrea Boriero 2021-01-25 19:37:23 +01:00 committed by Jan Schatteman
parent e0e44433a9
commit 24b79133f2
2 changed files with 83 additions and 32 deletions

View File

@ -10,10 +10,9 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.persistence.EntityManagerFactory;
import org.hibernate.testing.junit4.ClassLoadingIsolater;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.junit.Rule;
import org.junit.Test;
import org.hibernate.testing.orm.junit.ClassLoadingIsolaterExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.fail;
@ -22,44 +21,40 @@ import static org.junit.jupiter.api.Assertions.fail;
*
* @author Steve Ebersole
*/
@BaseUnitTest
public class NoCdiAvailableTest {
@ExtendWith(ClassLoadingIsolaterExtension.class)
public class NoCdiAvailableTest implements ClassLoadingIsolaterExtension.IsolatedClassLoaderProvider {
public static final String[] EXCLUDED_PACKAGES = new String[] {
"javax.enterprise.inject.",
"javax.enterprise.context."
};
@Override
public ClassLoader buildIsolatedClassLoader() {
return new ClassLoader( NoCdiAvailableTest.class.getClassLoader() ) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
for ( String excludedPackage : EXCLUDED_PACKAGES ) {
if ( name.startsWith( excludedPackage ) ) {
throw new CdiClassLoadException( "CDI classes [" + name + "] excluded from load" );
}
}
return super.loadClass( name );
}
};
}
@Override
public void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader) {
// nothing to do
}
private static class CdiClassLoadException extends RuntimeException {
private CdiClassLoadException(String message) {
super( message );
}
}
@Rule public ClassLoadingIsolater isolater = new ClassLoadingIsolater(
new ClassLoadingIsolater.IsolatedClassLoaderProvider() {
@Override
public ClassLoader buildIsolatedClassLoader() {
return new ClassLoader( NoCdiAvailableTest.class.getClassLoader() ) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
for ( String excludedPackage : EXCLUDED_PACKAGES ) {
if ( name.startsWith( excludedPackage ) ) {
throw new CdiClassLoadException( "CDI classes [" + name + "] excluded from load" );
}
}
return super.loadClass( name );
}
};
}
@Override
public void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader) {
// nothing to do
}
}
);
@Test
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
Class delegateClass = Thread.currentThread().getContextClassLoader().loadClass(
@ -71,7 +66,7 @@ public class NoCdiAvailableTest {
entityManagerFactory = (EntityManagerFactory) mainMethod.invoke( null );
}
finally {
if (entityManagerFactory != null ) {
if ( entityManagerFactory != null ) {
entityManagerFactory.close();
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.testing.orm.junit;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.jboss.logging.Logger;
public class ClassLoadingIsolaterExtension implements AfterEachCallback, BeforeEachCallback {
private static final Logger log = Logger.getLogger( ClassLoadingIsolaterExtension.class );
private IsolatedClassLoaderProvider provider;
public interface IsolatedClassLoaderProvider {
ClassLoader buildIsolatedClassLoader();
void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader);
}
private ClassLoader isolatedClassLoader;
private ClassLoader originalTCCL;
@Override
public void afterEach(ExtensionContext context) throws Exception {
assert Thread.currentThread().getContextClassLoader() == isolatedClassLoader;
log.infof( "Reverting TCCL [%s] -> [%s]", isolatedClassLoader, originalTCCL );
Thread.currentThread().setContextClassLoader( originalTCCL );
provider.releaseIsolatedClassLoader( isolatedClassLoader );
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Object testInstance = context.getTestInstance().get();
if ( !( testInstance instanceof IsolatedClassLoaderProvider ) ) {
throw new RuntimeException(
"Test @ExtendWith( ClassLoadingIsolaterExtension.class ) have to implement ClassLoadingIsolaterExtension.IsolatedClassLoaderProvider" );
}
provider = (IsolatedClassLoaderProvider) testInstance;
originalTCCL = Thread.currentThread().getContextClassLoader();
isolatedClassLoader = provider.buildIsolatedClassLoader();
log.infof( "Overriding TCCL [%s] -> [%s]", originalTCCL, isolatedClassLoader );
Thread.currentThread().setContextClassLoader( isolatedClassLoader );
}
}