GH-453 - Guard against AutoConfiguration- and EntityScanPackages bean definitions not having any constructor argument defined.

This commit is contained in:
Oliver Drotbohm
2024-01-16 18:09:34 +01:00
parent 6afc00c554
commit f98162ae87
2 changed files with 54 additions and 6 deletions

View File

@@ -43,8 +43,8 @@ import org.springframework.util.StringUtils;
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
class ModuleTestAutoConfiguration {
private static final String AUTOCONFIG_PACKAGES = "org.springframework.boot.autoconfigure.AutoConfigurationPackages";
private static final String ENTITY_SCAN_PACKAGE = "org.springframework.boot.autoconfigure.domain.EntityScanPackages";
static final String AUTOCONFIG_PACKAGES = "org.springframework.boot.autoconfigure.AutoConfigurationPackages";
static final String ENTITY_SCAN_PACKAGE = "org.springframework.boot.autoconfigure.domain.EntityScanPackages";
static class AutoConfigurationAndEntityScanPackageCustomizer implements ImportBeanDefinitionRegistrar {
@@ -67,7 +67,7 @@ class ModuleTestAutoConfiguration {
setBasePackagesOn(registry, ENTITY_SCAN_PACKAGE, basePackages);
}
private void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName, List<String> packages) {
void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName, List<String> packages) {
if (!registry.containsBeanDefinition(beanName)) {
return;
@@ -77,9 +77,11 @@ class ModuleTestAutoConfiguration {
var definition = registry.getBeanDefinition(beanName);
var holder = definition.getConstructorArgumentValues().getArgumentValue(0, String[].class);
Arrays.stream((String[]) holder.getValue())
.filter(it -> it.startsWith("org.springframework.modulith"))
.forEach(packagesToSet::add);
if (holder != null) {
Arrays.stream((String[]) holder.getValue())
.filter(it -> it.startsWith("org.springframework.modulith"))
.forEach(packagesToSet::add);
}
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, packagesToSet.toArray(String[]::new));
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.test;
import static org.assertj.core.api.Assertions.*;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
/**
* Unit tests for {@link ModuleTestAutoConfiguration}.
*
* @author Oliver Drotbohm
*/
class ModuleTestAutoConfigurationUnitTests {
@Test // GH-453
void handlesNoConstructorArgumentOnPackagesBeanDefinition() {
var definition = new RootBeanDefinition(AutoConfigurationPackages.class);
var factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition(ModuleTestAutoConfiguration.AUTOCONFIG_PACKAGES, definition);
assertThatNoException().isThrownBy(() -> {
new ModuleTestAutoConfiguration.AutoConfigurationAndEntityScanPackageCustomizer()
.setBasePackagesOn(factory, ModuleTestAutoConfiguration.AUTOCONFIG_PACKAGES, Collections.emptyList());
});
}
}