Polishing

This commit is contained in:
Sam Brannen 2023-09-03 16:50:14 +02:00
parent 759e3d4aa6
commit 7882d265c6
4 changed files with 31 additions and 10 deletions

View File

@ -674,9 +674,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
private void registerRuntimeHints(RuntimeHints hints) {
for (PropertySourceDescriptor descriptor : this.descriptors) {
Class<?> factory = descriptor.propertySourceFactory();
if (factory != null) {
hints.reflection().registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
Class<?> factoryClass = descriptor.propertySourceFactory();
if (factoryClass != null) {
hints.reflection().registerType(factoryClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
for (String location : descriptor.locations()) {
Resource resource = this.resourceResolver.apply(location);

View File

@ -18,9 +18,14 @@ package org.springframework.test.context.aot;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;
import org.springframework.aot.hint.ResourceHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertySourceDescriptor;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.ClassUtils;
@ -68,8 +73,10 @@ class MergedContextConfigurationRuntimeHints {
// @ContextConfiguration(locations = ...)
registerClasspathResources(mergedConfig.getLocations(), runtimeHints, classLoader);
// @TestPropertySource(locations = ... )
registerClasspathResources(mergedConfig.getPropertySourceLocations(), runtimeHints, classLoader);
for (PropertySourceDescriptor descriptor : mergedConfig.getPropertySourceDescriptors()) {
// @TestPropertySource(locations = ...)
registerClasspathResources(descriptor.locations().stream(), runtimeHints, classLoader);
}
// @WebAppConfiguration(value = ...)
if (webMergedContextConfigurationClass.isInstance(mergedConfig)) {
@ -89,12 +96,17 @@ class MergedContextConfigurationRuntimeHints {
runtimeHints.reflection().registerType(type, INVOKE_DECLARED_CONSTRUCTORS);
}
private void registerClasspathResources(String[] paths, RuntimeHints runtimeHints, ClassLoader classLoader) {
private void registerClasspathResources(String[] locations, RuntimeHints runtimeHints, ClassLoader classLoader) {
registerClasspathResources(Arrays.stream(locations), runtimeHints, classLoader);
}
private void registerClasspathResources(Stream<String> locations, RuntimeHints runtimeHints, ClassLoader classLoader) {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
Arrays.stream(paths)
.filter(path -> path.startsWith(CLASSPATH_URL_PREFIX))
.map(resourceLoader::getResource)
.forEach(runtimeHints.resources()::registerResource);
ResourceHints resourceHints = runtimeHints.resources();
locations.map(resourceLoader::getResource)
.filter(ClassPathResource.class::isInstance)
.filter(Resource::exists)
.forEach(resourceHints::registerResource);
}
private void registerClasspathResourceDirectoryStructure(String directory, RuntimeHints runtimeHints) {

View File

@ -29,6 +29,9 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* Uses configuration identical to {@link BasicSpringJupiterTests}.
*
* <p>NOTE: if you modify the configuration for this test class, you must also
* modify the configuration for {@link BasicSpringJupiterTests} accordingly.
*
* @author Sam Brannen
* @since 6.0
*/

View File

@ -34,6 +34,12 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;
/**
* Uses configuration identical to {@link BasicSpringJupiterSharedConfigTests}.
*
* <p>NOTE: if you modify the configuration for this test class, you must also
* modify the configuration for {@link BasicSpringJupiterSharedConfigTests}
* accordingly.
*
* @author Sam Brannen
* @since 6.0
*/