From 01bb806672c4f9c844c51d1645e7ac78650ee8e4 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 23 Nov 2023 10:40:01 +0800 Subject: [PATCH 1/2] Treat null as CloudPlatform.NONE See gh-38510 --- .../boot/context/config/ConfigDataProperties.java | 7 ++++--- .../boot/context/config/ConfigDataPropertiesTests.java | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java index 24f45d58f7..6edecf4154 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java @@ -32,6 +32,7 @@ import org.springframework.util.ObjectUtils; * * @author Phillip Webb * @author Madhura Bhave + * @author Yanming Zhou */ class ConfigDataProperties { @@ -118,14 +119,14 @@ class ConfigDataProperties { if (activationContext == null) { return false; } - boolean activate = true; - activate = activate && isActive(activationContext.getCloudPlatform()); + boolean activate = isActive(activationContext.getCloudPlatform()); activate = activate && isActive(activationContext.getProfiles()); return activate; } private boolean isActive(CloudPlatform cloudPlatform) { - return this.onCloudPlatform == null || this.onCloudPlatform == cloudPlatform; + return this.onCloudPlatform == null || this.onCloudPlatform == CloudPlatform.NONE && cloudPlatform == null + || this.onCloudPlatform == cloudPlatform; } private boolean isActive(Profiles profiles) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java index e4027f513a..3b45e84f7e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java @@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Madhura Bhave + * @author Yanming Zhou */ class ConfigDataPropertiesTests { @@ -98,6 +99,13 @@ class ConfigDataPropertiesTests { assertThat(properties.isActive(context)).isFalse(); } + @Test + void isActiveWhenSpecificNoneCloudPlatformAgainstNullCloudPlatform() { + ConfigDataProperties properties = new ConfigDataProperties(NO_IMPORTS, new Activate(CloudPlatform.NONE, null)); + ConfigDataActivationContext context = new ConfigDataActivationContext(NULL_CLOUD_PLATFORM, NULL_PROFILES); + assertThat(properties.isActive(context)).isTrue(); + } + @Test void isActiveWhenNullProfilesAgainstNullProfiles() { ConfigDataProperties properties = new ConfigDataProperties(NO_IMPORTS, new Activate(null, null)); From c3a5e7695a75e0ac8b0ad2a933c170642f2a09a5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 5 Jan 2024 16:23:18 +0000 Subject: [PATCH 2/2] Polish "Treat null as CloudPlatform.NONE" See gh-38510 --- .../boot/context/config/ConfigDataProperties.java | 8 ++++---- .../boot/context/config/ConfigDataPropertiesTests.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java index 6edecf4154..cdf18ae00a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -119,14 +119,14 @@ class ConfigDataProperties { if (activationContext == null) { return false; } - boolean activate = isActive(activationContext.getCloudPlatform()); + CloudPlatform cloudPlatform = activationContext.getCloudPlatform(); + boolean activate = isActive((cloudPlatform != null) ? cloudPlatform : CloudPlatform.NONE); activate = activate && isActive(activationContext.getProfiles()); return activate; } private boolean isActive(CloudPlatform cloudPlatform) { - return this.onCloudPlatform == null || this.onCloudPlatform == CloudPlatform.NONE && cloudPlatform == null - || this.onCloudPlatform == cloudPlatform; + return this.onCloudPlatform == null || this.onCloudPlatform == cloudPlatform; } private boolean isActive(Profiles profiles) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java index 3b45e84f7e..b3635903f2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -100,7 +100,7 @@ class ConfigDataPropertiesTests { } @Test - void isActiveWhenSpecificNoneCloudPlatformAgainstNullCloudPlatform() { + void isActiveWhenNoneCloudPlatformAgainstNullCloudPlatform() { ConfigDataProperties properties = new ConfigDataProperties(NO_IMPORTS, new Activate(CloudPlatform.NONE, null)); ConfigDataActivationContext context = new ConfigDataActivationContext(NULL_CLOUD_PLATFORM, NULL_PROFILES); assertThat(properties.isActive(context)).isTrue();