16
16
17
17
package org .springframework .boot .env ;
18
18
19
+ import java .util .Collections ;
19
20
import java .util .Map ;
20
21
21
22
import org .junit .Before ;
22
23
import org .junit .Test ;
23
24
24
- import org .springframework .boot .origin . OriginTrackedValue ;
25
+ import org .springframework .boot .env . SystemEnvironmentPropertySourceEnvironmentPostProcessor . OriginAwareSystemEnvironmentPropertySource ;
25
26
import org .springframework .boot .origin .SystemEnvironmentOrigin ;
26
27
import org .springframework .core .env .ConfigurableEnvironment ;
27
28
import org .springframework .core .env .PropertySource ;
28
29
import org .springframework .core .env .StandardEnvironment ;
30
+ import org .springframework .core .env .SystemEnvironmentPropertySource ;
29
31
30
32
import static org .assertj .core .api .Assertions .assertThat ;
31
33
@@ -50,27 +52,49 @@ public void postProcessShouldReplaceSystemEnvironmentPropertySource()
50
52
postProcessor .postProcessEnvironment (this .environment , null );
51
53
PropertySource <?> replaced = this .environment .getPropertySources ()
52
54
.get ("systemEnvironment" );
53
- assertThat (replaced ).isInstanceOf (OriginTrackedSystemPropertySource .class );
55
+ assertThat (replaced ).isInstanceOf (OriginAwareSystemEnvironmentPropertySource .class );
54
56
}
55
57
56
58
@ Test
57
59
@ SuppressWarnings ("unchecked" )
58
- public void replacedPropertySourceShouldHaveOriginTrackedValues () throws Exception {
60
+ public void replacedPropertySourceShouldBeOriginAware () throws Exception {
59
61
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor ();
60
62
PropertySource <?> original = this .environment .getPropertySources ()
61
63
.get ("systemEnvironment" );
62
64
postProcessor .postProcessEnvironment (this .environment , null );
63
- PropertySource <?> replaced = this .environment .getPropertySources ()
65
+ OriginAwareSystemEnvironmentPropertySource replaced = ( OriginAwareSystemEnvironmentPropertySource ) this .environment .getPropertySources ()
64
66
.get ("systemEnvironment" );
65
67
Map <String , Object > originalMap = (Map <String , Object >) original .getSource ();
66
- Map <String , OriginTrackedValue > replacedMap = ( Map < String , OriginTrackedValue >) replaced
68
+ Map <String , Object > replacedMap = replaced
67
69
.getSource ();
68
70
for (Map .Entry <String , Object > entry : originalMap .entrySet ()) {
69
- OriginTrackedValue actual = replacedMap .get (entry .getKey ());
70
- assertThat (actual .getValue ()).isEqualTo (entry .getValue ());
71
- assertThat (actual .getOrigin ())
72
- .isEqualTo (new SystemEnvironmentOrigin (entry .getKey ()));
71
+ Object actual = replacedMap .get (entry .getKey ());
72
+ assertThat (actual ).isEqualTo (entry .getValue ());
73
+ assertThat (replaced .getOrigin (entry .getKey ())).isInstanceOf (SystemEnvironmentOrigin .class );
73
74
}
74
75
}
75
76
77
+ @ Test
78
+ public void replacedPropertySourceWhenPropertyAbsentShouldReturnNullOrigin () throws Exception {
79
+ SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor ();
80
+ postProcessor .postProcessEnvironment (this .environment , null );
81
+ OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource ) this .environment .getPropertySources ()
82
+ .get ("systemEnvironment" );
83
+ assertThat (replaced .getOrigin ("NON_EXISTENT" )).isNull ();
84
+ }
85
+
86
+ @ Test
87
+ @ SuppressWarnings ("unchecked" )
88
+ public void replacedPropertySourceShouldResolveProperty () throws Exception {
89
+ SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor ();
90
+ Map <String , Object > source = Collections .singletonMap ("FOO_BAR_BAZ" , "hello" );
91
+ this .environment .getPropertySources ().replace ("systemEnvironment" , new SystemEnvironmentPropertySource ("systemEnvironment" , source ));
92
+ postProcessor .postProcessEnvironment (this .environment , null );
93
+ OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource ) this .environment .getPropertySources ()
94
+ .get ("systemEnvironment" );
95
+ SystemEnvironmentOrigin origin = (SystemEnvironmentOrigin ) replaced .getOrigin ("foo.bar.baz" );
96
+ assertThat (origin .getProperty ()).isEqualTo ("FOO_BAR_BAZ" );
97
+ assertThat (replaced .getProperty ("foo.bar.baz" )).isEqualTo ("hello" );
98
+ }
99
+
76
100
}
0 commit comments