From 5603d61909637d3fd93307ce50e826eb4f8a3873 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 26 Oct 2018 15:30:28 -0700 Subject: [PATCH] Polish "Consider aliases when checking descendants" See gh-14967 --- .../AliasedConfigurationPropertySource.java | 25 +++++++++--------- .../ConfigurationPropertyNameAliases.java | 10 ++++--- ...iasedConfigurationPropertySourceTests.java | 26 +++++-------------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySource.java index 6b2eefa708..42beffcb55 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySource.java @@ -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 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; + } } } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameAliases.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameAliases.java index 36b33f74e4..3ca9f6beb6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameAliases.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameAliases.java @@ -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 { private final MultiValueMap aliases = new LinkedMultiValueMap<>(); @@ -75,8 +76,9 @@ public final class ConfigurationPropertyNameAliases { .findFirst().orElse(null); } - public Set getAllNames() { - return this.aliases.keySet(); + @Override + public Iterator iterator() { + return this.aliases.keySet().iterator(); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java index fd1a7af93e..8d4d15a527 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java @@ -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); }