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
31a5e85a
Commit
31a5e85a
authored
Dec 07, 2018
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Perform placeholder resolution in test env post processor
Fixes gh-15354
parent
4c6c07dd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
10 deletions
+73
-10
SpringBootTestRandomPortEnvironmentPostProcessor.java
...web/SpringBootTestRandomPortEnvironmentPostProcessor.java
+22
-10
SpringBootTestRandomPortEnvironmentPostProcessorTests.java
...pringBootTestRandomPortEnvironmentPostProcessorTests.java
+51
-0
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java
View file @
31a5e85a
...
@@ -19,7 +19,7 @@ import java.util.Objects;
...
@@ -19,7 +19,7 @@ import java.util.Objects;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.env.EnvironmentPostProcessor
;
import
org.springframework.boot.env.EnvironmentPostProcessor
;
import
org.springframework.core.convert.Conversion
Service
;
import
org.springframework.core.convert.Conversion
FailedException
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.PropertySource
;
...
@@ -46,8 +46,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
...
@@ -46,8 +46,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
SpringApplication
application
)
{
SpringApplication
application
)
{
MapPropertySource
source
=
(
MapPropertySource
)
environment
.
getPropertySources
()
MapPropertySource
source
=
(
MapPropertySource
)
environment
.
getPropertySources
()
.
get
(
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
);
.
get
(
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
);
if
(
source
==
null
if
(
source
==
null
||
isTestServerPortFixed
(
source
,
environment
)
||
isTestServerPortFixed
(
source
,
environment
.
getConversionService
())
||
isTestManagementPortConfigured
(
source
))
{
||
isTestManagementPortConfigured
(
source
))
{
return
;
return
;
}
}
...
@@ -67,9 +66,9 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
...
@@ -67,9 +66,9 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
}
}
private
boolean
isTestServerPortFixed
(
MapPropertySource
source
,
private
boolean
isTestServerPortFixed
(
MapPropertySource
source
,
Con
versionService
conversionService
)
{
Con
figurableEnvironment
environment
)
{
return
!
Integer
.
valueOf
(
0
)
.
equals
(
return
!
Integer
.
valueOf
(
0
)
getPropertyAsInteger
(
source
,
SERVER_PORT_PROPERTY
,
conversionService
));
.
equals
(
getPropertyAsInteger
(
source
,
SERVER_PORT_PROPERTY
,
environment
));
}
}
private
boolean
isTestManagementPortConfigured
(
PropertySource
<?>
source
)
{
private
boolean
isTestManagementPortConfigured
(
PropertySource
<?>
source
)
{
...
@@ -81,13 +80,12 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
...
@@ -81,13 +80,12 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
return
environment
.
getPropertySources
().
stream
()
return
environment
.
getPropertySources
().
stream
()
.
filter
((
source
)
->
!
source
.
getName
().
equals
(
.
filter
((
source
)
->
!
source
.
getName
().
equals
(
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
))
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
))
.
map
((
source
)
->
getPropertyAsInteger
(
source
,
property
,
.
map
((
source
)
->
getPropertyAsInteger
(
source
,
property
,
environment
))
environment
.
getConversionService
()))
.
filter
(
Objects:
:
nonNull
).
findFirst
().
orElse
(
defaultValue
);
.
filter
(
Objects:
:
nonNull
).
findFirst
().
orElse
(
defaultValue
);
}
}
private
Integer
getPropertyAsInteger
(
PropertySource
<?>
source
,
String
property
,
private
Integer
getPropertyAsInteger
(
PropertySource
<?>
source
,
String
property
,
Con
versionService
conversionService
)
{
Con
figurableEnvironment
environment
)
{
Object
value
=
source
.
getProperty
(
property
);
Object
value
=
source
.
getProperty
(
property
);
if
(
value
==
null
)
{
if
(
value
==
null
)
{
return
null
;
return
null
;
...
@@ -95,7 +93,21 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
...
@@ -95,7 +93,21 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
if
(
ClassUtils
.
isAssignableValue
(
Integer
.
class
,
value
))
{
if
(
ClassUtils
.
isAssignableValue
(
Integer
.
class
,
value
))
{
return
(
Integer
)
value
;
return
(
Integer
)
value
;
}
}
return
conversionService
.
convert
(
value
,
Integer
.
class
);
try
{
return
environment
.
getConversionService
().
convert
(
value
,
Integer
.
class
);
}
catch
(
ConversionFailedException
ex
)
{
if
(
ClassUtils
.
isAssignable
(
value
.
getClass
(),
String
.
class
))
{
return
getResolvedValueIfPossible
(
environment
,
(
String
)
value
);
}
throw
ex
;
}
}
private
Integer
getResolvedValueIfPossible
(
ConfigurableEnvironment
environment
,
String
value
)
{
String
resolvedValue
=
environment
.
resolveRequiredPlaceholders
(
value
);
return
environment
.
getConversionService
().
convert
(
resolvedValue
,
Integer
.
class
);
}
}
}
}
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java
View file @
31a5e85a
...
@@ -28,6 +28,7 @@ import org.springframework.mock.env.MockEnvironment;
...
@@ -28,6 +28,7 @@ import org.springframework.mock.env.MockEnvironment;
import
org.springframework.test.context.support.TestPropertySourceUtils
;
import
org.springframework.test.context.support.TestPropertySourceUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalArgumentException
;
/**
/**
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
...
@@ -141,6 +142,56 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
...
@@ -141,6 +142,56 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
assertThat
(
this
.
environment
.
getProperty
(
"management.server.port"
)).
isEqualTo
(
"0"
);
assertThat
(
this
.
environment
.
getProperty
(
"management.server.port"
)).
isEqualTo
(
"0"
);
}
}
@Test
public
void
postProcessWhenManagementServerPortPlaceholderPresentShouldResolvePlaceholder
()
{
addTestPropertySource
(
"0"
,
null
);
MapPropertySource
testPropertySource
=
(
MapPropertySource
)
this
.
propertySources
.
get
(
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
);
testPropertySource
.
getSource
().
put
(
"port"
,
"9090"
);
this
.
propertySources
.
addLast
(
new
MapPropertySource
(
"other"
,
Collections
.
singletonMap
(
"management.server.port"
,
"${port}"
)));
this
.
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
assertThat
(
this
.
environment
.
getProperty
(
"server.port"
)).
isEqualTo
(
"0"
);
assertThat
(
this
.
environment
.
getProperty
(
"management.server.port"
)).
isEqualTo
(
"0"
);
}
@Test
public
void
postProcessWhenManagementServerPortPlaceholderAbsentShouldFail
()
{
addTestPropertySource
(
"0"
,
null
);
this
.
propertySources
.
addLast
(
new
MapPropertySource
(
"other"
,
Collections
.
singletonMap
(
"management.server.port"
,
"${port}"
)));
assertThatIllegalArgumentException
().
isThrownBy
(
()
->
this
.
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
))
.
withMessage
(
"Could not resolve placeholder 'port' in value \"${port}\""
);
}
@Test
public
void
postProcessWhenServerPortPlaceholderPresentShouldResolvePlaceholder
()
{
addTestPropertySource
(
"0"
,
null
);
MapPropertySource
testPropertySource
=
(
MapPropertySource
)
this
.
propertySources
.
get
(
TestPropertySourceUtils
.
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
);
testPropertySource
.
getSource
().
put
(
"port"
,
"8080"
);
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"server.port"
,
"${port}"
);
source
.
put
(
"management.server.port"
,
"9090"
);
this
.
propertySources
.
addLast
(
new
MapPropertySource
(
"other"
,
source
));
this
.
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
assertThat
(
this
.
environment
.
getProperty
(
"server.port"
)).
isEqualTo
(
"0"
);
assertThat
(
this
.
environment
.
getProperty
(
"management.server.port"
)).
isEqualTo
(
"0"
);
}
@Test
public
void
postProcessWhenServerPortPlaceholderAbsentShouldFail
()
{
addTestPropertySource
(
"0"
,
null
);
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"server.port"
,
"${port}"
);
source
.
put
(
"management.server.port"
,
"9090"
);
this
.
propertySources
.
addLast
(
new
MapPropertySource
(
"other"
,
source
));
assertThatIllegalArgumentException
().
isThrownBy
(
()
->
this
.
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
))
.
withMessage
(
"Could not resolve placeholder 'port' in value \"${port}\""
);
}
private
void
addTestPropertySource
(
String
serverPort
,
String
managementPort
)
{
private
void
addTestPropertySource
(
String
serverPort
,
String
managementPort
)
{
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"server.port"
,
serverPort
);
source
.
put
(
"server.port"
,
serverPort
);
...
...
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