From 1d302f4c63f1611eca4607e415ab846e77f420b4 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 7 Apr 2021 22:05:56 -0700 Subject: [PATCH] Add customer Application Environment subclasses Add custom `ApplicationEnvironment`, `ApplicationServletEnvironment` and `ApplicationReactiveWebEnvironment` subclasses for use with `SpringApplication`. The subclasses all disable the resolution of active and default profiles using properties since this is handled directly by the `ConfigDataEnvironmentPostProcessor`. Closes gh-24892 See gh-24890 --- .../boot/ApplicationEnvironment.java | 38 +++++++++++++ .../ApplicationReactiveWebEnvironment.java | 39 +++++++++++++ .../boot/ApplicationServletEnvironment.java | 39 +++++++++++++ .../boot/SpringApplication.java | 14 ++--- .../config/ConfigFileApplicationListener.java | 14 +++-- .../boot/context/config/Profiles.java | 3 + .../StandardReactiveWebEnvironment.java | 11 +++- .../AbstractApplicationEnvironmentTests.java | 55 +++++++++++++++++++ .../boot/ApplicationEnvironmentTests.java | 33 +++++++++++ ...pplicationReactiveWebEnvironmentTests.java | 33 +++++++++++ .../ApplicationServletEnvironmentTests.java | 33 +++++++++++ .../boot/MockApplicationEnvironment.java | 39 +++++++++++++ .../boot/SpringApplicationTests.java | 13 ++--- .../config/ConfigDataEnvironmentTests.java | 29 +++++++++- ...ontributorsWhenNoProfilesActive.properties | 1 + 15 files changed, 372 insertions(+), 22 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationEnvironment.java create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationReactiveWebEnvironment.java create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationServletEnvironment.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/AbstractApplicationEnvironmentTests.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationEnvironmentTests.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationReactiveWebEnvironmentTests.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationServletEnvironmentTests.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/MockApplicationEnvironment.java create mode 100644 spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive.properties diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationEnvironment.java new file mode 100644 index 000000000000..3cec5cab59de --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationEnvironment.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.core.env.StandardEnvironment; + +/** + * {@link StandardEnvironment} for typical use in a typical {@link SpringApplication}. + * + * @author Phillip Webb + */ +class ApplicationEnvironment extends StandardEnvironment { + + @Override + protected String doGetActiveProfilesProperty() { + return null; + } + + @Override + protected String doGetDefaultProfilesProperty() { + return null; + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationReactiveWebEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationReactiveWebEnvironment.java new file mode 100644 index 000000000000..64d141fc8a21 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationReactiveWebEnvironment.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; + +/** + * {@link StandardReactiveWebEnvironment} for typical use in a typical + * {@link SpringApplication}. + * + * @author Phillip Webb + */ +class ApplicationReactiveWebEnvironment extends StandardReactiveWebEnvironment { + + @Override + protected String doGetActiveProfilesProperty() { + return null; + } + + @Override + protected String doGetDefaultProfilesProperty() { + return null; + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationServletEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationServletEnvironment.java new file mode 100644 index 000000000000..7efa2267cc2b --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationServletEnvironment.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.web.context.support.StandardServletEnvironment; + +/** + * {@link StandardServletEnvironment} for typical use in a typical + * {@link SpringApplication}. + * + * @author Phillip Webb + */ +class ApplicationServletEnvironment extends StandardServletEnvironment { + + @Override + protected String doGetActiveProfilesProperty() { + return null; + } + + @Override + protected String doGetDefaultProfilesProperty() { + return null; + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index eb5aaa258b93..1a4dfb858fd9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -47,7 +47,6 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; -import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -82,7 +81,6 @@ import org.springframework.util.ReflectionUtils; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; -import org.springframework.web.context.support.StandardServletEnvironment; /** * Class that can be used to bootstrap and launch a Spring application from a Java main @@ -391,11 +389,11 @@ private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners private Class deduceEnvironmentClass() { switch (this.webApplicationType) { case SERVLET: - return StandardServletEnvironment.class; + return ApplicationServletEnvironment.class; case REACTIVE: - return StandardReactiveWebEnvironment.class; + return ApplicationReactiveWebEnvironment.class; default: - return StandardEnvironment.class; + return ApplicationEnvironment.class; } } @@ -493,11 +491,11 @@ private ConfigurableEnvironment getOrCreateEnvironment() { } switch (this.webApplicationType) { case SERVLET: - return new StandardServletEnvironment(); + return new ApplicationServletEnvironment(); case REACTIVE: - return new StandardReactiveWebEnvironment(); + return new ApplicationReactiveWebEnvironment(); default: - return new StandardEnvironment(); + return new ApplicationEnvironment(); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index ee4f6b48a8ba..9e47dd580ed2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -61,6 +61,7 @@ import org.springframework.context.annotation.ConfigurationClassPostProcessor; import org.springframework.context.event.SmartApplicationListener; import org.springframework.core.Ordered; +import org.springframework.core.env.AbstractEnvironment; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; @@ -361,13 +362,18 @@ private void initializeProfiles() { this.profiles.addAll(includedViaProperty); addActiveProfiles(activatedViaProperty); if (this.profiles.size() == 1) { // only has null profile - for (String defaultProfileName : this.environment.getDefaultProfiles()) { + for (String defaultProfileName : getDefaultProfiles(binder)) { Profile defaultProfile = new Profile(defaultProfileName, true); this.profiles.add(defaultProfile); } } } + private String[] getDefaultProfiles(Binder binder) { + return binder.bind(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, STRING_ARRAY) + .orElseGet(this.environment::getDefaultProfiles); + } + private List getOtherActiveProfiles(Set activatedViaProperty, Set includedViaProperty) { return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter( @@ -777,9 +783,9 @@ private void applyActiveProfiles(PropertySource defaultProperties) { if (defaultProperties != null) { Binder binder = new Binder(ConfigurationPropertySources.from(defaultProperties), new PropertySourcesPlaceholdersResolver(this.environment)); - activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.include")); + activeProfiles.addAll(bindStringList(binder, "spring.profiles.include")); if (!this.activatedProfiles) { - activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.active")); + activeProfiles.addAll(bindStringList(binder, "spring.profiles.active")); } } this.processedProfiles.stream().filter(this::isDefaultProfile).map(Profile::getName) @@ -791,7 +797,7 @@ private boolean isDefaultProfile(Profile profile) { return profile != null && !profile.isDefaultProfile(); } - private List getDefaultProfiles(Binder binder, String property) { + private List bindStringList(Binder binder, String property) { return binder.bind(property, STRING_LIST).orElse(Collections.emptyList()); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java index 4f41785cdf2c..647db0cd442a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java @@ -109,6 +109,9 @@ private boolean hasExplicit(Supplier supplier, String propertyValue, S if (!StringUtils.hasLength(propertyValue)) { return !unset.equals(profiles); } + if (unset.equals(profiles)) { + return false; + } Set propertyProfiles = StringUtils .commaDelimitedListToSet(StringUtils.trimAllWhitespace(propertyValue)); return !propertyProfiles.equals(profiles); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/StandardReactiveWebEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/StandardReactiveWebEnvironment.java index 696761741740..d1b2d9eb12f3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/StandardReactiveWebEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/StandardReactiveWebEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -17,6 +17,7 @@ package org.springframework.boot.web.reactive.context; import org.springframework.core.env.Environment; +import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.StandardEnvironment; /** @@ -29,4 +30,12 @@ */ public class StandardReactiveWebEnvironment extends StandardEnvironment implements ConfigurableReactiveWebEnvironment { + public StandardReactiveWebEnvironment() { + super(); + } + + protected StandardReactiveWebEnvironment(MutablePropertySources propertySources) { + super(propertySources); + } + } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/AbstractApplicationEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/AbstractApplicationEnvironmentTests.java new file mode 100644 index 000000000000..5f5f789179a2 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/AbstractApplicationEnvironmentTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.env.AbstractEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.mock.env.MockPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Base class for {@link SpringApplication} {@link Environment} tests. + * + * @author Phillip Webb + */ +public abstract class AbstractApplicationEnvironmentTests { + + @Test + void getActiveProfilesDoesNotResolveProperty() { + StandardEnvironment environment = createEnvironment(); + new MockPropertySource().withProperty("", ""); + environment.getPropertySources().addFirst( + new MockPropertySource().withProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "test")); + assertThat(environment.getActiveProfiles()).isEmpty(); + } + + @Test + void getDefaultProfilesDoesNotResolveProperty() { + StandardEnvironment environment = createEnvironment(); + new MockPropertySource().withProperty("", ""); + environment.getPropertySources().addFirst( + new MockPropertySource().withProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "test")); + assertThat(environment.getDefaultProfiles()).containsExactly("default"); + } + + protected abstract StandardEnvironment createEnvironment(); + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationEnvironmentTests.java new file mode 100644 index 000000000000..458b6b6ca876 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationEnvironmentTests.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.core.env.StandardEnvironment; + +/** + * Tests for {@link ApplicationEnvironment}. + * + * @author Phillip Webb + */ +class ApplicationEnvironmentTests extends AbstractApplicationEnvironmentTests { + + @Override + protected StandardEnvironment createEnvironment() { + return new ApplicationEnvironment(); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationReactiveWebEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationReactiveWebEnvironmentTests.java new file mode 100644 index 000000000000..3f9a0cac8274 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationReactiveWebEnvironmentTests.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.core.env.StandardEnvironment; + +/** + * Tests for {@link ApplicationReactiveWebEnvironment}. + * + * @author Phillip Webb + */ +class ApplicationReactiveWebEnvironmentTests extends AbstractApplicationEnvironmentTests { + + @Override + protected StandardEnvironment createEnvironment() { + return new ApplicationReactiveWebEnvironment(); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationServletEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationServletEnvironmentTests.java new file mode 100644 index 000000000000..f2beed2fe618 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationServletEnvironmentTests.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.core.env.StandardEnvironment; + +/** + * Tests for {@link ApplicationServletEnvironment}. + * + * @author Phillip Webb + */ +class ApplicationServletEnvironmentTests extends AbstractApplicationEnvironmentTests { + + @Override + protected StandardEnvironment createEnvironment() { + return new ApplicationServletEnvironment(); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/MockApplicationEnvironment.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/MockApplicationEnvironment.java new file mode 100644 index 000000000000..4369d6a67faf --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/MockApplicationEnvironment.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +import org.springframework.mock.env.MockEnvironment; + +/** + * {@link MockEnvironment} with the same property overrides as + * {@link ApplicationEnvironment}. + * + * @author Phillip Webb + */ +public class MockApplicationEnvironment extends MockEnvironment { + + @Override + protected String doGetActiveProfilesProperty() { + return null; + } + + @Override + protected String doGetDefaultProfilesProperty() { + return null; + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 8b8d437746db..0e931b933a9c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -70,7 +70,6 @@ import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext; -import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.context.ApplicationContext; @@ -476,7 +475,7 @@ void environmentForWeb() { SpringApplication application = new SpringApplication(ExampleWebConfig.class); application.setWebApplicationType(WebApplicationType.SERVLET); this.context = application.run(); - assertThat(this.context.getEnvironment()).isInstanceOf(StandardServletEnvironment.class); + assertThat(this.context.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class); } @Test @@ -484,7 +483,7 @@ void environmentForReactiveWeb() { SpringApplication application = new SpringApplication(ExampleReactiveWebConfig.class); application.setWebApplicationType(WebApplicationType.REACTIVE); this.context = application.run(); - assertThat(this.context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class); + assertThat(this.context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class); } @Test @@ -1061,7 +1060,7 @@ void getApplicationArgumentsBean() { void webApplicationSwitchedOffInListener() { TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); application.addListeners((ApplicationListener) (event) -> { - assertThat(event.getEnvironment()).isInstanceOf(StandardServletEnvironment.class); + assertThat(event.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(event.getEnvironment(), "foo=bar"); event.getSpringApplication().setWebApplicationType(WebApplicationType.NONE); }); @@ -1087,7 +1086,7 @@ void webApplicationConfiguredViaAPropertyHasTheCorrectTypeOfContextAndEnvironmen ConfigurableApplicationContext context = new SpringApplication(ExampleWebConfig.class) .run("--spring.main.web-application-type=servlet"); assertThat(context).isInstanceOf(WebApplicationContext.class); - assertThat(context.getEnvironment()).isInstanceOf(StandardServletEnvironment.class); + assertThat(context.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class); } @Test @@ -1095,7 +1094,7 @@ void reactiveApplicationConfiguredViaAPropertyHasTheCorrectTypeOfContextAndEnvir ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class) .run("--spring.main.web-application-type=reactive"); assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class); - assertThat(context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class); + assertThat(context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class); } @Test @@ -1103,7 +1102,7 @@ void environmentIsConvertedIfTypeDoesNotMatch() { ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class) .run("--spring.profiles.active=withwebapplicationtype"); assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class); - assertThat(context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class); + assertThat(context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java index c690f079f51c..81715f4e853d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java @@ -30,6 +30,7 @@ import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; +import org.springframework.boot.MockApplicationEnvironment; import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.ImportPhase; import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind; import org.springframework.boot.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource; @@ -40,7 +41,6 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; -import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; @@ -59,7 +59,7 @@ class ConfigDataEnvironmentTests { private DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext(); - private MockEnvironment environment = new MockEnvironment(); + private MockApplicationEnvironment environment = new MockApplicationEnvironment(); private ResourceLoader resourceLoader = new DefaultResourceLoader(); @@ -216,6 +216,31 @@ protected ConfigDataEnvironmentContributors createContributors( assertThat(this.environment.getActiveProfiles()).containsExactly("test"); } + @Test + void processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive(TestInfo info) { + this.environment.setProperty("spring.config.location", getConfigLocation(info)); + ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext, + this.environment, this.resourceLoader, this.additionalProfiles, null) { + + @Override + protected ConfigDataEnvironmentContributors createContributors( + List contributors) { + Map source = new LinkedHashMap<>(); + source.put("spring.profiles.active", "ignore1"); + source.put("spring.profiles.include", "ignore2"); + ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source)), + ConfigData.Option.IGNORE_PROFILES); + contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"), + mock(ConfigDataResource.class), false, data, 0)); + return super.createContributors(contributors); + } + + }; + configDataEnvironment.processAndApply(); + assertThat(this.environment.getActiveProfiles()).isEmpty(); + assertThat(this.environment.getProperty("spring")).isEqualTo("boot"); + } + @Test @Disabled("Disabled until spring.profiles support is dropped") void processAndApplyWhenHasInvalidPropertyThrowsException() { diff --git a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive.properties b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive.properties new file mode 100644 index 000000000000..74bc31f180a7 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive.properties @@ -0,0 +1 @@ +spring=boot