From 8d6b442dc4e873a1bd872ae0c66f5c4d3567e37b Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 4 May 2022 15:24:50 -0700 Subject: [PATCH] Remove support for spring.profiles Closes gh-22523 --- .../context/config/ConfigDataEnvironment.java | 2 +- .../context/config/ConfigDataProperties.java | 47 +------------------ .../InvalidConfigDataPropertyException.java | 26 +++++----- ...ironmentPostProcessorIntegrationTests.java | 5 +- .../config/ConfigDataPropertiesTests.java | 22 --------- ...validConfigDataPropertyExceptionTests.java | 36 ++------------ .../test/resources/invalidproperty.properties | 2 +- .../src/test/resources/testprofiles.yml | 6 +-- 8 files changed, 22 insertions(+), 124 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java index c72d9b05c4..a9d9f4112e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java @@ -357,7 +357,7 @@ class ConfigDataEnvironment { private void checkForInvalidProperties(ConfigDataEnvironmentContributors contributors) { for (ConfigDataEnvironmentContributor contributor : contributors) { - InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor); + InvalidConfigDataPropertyException.throwIfPropertyFound(contributor); } } 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 e9337c70f1..6d54d0fe44 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 @@ -21,12 +21,9 @@ import java.util.List; import java.util.function.Predicate; import org.springframework.boot.cloud.CloudPlatform; -import org.springframework.boot.context.properties.bind.BindContext; -import org.springframework.boot.context.properties.bind.BindHandler; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Name; -import org.springframework.boot.context.properties.source.ConfigurationProperty; import org.springframework.boot.context.properties.source.ConfigurationPropertyName; import org.springframework.util.ObjectUtils; @@ -40,13 +37,8 @@ class ConfigDataProperties { private static final ConfigurationPropertyName NAME = ConfigurationPropertyName.of("spring.config"); - private static final ConfigurationPropertyName LEGACY_PROFILES_NAME = ConfigurationPropertyName - .of("spring.profiles"); - private static final Bindable BINDABLE_PROPERTIES = Bindable.of(ConfigDataProperties.class); - private static final Bindable BINDABLE_STRING_ARRAY = Bindable.of(String[].class); - private final List imports; private final Activate activate; @@ -87,13 +79,6 @@ class ConfigDataProperties { return new ConfigDataProperties(null, this.activate); } - ConfigDataProperties withLegacyProfiles(String[] legacyProfiles, ConfigurationProperty property) { - if (this.activate != null && !ObjectUtils.isEmpty(this.activate.onProfile)) { - throw new InvalidConfigDataPropertyException(property, false, NAME.append("activate.on-profile"), null); - } - return new ConfigDataProperties(this.imports, new Activate(this.activate.onCloudPlatform, legacyProfiles)); - } - /** * Factory method used to create {@link ConfigDataProperties} from the given * {@link Binder}. @@ -101,37 +86,7 @@ class ConfigDataProperties { * @return a {@link ConfigDataProperties} instance or {@code null} */ static ConfigDataProperties get(Binder binder) { - LegacyProfilesBindHandler legacyProfilesBindHandler = new LegacyProfilesBindHandler(); - String[] legacyProfiles = binder.bind(LEGACY_PROFILES_NAME, BINDABLE_STRING_ARRAY, legacyProfilesBindHandler) - .orElse(null); - ConfigDataProperties properties = binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler()) - .orElse(null); - if (!ObjectUtils.isEmpty(legacyProfiles)) { - properties = (properties != null) - ? properties.withLegacyProfiles(legacyProfiles, legacyProfilesBindHandler.getProperty()) - : new ConfigDataProperties(null, new Activate(null, legacyProfiles)); - } - return properties; - } - - /** - * {@link BindHandler} used to check for legacy processing properties. - */ - private static class LegacyProfilesBindHandler implements BindHandler { - - private ConfigurationProperty property; - - @Override - public Object onSuccess(ConfigurationPropertyName name, Bindable target, BindContext context, - Object result) { - this.property = context.getConfigurationProperty(); - return result; - } - - ConfigurationProperty getProperty() { - return this.property; - } - + return binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler()).orElse(null); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java index 3b812bacc0..e11ba60b02 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java @@ -22,8 +22,6 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import org.apache.commons.logging.Log; - import org.springframework.boot.context.properties.source.ConfigurationProperty; import org.springframework.boot.context.properties.source.ConfigurationPropertyName; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; @@ -38,14 +36,14 @@ import org.springframework.core.env.AbstractEnvironment; */ public class InvalidConfigDataPropertyException extends ConfigDataException { - private static final Map WARNINGS; + private static final Map ERRORS; static { - Map warnings = new LinkedHashMap<>(); - warnings.put(ConfigurationPropertyName.of("spring.profiles"), + Map errors = new LinkedHashMap<>(); + errors.put(ConfigurationPropertyName.of("spring.profiles"), ConfigurationPropertyName.of("spring.config.activate.on-profile")); - warnings.put(ConfigurationPropertyName.of("spring.profiles[0]"), + errors.put(ConfigurationPropertyName.of("spring.profiles[0]"), ConfigurationPropertyName.of("spring.config.activate.on-profile")); - WARNINGS = Collections.unmodifiableMap(warnings); + ERRORS = Collections.unmodifiableMap(errors); } private static final Set PROFILE_SPECIFIC_ERRORS; @@ -101,20 +99,18 @@ public class InvalidConfigDataPropertyException extends ConfigDataException { } /** - * Throw an {@link InvalidConfigDataPropertyException} or log a warning if the given - * {@link ConfigDataEnvironmentContributor} contains any invalid property. A warning - * is logged if the property is still supported, but not recommended. An error is - * thrown if the property is completely unsupported. - * @param logger the logger to use for warnings + * Throw an {@link InvalidConfigDataPropertyException} if the given + * {@link ConfigDataEnvironmentContributor} contains any invalid property. * @param contributor the contributor to check */ - static void throwOrWarn(Log logger, ConfigDataEnvironmentContributor contributor) { + static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor) { ConfigurationPropertySource propertySource = contributor.getConfigurationPropertySource(); if (propertySource != null) { - WARNINGS.forEach((name, replacement) -> { + ERRORS.forEach((name, replacement) -> { ConfigurationProperty property = propertySource.getConfigurationProperty(name); if (property != null) { - logger.warn(getMessage(property, false, replacement, contributor.getResource())); + throw new InvalidConfigDataPropertyException(property, false, replacement, + contributor.getResource()); } }); if (contributor.isFromProfileSpecificImport() diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 560129fd48..96a17e4aff 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -33,7 +33,6 @@ import org.apache.logging.log4j.util.Strings; import org.assertj.core.api.Condition; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -161,7 +160,8 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { @Test void runWhenActiveProfilesDoesNotLoadDefault() { ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofilesdocument", - "--spring.profiles.default=thedefault", "--spring.profiles.active=other"); + "--spring.config.location=classpath:configdata/profiles/", "--spring.profiles.default=thedefault", + "--spring.profiles.active=other"); String property = context.getEnvironment().getProperty("my.property"); assertThat(property).isEqualTo("fromotherprofile"); } @@ -591,7 +591,6 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { } @Test - @Disabled("Disabled until spring.profiles suppport is dropped") void runWhenUsingInvalidPropertyThrowsException() { assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy( () -> this.application.run("--spring.config.location=classpath:invalidproperty.properties")); 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 2c9cd2c6c6..407840ecf9 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 @@ -29,7 +29,6 @@ import org.springframework.boot.context.properties.source.MapConfigurationProper import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link ConfigDataProperties}. @@ -193,27 +192,6 @@ class ConfigDataPropertiesTests { assertThat(properties.isActive(context)).isFalse(); } - @Test - void isActiveWhenBindingToLegacyProperty() { - MapConfigurationPropertySource source = new MapConfigurationPropertySource(); - source.put("spring.profiles", "a,b"); - Binder binder = new Binder(source); - ConfigDataProperties properties = ConfigDataProperties.get(binder); - ConfigDataActivationContext context = new ConfigDataActivationContext(CloudPlatform.KUBERNETES, - createTestProfiles()); - assertThat(properties.isActive(context)).isTrue(); - } - - @Test - void getWhenHasLegacyAndNewPropertyThrowsException() { - MapConfigurationPropertySource source = new MapConfigurationPropertySource(); - source.put("spring.profiles", "a,b"); - source.put("spring.config.activate.on-profile", "a | b"); - Binder binder = new Binder(source); - assertThatExceptionOfType(InvalidConfigDataPropertyException.class) - .isThrownBy(() -> ConfigDataProperties.get(binder)); - } - @Test void getImportOriginWhenCommaListReturnsOrigin() { MapConfigurationPropertySource source = new MapConfigurationPropertySource(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java index 8612104b02..e47abe01d3 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java @@ -16,8 +16,6 @@ package org.springframework.boot.context.config; -import org.apache.commons.logging.Log; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind; @@ -30,8 +28,6 @@ import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatNoException; -import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mock; /** * Tests for {@link InvalidConfigDataPropertyException}. @@ -49,8 +45,6 @@ class InvalidConfigDataPropertyExceptionTests { private ConfigurationProperty property = new ConfigurationProperty(this.invalid, "bad", MockOrigin.of("origin")); - private Log logger = mock(Log.class); - @Test void createHasCorrectMessage() { assertThat(new InvalidConfigDataPropertyException(this.property, false, this.replacement, this.resource)) @@ -106,13 +100,12 @@ class InvalidConfigDataPropertyExceptionTests { } @Test - @Disabled("Disabled until spring.profiles support is dropped") void throwOrWarnWhenHasInvalidPropertyThrowsException() { MockPropertySource propertySource = new MockPropertySource(); propertySource.setProperty("spring.profiles", "a"); ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource); assertThatExceptionOfType(InvalidConfigDataPropertyException.class) - .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)) + .isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor)) .withMessageStartingWith("Property 'spring.profiles' is invalid and should be replaced with " + "'spring.config.activate.on-profile'"); } @@ -128,14 +121,13 @@ class InvalidConfigDataPropertyExceptionTests { void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyOnIgnoringProfilesContributorDoesNotThrowException() { ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor( "spring.profiles.active", ConfigData.Option.IGNORE_PROFILES); - assertThatNoException() - .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)); + assertThatNoException().isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor)); } private void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException(String name) { ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(name); assertThatExceptionOfType(InvalidConfigDataPropertyException.class) - .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)) + .isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor)) .withMessageStartingWith("Property '" + name + "' is invalid in a profile specific resource"); } @@ -153,27 +145,7 @@ class InvalidConfigDataPropertyExceptionTests { void throwOrWarnWhenHasNoInvalidPropertyDoesNothing() { ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor .ofExisting(new MockPropertySource()); - InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor); - } - - @Test - void throwOrWarnWhenHasWarningPropertyLogsWarning() { - MockPropertySource propertySource = new MockPropertySource(); - propertySource.setProperty("spring.profiles", "a"); - ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource); - InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor); - then(this.logger).should().warn("Property 'spring.profiles' is invalid and should be replaced with " - + "'spring.config.activate.on-profile' [origin: \"spring.profiles\" from property source \"mockProperties\"]"); - } - - @Test - void throwOrWarnWhenHasWarningPropertyWithListSyntaxLogsWarning() { - MockPropertySource propertySource = new MockPropertySource(); - propertySource.setProperty("spring.profiles[0]", "a"); - ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource); - InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor); - then(this.logger).should().warn("Property 'spring.profiles[0]' is invalid and should be replaced with " - + "'spring.config.activate.on-profile' [origin: \"spring.profiles[0]\" from property source \"mockProperties\"]"); + InvalidConfigDataPropertyException.throwIfPropertyFound(contributor); } private static class TestConfigDataResource extends ConfigDataResource { diff --git a/spring-boot-project/spring-boot/src/test/resources/invalidproperty.properties b/spring-boot-project/spring-boot/src/test/resources/invalidproperty.properties index aff8071d75..4729d6f015 100644 --- a/spring-boot-project/spring-boot/src/test/resources/invalidproperty.properties +++ b/spring-boot-project/spring-boot/src/test/resources/invalidproperty.properties @@ -1 +1 @@ -spring.profile=a \ No newline at end of file +spring.profiles=a \ No newline at end of file diff --git a/spring-boot-project/spring-boot/src/test/resources/testprofiles.yml b/spring-boot-project/spring-boot/src/test/resources/testprofiles.yml index cb6cde27e6..dbe7c5f272 100644 --- a/spring-boot-project/spring-boot/src/test/resources/testprofiles.yml +++ b/spring-boot-project/spring-boot/src/test/resources/testprofiles.yml @@ -3,12 +3,10 @@ my: property: fromyamlfile other: notempty --- -spring: - profiles: dev +spring.config.activate.on-profile: dev my: property: fromdevprofile --- -spring: - profiles: other +spring.config.activate.on-profile: other my: property: fromotherprofile