Skip to content

Commit

Permalink
Merge branch '2.5.x' into 2.6.x
Browse files Browse the repository at this point in the history
Closes gh-29374
  • Loading branch information
philwebb committed Jan 12, 2022
2 parents 72696cd + eb6b48f commit 91d2b1b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 26 deletions.
@@ -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.
Expand Down Expand Up @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -110,7 +111,8 @@ else if (config instanceof ReactiveWebMergedContextConfiguration) {
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,
Expand All @@ -121,23 +123,11 @@ else if (config instanceof ReactiveWebMergedContextConfiguration) {
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];
Expand All @@ -162,7 +152,7 @@ protected SpringApplication getSpringApplication() {
* @return a {@link ConfigurableEnvironment} instance
*/
protected ConfigurableEnvironment getEnvironment() {
return new StandardEnvironment();
return new TestEnvironment();
}

protected String[] getInlinedProperties(MergedContextConfiguration config) {
Expand Down Expand Up @@ -293,6 +283,9 @@ public void initialize(ConfigurableApplicationContext applicationContext) {

}

/**
* {@link ApplicationContextInitializer} used to set the parent context.
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
private static class ParentContextApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
Expand All @@ -310,4 +303,22 @@ public void initialize(ConfigurableApplicationContext applicationContext) {

}

/**
* 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;
}

}

}
Expand Up @@ -353,7 +353,9 @@ private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners
* @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());
Expand Down
Expand Up @@ -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"))
}
@@ -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.
Expand All @@ -21,7 +21,9 @@
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 {
Expand All @@ -38,8 +40,16 @@ public void run(String... args) {
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);
}

}
@@ -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();
}

}
@@ -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.
Expand Down Expand Up @@ -48,14 +48,14 @@ void after() {

@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");
}

Expand All @@ -68,13 +68,13 @@ void testGenericProfile(CapturedOutput output) {
* "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");
}

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

0 comments on commit 91d2b1b

Please sign in to comment.