Commit 61ff3c98 authored by Phillip Webb's avatar Phillip Webb

Polish 'Don't detect CloudPlatform when property is set'

See gh-25455
parent 70f44d3a
...@@ -140,7 +140,8 @@ public enum CloudPlatform { ...@@ -140,7 +140,8 @@ public enum CloudPlatform {
* @return if the platform is active. * @return if the platform is active.
*/ */
public boolean isActive(Environment environment) { public boolean isActive(Environment environment) {
return isEnforced(environment) || (isAutoDetectionEnabled(environment) && isDetected(environment)); String platformProperty = environment.getProperty(PROPERTY_NAME);
return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment));
} }
/** /**
...@@ -151,7 +152,10 @@ public enum CloudPlatform { ...@@ -151,7 +152,10 @@ public enum CloudPlatform {
* @since 2.3.0 * @since 2.3.0
*/ */
public boolean isEnforced(Environment environment) { public boolean isEnforced(Environment environment) {
String platform = environment.getProperty(PROPERTY_NAME); return isEnforced(environment.getProperty(PROPERTY_NAME));
}
private boolean isEnforced(String platform) {
return name().equalsIgnoreCase(platform); return name().equalsIgnoreCase(platform);
} }
...@@ -164,16 +168,6 @@ public enum CloudPlatform { ...@@ -164,16 +168,6 @@ public enum CloudPlatform {
*/ */
public abstract boolean isDetected(Environment environment); public abstract boolean isDetected(Environment environment);
/**
* Determines if it is enabled that the platform is detected by looking for
* platform-specific environment variables.
* @param environment the environment
* @return if the platform auto-detection is enabled.
*/
private boolean isAutoDetectionEnabled(Environment environment) {
return environment.getProperty(PROPERTY_NAME) == null;
}
/** /**
* Returns if the platform is behind a load balancer and uses * Returns if the platform is behind a load balancer and uses
* {@literal X-Forwarded-For} headers. * {@literal X-Forwarded-For} headers.
......
...@@ -18,9 +18,7 @@ package org.springframework.boot.cloud; ...@@ -18,9 +18,7 @@ package org.springframework.boot.cloud;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -148,9 +146,8 @@ class CloudPlatformTests { ...@@ -148,9 +146,8 @@ class CloudPlatformTests {
envVars.put("EXAMPLE_SERVICE_PORT", "8080"); envVars.put("EXAMPLE_SERVICE_PORT", "8080");
Environment environment = getEnvironmentWithEnvVariables(envVars); Environment environment = getEnvironmentWithEnvVariables(envVars);
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none"); ((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
List<CloudPlatform> activeCloudPlatforms = Stream.of(CloudPlatform.values()) assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment)))
.filter((cloudPlatform) -> cloudPlatform.isActive(environment)).collect(Collectors.toList()); .containsExactly(CloudPlatform.NONE);
assertThat(activeCloudPlatforms).containsExactly(CloudPlatform.NONE);
} }
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) { private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment