Skip to content

Commit eff024b

Browse files
committed
Polish "Process additional profiles before config files processing"
See spring-projectsgh-25817
1 parent 97fd9a7 commit eff024b

File tree

5 files changed

+10
-48
lines changed

5 files changed

+10
-48
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@
154154
* @author Madhura Bhave
155155
* @author Brian Clozel
156156
* @author Ethan Rubinson
157-
* @author Nguyen Bao Sach
158157
* @since 1.0.0
159158
* @see #run(Class, String[])
160159
* @see #run(Class[], String[])

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

-11
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,6 @@ void addProfilesOrder() {
605605
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo");
606606
}
607607

608-
@Test
609-
void includeProfilesOrder() {
610-
SpringApplication application = new SpringApplication(ExampleConfig.class);
611-
application.setWebApplicationType(WebApplicationType.NONE);
612-
ConfigurableEnvironment environment = new StandardEnvironment();
613-
application.setEnvironment(environment);
614-
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo");
615-
// Since Boot 2.4 included profiles should always be last
616-
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo");
617-
}
618-
619608
@Test
620609
void addProfilesOrderWithProperties() {
621610
SpringApplication application = new SpringApplication(ExampleConfig.class);

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ void postProcessEnvironmentWhenNoLoaderCreatesDefaultLoaderInstance() {
8383
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
8484
verify(this.configDataEnvironment).processAndApply();
8585
assertThat(this.resourceLoaderCaptor.getValue()).isInstanceOf(DefaultResourceLoader.class);
86-
assertThat(this.environment.getActiveProfiles()).isEmpty();
8786
}
8887

8988
@Test
@@ -95,7 +94,6 @@ void postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance() {
9594
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
9695
verify(this.configDataEnvironment).processAndApply();
9796
assertThat(this.resourceLoaderCaptor.getValue()).isSameAs(resourceLoader);
98-
assertThat(this.environment.getActiveProfiles()).isEmpty();
9997
}
10098

10199
@Test
@@ -106,6 +104,14 @@ void postProcessEnvironmentWhenHasAdditionalProfilesOnSpringApplicationUsesAddit
106104
verify(this.postProcessor).getConfigDataEnvironment(any(), any(), this.additionalProfilesCaptor.capture());
107105
verify(this.configDataEnvironment).processAndApply();
108106
assertThat(this.additionalProfilesCaptor.getValue()).containsExactly("dev");
107+
}
108+
109+
@Test
110+
void postProcessEnvironmentWhenNoActiveProfiles() {
111+
willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
112+
this.postProcessor.postProcessEnvironment(this.environment, this.application);
113+
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
114+
verify(this.configDataEnvironment).processAndApply();
109115
assertThat(this.environment.getActiveProfiles()).isEmpty();
110116
}
111117

@@ -123,7 +129,7 @@ void postProcessEnvironmentWhenUseLegacyProcessingSwitchesToLegacyMethod() {
123129
}
124130

125131
@Test
126-
void postProcessEnvironmentWhenHasAdditionalProfilesViaProgrammaticallySettingAndUseLegacyProcessing() {
132+
void postProcessEnvironmentWhenHasAdditionalProfilesAndUseLegacyProcessing() {
127133
this.application.setAdditionalProfiles("dev");
128134
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener legacyListener = mock(
129135
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener.class);

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

-11
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,6 @@ void reverseOrderOfProfilesWithYamlAndNoOverride() {
168168
assertVersionProperty(this.context, "A", "C", "A");
169169
}
170170

171-
@Test
172-
void additionalProfilesViaProgrammaticallySetting() {
173-
// gh-25704
174-
SpringApplication application = new SpringApplication(Config.class);
175-
application.setWebApplicationType(WebApplicationType.NONE);
176-
application.setAdditionalProfiles("dev");
177-
this.context = application.run();
178-
assertThat(this.context.getEnvironment().acceptsProfiles(Profiles.of("dev"))).isTrue();
179-
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile");
180-
}
181-
182171
private void assertVersionProperty(ConfigurableApplicationContext context, String expectedVersion,
183172
String... expectedActiveProfiles) {
184173
assertThat(context.getEnvironment().getActiveProfiles()).isEqualTo(expectedActiveProfiles);

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

+1-22
Original file line numberDiff line numberDiff line change
@@ -1153,17 +1153,6 @@ void locationsWithWildcardFilesShouldIgnoreHiddenDirectories() {
11531153

11541154
@Test
11551155
void additionalProfilesCanBeIncludedFromProgrammaticallySetting() {
1156-
// gh-25704
1157-
SpringApplication application = new SpringApplication(Config.class);
1158-
application.setWebApplicationType(WebApplicationType.NONE);
1159-
application.setAdditionalProfiles("dev");
1160-
this.context = application.run();
1161-
// Active profile should win over default
1162-
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile");
1163-
}
1164-
1165-
@Test
1166-
void twoAdditionalProfilesCanBeIncludedFromProgrammaticallySetting() {
11671156
// gh-25704
11681157
SpringApplication application = new SpringApplication(Config.class);
11691158
application.setWebApplicationType(WebApplicationType.NONE);
@@ -1173,21 +1162,11 @@ void twoAdditionalProfilesCanBeIncludedFromProgrammaticallySetting() {
11731162
}
11741163

11751164
@Test
1176-
void includeProfilesOrder() {
1177-
SpringApplication application = new SpringApplication(Config.class);
1178-
application.setWebApplicationType(WebApplicationType.NONE);
1179-
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo");
1180-
// Before Boot 2.4 included profiles should always be first
1181-
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam");
1182-
}
1183-
1184-
@Test
1185-
void addProfilesOrder() {
1165+
void activeProfilesShouldTakePrecedenceOverAdditionalProfiles() {
11861166
SpringApplication application = new SpringApplication(Config.class);
11871167
application.setWebApplicationType(WebApplicationType.NONE);
11881168
application.setAdditionalProfiles("foo");
11891169
this.context = application.run("--spring.profiles.active=bar,spam");
1190-
// Before Boot 2.4 additional profiles should always be first
11911170
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam");
11921171
}
11931172

0 commit comments

Comments
 (0)