Skip to content

Commit 930c637

Browse files
committed
Merge pull request #25455 from nguyensach
* pr/25455: Polish 'Don't detect CloudPlatform when property is set' Don't detect CloudPlatform when property is set Closes gh-25455
2 parents fcb2210 + 61ff3c9 commit 930c637

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Phillip Webb
3030
* @author Brian Clozel
31+
* @author Nguyen Sach
3132
* @since 1.3.0
3233
*/
3334
public enum CloudPlatform {
@@ -131,13 +132,16 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
131132

132133
};
133134

135+
private static final String PROPERTY_NAME = "spring.main.cloud-platform";
136+
134137
/**
135138
* Determines if the platform is active (i.e. the application is running in it).
136139
* @param environment the environment
137140
* @return if the platform is active.
138141
*/
139142
public boolean isActive(Environment environment) {
140-
return isEnforced(environment) || isDetected(environment);
143+
String platformProperty = environment.getProperty(PROPERTY_NAME);
144+
return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment));
141145
}
142146

143147
/**
@@ -148,7 +152,10 @@ public boolean isActive(Environment environment) {
148152
* @since 2.3.0
149153
*/
150154
public boolean isEnforced(Environment environment) {
151-
String platform = environment.getProperty("spring.main.cloud-platform");
155+
return isEnforced(environment.getProperty(PROPERTY_NAME));
156+
}
157+
158+
private boolean isEnforced(String platform) {
152159
return name().equalsIgnoreCase(platform);
153160
}
154161

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.HashMap;
2121
import java.util.Map;
22+
import java.util.stream.Stream;
2223

2324
import org.junit.jupiter.api.Test;
2425

@@ -34,6 +35,7 @@
3435
* Tests for {@link CloudPlatform}.
3536
*
3637
* @author Phillip Webb
38+
* @author Nguyen Sach
3739
*/
3840
class CloudPlatformTests {
3941

@@ -137,6 +139,17 @@ void getActiveWhenHasEnforcedCloudPlatform() {
137139
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
138140
}
139141

142+
@Test
143+
void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
144+
Map<String, Object> envVars = new HashMap<>();
145+
envVars.put("EXAMPLE_SERVICE_HOST", "---");
146+
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
147+
Environment environment = getEnvironmentWithEnvVariables(envVars);
148+
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
149+
assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment)))
150+
.containsExactly(CloudPlatform.NONE);
151+
}
152+
140153
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
141154
MockEnvironment environment = new MockEnvironment();
142155
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(

0 commit comments

Comments
 (0)