Merge pull request #26205 from LichKing-lee
* gh-26205: Polish "Detect use of spring.profiles.include as a YAML list" Detect use of spring.profiles.include as a YAML list Closes gh-26205
This commit is contained in:
commit
e445179f49
|
|
@ -297,6 +297,8 @@ class ConfigDataEnvironment {
|
|||
binder.bind(Profiles.INCLUDE_PROFILES, STRING_LIST).ifBound((includes) -> {
|
||||
if (!contributor.isActive(activationContext)) {
|
||||
InactiveConfigDataAccessException.throwIfPropertyFound(contributor, Profiles.INCLUDE_PROFILES);
|
||||
InactiveConfigDataAccessException.throwIfPropertyFound(contributor,
|
||||
Profiles.INCLUDE_PROFILES.append("[0]"));
|
||||
}
|
||||
result.addAll(includes);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ import java.util.stream.Collectors;
|
|||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInfo;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import org.springframework.boot.ConfigurableBootstrapContext;
|
||||
import org.springframework.boot.DefaultBootstrapContext;
|
||||
|
|
@ -216,6 +218,53 @@ class ConfigDataEnvironmentTests {
|
|||
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({ "include", "include[0]" })
|
||||
void processAndApplyWhenHasProfileIncludeInProfileSpecificDocumentThrowsException(String property, TestInfo info) {
|
||||
this.environment.setProperty("spring.config.location", getConfigLocation(info));
|
||||
ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext,
|
||||
this.environment, this.resourceLoader, this.additionalProfiles, null) {
|
||||
|
||||
@Override
|
||||
protected ConfigDataEnvironmentContributors createContributors(
|
||||
List<ConfigDataEnvironmentContributor> contributors) {
|
||||
Map<String, Object> source = new LinkedHashMap<>();
|
||||
source.put("spring.config.activate.on-profile", "activate");
|
||||
source.put("spring.profiles." + property, "include");
|
||||
ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source)));
|
||||
contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"),
|
||||
mock(ConfigDataResource.class), false, data, 0));
|
||||
return super.createContributors(contributors);
|
||||
}
|
||||
|
||||
};
|
||||
assertThatExceptionOfType(InactiveConfigDataAccessException.class)
|
||||
.isThrownBy(configDataEnvironment::processAndApply);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({ "spring.profiles.include", "spring.profiles.include[0]" })
|
||||
void processAndApplyIncludesProfilesFromSpringProfilesInclude(String property, TestInfo info) {
|
||||
this.environment.setProperty("spring.config.location", getConfigLocation(info));
|
||||
ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext,
|
||||
this.environment, this.resourceLoader, this.additionalProfiles, null) {
|
||||
|
||||
@Override
|
||||
protected ConfigDataEnvironmentContributors createContributors(
|
||||
List<ConfigDataEnvironmentContributor> contributors) {
|
||||
Map<String, Object> source = new LinkedHashMap<>();
|
||||
source.put(property, "included");
|
||||
ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source)));
|
||||
contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"),
|
||||
mock(ConfigDataResource.class), false, data, 0));
|
||||
return super.createContributors(contributors);
|
||||
}
|
||||
|
||||
};
|
||||
configDataEnvironment.processAndApply();
|
||||
assertThat(this.environment.getActiveProfiles()).containsExactly("included");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Disabled until spring.profiles support is dropped")
|
||||
void processAndApplyWhenHasInvalidPropertyThrowsException() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue