Use side-effect free environment with tests rather than converting

Refine the logic introduced in 64270eca to use a side-effect free
Environment implementation rather than converting the Environment early.

Early conversion can cause condition evaluation issues if
`src/test/resources/application.properties` files are bound to the
`SpringApplication`. Specifically the `spring.main.web-application-type`
property can change the `Environment` type which must happen before
conditions are evaluated.

Fixes gh-29169
This commit is contained in:
Phillip Webb 2022-01-11 19:08:54 -08:00
parent 9cf55808fb
commit eb6b48fff0
7 changed files with 93 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@ -39,6 +39,7 @@ import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
@ -110,7 +111,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.setWebApplicationType(WebApplicationType.NONE);
}
application.setInitializers(initializers);
ConfigurableEnvironment environment = getEnvironment(application, config.getActiveProfiles());
ConfigurableEnvironment environment = getEnvironment();
setActiveProfiles(environment, config.getActiveProfiles());
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(null);
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
@ -121,23 +123,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
return application.run(args);
}
private ConfigurableEnvironment getEnvironment(SpringApplication application, String[] activeProfiles) {
ConfigurableEnvironment environment = getEnvironment();
boolean applicationEnvironment = false;
if (environment.getClass() == StandardEnvironment.class) {
environment = application.convertEnvironment(environment);
applicationEnvironment = true;
}
setActiveProfiles(environment, activeProfiles, applicationEnvironment);
return environment;
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
boolean applicationEnvironment) {
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
if (ObjectUtils.isEmpty(profiles)) {
return;
}
if (!applicationEnvironment) {
if (!(environment instanceof TestEnvironment)) {
environment.setActiveProfiles(profiles);
}
String[] pairs = new String[profiles.length];
@ -162,7 +152,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
* @return a {@link ConfigurableEnvironment} instance
*/
protected ConfigurableEnvironment getEnvironment() {
return new StandardEnvironment();
return new TestEnvironment();
}
protected String[] getInlinedProperties(MergedContextConfiguration config) {
@ -293,6 +283,9 @@ public class SpringBootContextLoader extends AbstractContextLoader {
}
/**
* {@link ApplicationContextInitializer} used to set the parent context.
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
private static class ParentContextApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@ -310,4 +303,22 @@ public class SpringBootContextLoader extends AbstractContextLoader {
}
/**
* Side-effect free {@link Environment} that doesn't set profiles directly from
* properties.
*/
static class TestEnvironment extends StandardEnvironment {
@Override
protected String doGetActiveProfilesProperty() {
return null;
}
@Override
protected String doGetDefaultProfilesProperty() {
return null;
}
}
}

View File

@ -389,7 +389,9 @@ public class SpringApplication {
* @param environment the environment to convert
* @return the converted environment
* @since 2.5.7
* @deprecated since 2.5.8 for removal in 2.7.0
*/
@Deprecated
public StandardEnvironment convertEnvironment(ConfigurableEnvironment environment) {
return new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());

View File

@ -6,7 +6,8 @@ plugins {
description = "Spring Boot profile smoke test"
dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-webflux"))
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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.
@ -21,7 +21,9 @@ import smoketest.profile.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
@SpringBootApplication
public class SampleProfileApplication implements CommandLineRunner {
@ -38,8 +40,16 @@ public class SampleProfileApplication implements CommandLineRunner {
System.out.println(this.helloWorldService.getMessage());
}
public static void main(String[] args) {
SpringApplication.run(SampleProfileApplication.class, args);
public static void main(String... args) {
SpringApplication application = new SpringApplication(SampleProfileApplication.class) {
@Override
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
}
};
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2012-2022 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 smoketest.profile;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
// gh-29169
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AttributeInjectionTests {
@Autowired(required = false)
private org.springframework.boot.web.servlet.error.ErrorAttributes errorAttributesServlet;
@Autowired(required = false)
private org.springframework.boot.web.reactive.error.ErrorAttributes errorAttributesReactive;
@Test
void contextLoads() {
assertThat(this.errorAttributesServlet).isNull();
assertThat(this.errorAttributesReactive).isNotNull();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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.
@ -48,14 +48,14 @@ class SampleProfileApplicationTests {
@Test
void testDefaultProfile(CapturedOutput output) {
SampleProfileApplication.main(new String[0]);
SampleProfileApplication.main();
assertThat(output).contains("Hello Phil");
}
@Test
void testGoodbyeProfile(CapturedOutput output) {
System.setProperty("spring.profiles.active", "goodbye");
SampleProfileApplication.main(new String[0]);
SampleProfileApplication.main();
assertThat(output).contains("Goodbye Everyone");
}
@ -68,13 +68,13 @@ class SampleProfileApplicationTests {
* "name" property.
*/
System.setProperty("spring.profiles.active", "generic");
SampleProfileApplication.main(new String[0]);
SampleProfileApplication.main();
assertThat(output).contains("Bonjour Phil");
}
@Test
void testGoodbyeProfileFromCommandline(CapturedOutput output) {
SampleProfileApplication.main(new String[] { "--spring.profiles.active=goodbye" });
SampleProfileApplication.main("--spring.profiles.active=goodbye");
assertThat(output).contains("Goodbye Everyone");
}

View File

@ -0,0 +1 @@
spring.main.web-application-type=reactive