Skip to content

Commit

Permalink
Merge branch '2.2.x' into 2.3.x
Browse files Browse the repository at this point in the history
Closes gh-21660
  • Loading branch information
philwebb committed Jun 3, 2020
2 parents 2dc8048 + 4fc0dec commit ebb3f5f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Random;

import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.PropertySourceOrigin;
import org.springframework.core.env.EnumerablePropertySource;
Expand Down Expand Up @@ -51,8 +52,6 @@
*/
class SpringConfigurationPropertySource implements ConfigurationPropertySource {

private static final ConfigurationPropertyName RANDOM = ConfigurationPropertyName.of("random");

private static final PropertyMapper[] DEFAULT_MAPPERS = { DefaultPropertyMapper.INSTANCE };

private static final PropertyMapper[] SYSTEM_ENVIRONMENT_MAPPERS = { SystemEnvironmentPropertyMapper.INSTANCE,
Expand Down Expand Up @@ -97,14 +96,21 @@ public ConfigurationProperty getConfigurationProperty(ConfigurationPropertyName

@Override
public ConfigurationPropertyState containsDescendantOf(ConfigurationPropertyName name) {
if (getPropertySource().getSource() instanceof Random) {
return containsDescendantOfForRandom(name);
PropertySource<?> source = getPropertySource();
if (source.getSource() instanceof Random) {
return containsDescendantOfForRandom("random", name);
}
if (source.getSource() instanceof PropertySource<?>
&& ((PropertySource<?>) source.getSource()).getSource() instanceof Random) {
// Assume wrapped random sources use the source name as the prefix
return containsDescendantOfForRandom(source.getName(), name);
}
return ConfigurationPropertyState.UNKNOWN;
}

private static ConfigurationPropertyState containsDescendantOfForRandom(ConfigurationPropertyName name) {
if (RANDOM.isAncestorOf(name) || name.equals(RANDOM)) {
private static ConfigurationPropertyState containsDescendantOfForRandom(String prefix,
ConfigurationPropertyName name) {
if (name.getNumberOfElements() > 1 && name.getElement(0, Form.DASHED).equals(prefix)) {
return ConfigurationPropertyState.PRESENT;
}
return ConfigurationPropertyState.ABSENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public class RandomValuePropertySource extends PropertySource<Random> {

private static final Log logger = LogFactory.getLog(RandomValuePropertySource.class);

public RandomValuePropertySource(String name) {
super(name, new Random());
}

public RandomValuePropertySource() {
this(RANDOM_PROPERTY_SOURCE_NAME);
}

public RandomValuePropertySource(String name) {
super(name, new Random());
}

@Override
public Object getProperty(String name) {
if (!name.startsWith(PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,105 @@ void getWhenEnumerableShouldBeIterable() {
void containsDescendantOfWhenRandomSourceAndRandomPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomValuePropertySource());
assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("random")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
ConfigurationPropertyName name = ConfigurationPropertyName.of("random");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

@Test
void containsDescendantOfWhenRandomSourceAndRandomPrefixedPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomValuePropertySource());
assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("random.something")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
ConfigurationPropertyName name = ConfigurationPropertyName.of("random.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(source.getConfigurationProperty(name)).isNotNull();
}

@Test
void containsDescendantOfWhenRandomSourceWithDifferentNameAndRandomPrefixedPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomValuePropertySource("different"));
ConfigurationPropertyName name = ConfigurationPropertyName.of("random.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(source.getConfigurationProperty(name)).isNotNull();
}

@Test
void containsDescendantOfWhenRandomSourceAndNonRandomPropertyReturnsAbsent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomValuePropertySource());
assertThat(source.containsDescendantOf(ConfigurationPropertyName.of("abandon.something")))
.isEqualTo(ConfigurationPropertyState.ABSENT);
ConfigurationPropertyName name = ConfigurationPropertyName.of("abandon.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

@Test
void containsDescendantOfWhenWrappedRandomSourceAndRandomPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomWrapperPropertySource());
ConfigurationPropertyName name = ConfigurationPropertyName.of("cachedrandom");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

@Test
void containsDescendantOfWhenWrappedRandomSourceAndRandomPrefixedPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomWrapperPropertySource());
ConfigurationPropertyName name = ConfigurationPropertyName.of("cachedrandom.something.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

@Test
void containsDescendantOfWhenWrappedRandomSourceWithMatchingNameAndRandomPrefixedPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomWrapperPropertySource("cachedrandom"));
ConfigurationPropertyName name = ConfigurationPropertyName.of("cachedrandom.something.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.PRESENT);
assertThat(source.getConfigurationProperty(name)).isNotNull();
}

@Test
void containsDescendantOfWhenWrappedRandomSourceAndRandomDashPrefixedPropertyReturnsPresent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomWrapperPropertySource());
ConfigurationPropertyName name = ConfigurationPropertyName.of("cached-random.something.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

@Test
void containsDescendantOfWhenWrappedRandomSourceAndNonRandomPropertyReturnsAbsent() {
SpringConfigurationPropertySource source = SpringConfigurationPropertySource
.from(new RandomWrapperPropertySource());
ConfigurationPropertyName name = ConfigurationPropertyName.of("abandon.something.int");
assertThat(source.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
assertThat(source.getConfigurationProperty(name)).isNull();
}

static class RandomWrapperPropertySource extends PropertySource<RandomValuePropertySource> {

private final String prefix;

RandomWrapperPropertySource() {
this("cachedRandom");
}

RandomWrapperPropertySource(String name) {
super(name, new RandomValuePropertySource());
this.prefix = name + ".";
}

@Override
public Object getProperty(String name) {
name = name.toLowerCase();
if (!name.startsWith(this.prefix)) {
return null;
}
return getSource().getProperty("random." + name.substring(this.prefix.length()));
}

}

/**
Expand Down

0 comments on commit ebb3f5f

Please sign in to comment.