Polish "Consider aliases when checking descendants"

See gh-14967
pull/16246/head
Phillip Webb 6 years ago
parent 256ca681fd
commit 5603d61909

@ -16,8 +16,6 @@
package org.springframework.boot.context.properties.source;
import java.util.Set;
import org.springframework.util.Assert;
/**
@ -60,16 +58,19 @@ class AliasedConfigurationPropertySource implements ConfigurationPropertySource
if (result != ConfigurationPropertyState.ABSENT) {
return result;
}
Set<ConfigurationPropertyName> aliasNames = this.aliases.getAllNames();
for (ConfigurationPropertyName configurationPropertyName : aliasNames) {
boolean descendantPresentInAlias = this.aliases
.getAliases(configurationPropertyName).stream()
.filter(name::isAncestorOf).findFirst().isPresent();
if (descendantPresentInAlias) {
ConfigurationProperty configurationProperty = this.getSource()
.getConfigurationProperty(configurationPropertyName);
if (configurationProperty != null) {
return ConfigurationPropertyState.PRESENT;
for (ConfigurationPropertyName alias : getAliases().getAliases(name)) {
ConfigurationPropertyState aliasResult = this.source
.containsDescendantOf(alias);
if (aliasResult != ConfigurationPropertyState.ABSENT) {
return aliasResult;
}
}
for (ConfigurationPropertyName from : getAliases()) {
for (ConfigurationPropertyName alias : getAliases().getAliases(from)) {
if (name.isAncestorOf(alias)) {
if (this.source.getConfigurationProperty(from) != null) {
return ConfigurationPropertyState.PRESENT;
}
}
}
}

@ -18,9 +18,9 @@ package org.springframework.boot.context.properties.source;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
@ -34,7 +34,8 @@ import org.springframework.util.MultiValueMap;
* @since 2.0.0
* @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases)
*/
public final class ConfigurationPropertyNameAliases {
public final class ConfigurationPropertyNameAliases
implements Iterable<ConfigurationPropertyName> {
private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>();
@ -75,8 +76,9 @@ public final class ConfigurationPropertyNameAliases {
.findFirst().orElse(null);
}
public Set<ConfigurationPropertyName> getAllNames() {
return this.aliases.keySet();
@Override
public Iterator<ConfigurationPropertyName> iterator() {
return this.aliases.keySet().iterator();
}
}

@ -16,11 +16,11 @@
package org.springframework.boot.context.properties.source;
import java.util.Collections;
import org.junit.Test;
import org.mockito.Answers;
import org.springframework.boot.origin.Origin;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -107,31 +107,19 @@ public class AliasedConfigurationPropertySourceTests {
.willReturn(ConfigurationPropertyState.ABSENT);
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
.willReturn(ConfigurationPropertyState.PRESENT);
ConfigurationPropertyName barBar = ConfigurationPropertyName.of("bar.bar");
given(source.getConfigurationProperty(barBar)).willReturn(
new ConfigurationProperty(barBar, "barBarValue", mock(Origin.class)));
ConfigurationPropertySource aliased = source
.withAliases(new ConfigurationPropertyNameAliases("bar.bar", "foo.foo"));
.withAliases(new ConfigurationPropertyNameAliases("foo", "bar"));
assertThat(aliased.containsDescendantOf(name))
.isEqualTo(ConfigurationPropertyState.PRESENT);
}
@Test
public void containsDescendantOfWhenPresentInAliasShouldReturnPresent() {
ConfigurationPropertyName name = ConfigurationPropertyName.of("baz");
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class,
withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));
given(source.containsDescendantOf(name))
.willReturn(ConfigurationPropertyState.ABSENT);
ConfigurationPropertyName barFoo = ConfigurationPropertyName.of("bar.foo");
given(source.getConfigurationProperty(barFoo)).willReturn(
new ConfigurationProperty(barFoo, "barFooValue", mock(Origin.class)));
ConfigurationPropertySource source = new MapConfigurationPropertySource(
Collections.singletonMap("foo.bar", "foobar"));
ConfigurationPropertySource aliased = source
.withAliases(new ConfigurationPropertyNameAliases("bar.foo", "baz.foo"));
assertThat(aliased.containsDescendantOf(name))
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "baz.foo"));
assertThat(aliased.containsDescendantOf(ConfigurationPropertyName.of("baz")))
.isEqualTo(ConfigurationPropertyState.PRESENT);
}

Loading…
Cancel
Save