20
20
21
21
import org .junit .jupiter .api .Test ;
22
22
23
+ import org .springframework .boot .context .properties .source .ConfigurationPropertySources ;
23
24
import org .springframework .boot .context .properties .source .InvalidConfigurationPropertyValueException ;
24
25
import org .springframework .boot .diagnostics .FailureAnalysis ;
25
26
import org .springframework .boot .origin .Origin ;
26
27
import org .springframework .boot .origin .OriginLookup ;
27
28
import org .springframework .core .env .EnumerablePropertySource ;
28
29
import org .springframework .core .env .MapPropertySource ;
30
+ import org .springframework .core .env .SystemEnvironmentPropertySource ;
29
31
import org .springframework .mock .env .MockEnvironment ;
32
+ import org .springframework .util .ObjectUtils ;
30
33
31
34
import static org .assertj .core .api .Assertions .assertThat ;
32
35
@@ -63,6 +66,26 @@ void analysisWithKnownProperty() {
63
66
.doesNotContain ("Additionally, this property is also set" );
64
67
}
65
68
69
+ @ Test
70
+ void analysisWithKnownPropertyFromSystemEnvironment () {
71
+ MapPropertySource source = new SystemEnvironmentPropertySource ("systemEnvironment" ,
72
+ Collections .singletonMap ("COM_EXAMPLE_TESTPROPERTY" , "invalid" ));
73
+ this .environment .getPropertySources ().addFirst (source );
74
+ ConfigurationPropertySources .attach (this .environment );
75
+ assertThat (this .environment .getProperty ("com.example.test-property" )).isEqualTo ("invalid" );
76
+ InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException (
77
+ "com.example.test-property" , "invalid" , "This is not valid." );
78
+ FailureAnalysis analysis = performAnalysis (failure );
79
+ assertThat (analysis .getDescription ()).contains ("com.example.test-property" )
80
+ .contains ("invalid" )
81
+ .contains ("property source \" systemEnvironment\" " );
82
+ assertThat (analysis .getCause ()).isSameAs (failure );
83
+ assertThat (analysis .getAction ()).contains ("Review the value of the property with the provided reason." );
84
+ assertThat (analysis .getDescription ()).contains ("Validation failed for the following reason" )
85
+ .contains ("This is not valid." )
86
+ .doesNotContain ("Additionally, this property is also set" );
87
+ }
88
+
66
89
@ Test
67
90
void analysisWithKnownPropertyAndNoReason () {
68
91
MapPropertySource source = new MapPropertySource ("test" , Collections .singletonMap ("test.property" , "invalid" ));
@@ -84,6 +107,8 @@ void analysisWithKnownPropertyAndOtherCandidates() {
84
107
this .environment .getPropertySources ().addFirst (OriginCapablePropertySource .get (source ));
85
108
this .environment .getPropertySources ().addLast (additional );
86
109
this .environment .getPropertySources ().addLast (OriginCapablePropertySource .get (another ));
110
+ this .environment .getPropertySources ().addLast (OriginCapablePropertySource .get ("another-again" , another ));
111
+ ConfigurationPropertySources .attach (this .environment );
87
112
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException (
88
113
"test.property" , "invalid" , "This is not valid." );
89
114
FailureAnalysis analysis = performAnalysis (failure );
@@ -92,7 +117,8 @@ void analysisWithKnownPropertyAndOtherCandidates() {
92
117
assertThat (analysis .getDescription ())
93
118
.contains ("Additionally, this property is also set in the following property sources:" )
94
119
.contains ("In 'additional' with the value 'valid'" )
95
- .contains ("In 'another' with the value 'test' (originating from 'TestOrigin test.property')" );
120
+ .contains ("In 'another' with the value 'test' (originating from 'TestOrigin test.property')" )
121
+ .doesNotContain ("another-again" );
96
122
}
97
123
98
124
@ Test
@@ -122,7 +148,11 @@ static class OriginCapablePropertySource<T> extends EnumerablePropertySource<T>
122
148
private final EnumerablePropertySource <T > propertySource ;
123
149
124
150
OriginCapablePropertySource (EnumerablePropertySource <T > propertySource ) {
125
- super (propertySource .getName (), propertySource .getSource ());
151
+ this (propertySource .getName (), propertySource );
152
+ }
153
+
154
+ OriginCapablePropertySource (String name , EnumerablePropertySource <T > propertySource ) {
155
+ super (name , propertySource .getSource ());
126
156
this .propertySource = propertySource ;
127
157
}
128
158
@@ -138,20 +168,53 @@ public String[] getPropertyNames() {
138
168
139
169
@ Override
140
170
public Origin getOrigin (String name ) {
141
- return new Origin () {
142
-
143
- @ Override
144
- public String toString () {
145
- return "TestOrigin " + name ;
146
- }
147
-
148
- };
171
+ return new TestOrigin (name , this .propertySource .getName ());
149
172
}
150
173
151
174
static <T > OriginCapablePropertySource <T > get (EnumerablePropertySource <T > propertySource ) {
152
175
return new OriginCapablePropertySource <>(propertySource );
153
176
}
154
177
178
+ static <T > OriginCapablePropertySource <T > get (String name , EnumerablePropertySource <T > propertySource ) {
179
+ return new OriginCapablePropertySource <>(name , propertySource );
180
+ }
181
+
182
+ static final class TestOrigin implements Origin {
183
+
184
+ private final String name ;
185
+
186
+ private final String sourceName ;
187
+
188
+ private TestOrigin (String name , String sourceName ) {
189
+ this .name = name ;
190
+ this .sourceName = sourceName ;
191
+ }
192
+
193
+ @ Override
194
+ public boolean equals (Object obj ) {
195
+ if (this == obj ) {
196
+ return true ;
197
+ }
198
+ if (obj == null || getClass () != obj .getClass ()) {
199
+ return false ;
200
+ }
201
+ TestOrigin other = (TestOrigin ) obj ;
202
+ return ObjectUtils .nullSafeEquals (this .name , other .name )
203
+ && ObjectUtils .nullSafeEquals (this .sourceName , other .sourceName );
204
+ }
205
+
206
+ @ Override
207
+ public int hashCode () {
208
+ return ObjectUtils .nullSafeHashCode (this .name );
209
+ }
210
+
211
+ @ Override
212
+ public String toString () {
213
+ return "TestOrigin " + this .name ;
214
+ }
215
+
216
+ }
217
+
155
218
}
156
219
157
220
}
0 commit comments