Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
8d70010f
Commit
8d70010f
authored
Jul 28, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add expanded profile groups to environment
Fixes gh-22605
parent
ddb4de2d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
12 deletions
+49
-12
Profiles.java
...ava/org/springframework/boot/context/config/Profiles.java
+18
-12
ConfigDataEnvironmentTests.java
...ework/boot/context/config/ConfigDataEnvironmentTests.java
+9
-0
ProfilesTests.java
...rg/springframework/boot/context/config/ProfilesTests.java
+20
-0
ConfigDataEnvironmentTests-processAndApplySetsActiveProfilesAndProfileGroups.properties
...cessAndApplySetsActiveProfilesAndProfileGroups.properties
+2
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java
View file @
8d70010f
...
@@ -62,8 +62,6 @@ public class Profiles implements Iterable<String> {
...
@@ -62,8 +62,6 @@ public class Profiles implements Iterable<String> {
private
final
List
<
String
>
defaultProfiles
;
private
final
List
<
String
>
defaultProfiles
;
private
final
List
<
String
>
acceptedProfiles
;
/**
/**
* Create a new {@link Profiles} instance based on the {@link Environment} and
* Create a new {@link Profiles} instance based on the {@link Environment} and
* {@link Binder}.
* {@link Binder}.
...
@@ -73,11 +71,19 @@ public class Profiles implements Iterable<String> {
...
@@ -73,11 +71,19 @@ public class Profiles implements Iterable<String> {
*/
*/
Profiles
(
Environment
environment
,
Binder
binder
,
Collection
<
String
>
additionalProfiles
)
{
Profiles
(
Environment
environment
,
Binder
binder
,
Collection
<
String
>
additionalProfiles
)
{
this
.
groups
=
binder
.
bind
(
"spring.profiles.group"
,
STRING_STRINGS_MAP
).
orElseGet
(
LinkedMultiValueMap:
:
new
);
this
.
groups
=
binder
.
bind
(
"spring.profiles.group"
,
STRING_STRINGS_MAP
).
orElseGet
(
LinkedMultiValueMap:
:
new
);
this
.
activeProfiles
=
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getActiveProfiles
,
this
.
activeProfiles
=
expandProfiles
(
getActivatedProfiles
(
environment
,
binder
,
additionalProfiles
));
this
.
defaultProfiles
=
expandProfiles
(
getDefaultProfiles
(
environment
,
binder
));
}
private
List
<
String
>
getActivatedProfiles
(
Environment
environment
,
Binder
binder
,
Collection
<
String
>
additionalProfiles
)
{
return
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getActiveProfiles
,
AbstractEnvironment
.
ACTIVE_PROFILES_PROPERTY_NAME
,
UNSET_ACTIVE
),
additionalProfiles
);
AbstractEnvironment
.
ACTIVE_PROFILES_PROPERTY_NAME
,
UNSET_ACTIVE
),
additionalProfiles
);
this
.
defaultProfiles
=
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getDefaultProfiles
,
}
private
List
<
String
>
getDefaultProfiles
(
Environment
environment
,
Binder
binder
)
{
return
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getDefaultProfiles
,
AbstractEnvironment
.
DEFAULT_PROFILES_PROPERTY_NAME
,
UNSET_DEFAULT
));
AbstractEnvironment
.
DEFAULT_PROFILES_PROPERTY_NAME
,
UNSET_DEFAULT
));
this
.
acceptedProfiles
=
expandAcceptedProfiles
(
this
.
activeProfiles
,
this
.
defaultProfiles
);
}
}
private
String
[]
get
(
Environment
environment
,
Binder
binder
,
Supplier
<
String
[]>
supplier
,
String
propertyName
,
private
String
[]
get
(
Environment
environment
,
Binder
binder
,
Supplier
<
String
[]>
supplier
,
String
propertyName
,
...
@@ -99,16 +105,16 @@ public class Profiles implements Iterable<String> {
...
@@ -99,16 +105,16 @@ public class Profiles implements Iterable<String> {
return
!
propertyProfiles
.
equals
(
profiles
);
return
!
propertyProfiles
.
equals
(
profiles
);
}
}
private
List
<
String
>
expand
AcceptedProfiles
(
List
<
String
>
activeProfiles
,
List
<
String
>
defaultP
rofiles
)
{
private
List
<
String
>
expand
Profiles
(
List
<
String
>
p
rofiles
)
{
Deque
<
String
>
stack
=
new
ArrayDeque
<>();
Deque
<
String
>
stack
=
new
ArrayDeque
<>();
asReversedList
(
(!
activeProfiles
.
isEmpty
())
?
activeProfiles
:
defaultP
rofiles
).
forEach
(
stack:
:
push
);
asReversedList
(
p
rofiles
).
forEach
(
stack:
:
push
);
Set
<
String
>
accept
edProfiles
=
new
LinkedHashSet
<>();
Set
<
String
>
expand
edProfiles
=
new
LinkedHashSet
<>();
while
(!
stack
.
isEmpty
())
{
while
(!
stack
.
isEmpty
())
{
String
current
=
stack
.
pop
();
String
current
=
stack
.
pop
();
accept
edProfiles
.
add
(
current
);
expand
edProfiles
.
add
(
current
);
asReversedList
(
this
.
groups
.
get
(
current
)).
forEach
(
stack:
:
push
);
asReversedList
(
this
.
groups
.
get
(
current
)).
forEach
(
stack:
:
push
);
}
}
return
asUniqueItemList
(
StringUtils
.
toStringArray
(
accept
edProfiles
));
return
asUniqueItemList
(
StringUtils
.
toStringArray
(
expand
edProfiles
));
}
}
private
List
<
String
>
asReversedList
(
List
<
String
>
list
)
{
private
List
<
String
>
asReversedList
(
List
<
String
>
list
)
{
...
@@ -161,7 +167,7 @@ public class Profiles implements Iterable<String> {
...
@@ -161,7 +167,7 @@ public class Profiles implements Iterable<String> {
* @return the accepted profiles
* @return the accepted profiles
*/
*/
public
List
<
String
>
getAccepted
()
{
public
List
<
String
>
getAccepted
()
{
return
this
.
accepted
Profiles
;
return
(!
this
.
activeProfiles
.
isEmpty
())
?
this
.
activeProfiles
:
this
.
default
Profiles
;
}
}
/**
/**
...
@@ -170,7 +176,7 @@ public class Profiles implements Iterable<String> {
...
@@ -170,7 +176,7 @@ public class Profiles implements Iterable<String> {
* @return if the profile is active
* @return if the profile is active
*/
*/
public
boolean
isAccepted
(
String
profile
)
{
public
boolean
isAccepted
(
String
profile
)
{
return
this
.
acceptedProfiles
.
contains
(
profile
);
return
getAccepted
()
.
contains
(
profile
);
}
}
@Override
@Override
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java
View file @
8d70010f
...
@@ -174,6 +174,15 @@ class ConfigDataEnvironmentTests {
...
@@ -174,6 +174,15 @@ class ConfigDataEnvironmentTests {
assertThat
(
this
.
environment
.
getActiveProfiles
()).
containsExactly
(
"one"
,
"two"
,
"three"
);
assertThat
(
this
.
environment
.
getActiveProfiles
()).
containsExactly
(
"one"
,
"two"
,
"three"
);
}
}
@Test
void
processAndApplySetsActiveProfilesAndProfileGroups
(
TestInfo
info
)
{
this
.
environment
.
setProperty
(
"spring.config.location"
,
getConfigLocation
(
info
));
ConfigDataEnvironment
configDataEnvironment
=
new
ConfigDataEnvironment
(
this
.
logFactory
,
this
.
environment
,
this
.
resourceLoader
,
this
.
additionalProfiles
);
configDataEnvironment
.
processAndApply
();
assertThat
(
this
.
environment
.
getActiveProfiles
()).
containsExactly
(
"one"
,
"four"
,
"five"
,
"two"
,
"three"
);
}
@Test
@Test
@Disabled
(
"Disabled until spring.profiles suppport is dropped"
)
@Disabled
(
"Disabled until spring.profiles suppport is dropped"
)
void
processAndApplyWhenHasInvalidPropertyThrowsException
()
{
void
processAndApplyWhenHasInvalidPropertyThrowsException
()
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ProfilesTests.java
View file @
8d70010f
...
@@ -114,6 +114,16 @@ class ProfilesTests {
...
@@ -114,6 +114,16 @@ class ProfilesTests {
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
}
}
@Test
void
getActiveWithProfileGroups
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
environment
.
setProperty
(
"spring.profiles.active"
,
"a,b,c"
);
environment
.
setProperty
(
"spring.profiles.group.a"
,
"d,e"
);
Binder
binder
=
Binder
.
get
(
environment
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"d"
,
"e"
,
"b"
,
"c"
);
}
@Test
@Test
void
getActiveWhenHasAdditionalIncludesAdditional
()
{
void
getActiveWhenHasAdditionalIncludesAdditional
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
MockEnvironment
environment
=
new
MockEnvironment
();
...
@@ -189,6 +199,16 @@ class ProfilesTests {
...
@@ -189,6 +199,16 @@ class ProfilesTests {
assertThat
(
profiles
.
getDefault
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
assertThat
(
profiles
.
getDefault
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
}
}
@Test
void
getDefaultWithProfileGroups
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
environment
.
setProperty
(
"spring.profiles.default"
,
"a,b,c"
);
environment
.
setProperty
(
"spring.profiles.group.a"
,
"d,e"
);
Binder
binder
=
Binder
.
get
(
environment
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getDefault
()).
containsExactly
(
"a"
,
"d"
,
"e"
,
"b"
,
"c"
);
}
@Test
@Test
void
getDefaultWhenEnvironmentProfilesInBindNotationAndEnvironmentPropertyReturnsEnvironmentProfiles
()
{
void
getDefaultWhenEnvironmentProfilesInBindNotationAndEnvironmentPropertyReturnsEnvironmentProfiles
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
MockEnvironment
environment
=
new
MockEnvironment
();
...
...
spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplySetsActiveProfilesAndProfileGroups.properties
0 → 100644
View file @
8d70010f
spring.profiles.active
=
one,two,three
spring.profiles.group.one
=
four,five
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment