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
acaef9c9
Commit
acaef9c9
authored
Jun 27, 2017
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make origin aware system property source
Closes gh-9252
parent
7275c879
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
162 deletions
+57
-162
OriginTrackedSystemPropertySource.java
...framework/boot/env/OriginTrackedSystemPropertySource.java
+0
-64
SystemEnvironmentPropertySourceEnvironmentPostProcessor.java
...temEnvironmentPropertySourceEnvironmentPostProcessor.java
+24
-11
OriginTrackedSystemPropertySourceTests.java
...work/boot/env/OriginTrackedSystemPropertySourceTests.java
+0
-78
SystemEnvironmentPropertySourceEnvironmentPostProcessorTests.java
...vironmentPropertySourceEnvironmentPostProcessorTests.java
+33
-9
No files found.
spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedSystemPropertySource.java
deleted
100644 → 0
View file @
7275c879
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
env
;
import
java.util.Map
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.boot.origin.OriginLookup
;
import
org.springframework.boot.origin.OriginTrackedValue
;
import
org.springframework.core.env.SystemEnvironmentPropertySource
;
/**
* {@link SystemEnvironmentPropertySource} and {@link OriginLookup} backed by a map
* containing {@link OriginTrackedValue OriginTrackedValues}.
*
* @author Madhura Bhave
* @since 2.0.0
*/
public
class
OriginTrackedSystemPropertySource
extends
SystemEnvironmentPropertySource
implements
OriginLookup
<
String
>
{
/**
* Create a new {@code OriginTrackedSystemPropertySource} with the given name and
* delegating to the given {@code MapPropertySource}.
* @param name the property name
* @param source the underlying map
*/
public
OriginTrackedSystemPropertySource
(
String
name
,
Map
<
String
,
Object
>
source
)
{
super
(
name
,
source
);
}
@Override
public
Object
getProperty
(
String
name
)
{
Object
value
=
super
.
getProperty
(
name
);
if
(
value
instanceof
OriginTrackedValue
)
{
return
((
OriginTrackedValue
)
value
).
getValue
();
}
return
value
;
}
@Override
public
Origin
getOrigin
(
String
key
)
{
Object
value
=
super
.
getProperty
(
key
);
if
(
value
instanceof
OriginTrackedValue
)
{
return
((
OriginTrackedValue
)
value
).
getOrigin
();
}
return
null
;
}
}
spring-boot/src/main/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessor.java
View file @
acaef9c9
...
...
@@ -16,12 +16,11 @@
package
org
.
springframework
.
boot
.
env
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.origin.OriginTrackedValue
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.boot.origin.OriginLookup
;
import
org.springframework.boot.origin.SystemEnvironmentOrigin
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.env.ConfigurableEnvironment
;
...
...
@@ -32,7 +31,7 @@ import org.springframework.core.env.SystemEnvironmentPropertySource;
/**
* An {@link EnvironmentPostProcessor} that replaces the systemEnvironment
* {@link SystemEnvironmentPropertySource} with an
* {@link Origin
TrackedSystem
PropertySource} that can track the
* {@link Origin
AwareSystemEnvironment
PropertySource} that can track the
* {@link SystemEnvironmentOrigin} for every system environment property.
*
* @author Madhura Bhave
...
...
@@ -63,17 +62,11 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor
@SuppressWarnings
(
"unchecked"
)
private
void
replacePropertySource
(
ConfigurableEnvironment
environment
,
String
sourceName
,
PropertySource
<?>
propertySource
)
{
if
(
propertySource
.
getSource
()
instanceof
Map
)
{
Map
<
String
,
Object
>
originalSource
=
(
Map
<
String
,
Object
>)
propertySource
.
getSource
();
Map
<
String
,
Object
>
originTrackedSource
=
new
LinkedHashMap
<>(
originalSource
);
originTrackedSource
.
entrySet
().
forEach
(
e
->
e
.
setValue
(
OriginTrackedValue
.
of
(
e
.
getValue
(),
new
SystemEnvironmentOrigin
(
e
.
getKey
()))));
OriginTrackedSystemPropertySource
source
=
new
OriginTrackedSystemPropertySource
(
sourceName
,
Collections
.
unmodifiableMap
(
originTrackedSource
));
SystemEnvironmentPropertySource
source
=
new
OriginAwareSystemEnvironmentPropertySource
(
sourceName
,
originalSource
);
environment
.
getPropertySources
().
replace
(
sourceName
,
source
);
}
}
@Override
public
int
getOrder
()
{
...
...
@@ -84,4 +77,24 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor
this
.
order
=
order
;
}
/**
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
*/
protected
static
class
OriginAwareSystemEnvironmentPropertySource
extends
SystemEnvironmentPropertySource
implements
OriginLookup
<
String
>
{
OriginAwareSystemEnvironmentPropertySource
(
String
name
,
Map
<
String
,
Object
>
source
)
{
super
(
name
,
source
);
}
@Override
public
Origin
getOrigin
(
String
key
)
{
String
property
=
resolvePropertyName
(
key
);
if
(
super
.
containsProperty
(
property
))
{
return
new
SystemEnvironmentOrigin
(
property
);
}
return
null
;
}
}
}
spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedSystemPropertySourceTests.java
deleted
100644 → 0
View file @
7275c879
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
env
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
org.junit.Test
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.boot.origin.OriginTrackedValue
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for {@link OriginTrackedSystemPropertySource}.
*
* @author Madhura Bhave
*/
public
class
OriginTrackedSystemPropertySourceTests
{
private
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<>();
private
OriginTrackedSystemPropertySource
source
=
new
OriginTrackedSystemPropertySource
(
"test"
,
this
.
map
);
private
Origin
origin
=
mock
(
Origin
.
class
);
@Test
public
void
getPropertyWhenMissingShouldReturnNull
()
throws
Exception
{
assertThat
(
this
.
source
.
getProperty
(
"test"
)).
isNull
();
}
@Test
public
void
getPropertyWhenNonTrackedShouldReturnValue
()
throws
Exception
{
this
.
map
.
put
(
"test"
,
"foo"
);
assertThat
(
this
.
source
.
getProperty
(
"test"
)).
isEqualTo
(
"foo"
);
}
@Test
public
void
getPropertyWhenTrackedShouldReturnValue
()
throws
Exception
{
this
.
map
.
put
(
"test"
,
OriginTrackedValue
.
of
(
"foo"
,
this
.
origin
));
assertThat
(
this
.
source
.
getProperty
(
"test"
)).
isEqualTo
(
"foo"
);
}
@Test
public
void
getPropertyOriginWhenMissingShouldReturnNull
()
throws
Exception
{
assertThat
(
this
.
source
.
getOrigin
(
"test"
)).
isNull
();
}
@Test
public
void
getPropertyOriginWhenNonTrackedShouldReturnNull
()
throws
Exception
{
this
.
map
.
put
(
"test"
,
"foo"
);
assertThat
(
this
.
source
.
getOrigin
(
"test"
)).
isNull
();
}
@Test
public
void
getPropertyOriginWhenTrackedShouldReturnOrigin
()
throws
Exception
{
this
.
map
.
put
(
"test"
,
OriginTrackedValue
.
of
(
"foo"
,
this
.
origin
));
assertThat
(
this
.
source
.
getOrigin
(
"test"
)).
isEqualTo
(
this
.
origin
);
}
}
spring-boot/src/test/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessorTests.java
View file @
acaef9c9
...
...
@@ -16,16 +16,18 @@
package
org
.
springframework
.
boot
.
env
;
import
java.util.Collections
;
import
java.util.Map
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.
origin.OriginTrackedValu
e
;
import
org.springframework.boot.
env.SystemEnvironmentPropertySourceEnvironmentPostProcessor.OriginAwareSystemEnvironmentPropertySourc
e
;
import
org.springframework.boot.origin.SystemEnvironmentOrigin
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.core.env.SystemEnvironmentPropertySource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -50,27 +52,49 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
PropertySource
<?>
replaced
=
this
.
environment
.
getPropertySources
()
.
get
(
"systemEnvironment"
);
assertThat
(
replaced
).
isInstanceOf
(
Origin
TrackedSystem
PropertySource
.
class
);
assertThat
(
replaced
).
isInstanceOf
(
Origin
AwareSystemEnvironment
PropertySource
.
class
);
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
replacedPropertySourceShould
HaveOriginTrackedValues
()
throws
Exception
{
public
void
replacedPropertySourceShould
BeOriginAware
()
throws
Exception
{
SystemEnvironmentPropertySourceEnvironmentPostProcessor
postProcessor
=
new
SystemEnvironmentPropertySourceEnvironmentPostProcessor
();
PropertySource
<?>
original
=
this
.
environment
.
getPropertySources
()
.
get
(
"systemEnvironment"
);
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
PropertySource
<?>
replaced
=
this
.
environment
.
getPropertySources
()
OriginAwareSystemEnvironmentPropertySource
replaced
=
(
OriginAwareSystemEnvironmentPropertySource
)
this
.
environment
.
getPropertySources
()
.
get
(
"systemEnvironment"
);
Map
<
String
,
Object
>
originalMap
=
(
Map
<
String
,
Object
>)
original
.
getSource
();
Map
<
String
,
O
riginTrackedValue
>
replacedMap
=
(
Map
<
String
,
OriginTrackedValue
>)
replaced
Map
<
String
,
O
bject
>
replacedMap
=
replaced
.
getSource
();
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
originalMap
.
entrySet
())
{
OriginTrackedValue
actual
=
replacedMap
.
get
(
entry
.
getKey
());
assertThat
(
actual
.
getValue
()).
isEqualTo
(
entry
.
getValue
());
assertThat
(
actual
.
getOrigin
())
.
isEqualTo
(
new
SystemEnvironmentOrigin
(
entry
.
getKey
()));
Object
actual
=
replacedMap
.
get
(
entry
.
getKey
());
assertThat
(
actual
).
isEqualTo
(
entry
.
getValue
());
assertThat
(
replaced
.
getOrigin
(
entry
.
getKey
())).
isInstanceOf
(
SystemEnvironmentOrigin
.
class
);
}
}
@Test
public
void
replacedPropertySourceWhenPropertyAbsentShouldReturnNullOrigin
()
throws
Exception
{
SystemEnvironmentPropertySourceEnvironmentPostProcessor
postProcessor
=
new
SystemEnvironmentPropertySourceEnvironmentPostProcessor
();
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
OriginAwareSystemEnvironmentPropertySource
replaced
=
(
OriginAwareSystemEnvironmentPropertySource
)
this
.
environment
.
getPropertySources
()
.
get
(
"systemEnvironment"
);
assertThat
(
replaced
.
getOrigin
(
"NON_EXISTENT"
)).
isNull
();
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
replacedPropertySourceShouldResolveProperty
()
throws
Exception
{
SystemEnvironmentPropertySourceEnvironmentPostProcessor
postProcessor
=
new
SystemEnvironmentPropertySourceEnvironmentPostProcessor
();
Map
<
String
,
Object
>
source
=
Collections
.
singletonMap
(
"FOO_BAR_BAZ"
,
"hello"
);
this
.
environment
.
getPropertySources
().
replace
(
"systemEnvironment"
,
new
SystemEnvironmentPropertySource
(
"systemEnvironment"
,
source
));
postProcessor
.
postProcessEnvironment
(
this
.
environment
,
null
);
OriginAwareSystemEnvironmentPropertySource
replaced
=
(
OriginAwareSystemEnvironmentPropertySource
)
this
.
environment
.
getPropertySources
()
.
get
(
"systemEnvironment"
);
SystemEnvironmentOrigin
origin
=
(
SystemEnvironmentOrigin
)
replaced
.
getOrigin
(
"foo.bar.baz"
);
assertThat
(
origin
.
getProperty
()).
isEqualTo
(
"FOO_BAR_BAZ"
);
assertThat
(
replaced
.
getProperty
(
"foo.bar.baz"
)).
isEqualTo
(
"hello"
);
}
}
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