Skip to content

Commit 658775b

Browse files
committed
Avoid unnecessary list creation & processing in AbstractTestContextBootstrapper
Closes gh-35995
1 parent ea7a1d7 commit 658775b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ public final List<TestExecutionListener> getTestExecutionListeners() {
152152
listeners.addAll(getDefaultTestExecutionListeners());
153153
}
154154

155-
listeners.addAll(0, instantiateListeners(testExecutionListeners.listeners()));
155+
if (testExecutionListeners.listeners().length > 0) {
156+
listeners.addAll(0, instantiateListeners(testExecutionListeners.listeners()));
157+
}
156158

157159
descriptor = (inheritListeners ? parentDescriptor : null);
158160
}
@@ -316,17 +318,25 @@ private MergedContextConfiguration buildMergedContextConfiguration(Class<?> test
316318
}
317319
if (contextLoader instanceof SmartContextLoader smartContextLoader) {
318320
smartContextLoader.processContextConfiguration(configAttributes);
319-
locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
320-
classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
321+
if (configAttributes.getLocations().length > 0) {
322+
locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
323+
}
324+
if (configAttributes.getClasses().length > 0) {
325+
classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
326+
}
321327
}
322328
else {
323329
@SuppressWarnings("deprecation")
324330
String[] processedLocations = contextLoader.processLocations(
325331
configAttributes.getDeclaringClass(), configAttributes.getLocations());
326-
locations.addAll(0, Arrays.asList(processedLocations));
332+
if (processedLocations.length > 0) {
333+
locations.addAll(0, Arrays.asList(processedLocations));
334+
}
327335
// Legacy ContextLoaders don't know how to process classes
328336
}
329-
initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
337+
if (configAttributes.getInitializers().length > 0) {
338+
initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
339+
}
330340
if (!configAttributes.isInheritLocations()) {
331341
break;
332342
}
@@ -401,7 +411,9 @@ private List<ContextCustomizerFactory> getContextCustomizerFactories(Class<?> te
401411

402412
boolean inheritFactories = annotation.inheritFactories();
403413
AnnotationDescriptor<ContextCustomizerFactories> parentDescriptor = descriptor.next();
404-
factories.addAll(0, instantiateCustomizerFactories(annotation.factories()));
414+
if (annotation.factories().length > 0) {
415+
factories.addAll(0, instantiateCustomizerFactories(annotation.factories()));
416+
}
405417

406418
// If there are no factories to inherit, we might need to merge the
407419
// locally declared factories with the defaults.

0 commit comments

Comments
 (0)