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
This commit is contained in:
parent
d938dd97bf
commit
1d302f4c63
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -47,7 +47,6 @@ import org.springframework.boot.context.properties.bind.Binder;
|
||||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||||
import org.springframework.boot.convert.ApplicationConversionService;
|
import org.springframework.boot.convert.ApplicationConversionService;
|
||||||
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
|
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.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
|
@ -82,7 +81,6 @@ import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StopWatch;
|
import org.springframework.util.StopWatch;
|
||||||
import org.springframework.util.StringUtils;
|
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
|
* Class that can be used to bootstrap and launch a Spring application from a Java main
|
||||||
|
@ -391,11 +389,11 @@ public class SpringApplication {
|
||||||
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
|
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
|
||||||
switch (this.webApplicationType) {
|
switch (this.webApplicationType) {
|
||||||
case SERVLET:
|
case SERVLET:
|
||||||
return StandardServletEnvironment.class;
|
return ApplicationServletEnvironment.class;
|
||||||
case REACTIVE:
|
case REACTIVE:
|
||||||
return StandardReactiveWebEnvironment.class;
|
return ApplicationReactiveWebEnvironment.class;
|
||||||
default:
|
default:
|
||||||
return StandardEnvironment.class;
|
return ApplicationEnvironment.class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -493,11 +491,11 @@ public class SpringApplication {
|
||||||
}
|
}
|
||||||
switch (this.webApplicationType) {
|
switch (this.webApplicationType) {
|
||||||
case SERVLET:
|
case SERVLET:
|
||||||
return new StandardServletEnvironment();
|
return new ApplicationServletEnvironment();
|
||||||
case REACTIVE:
|
case REACTIVE:
|
||||||
return new StandardReactiveWebEnvironment();
|
return new ApplicationReactiveWebEnvironment();
|
||||||
default:
|
default:
|
||||||
return new StandardEnvironment();
|
return new ApplicationEnvironment();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
|
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
|
||||||
import org.springframework.context.event.SmartApplicationListener;
|
import org.springframework.context.event.SmartApplicationListener;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.env.AbstractEnvironment;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.env.MutablePropertySources;
|
import org.springframework.core.env.MutablePropertySources;
|
||||||
|
@ -361,13 +362,18 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
this.profiles.addAll(includedViaProperty);
|
this.profiles.addAll(includedViaProperty);
|
||||||
addActiveProfiles(activatedViaProperty);
|
addActiveProfiles(activatedViaProperty);
|
||||||
if (this.profiles.size() == 1) { // only has null profile
|
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);
|
Profile defaultProfile = new Profile(defaultProfileName, true);
|
||||||
this.profiles.add(defaultProfile);
|
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<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty,
|
private List<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty,
|
||||||
Set<Profile> includedViaProperty) {
|
Set<Profile> includedViaProperty) {
|
||||||
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter(
|
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter(
|
||||||
|
@ -777,9 +783,9 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
if (defaultProperties != null) {
|
if (defaultProperties != null) {
|
||||||
Binder binder = new Binder(ConfigurationPropertySources.from(defaultProperties),
|
Binder binder = new Binder(ConfigurationPropertySources.from(defaultProperties),
|
||||||
new PropertySourcesPlaceholdersResolver(this.environment));
|
new PropertySourcesPlaceholdersResolver(this.environment));
|
||||||
activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.include"));
|
activeProfiles.addAll(bindStringList(binder, "spring.profiles.include"));
|
||||||
if (!this.activatedProfiles) {
|
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)
|
this.processedProfiles.stream().filter(this::isDefaultProfile).map(Profile::getName)
|
||||||
|
@ -791,7 +797,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
return profile != null && !profile.isDefaultProfile();
|
return profile != null && !profile.isDefaultProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getDefaultProfiles(Binder binder, String property) {
|
private List<String> bindStringList(Binder binder, String property) {
|
||||||
return binder.bind(property, STRING_LIST).orElse(Collections.emptyList());
|
return binder.bind(property, STRING_LIST).orElse(Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -109,6 +109,9 @@ public class Profiles implements Iterable<String> {
|
||||||
if (!StringUtils.hasLength(propertyValue)) {
|
if (!StringUtils.hasLength(propertyValue)) {
|
||||||
return !unset.equals(profiles);
|
return !unset.equals(profiles);
|
||||||
}
|
}
|
||||||
|
if (unset.equals(profiles)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Set<String> propertyProfiles = StringUtils
|
Set<String> propertyProfiles = StringUtils
|
||||||
.commaDelimitedListToSet(StringUtils.trimAllWhitespace(propertyValue));
|
.commaDelimitedListToSet(StringUtils.trimAllWhitespace(propertyValue));
|
||||||
return !propertyProfiles.equals(profiles);
|
return !propertyProfiles.equals(profiles);
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
package org.springframework.boot.web.reactive.context;
|
||||||
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.core.env.MutablePropertySources;
|
||||||
import org.springframework.core.env.StandardEnvironment;
|
import org.springframework.core.env.StandardEnvironment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,4 +30,12 @@ import org.springframework.core.env.StandardEnvironment;
|
||||||
*/
|
*/
|
||||||
public class StandardReactiveWebEnvironment extends StandardEnvironment implements ConfigurableReactiveWebEnvironment {
|
public class StandardReactiveWebEnvironment extends StandardEnvironment implements ConfigurableReactiveWebEnvironment {
|
||||||
|
|
||||||
|
public StandardReactiveWebEnvironment() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected StandardReactiveWebEnvironment(MutablePropertySources propertySources) {
|
||||||
|
super(propertySources);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -70,7 +70,6 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor
|
||||||
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
|
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
|
||||||
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
|
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
|
||||||
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
|
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.AnnotationConfigServletWebApplicationContext;
|
||||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
@ -476,7 +475,7 @@ class SpringApplicationTests {
|
||||||
SpringApplication application = new SpringApplication(ExampleWebConfig.class);
|
SpringApplication application = new SpringApplication(ExampleWebConfig.class);
|
||||||
application.setWebApplicationType(WebApplicationType.SERVLET);
|
application.setWebApplicationType(WebApplicationType.SERVLET);
|
||||||
this.context = application.run();
|
this.context = application.run();
|
||||||
assertThat(this.context.getEnvironment()).isInstanceOf(StandardServletEnvironment.class);
|
assertThat(this.context.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -484,7 +483,7 @@ class SpringApplicationTests {
|
||||||
SpringApplication application = new SpringApplication(ExampleReactiveWebConfig.class);
|
SpringApplication application = new SpringApplication(ExampleReactiveWebConfig.class);
|
||||||
application.setWebApplicationType(WebApplicationType.REACTIVE);
|
application.setWebApplicationType(WebApplicationType.REACTIVE);
|
||||||
this.context = application.run();
|
this.context = application.run();
|
||||||
assertThat(this.context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class);
|
assertThat(this.context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1061,7 +1060,7 @@ class SpringApplicationTests {
|
||||||
void webApplicationSwitchedOffInListener() {
|
void webApplicationSwitchedOffInListener() {
|
||||||
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
|
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
|
||||||
application.addListeners((ApplicationListener<ApplicationEnvironmentPreparedEvent>) (event) -> {
|
application.addListeners((ApplicationListener<ApplicationEnvironmentPreparedEvent>) (event) -> {
|
||||||
assertThat(event.getEnvironment()).isInstanceOf(StandardServletEnvironment.class);
|
assertThat(event.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class);
|
||||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(event.getEnvironment(), "foo=bar");
|
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(event.getEnvironment(), "foo=bar");
|
||||||
event.getSpringApplication().setWebApplicationType(WebApplicationType.NONE);
|
event.getSpringApplication().setWebApplicationType(WebApplicationType.NONE);
|
||||||
});
|
});
|
||||||
|
@ -1087,7 +1086,7 @@ class SpringApplicationTests {
|
||||||
ConfigurableApplicationContext context = new SpringApplication(ExampleWebConfig.class)
|
ConfigurableApplicationContext context = new SpringApplication(ExampleWebConfig.class)
|
||||||
.run("--spring.main.web-application-type=servlet");
|
.run("--spring.main.web-application-type=servlet");
|
||||||
assertThat(context).isInstanceOf(WebApplicationContext.class);
|
assertThat(context).isInstanceOf(WebApplicationContext.class);
|
||||||
assertThat(context.getEnvironment()).isInstanceOf(StandardServletEnvironment.class);
|
assertThat(context.getEnvironment()).isInstanceOf(ApplicationServletEnvironment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1095,7 +1094,7 @@ class SpringApplicationTests {
|
||||||
ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class)
|
ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class)
|
||||||
.run("--spring.main.web-application-type=reactive");
|
.run("--spring.main.web-application-type=reactive");
|
||||||
assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class);
|
assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class);
|
||||||
assertThat(context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class);
|
assertThat(context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1103,7 +1102,7 @@ class SpringApplicationTests {
|
||||||
ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class)
|
ConfigurableApplicationContext context = new SpringApplication(ExampleReactiveWebConfig.class)
|
||||||
.run("--spring.profiles.active=withwebapplicationtype");
|
.run("--spring.profiles.active=withwebapplicationtype");
|
||||||
assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class);
|
assertThat(context).isInstanceOf(ReactiveWebApplicationContext.class);
|
||||||
assertThat(context.getEnvironment()).isInstanceOf(StandardReactiveWebEnvironment.class);
|
assertThat(context.getEnvironment()).isInstanceOf(ApplicationReactiveWebEnvironment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.junit.jupiter.api.TestInfo;
|
||||||
|
|
||||||
import org.springframework.boot.ConfigurableBootstrapContext;
|
import org.springframework.boot.ConfigurableBootstrapContext;
|
||||||
import org.springframework.boot.DefaultBootstrapContext;
|
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.ImportPhase;
|
||||||
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
|
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
|
||||||
import org.springframework.boot.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource;
|
import org.springframework.boot.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource;
|
||||||
|
@ -40,7 +41,6 @@ import org.springframework.core.env.MapPropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.mock.env.MockEnvironment;
|
|
||||||
import org.springframework.mock.env.MockPropertySource;
|
import org.springframework.mock.env.MockPropertySource;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -59,7 +59,7 @@ class ConfigDataEnvironmentTests {
|
||||||
|
|
||||||
private DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
|
private DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
|
||||||
|
|
||||||
private MockEnvironment environment = new MockEnvironment();
|
private MockApplicationEnvironment environment = new MockApplicationEnvironment();
|
||||||
|
|
||||||
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||||
|
|
||||||
|
@ -216,6 +216,31 @@ class ConfigDataEnvironmentTests {
|
||||||
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
|
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<ConfigDataEnvironmentContributor> contributors) {
|
||||||
|
Map<String, Object> 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
|
@Test
|
||||||
@Disabled("Disabled until spring.profiles support is dropped")
|
@Disabled("Disabled until spring.profiles support is dropped")
|
||||||
void processAndApplyWhenHasInvalidPropertyThrowsException() {
|
void processAndApplyWhenHasInvalidPropertyThrowsException() {
|
||||||
|
|
Loading…
Reference in New Issue