Skip to content

Commit a3b6b41

Browse files
committed
Polish BootstrapUtils
1 parent 1e2541a commit a3b6b41

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

spring-test/src/main/java/org/springframework/test/context/BootstrapUtils.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.logging.LogFactory;
2626

2727
import org.springframework.beans.BeanUtils;
28+
import org.springframework.core.log.LogMessage;
2829
import org.springframework.lang.Nullable;
2930
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
3031
import org.springframework.util.ClassUtils;
@@ -74,36 +75,33 @@ public abstract class BootstrapUtils {
7475
@SuppressWarnings("unchecked")
7576
static BootstrapContext createBootstrapContext(Class<?> testClass) {
7677
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate();
78+
String className = DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME;
7779
Class<? extends BootstrapContext> clazz = null;
7880
try {
79-
clazz = (Class<? extends BootstrapContext>) ClassUtils.forName(
80-
DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME, BootstrapUtils.class.getClassLoader());
81-
Constructor<? extends BootstrapContext> constructor = clazz.getConstructor(
82-
Class.class, CacheAwareContextLoaderDelegate.class);
83-
if (logger.isDebugEnabled()) {
84-
logger.debug(String.format("Instantiating BootstrapContext using constructor [%s]", constructor));
85-
}
81+
clazz = (Class<? extends BootstrapContext>)
82+
ClassUtils.forName(className, BootstrapUtils.class.getClassLoader());
83+
Constructor<? extends BootstrapContext> constructor =
84+
clazz.getConstructor(Class.class, CacheAwareContextLoaderDelegate.class);
85+
logger.debug(LogMessage.format("Instantiating BootstrapContext using constructor [%s]", constructor));
8686
return BeanUtils.instantiateClass(constructor, testClass, cacheAwareContextLoaderDelegate);
8787
}
8888
catch (Throwable ex) {
89-
throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", ex);
89+
throw new IllegalStateException("Could not load BootstrapContext [%s]".formatted(className), ex);
9090
}
9191
}
9292

9393
@SuppressWarnings("unchecked")
9494
private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDelegate() {
95+
String className = DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME;
9596
Class<? extends CacheAwareContextLoaderDelegate> clazz = null;
9697
try {
97-
clazz = (Class<? extends CacheAwareContextLoaderDelegate>) ClassUtils.forName(
98-
DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME, BootstrapUtils.class.getClassLoader());
99-
if (logger.isDebugEnabled()) {
100-
logger.debug(String.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]",
101-
clazz.getName()));
102-
}
98+
clazz = (Class<? extends CacheAwareContextLoaderDelegate>)
99+
ClassUtils.forName(className, BootstrapUtils.class.getClassLoader());
100+
logger.debug(LogMessage.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]", className));
103101
return BeanUtils.instantiateClass(clazz, CacheAwareContextLoaderDelegate.class);
104102
}
105103
catch (Throwable ex) {
106-
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", ex);
104+
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [%s]".formatted(className), ex);
107105
}
108106
}
109107

@@ -153,10 +151,8 @@ static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext b
153151
if (clazz == null) {
154152
clazz = resolveDefaultTestContextBootstrapper(testClass);
155153
}
156-
if (logger.isDebugEnabled()) {
157-
logger.debug(String.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
158-
testClass.getName(), clazz.getName()));
159-
}
154+
logger.debug(LogMessage.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
155+
testClass.getName(), clazz.getName()));
160156
TestContextBootstrapper testContextBootstrapper =
161157
BeanUtils.instantiateClass(clazz, TestContextBootstrapper.class);
162158
testContextBootstrapper.setBootstrapContext(bootstrapContext);
@@ -166,9 +162,9 @@ static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext b
166162
throw ex;
167163
}
168164
catch (Throwable ex) {
169-
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz +
170-
"]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.",
171-
ex);
165+
throw new IllegalStateException("""
166+
Could not load TestContextBootstrapper [%s]. Specify @BootstrapWith's 'value' \
167+
attribute or make the default bootstrapper class available.""".formatted(clazz), ex);
172168
}
173169
}
174170

0 commit comments

Comments
 (0)