Created a ClassLoadingIsolaterExtension to remove the need of @Rule in tests using ClassLoadingIsolater
This commit is contained in:
parent
e0e44433a9
commit
24b79133f2
|
@ -10,10 +10,9 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
import org.hibernate.testing.junit4.ClassLoadingIsolater;
|
import org.hibernate.testing.orm.junit.ClassLoadingIsolaterExtension;
|
||||||
import org.hibernate.testing.orm.junit.BaseUnitTest;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
@ -22,44 +21,40 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
@BaseUnitTest
|
@ExtendWith(ClassLoadingIsolaterExtension.class)
|
||||||
public class NoCdiAvailableTest {
|
public class NoCdiAvailableTest implements ClassLoadingIsolaterExtension.IsolatedClassLoaderProvider {
|
||||||
public static final String[] EXCLUDED_PACKAGES = new String[] {
|
public static final String[] EXCLUDED_PACKAGES = new String[] {
|
||||||
"javax.enterprise.inject.",
|
"javax.enterprise.inject.",
|
||||||
"javax.enterprise.context."
|
"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 static class CdiClassLoadException extends RuntimeException {
|
||||||
private CdiClassLoadException(String message) {
|
private CdiClassLoadException(String message) {
|
||||||
super( 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
|
@Test
|
||||||
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
|
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
|
||||||
Class delegateClass = Thread.currentThread().getContextClassLoader().loadClass(
|
Class delegateClass = Thread.currentThread().getContextClassLoader().loadClass(
|
||||||
|
@ -71,7 +66,7 @@ public class NoCdiAvailableTest {
|
||||||
entityManagerFactory = (EntityManagerFactory) mainMethod.invoke( null );
|
entityManagerFactory = (EntityManagerFactory) mainMethod.invoke( null );
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (entityManagerFactory != null ) {
|
if ( entityManagerFactory != null ) {
|
||||||
entityManagerFactory.close();
|
entityManagerFactory.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 );
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue