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,22 +21,13 @@ 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."
|
||||||
};
|
};
|
||||||
|
|
||||||
private static class CdiClassLoadException extends RuntimeException {
|
|
||||||
private CdiClassLoadException(String message) {
|
|
||||||
super( message );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Rule public ClassLoadingIsolater isolater = new ClassLoadingIsolater(
|
|
||||||
new ClassLoadingIsolater.IsolatedClassLoaderProvider() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClassLoader buildIsolatedClassLoader() {
|
public ClassLoader buildIsolatedClassLoader() {
|
||||||
return new ClassLoader( NoCdiAvailableTest.class.getClassLoader() ) {
|
return new ClassLoader( NoCdiAvailableTest.class.getClassLoader() ) {
|
||||||
|
@ -56,9 +46,14 @@ public class NoCdiAvailableTest {
|
||||||
@Override
|
@Override
|
||||||
public void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader) {
|
public void releaseIsolatedClassLoader(ClassLoader isolatedClassLoader) {
|
||||||
// nothing to do
|
// nothing to do
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CdiClassLoadException extends RuntimeException {
|
||||||
|
private CdiClassLoadException(String message) {
|
||||||
|
super( message );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
|
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
|
||||||
|
|
|
@ -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