Polish BootstrapUtils

This commit is contained in:
Sam Brannen
2022-09-13 11:28:57 +02:00
parent 1e2541a0a3
commit a3b6b41bd7

View File

@@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.util.ClassUtils;
@@ -74,36 +75,33 @@ public abstract class BootstrapUtils {
@SuppressWarnings("unchecked")
static BootstrapContext createBootstrapContext(Class<?> testClass) {
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate();
String className = DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME;
Class<? extends BootstrapContext> clazz = null;
try {
clazz = (Class<? extends BootstrapContext>) ClassUtils.forName(
DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME, BootstrapUtils.class.getClassLoader());
Constructor<? extends BootstrapContext> constructor = clazz.getConstructor(
Class.class, CacheAwareContextLoaderDelegate.class);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating BootstrapContext using constructor [%s]", constructor));
}
clazz = (Class<? extends BootstrapContext>)
ClassUtils.forName(className, BootstrapUtils.class.getClassLoader());
Constructor<? extends BootstrapContext> constructor =
clazz.getConstructor(Class.class, CacheAwareContextLoaderDelegate.class);
logger.debug(LogMessage.format("Instantiating BootstrapContext using constructor [%s]", constructor));
return BeanUtils.instantiateClass(constructor, testClass, cacheAwareContextLoaderDelegate);
}
catch (Throwable ex) {
throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", ex);
throw new IllegalStateException("Could not load BootstrapContext [%s]".formatted(className), ex);
}
}
@SuppressWarnings("unchecked")
private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDelegate() {
String className = DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME;
Class<? extends CacheAwareContextLoaderDelegate> clazz = null;
try {
clazz = (Class<? extends CacheAwareContextLoaderDelegate>) ClassUtils.forName(
DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME, BootstrapUtils.class.getClassLoader());
if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]",
clazz.getName()));
}
clazz = (Class<? extends CacheAwareContextLoaderDelegate>)
ClassUtils.forName(className, BootstrapUtils.class.getClassLoader());
logger.debug(LogMessage.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]", className));
return BeanUtils.instantiateClass(clazz, CacheAwareContextLoaderDelegate.class);
}
catch (Throwable ex) {
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", ex);
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [%s]".formatted(className), ex);
}
}
@@ -153,10 +151,8 @@ public abstract class BootstrapUtils {
if (clazz == null) {
clazz = resolveDefaultTestContextBootstrapper(testClass);
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
testClass.getName(), clazz.getName()));
}
logger.debug(LogMessage.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
testClass.getName(), clazz.getName()));
TestContextBootstrapper testContextBootstrapper =
BeanUtils.instantiateClass(clazz, TestContextBootstrapper.class);
testContextBootstrapper.setBootstrapContext(bootstrapContext);
@@ -166,9 +162,9 @@ public abstract class BootstrapUtils {
throw ex;
}
catch (Throwable ex) {
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz +
"]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.",
ex);
throw new IllegalStateException("""
Could not load TestContextBootstrapper [%s]. Specify @BootstrapWith's 'value' \
attribute or make the default bootstrapper class available.""".formatted(clazz), ex);
}
}