Skip to content

Commit

Permalink
Merge branch '2.4.x'
Browse files Browse the repository at this point in the history
Closes gh-26401
  • Loading branch information
philwebb committed May 7, 2021
2 parents 2976fd9 + fef62f7 commit 777abc4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ public Options getOptions(PropertySource<?> propertySource) {
@FunctionalInterface
public interface PropertySourceOptions {

/**
* {@link PropertySourceOptions} instance that always returns
* {@link Options#NONE}.
* @since 2.4.6
*/
PropertySourceOptions ALWAYS_NONE = new AlwaysPropertySourceOptions(Options.NONE);

/**
* Return the options that should apply for the given property source.
* @param propertySource the property source
Expand All @@ -143,6 +150,9 @@ static PropertySourceOptions always(Option... options) {
* @return a new {@link PropertySourceOptions} instance
*/
static PropertySourceOptions always(Options options) {
if (options == Options.NONE) {
return ALWAYS_NONE;
}
return new AlwaysPropertySourceOptions(options);
}

Expand Down Expand Up @@ -176,7 +186,7 @@ public static final class Options {
/**
* No options.
*/
public static final Options NONE = Options.of();
public static final Options NONE = new Options(Collections.emptySet());

private final Set<Option> options;

Expand Down Expand Up @@ -253,8 +263,10 @@ private Options copy(Consumer<EnumSet<Option>> processor) {
*/
public static Options of(Option... options) {
Assert.notNull(options, "Options must not be null");
return new Options(
(options.length != 0) ? EnumSet.copyOf(Arrays.asList(options)) : EnumSet.noneOf(Option.class));
if (options.length == 0) {
return NONE;
}
return new Options(EnumSet.copyOf(Arrays.asList(options)));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ enum Kind {
BOUND_IMPORT,

/**
* A valid location that contained noething to load.
* A valid location that contained nothing to load.
*/
EMPTY_LOCATION;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.util.List;

import org.springframework.boot.context.config.ConfigData.Option;
import org.springframework.boot.context.config.ConfigData.PropertySourceOptions;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedResource;
import org.springframework.core.env.PropertySource;
Expand All @@ -33,6 +35,10 @@
*/
public class StandardConfigDataLoader implements ConfigDataLoader<StandardConfigDataResource> {

private static final PropertySourceOptions PROFILE_SPECIFIC = PropertySourceOptions.always(Option.PROFILE_SPECIFIC);

private static final PropertySourceOptions NON_PROFILE_SPECIFIC = PropertySourceOptions.ALWAYS_NONE;

@Override
public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResource resource)
throws IOException, ConfigDataNotFoundException {
Expand All @@ -46,7 +52,8 @@ public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResour
String name = String.format("Config resource '%s' via location '%s'", resource,
reference.getConfigDataLocation());
List<PropertySource<?>> propertySources = reference.getPropertySourceLoader().load(name, originTrackedResource);
return new ConfigData(propertySources);
PropertySourceOptions options = (resource.getProfile() != null) ? PROFILE_SPECIFIC : NON_PROFILE_SPECIFIC;
return new ConfigData(propertySources, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.apache.logging.log4j.util.Strings;
import org.assertj.core.api.Condition;
Expand Down Expand Up @@ -407,9 +405,7 @@ void runWhenHasActiveProfileConfigurationInMultiDocumentFileLoadsInExpectedOrder
String property = context.getEnvironment().getProperty("my.property");
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev");
assertThat(property).isEqualTo("fromdevprofile");
List<String> names = StreamSupport.stream(context.getEnvironment().getPropertySources().spliterator(), false)
.map(org.springframework.core.env.PropertySource::getName).collect(Collectors.toList());
assertThat(names).contains(
assertThat(context.getEnvironment().getPropertySources()).extracting("name").contains(
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #0)",
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #1)");
}
Expand Down Expand Up @@ -600,6 +596,14 @@ void runWhenImportFromEarlierDocumentUsesPlaceholder() {
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported");
}

@Test
void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-profile-variant.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
}

@Test
void runWhenHasPropertyInProfileDocumentThrowsException() {
assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.application.run(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my.value=notimported-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my.value=iwasimported-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my.value=iwasimported
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.config.import=classpath:application-import-with-profile-variant-imported.properties
my.value=notimported

0 comments on commit 777abc4

Please sign in to comment.