diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java index f74b4135f0..c4f61b2a87 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java @@ -16,6 +16,7 @@ package org.springframework.boot.cloud; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; @@ -131,6 +132,8 @@ public enum CloudPlatform { }; + private static final String PROPERTY_NAME = "spring.main.cloud-platform"; + /** * Determines if the platform is active (i.e. the application is running in it). * @param environment the environment @@ -148,7 +151,21 @@ public enum CloudPlatform { * @since 2.3.0 */ public boolean isEnforced(Environment environment) { - String platform = environment.getProperty("spring.main.cloud-platform"); + return isEnforced(environment.getProperty(PROPERTY_NAME)); + } + + /** + * Determines if the platform is enforced by looking at the + * {@code "spring.main.cloud-platform"} configuration property. + * @param binder the binder + * @return if the platform is enforced + * @since 2.4.0 + */ + public boolean isEnforced(Binder binder) { + return isEnforced(binder.bind(PROPERTY_NAME, String.class).orElse(null)); + } + + private boolean isEnforced(String platform) { return name().equalsIgnoreCase(platform); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java index c308c057d2..119fb5e238 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java @@ -22,6 +22,8 @@ import java.util.Map; import org.junit.jupiter.api.Test; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.source.MockConfigurationPropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; @@ -137,6 +139,44 @@ class CloudPlatformTests { assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES); } + @Test + void isEnforcedWhenEnvironmentPropertyMatchesReturnsTrue() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.main.cloud-platform", "kubernetes"); + assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isTrue(); + } + + @Test + void isEnforcedWhenEnvironmentPropertyDoesNotMatchReturnsFalse() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.main.cloud-platform", "heroku"); + assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isFalse(); + } + + @Test + void isEnforcedWhenEnvironmentPropertyIsMissingatchReturnsFalse() { + MockEnvironment environment = new MockEnvironment(); + assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isFalse(); + } + + @Test + void isEnforcedWhenBinderPropertyMatchesReturnsTrue() { + Binder binder = new Binder(new MockConfigurationPropertySource("spring.main.cloud-platform", "kubernetes")); + assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isTrue(); + } + + @Test + void isEnforcedWhenBinderPropertyDoesNotMatchReturnsFalse() { + Binder binder = new Binder(new MockConfigurationPropertySource("spring.main.cloud-platform", "heroku")); + assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse(); + } + + @Test + void isEnforcedWhenBinderPropertyIsMissingatchReturnsFalse() { + Binder binder = new Binder(new MockConfigurationPropertySource()); + assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse(); + } + private Environment getEnvironmentWithEnvVariables(Map environmentVariables) { MockEnvironment environment = new MockEnvironment(); PropertySource propertySource = new SystemEnvironmentPropertySource(