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
ebb3f5f2
Commit
ebb3f5f2
authored
Jun 03, 2020
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.2.x' into 2.3.x
Closes gh-21660
parents
2dc8048d
4fc0dec6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
16 deletions
+103
-16
SpringConfigurationPropertySource.java
.../properties/source/SpringConfigurationPropertySource.java
+12
-6
RandomValuePropertySource.java
...g/springframework/boot/env/RandomValuePropertySource.java
+4
-4
SpringConfigurationPropertySourceTests.java
...erties/source/SpringConfigurationPropertySourceTests.java
+87
-6
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java
View file @
ebb3f5f2
...
@@ -19,6 +19,7 @@ package org.springframework.boot.context.properties.source;
...
@@ -19,6 +19,7 @@ package org.springframework.boot.context.properties.source;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.Random
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.boot.origin.PropertySourceOrigin
;
import
org.springframework.boot.origin.PropertySourceOrigin
;
import
org.springframework.core.env.EnumerablePropertySource
;
import
org.springframework.core.env.EnumerablePropertySource
;
...
@@ -51,8 +52,6 @@ import org.springframework.util.Assert;
...
@@ -51,8 +52,6 @@ import org.springframework.util.Assert;
*/
*/
class
SpringConfigurationPropertySource
implements
ConfigurationPropertySource
{
class
SpringConfigurationPropertySource
implements
ConfigurationPropertySource
{
private
static
final
ConfigurationPropertyName
RANDOM
=
ConfigurationPropertyName
.
of
(
"random"
);
private
static
final
PropertyMapper
[]
DEFAULT_MAPPERS
=
{
DefaultPropertyMapper
.
INSTANCE
};
private
static
final
PropertyMapper
[]
DEFAULT_MAPPERS
=
{
DefaultPropertyMapper
.
INSTANCE
};
private
static
final
PropertyMapper
[]
SYSTEM_ENVIRONMENT_MAPPERS
=
{
SystemEnvironmentPropertyMapper
.
INSTANCE
,
private
static
final
PropertyMapper
[]
SYSTEM_ENVIRONMENT_MAPPERS
=
{
SystemEnvironmentPropertyMapper
.
INSTANCE
,
...
@@ -97,14 +96,21 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
...
@@ -97,14 +96,21 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
@Override
@Override
public
ConfigurationPropertyState
containsDescendantOf
(
ConfigurationPropertyName
name
)
{
public
ConfigurationPropertyState
containsDescendantOf
(
ConfigurationPropertyName
name
)
{
if
(
getPropertySource
().
getSource
()
instanceof
Random
)
{
PropertySource
<?>
source
=
getPropertySource
();
return
containsDescendantOfForRandom
(
name
);
if
(
source
.
getSource
()
instanceof
Random
)
{
return
containsDescendantOfForRandom
(
"random"
,
name
);
}
if
(
source
.
getSource
()
instanceof
PropertySource
<?>
&&
((
PropertySource
<?>)
source
.
getSource
()).
getSource
()
instanceof
Random
)
{
// Assume wrapped random sources use the source name as the prefix
return
containsDescendantOfForRandom
(
source
.
getName
(),
name
);
}
}
return
ConfigurationPropertyState
.
UNKNOWN
;
return
ConfigurationPropertyState
.
UNKNOWN
;
}
}
private
static
ConfigurationPropertyState
containsDescendantOfForRandom
(
ConfigurationPropertyName
name
)
{
private
static
ConfigurationPropertyState
containsDescendantOfForRandom
(
String
prefix
,
if
(
RANDOM
.
isAncestorOf
(
name
)
||
name
.
equals
(
RANDOM
))
{
ConfigurationPropertyName
name
)
{
if
(
name
.
getNumberOfElements
()
>
1
&&
name
.
getElement
(
0
,
Form
.
DASHED
).
equals
(
prefix
))
{
return
ConfigurationPropertyState
.
PRESENT
;
return
ConfigurationPropertyState
.
PRESENT
;
}
}
return
ConfigurationPropertyState
.
ABSENT
;
return
ConfigurationPropertyState
.
ABSENT
;
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java
View file @
ebb3f5f2
...
@@ -62,14 +62,14 @@ public class RandomValuePropertySource extends PropertySource<Random> {
...
@@ -62,14 +62,14 @@ public class RandomValuePropertySource extends PropertySource<Random> {
private
static
final
Log
logger
=
LogFactory
.
getLog
(
RandomValuePropertySource
.
class
);
private
static
final
Log
logger
=
LogFactory
.
getLog
(
RandomValuePropertySource
.
class
);
public
RandomValuePropertySource
(
String
name
)
{
super
(
name
,
new
Random
());
}
public
RandomValuePropertySource
()
{
public
RandomValuePropertySource
()
{
this
(
RANDOM_PROPERTY_SOURCE_NAME
);
this
(
RANDOM_PROPERTY_SOURCE_NAME
);
}
}
public
RandomValuePropertySource
(
String
name
)
{
super
(
name
,
new
Random
());
}
@Override
@Override
public
Object
getProperty
(
String
name
)
{
public
Object
getProperty
(
String
name
)
{
if
(!
name
.
startsWith
(
PREFIX
))
{
if
(!
name
.
startsWith
(
PREFIX
))
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourceTests.java
View file @
ebb3f5f2
...
@@ -146,24 +146,105 @@ class SpringConfigurationPropertySourceTests {
...
@@ -146,24 +146,105 @@ class SpringConfigurationPropertySourceTests {
void
containsDescendantOfWhenRandomSourceAndRandomPropertyReturnsPresent
()
{
void
containsDescendantOfWhenRandomSourceAndRandomPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomValuePropertySource
());
.
from
(
new
RandomValuePropertySource
());
assertThat
(
source
.
containsDescendantOf
(
ConfigurationPropertyName
.
of
(
"random"
)))
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"random"
);
.
isEqualTo
(
ConfigurationPropertyState
.
PRESENT
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
}
@Test
@Test
void
containsDescendantOfWhenRandomSourceAndRandomPrefixedPropertyReturnsPresent
()
{
void
containsDescendantOfWhenRandomSourceAndRandomPrefixedPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomValuePropertySource
());
.
from
(
new
RandomValuePropertySource
());
assertThat
(
source
.
containsDescendantOf
(
ConfigurationPropertyName
.
of
(
"random.something"
)))
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"random.int"
);
.
isEqualTo
(
ConfigurationPropertyState
.
PRESENT
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
PRESENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNotNull
();
}
@Test
void
containsDescendantOfWhenRandomSourceWithDifferentNameAndRandomPrefixedPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomValuePropertySource
(
"different"
));
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"random.int"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
PRESENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNotNull
();
}
}
@Test
@Test
void
containsDescendantOfWhenRandomSourceAndNonRandomPropertyReturnsAbsent
()
{
void
containsDescendantOfWhenRandomSourceAndNonRandomPropertyReturnsAbsent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomValuePropertySource
());
.
from
(
new
RandomValuePropertySource
());
assertThat
(
source
.
containsDescendantOf
(
ConfigurationPropertyName
.
of
(
"abandon.something"
)))
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"abandon.int"
);
.
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
@Test
void
containsDescendantOfWhenWrappedRandomSourceAndRandomPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomWrapperPropertySource
());
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"cachedrandom"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
@Test
void
containsDescendantOfWhenWrappedRandomSourceAndRandomPrefixedPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomWrapperPropertySource
());
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"cachedrandom.something.int"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
@Test
void
containsDescendantOfWhenWrappedRandomSourceWithMatchingNameAndRandomPrefixedPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomWrapperPropertySource
(
"cachedrandom"
));
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"cachedrandom.something.int"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
PRESENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNotNull
();
}
@Test
void
containsDescendantOfWhenWrappedRandomSourceAndRandomDashPrefixedPropertyReturnsPresent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomWrapperPropertySource
());
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"cached-random.something.int"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
@Test
void
containsDescendantOfWhenWrappedRandomSourceAndNonRandomPropertyReturnsAbsent
()
{
SpringConfigurationPropertySource
source
=
SpringConfigurationPropertySource
.
from
(
new
RandomWrapperPropertySource
());
ConfigurationPropertyName
name
=
ConfigurationPropertyName
.
of
(
"abandon.something.int"
);
assertThat
(
source
.
containsDescendantOf
(
name
)).
isEqualTo
(
ConfigurationPropertyState
.
ABSENT
);
assertThat
(
source
.
getConfigurationProperty
(
name
)).
isNull
();
}
static
class
RandomWrapperPropertySource
extends
PropertySource
<
RandomValuePropertySource
>
{
private
final
String
prefix
;
RandomWrapperPropertySource
()
{
this
(
"cachedRandom"
);
}
RandomWrapperPropertySource
(
String
name
)
{
super
(
name
,
new
RandomValuePropertySource
());
this
.
prefix
=
name
+
"."
;
}
@Override
public
Object
getProperty
(
String
name
)
{
name
=
name
.
toLowerCase
();
if
(!
name
.
startsWith
(
this
.
prefix
))
{
return
null
;
}
return
getSource
().
getProperty
(
"random."
+
name
.
substring
(
this
.
prefix
.
length
()));
}
}
}
/**
/**
...
...
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