Browse Source

Polish "Process additional profiles before config files processing"

See gh-25817
pull/26446/head
Madhura Bhave 5 years ago
parent
commit
2e1b20ce2b
  1. 1
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 11
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  3. 12
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java
  4. 11
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerLegacyReproTests.java
  5. 23
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

1
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -154,7 +154,6 @@ import org.springframework.web.context.support.StandardServletEnvironment; @@ -154,7 +154,6 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* @author Madhura Bhave
* @author Brian Clozel
* @author Ethan Rubinson
* @author Nguyen Bao Sach
* @since 1.0.0
* @see #run(Class, String[])
* @see #run(Class[], String[])

11
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -605,17 +605,6 @@ class SpringApplicationTests { @@ -605,17 +605,6 @@ class SpringApplicationTests {
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo");
}
@Test
void includeProfilesOrder() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo");
// Since Boot 2.4 included profiles should always be last
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo");
}
@Test
void addProfilesOrderWithProperties() {
SpringApplication application = new SpringApplication(ExampleConfig.class);

12
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java

@ -83,7 +83,6 @@ class ConfigDataEnvironmentPostProcessorTests { @@ -83,7 +83,6 @@ class ConfigDataEnvironmentPostProcessorTests {
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
verify(this.configDataEnvironment).processAndApply();
assertThat(this.resourceLoaderCaptor.getValue()).isInstanceOf(DefaultResourceLoader.class);
assertThat(this.environment.getActiveProfiles()).isEmpty();
}
@Test
@ -95,7 +94,6 @@ class ConfigDataEnvironmentPostProcessorTests { @@ -95,7 +94,6 @@ class ConfigDataEnvironmentPostProcessorTests {
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
verify(this.configDataEnvironment).processAndApply();
assertThat(this.resourceLoaderCaptor.getValue()).isSameAs(resourceLoader);
assertThat(this.environment.getActiveProfiles()).isEmpty();
}
@Test
@ -106,6 +104,14 @@ class ConfigDataEnvironmentPostProcessorTests { @@ -106,6 +104,14 @@ class ConfigDataEnvironmentPostProcessorTests {
verify(this.postProcessor).getConfigDataEnvironment(any(), any(), this.additionalProfilesCaptor.capture());
verify(this.configDataEnvironment).processAndApply();
assertThat(this.additionalProfilesCaptor.getValue()).containsExactly("dev");
}
@Test
void postProcessEnvironmentWhenNoActiveProfiles() {
willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
this.postProcessor.postProcessEnvironment(this.environment, this.application);
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
verify(this.configDataEnvironment).processAndApply();
assertThat(this.environment.getActiveProfiles()).isEmpty();
}
@ -123,7 +129,7 @@ class ConfigDataEnvironmentPostProcessorTests { @@ -123,7 +129,7 @@ class ConfigDataEnvironmentPostProcessorTests {
}
@Test
void postProcessEnvironmentWhenHasAdditionalProfilesViaProgrammaticallySettingAndUseLegacyProcessing() {
void postProcessEnvironmentWhenHasAdditionalProfilesAndUseLegacyProcessing() {
this.application.setAdditionalProfiles("dev");
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener legacyListener = mock(
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener.class);

11
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerLegacyReproTests.java

@ -168,17 +168,6 @@ class ConfigFileApplicationListenerLegacyReproTests { @@ -168,17 +168,6 @@ class ConfigFileApplicationListenerLegacyReproTests {
assertVersionProperty(this.context, "A", "C", "A");
}
@Test
void additionalProfilesViaProgrammaticallySetting() {
// gh-25704
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("dev");
this.context = application.run();
assertThat(this.context.getEnvironment().acceptsProfiles(Profiles.of("dev"))).isTrue();
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile");
}
private void assertVersionProperty(ConfigurableApplicationContext context, String expectedVersion,
String... expectedActiveProfiles) {
assertThat(context.getEnvironment().getActiveProfiles()).isEqualTo(expectedActiveProfiles);

23
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

@ -1153,17 +1153,6 @@ class ConfigFileApplicationListenerTests { @@ -1153,17 +1153,6 @@ class ConfigFileApplicationListenerTests {
@Test
void additionalProfilesCanBeIncludedFromProgrammaticallySetting() {
// gh-25704
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("dev");
this.context = application.run();
// Active profile should win over default
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile");
}
@Test
void twoAdditionalProfilesCanBeIncludedFromProgrammaticallySetting() {
// gh-25704
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
@ -1173,21 +1162,11 @@ class ConfigFileApplicationListenerTests { @@ -1173,21 +1162,11 @@ class ConfigFileApplicationListenerTests {
}
@Test
void includeProfilesOrder() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo");
// Before Boot 2.4 included profiles should always be first
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam");
}
@Test
void addProfilesOrder() {
void activeProfilesShouldTakePrecedenceOverAdditionalProfiles() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("foo");
this.context = application.run("--spring.profiles.active=bar,spam");
// Before Boot 2.4 additional profiles should always be first
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam");
}

Loading…
Cancel
Save