Skip to content

Commit 86423c9

Browse files
committed
Merge branch 'gh-26461'
Closes gh-26461
2 parents 9c6b1b6 + 8807f94 commit 86423c9

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -227,15 +227,15 @@ public String[] getActiveProfiles() {
227227
/**
228228
* Return the set of active profiles as explicitly set through
229229
* {@link #setActiveProfiles} or if the current set of active profiles
230-
* is empty, check for the presence of the {@value #ACTIVE_PROFILES_PROPERTY_NAME}
231-
* property and assign its value to the set of active profiles.
230+
* is empty, check for the presence of {@link #doGetActiveProfilesProperty()}
231+
* and assign its value to the set of active profiles.
232232
* @see #getActiveProfiles()
233-
* @see #ACTIVE_PROFILES_PROPERTY_NAME
233+
* @see #doGetActiveProfilesProperty()
234234
*/
235235
protected Set<String> doGetActiveProfiles() {
236236
synchronized (this.activeProfiles) {
237237
if (this.activeProfiles.isEmpty()) {
238-
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
238+
String profiles = doGetActiveProfilesProperty();
239239
if (StringUtils.hasText(profiles)) {
240240
setActiveProfiles(StringUtils.commaDelimitedListToStringArray(
241241
StringUtils.trimAllWhitespace(profiles)));
@@ -245,6 +245,15 @@ protected Set<String> doGetActiveProfiles() {
245245
}
246246
}
247247

248+
/**
249+
* Return the property value for the active profiles.
250+
* @since 5.3.4
251+
* @see #ACTIVE_PROFILES_PROPERTY_NAME
252+
*/
253+
protected String doGetActiveProfilesProperty() {
254+
return getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
255+
}
256+
248257
@Override
249258
public void setActiveProfiles(String... profiles) {
250259
Assert.notNull(profiles, "Profile array must not be null");
@@ -282,18 +291,17 @@ public String[] getDefaultProfiles() {
282291
* Return the set of default profiles explicitly set via
283292
* {@link #setDefaultProfiles(String...)} or if the current set of default profiles
284293
* consists only of {@linkplain #getReservedDefaultProfiles() reserved default
285-
* profiles}, then check for the presence of the
286-
* {@value #DEFAULT_PROFILES_PROPERTY_NAME} property and assign its value (if any)
287-
* to the set of default profiles.
294+
* profiles}, then check for the presence of {@link #doGetActiveProfilesProperty()}
295+
* and assign its value (if any) to the set of default profiles.
288296
* @see #AbstractEnvironment()
289297
* @see #getDefaultProfiles()
290-
* @see #DEFAULT_PROFILES_PROPERTY_NAME
291298
* @see #getReservedDefaultProfiles()
299+
* @see #doGetDefaultProfilesProperty()
292300
*/
293301
protected Set<String> doGetDefaultProfiles() {
294302
synchronized (this.defaultProfiles) {
295303
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) {
296-
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
304+
String profiles = doGetDefaultProfilesProperty();
297305
if (StringUtils.hasText(profiles)) {
298306
setDefaultProfiles(StringUtils.commaDelimitedListToStringArray(
299307
StringUtils.trimAllWhitespace(profiles)));
@@ -303,6 +311,15 @@ protected Set<String> doGetDefaultProfiles() {
303311
}
304312
}
305313

314+
/**
315+
* Return the property value for the default profiles.
316+
* @since 5.3.4
317+
* @see #DEFAULT_PROFILES_PROPERTY_NAME
318+
*/
319+
protected String doGetDefaultProfilesProperty() {
320+
return getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
321+
}
322+
306323
/**
307324
* Specify the set of profiles to be made active by default if no other profiles
308325
* are explicitly made active through {@link #setActiveProfiles}.

spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
2123
import java.util.Set;
2224

2325
import org.junit.jupiter.api.Test;
@@ -105,6 +107,28 @@ protected Set<String> getReservedDefaultProfiles() {
105107
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isFalse();
106108
}
107109

110+
@Test
111+
public void withNoProfileProperties() {
112+
ConfigurableEnvironment env = new AbstractEnvironment() {
113+
@Override
114+
protected String doGetActiveProfilesProperty() {
115+
return null;
116+
}
117+
118+
@Override
119+
protected String doGetDefaultProfilesProperty() {
120+
return null;
121+
}
122+
};
123+
Map<String, Object> values = new LinkedHashMap<>();
124+
values.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "a,b,c");
125+
values.put(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "d,e,f");
126+
PropertySource<?> propertySource = new MapPropertySource("test", values);
127+
env.getPropertySources().addFirst(propertySource);
128+
assertThat(env.getActiveProfiles()).isEmpty();
129+
assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
130+
}
131+
108132
private Profiles defaultProfile() {
109133
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
110134
}

0 commit comments

Comments
 (0)