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
6ae02196
Commit
6ae02196
authored
Nov 12, 2015
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use TestPropertySourceUtils to convert properties
Fixes gh-4384
parent
09b5222f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
36 deletions
+19
-36
SpringApplicationContextLoader.java
...ngframework/boot/test/SpringApplicationContextLoader.java
+2
-32
SpringApplicationContextLoaderTests.java
...mework/boot/test/SpringApplicationContextLoaderTests.java
+17
-4
No files found.
spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java
View file @
6ae02196
...
...
@@ -16,8 +16,6 @@
package
org
.
springframework
.
boot
.
test
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
java.lang.annotation.Annotation
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -26,7 +24,6 @@ import java.util.LinkedHashMap;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Properties
;
import
java.util.Set
;
import
org.springframework.beans.BeanUtils
;
...
...
@@ -75,8 +72,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
*/
public
class
SpringApplicationContextLoader
extends
AbstractContextLoader
{
private
static
final
String
LINE_SEPARATOR
=
System
.
getProperty
(
"line.separator"
);
@Override
public
ApplicationContext
loadContext
(
final
MergedContextConfiguration
config
)
throws
Exception
{
...
...
@@ -146,8 +141,8 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
Map
<
String
,
Object
>
properties
=
new
LinkedHashMap
<
String
,
Object
>();
// JMX bean names will clash if the same bean is used in multiple contexts
disableJmx
(
properties
);
properties
.
putAll
(
extractEnvironmentProperties
(
config
.
getPropertySourceProperties
()));
properties
.
putAll
(
TestPropertySourceUtils
.
convertInlinedPropertiesToMap
(
config
.
getPropertySourceProperties
()));
if
(!
TestAnnotations
.
isIntegrationTest
(
config
))
{
properties
.
putAll
(
getDefaultEnvironmentProperties
());
}
...
...
@@ -158,31 +153,6 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
properties
.
put
(
"spring.jmx.enabled"
,
"false"
);
}
final
Map
<
String
,
Object
>
extractEnvironmentProperties
(
String
[]
values
)
{
// Instead of parsing the keys ourselves, we rely on standard handling
if
(
values
==
null
)
{
return
Collections
.
emptyMap
();
}
String
content
=
StringUtils
.
arrayToDelimitedString
(
values
,
LINE_SEPARATOR
);
Properties
properties
=
new
Properties
();
try
{
properties
.
load
(
new
StringReader
(
content
));
return
asMap
(
properties
);
}
catch
(
IOException
ex
)
{
throw
new
IllegalStateException
(
"Unexpected could not load properties from '"
+
content
+
"'"
,
ex
);
}
}
private
Map
<
String
,
Object
>
asMap
(
Properties
properties
)
{
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
for
(
String
name
:
properties
.
stringPropertyNames
())
{
map
.
put
(
name
,
properties
.
getProperty
(
name
));
}
return
map
;
}
private
Map
<
String
,
String
>
getDefaultEnvironmentProperties
()
{
return
Collections
.
singletonMap
(
"server.port"
,
"-1"
);
}
...
...
spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java
View file @
6ae02196
...
...
@@ -18,11 +18,13 @@ package org.springframework.boot.test;
import
java.util.Map
;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.TestContext
;
import
org.springframework.test.context.TestContextManager
;
import
org.springframework.test.context.support.TestPropertySourceUtils
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -35,8 +37,6 @@ import static org.junit.Assert.assertTrue;
*/
public
class
SpringApplicationContextLoaderTests
{
private
final
SpringApplicationContextLoader
loader
=
new
SpringApplicationContextLoader
();
@Test
public
void
environmentPropertiesSimple
()
throws
Exception
{
Map
<
String
,
Object
>
config
=
getEnvironmentProperties
(
SimpleConfig
.
class
);
...
...
@@ -72,6 +72,15 @@ public class SpringApplicationContextLoaderTests {
assertKey
(
config
,
"anotherKey"
,
"another=Value"
);
}
@Test
@Ignore
public
void
environmentPropertiesNewLineInValue
()
throws
Exception
{
// gh-4384
Map
<
String
,
Object
>
config
=
getEnvironmentProperties
(
NewLineInValue
.
class
);
assertKey
(
config
,
"key"
,
"myValue"
);
assertKey
(
config
,
"variables"
,
"foo=FOO\n bar=BAR"
);
}
private
Map
<
String
,
Object
>
getEnvironmentProperties
(
Class
<?>
testClass
)
throws
Exception
{
TestContext
context
=
new
ExposedTestContextManager
(
testClass
)
...
...
@@ -79,8 +88,8 @@ public class SpringApplicationContextLoaderTests {
new
IntegrationTestPropertiesListener
().
prepareTestInstance
(
context
);
MergedContextConfiguration
config
=
(
MergedContextConfiguration
)
ReflectionTestUtils
.
getField
(
context
,
"mergedContextConfiguration"
);
return
this
.
loader
.
extractEnvironmentProperties
(
config
.
getPropertySourceProperties
());
return
TestPropertySourceUtils
.
convertInlinedPropertiesToMap
(
config
.
getPropertySourceProperties
());
}
private
void
assertKey
(
Map
<
String
,
Object
>
actual
,
String
key
,
Object
value
)
{
...
...
@@ -108,6 +117,10 @@ public class SpringApplicationContextLoaderTests {
static
class
AnotherSeparatorInValue
{
}
@IntegrationTest
({
"key=myValue"
,
"variables=foo=FOO\n bar=BAR"
})
static
class
NewLineInValue
{
}
/**
* {@link TestContextManager} which exposes the {@link TestContext}.
*/
...
...
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