-
Notifications
You must be signed in to change notification settings - Fork 40.1k
/
ConfigDataEnvironmentPostProcessorIntegrationTests.java
900 lines (779 loc) · 38.8 KB
/
ConfigDataEnvironmentPostProcessorIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/*
* Copyright 2012-2021 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
*
* https://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.context.config;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.logging.log4j.util.Strings;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.context.properties.bind.BindContext;
import org.springframework.boot.context.properties.bind.BindException;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.origin.Origin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.Profiles;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Integration tests for {@link ConfigDataEnvironmentPostProcessor}.
*
* @author Madhura Bhave
* @author Phillip Webb
*/
class ConfigDataEnvironmentPostProcessorIntegrationTests {
private SpringApplication application;
@TempDir
public File temp;
@BeforeEach
void setup() {
this.application = new SpringApplication(Config.class);
this.application.setWebApplicationType(WebApplicationType.NONE);
}
@AfterEach
void clearProperties() {
System.clearProperty("the.property");
}
@Test
void runWhenUsingCustomResourceLoader() {
this.application.setResourceLoader(new ResourceLoader() {
@Override
public Resource getResource(String location) {
if (location.equals("classpath:/custom.properties")) {
return new ByteArrayResource("the.property: fromcustom".getBytes(), location);
}
return new ClassPathResource("doesnotexist");
}
@Override
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
});
ConfigurableApplicationContext context = this.application.run("--spring.config.name=custom");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromcustom");
}
@Test
void runLoadsApplicationPropertiesOnClasspath() {
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("foo");
assertThat(property).isEqualTo("bucket");
}
@Test
void runLoadsApplicationYamlOnClasspath() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=customapplication");
String property = context.getEnvironment().getProperty("yamlkey");
assertThat(property).isEqualTo("yamlvalue");
}
@Test
void runLoadsFileWithCustomName() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testproperties");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
}
@Test
void runWhenPropertiesAndYamlShouldPreferProperties() {
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("duplicate");
assertThat(property).isEqualTo("properties");
}
@Test
void runWhenMultipleCustomNamesLoadsEachName() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=moreproperties,testproperties");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
}
@Test
void runWhenNoActiveProfilesLoadsDefaultProfileFile() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdefaultpropertiesfile");
}
@Test
void runWhenActiveProfilesDoesNotLoadDefault() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofilesdocument",
"--spring.profiles.default=thedefault", "--spring.profiles.active=other");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromotherprofile");
}
@Test
void runWhenHasCustomDefaultProfileLoadsDefaultProfileFile() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles",
"--spring.profiles.default=thedefault");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromdefaultpropertiesfile");
}
@Test
void runWhenHasCustomSpringConfigLocationLoadsAllFromSpecifiedLocation() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application.properties,classpath:testproperties.properties");
String property1 = context.getEnvironment().getProperty("the.property");
String property2 = context.getEnvironment().getProperty("my.property");
String property3 = context.getEnvironment().getProperty("foo");
assertThat(property1).isEqualTo("frompropertiesfile");
assertThat(property2).isEqualTo("frompropertiesfile");
assertThat(property3).isEqualTo("bucket");
}
@Test
void runWhenOneCustomLocationDoesNotExistLoadsOthers() {
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:application.properties,classpath:testproperties.properties,optional:classpath:nonexistent.properties");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
}
@Test
void runWhenProfileSpecificMandatoryLocationDoesNotExistShouldNotFail() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromyamlfile");
}
@Test
void runWhenProfileSpecificMandatoryLocationDoesNotExistShouldFailWhenProfileActive() {
this.application.setAdditionalProfiles("prod");
assertThatExceptionOfType(ConfigDataResourceNotFoundException.class).isThrownBy(() -> this.application
.run("--spring.config.name=testprofiles", "--spring.config.location=classpath:configdata/profiles/"));
}
@Test
void runWhenHasActiveProfilesFromMultipleLocationsActivatesProfileFromOneLocation() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:enableprofile.properties,classpath:enableother.properties");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.getActiveProfiles()).containsExactly("other");
String property = environment.getProperty("other.property");
assertThat(property).isEqualTo("fromotherpropertiesfile");
}
@Test
void runWhenHasActiveProfilesFromMultipleAdditionaLocationsWithOneSwitchedOffLoadsExpectedProperties() {
ConfigurableApplicationContext context = this.application.run(
"--spring.config.additional-location=classpath:enabletwoprofiles.properties,classpath:enableprofile.properties");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.getActiveProfiles()).containsExactly("myprofile");
String property = environment.getProperty("my.property");
assertThat(property).isEqualTo("fromprofilepropertiesfile");
}
@Test
void runWhenHaslocalFileLoadsWithLocalFileTakingPrecedenceOverClasspath() throws Exception {
File localFile = new File(new File("."), "application.properties");
assertThat(localFile.exists()).isFalse();
try {
Properties properties = new Properties();
properties.put("my.property", "fromlocalfile");
try (OutputStream outputStream = new FileOutputStream(localFile)) {
properties.store(outputStream, "");
}
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromlocalfile");
}
finally {
localFile.delete();
}
}
@Test
void runWhenHasCommandLinePropertiesLoadsWithCommandLineTakingPrecedence() {
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources()
.addFirst(new SimpleCommandLinePropertySource("--the.property=fromcommandline"));
this.application.setEnvironment(environment);
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testproperties");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromcommandline");
}
@Test
void runWhenHasSystemPropertyLoadsWithSystemPropertyTakingPrecedence() {
System.setProperty("the.property", "fromsystem");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testproperties");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromsystem");
}
@Test
void runWhenHasDefaultPropertiesIncludesDefaultPropertiesLast() {
this.application.setDefaultProperties(Collections.singletonMap("my.fallback", "foo"));
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("my.fallback");
assertThat(property).isEqualTo("foo");
}
@Test
void runWhenHasDefaultPropertiesWithConfigLocationConfigurationLoadsExpectedProperties() {
this.application.setDefaultProperties(Collections.singletonMap("spring.config.name", "testproperties"));
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
}
@Test
void runWhenHasActiveProfilesFromDefaultPropertiesAndFileLoadsWithFileTakingPrecedence() {
this.application.setDefaultProperties(Collections.singletonMap("spring.profiles.active", "dev"));
ConfigurableApplicationContext context = this.application.run("--spring.config.name=enableprofile");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("myprofile");
}
@Test
void runWhenProgrammaticallySetProfilesLoadsWithSetProfilesTakePrecedenceOverDefaultProfile() {
this.application.setAdditionalProfiles("other");
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromotherpropertiesfile");
}
@Test
void runWhenTwoProfilesSetProgrammaticallyLoadsWithPreservedProfileOrder() {
this.application.setAdditionalProfiles("other", "dev");
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdevpropertiesfile");
}
@Test
void runWhenProfilesPresentBeforeConfigFileProcessingAugmentsProfileActivatedByConfigFile() {
this.application.setAdditionalProfiles("other");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=enableprofile");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("other", "myprofile");
String property = context.getEnvironment().getProperty("other.property");
assertThat(property).isEqualTo("fromotherpropertiesfile");
property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromprofilepropertiesfile");
}
@Test
void runWhenProfilePropertiesUsedInPlaceholdersLoadsWithResolvedPlaceholders() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=enableprofile");
String property = context.getEnvironment().getProperty("one.more");
assertThat(property).isEqualTo("fromprofilepropertiesfile");
}
@Test
void runWhenDuplicateProfileSetProgrammaticallyAndViaPropertyLoadsWithProfiles() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application.run("--spring.profiles.active=dev,other");
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev", "other");
assertThat(context.getEnvironment().getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
}
@Test
void runWhenProfilesActivatedViaBracketNotationSetsProfiles() {
ConfigurableApplicationContext context = this.application.run("--spring.profiles.active[0]=dev",
"--spring.profiles.active[1]=other");
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev", "other");
assertThat(context.getEnvironment().getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
}
@Test
void loadWhenProfileInMultiDocumentFilesLoadsExpectedProperties() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdevprofile");
property = context.getEnvironment().getProperty("my.other");
assertThat(property).isEqualTo("notempty");
}
@Test
void runWhenMultipleActiveProfilesWithMultiDocumentFilesLoadsInOrderOfDocument() {
this.application.setAdditionalProfiles("other", "dev");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromotherprofile");
property = context.getEnvironment().getProperty("my.other");
assertThat(property).isEqualTo("notempty");
property = context.getEnvironment().getProperty("dev.property");
assertThat(property).isEqualTo("devproperty");
}
@Test
void runWhenHasAndProfileExpressionLoadsExpectedProperties() {
assertProfileExpression("devandother", "dev", "other");
}
@Test
void runWhenHasComplexProfileExpressionsLoadsExpectedProperties() {
assertProfileExpression("devorotherandanother", "dev", "another");
}
@Test
void runWhenProfileExpressionsDoNotMatchLoadsExpectedProperties() {
assertProfileExpression("fromyamlfile", "dev");
}
@Test
void runWhenHasNegatedProfilesLoadsExpectedProperties() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testnegatedprofiles",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromnototherprofile");
property = context.getEnvironment().getProperty("my.notother");
assertThat(property).isEqualTo("foo");
}
@Test
void runWhenHasNegatedProfilesWithProfileActiveLoadsExpectedProperties() {
this.application.setAdditionalProfiles("other");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testnegatedprofiles",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromotherprofile");
property = context.getEnvironment().getProperty("my.notother");
assertThat(property).isNull();
}
@Test
void runWhenHasActiveProfileConfigurationInMultiDocumentFileLoadsInExpectedOrder() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testsetprofiles",
"--spring.config.location=classpath:configdata/profiles/");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("dev");
String property = context.getEnvironment().getProperty("my.property");
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev");
assertThat(property).isEqualTo("fromdevprofile");
assertThat(context.getEnvironment().getPropertySources()).extracting("name").contains(
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #0)",
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #1)");
}
@Test
void runWhenHasYamlWithCommaSeparatedMultipleProfilesLoadsExpectedProperties() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testsetmultiprofiles");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("dev", "healthcheck");
}
@Test
void runWhenHasYamlWithListProfilesLoadsExpectedProperties() {
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testsetmultiprofileslist");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("dev", "healthcheck");
}
@Test
void loadWhenHasWhitespaceTrims() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=testsetmultiprofileswhitespace");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("dev", "healthcheck");
}
@Test
void loadWhenHasConfigLocationAsFile() {
String location = "file:src/test/resources/specificlocation.properties";
ConfigurableApplicationContext context = this.application.run("--spring.config.location=" + location);
assertThat(context.getEnvironment()).has(matchingPropertySource("Config resource 'file [" + Strings
.join(Arrays.asList("src", "test", "resources", "specificlocation.properties"), File.separatorChar)
+ "]' via location '" + location + "'"));
}
@Test
void loadWhenHasRelativeConfigLocationUsesFileLocation() {
String location = "src/test/resources/specificlocation.properties";
ConfigurableApplicationContext context = this.application.run("--spring.config.location=" + location);
assertThat(context.getEnvironment()).has(matchingPropertySource("Config resource 'file [" + Strings
.join(Arrays.asList("src", "test", "resources", "specificlocation.properties"), File.separatorChar)
+ "]' via location '" + location + "'"));
}
@Test
void loadWhenCustomDefaultProfileAndActiveFromPreviousSourceDoesNotActivateDefault() {
ConfigurableApplicationContext context = this.application.run("--spring.profiles.default=customdefault",
"--spring.profiles.active=dev");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdevpropertiesfile");
assertThat(context.getEnvironment().containsProperty("customdefault")).isFalse();
}
@Test
void runWhenCustomDefaultProfileSameAsActiveFromFileActivatesProfile() {
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:configdata/profiles/", "--spring.profiles.default=customdefault",
"--spring.config.name=customprofile");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.containsProperty("customprofile")).isTrue();
assertThat(environment.containsProperty("customprofile-customdefault")).isTrue();
assertThat(environment.acceptsProfiles(Profiles.of("customdefault"))).isTrue();
}
@Test
void runWhenActiveProfilesCanBeConfiguredUsingPlaceholdersResolvedAgainstTheEnvironmentLoadsExpectedProperties() {
ConfigurableApplicationContext context = this.application.run("--activeProfile=testPropertySource",
"--spring.config.name=testactiveprofiles");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("testPropertySource");
}
@Test
void runWhenHasAdditionalLocationLoadsWithAdditionalTakingPrecedenceOverDefaultLocation() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.additional-location=classpath:override.properties");
assertThat(context.getEnvironment().getProperty("foo")).isEqualTo("bar");
assertThat(context.getEnvironment().getProperty("value")).isEqualTo("1234");
}
@Test
void runWhenMultipleAdditionalLocationsLoadsWithLastWinning() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.additional-location=classpath:override.properties,classpath:some.properties");
assertThat(context.getEnvironment().getProperty("foo")).isEqualTo("spam");
assertThat(context.getEnvironment().getProperty("value")).isEqualTo("1234");
}
@Test
void runWhenAdditionalLocationAndLocationLoadsWithAdditionalTakingPrecedenceOverConfigured() {
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:some.properties",
"--spring.config.additional-location=classpath:override.properties");
assertThat(context.getEnvironment().getProperty("foo")).isEqualTo("bar");
assertThat(context.getEnvironment().getProperty("value")).isNull();
}
@Test
void runWhenPropertiesFromCustomPropertySourceLoaderShouldLoadFromCustomSource() {
ConfigurableApplicationContext context = this.application.run();
assertThat(context.getEnvironment().getProperty("customloader1")).isEqualTo("true");
}
@Test
void runWhenCustomDefaultPropertySourceLoadsWithoutReplacingCustomSource() {
// gh-17011
Map<String, Object> source = new HashMap<>();
source.put("mapkey", "mapvalue");
MapPropertySource propertySource = new MapPropertySource("defaultProperties", source) {
@Override
public Object getProperty(String name) {
if ("spring.config.name".equals(name)) {
return "gh17001";
}
return super.getProperty(name);
}
};
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(propertySource);
this.application.setEnvironment(environment);
ConfigurableApplicationContext context = this.application.run();
assertThat(context.getEnvironment().getProperty("mapkey")).isEqualTo("mapvalue");
assertThat(context.getEnvironment().getProperty("gh17001loaded")).isEqualTo("true");
}
@Test
void runWhenConfigLocationHasUnknownFileExtensionFailsFast() {
String location = "classpath:application.unknown";
assertThatIllegalStateException().isThrownBy(() -> this.application.run("--spring.config.location=" + location))
.withMessageContaining("Unable to load config data").withMessageContaining(location)
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageContaining("File extension is not known")
.hasMessageContaining("it must end in '/'"));
}
@Test
void runWhenConfigLocationHasOptionalMissingDirectoryContinuesToLoad() {
String location = "optional:classpath:application.unknown/";
this.application.run("--spring.config.location=" + location);
}
@Test
void runWhenConfigLocationHasNonOptionalMissingFileDirectoryThrowsResourceNotFoundException() {
File location = new File(this.temp, "application.unknown");
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(() -> this.application
.run("--spring.config.location=" + StringUtils.cleanPath(location.getAbsolutePath()) + "/"));
}
@Test
void runWhenConfigLocationHasNonOptionalMissingClasspathDirectoryThrowsLocationNotFoundException() {
String location = "classpath:application.unknown/";
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class)
.isThrownBy(() -> this.application.run("--spring.config.location=" + location));
}
@Test
void runWhenConfigLocationHasNonOptionalEmptyFileDirectoryDoesNotThrowException() {
File location = new File(this.temp, "application.empty");
location.mkdirs();
assertThatNoException().isThrownBy(() -> this.application
.run("--spring.config.location=" + StringUtils.cleanPath(location.getAbsolutePath()) + "/"));
}
@Test
void runWhenConfigLocationHasMandatoryDirectoryThatDoesntExistThrowsException() {
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(
() -> this.application.run("--spring.config.location=" + StringUtils.cleanPath("invalid/")));
}
@Test
void runWhenConfigLocationHasNonOptionalEmptyFileDoesNotThrowException() throws IOException {
File location = new File(this.temp, "application.properties");
FileCopyUtils.copy(new byte[0], location);
assertThatNoException()
.isThrownBy(() -> this.application.run("--spring.config.location=classpath:/application.properties,"
+ StringUtils.cleanPath(location.getAbsolutePath())));
}
@Test
void runWhenResolvedIsOptionalDoesNotThrowException() {
ApplicationContext context = this.application.run("--spring.config.location=test:optionalresult");
assertThat(context.getEnvironment().containsProperty("spring")).isFalse();
}
@Test
@Disabled("Disabled until spring.profiles suppport is dropped")
void runWhenUsingInvalidPropertyThrowsException() {
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(
() -> this.application.run("--spring.config.location=classpath:invalidproperty.properties"));
}
@Test
void runWhenImportUsesPlaceholder() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-placeholder.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported");
}
@Test
void runWhenImportFromEarlierDocumentUsesPlaceholder() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-placeholder-in-document.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported");
}
@Test
void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-profile-variant.properties");
assertThat(context.getEnvironment().getProperty("my.value"))
.isEqualTo("application-import-with-profile-variant-dev");
}
@Test
void runWhenImportWithProfileVariantAndDirectProfileImportOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:application-import-with-profile-variant-and-direct-profile-import.properties");
assertThat(context.getEnvironment().getProperty("my.value"))
.isEqualTo("application-import-with-profile-variant-imported-dev");
}
@Test
void runWhenHasPropertyInProfileDocumentThrowsException() {
assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.application.run(
"--spring.config.location=classpath:application-import-with-placeholder-in-profile-document.properties"))
.withCauseInstanceOf(InactiveConfigDataAccessException.class);
}
@Test
void runWhenHasNonOptionalImportThrowsException() {
assertThatExceptionOfType(ConfigDataResourceNotFoundException.class).isThrownBy(
() -> this.application.run("--spring.config.location=classpath:missing-appplication.properties"));
}
@Test
void runWhenHasNonOptionalImportAndIgnoreNotFoundPropertyDoesNotThrowException() {
this.application.run("--spring.config.on-not-found=ignore",
"--spring.config.location=classpath:missing-appplication.properties");
}
@Test
void runWhenHasIncludedProfilesActivatesProfiles() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-include-profiles.properties");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactlyInAnyOrder("p1", "p2", "p3", "p4",
"p5");
}
@Test
void runWhenHasIncludedProfilesWithPlaceholderActivatesProfiles() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-include-profiles-with-placeholder.properties");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactlyInAnyOrder("p1", "p2", "p3", "p4",
"p5");
}
@Test
void runWhenHasIncludedProfilesWithProfileSpecificDocumentThrowsException() {
assertThatExceptionOfType(InactiveConfigDataAccessException.class).isThrownBy(() -> this.application.run(
"--spring.config.location=classpath:application-include-profiles-in-profile-specific-document.properties"));
}
@Test
void runWhenHasIncludedProfilesWithListSyntaxWithProfileSpecificDocumentThrowsException() {
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(() -> this.application.run(
"--spring.config.name=application-include-profiles-list-in-profile-specific-file",
"--spring.profiles.active=test"));
}
@Test
void runWhenImportingIncludesParentOrigin() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-placeholder.properties");
Binder binder = Binder.get(context.getEnvironment());
List<ConfigurationProperty> properties = new ArrayList<>();
BindHandler bindHandler = new BindHandler() {
@Override
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
Object result) {
properties.add(context.getConfigurationProperty());
return result;
}
};
binder.bind("my.value", Bindable.of(String.class), bindHandler);
assertThat(properties).hasSize(1);
Origin origin = properties.get(0).getOrigin();
assertThat(origin.toString()).contains("application-import-with-placeholder-imported");
assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
}
@Test
void runWhenHasWildcardLocationLoadsFromAllMatchingLocations() {
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=file:src/test/resources/config/*/", "--spring.config.name=testproperties");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.getProperty("first.property")).isEqualTo("apple");
assertThat(environment.getProperty("second.property")).isEqualTo("ball");
}
@Test
void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
assertThatNoException()
.isThrownBy(() -> this.application.run("--spring.config.location=file:src/test/resources/config/*/"));
}
@Test
void runWhenMandatoryWildcardLocationHasNoSubdirectories() {
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(
() -> this.application.run("--spring.config.location=file:src/test/resources/config/0-empty/*/"))
.withMessage(
"Config data location 'file:src/test/resources/config/0-empty/*/' contains no subdirectories");
}
@Test
void runWhenHasMandatoryWildcardLocationThatDoesNotExist() {
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class)
.isThrownBy(() -> this.application.run("--spring.config.location=file:invalid/*/"));
}
@Test
void runWhenHasOptionalWildcardLocationThatDoesNotExistDoesNotThrow() {
assertThatNoException()
.isThrownBy(() -> this.application.run("--spring.config.location=optional:file:invalid/*/"));
}
@Test
void runWhenOptionalWildcardLocationHasNoSubdirectoriesDoesNotThrow() {
assertThatNoException().isThrownBy(() -> this.application
.run("--spring.config.location=optional:file:src/test/resources/config/0-empty/*/"));
}
@Test // gh-24990
void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=application-activate-on-profile-in-profile-specific-file");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.getProperty("test1")).isEqualTo("test1");
assertThat(environment.getProperty("test2")).isEqualTo("test2");
}
@Test // gh-26752
void runWhenHasProfileSpecificImportWithImportDoesNotImportSecondProfileSpecificFile() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=application-profile-specific-import-with-import");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.containsProperty("application-profile-specific-import-with-import")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p1")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p2")).isFalse();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isFalse();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isFalse();
}
@Test // gh-26753
void runWhenHasProfileSpecificImportWithCustomImportDoesNotResolveProfileSpecific() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=application-profile-specific-import-with-custom-import");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.containsProperty("test:boot")).isTrue();
assertThat(environment.containsProperty("test:boot:ps")).isFalse();
}
@Test // gh-26593
void runWhenHasFilesInRootAndConfigWithProfiles() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=file-in-root-and-config-with-profile", "--spring.profiles.active=p1,p2");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.containsProperty("file-in-root-and-config-with-profile")).isTrue();
assertThat(environment.containsProperty("file-in-root-and-config-with-profile-p1")).isTrue();
assertThat(environment.containsProperty("file-in-root-and-config-with-profile-p2")).isTrue();
assertThat(environment.containsProperty("config-file-in-root-and-config-with-profile")).isTrue();
assertThat(environment.containsProperty("config-file-in-root-and-config-with-profile-p1")).isTrue();
assertThat(environment.containsProperty("config-file-in-root-and-config-with-profile-p2")).isTrue();
assertThat(environment.getProperty("v1")).isEqualTo("config-file-in-root-and-config-with-profile-p2");
assertThat(environment.getProperty("v2")).isEqualTo("file-in-root-and-config-with-profile-p2");
}
private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {
@Override
public boolean matches(ConfigurableEnvironment value) {
value.getPropertySources().forEach((ps) -> System.out.println(ps.getName()));
return value.getPropertySources().contains(sourceName);
}
};
}
private void assertProfileExpression(String value, String... activeProfiles) {
this.application.setAdditionalProfiles(activeProfiles);
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofileexpression",
"--spring.config.location=classpath:configdata/profiles/");
String property = context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo(value);
}
@Configuration(proxyBeanMethods = false)
static class Config {
}
static class LocationResolver implements ConfigDataLocationResolver<TestConfigDataResource> {
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
return location.hasPrefix("test:");
}
@Override
public List<TestConfigDataResource> resolve(ConfigDataLocationResolverContext context,
ConfigDataLocation location)
throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException {
return Collections.singletonList(new TestConfigDataResource(location, false));
}
@Override
public List<TestConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
ConfigDataLocation location, org.springframework.boot.context.config.Profiles profiles)
throws ConfigDataLocationNotFoundException {
return Collections.singletonList(new TestConfigDataResource(location, true));
}
}
static class Loader implements ConfigDataLoader<TestConfigDataResource> {
@Override
public ConfigData load(ConfigDataLoaderContext context, TestConfigDataResource resource)
throws IOException, ConfigDataResourceNotFoundException {
if (resource.isOptional()) {
return null;
}
Map<String, Object> map = new LinkedHashMap<>();
if (!resource.isProfileSpecific()) {
map.put("spring", "boot");
}
String suffix = (!resource.isProfileSpecific()) ? "" : ":ps";
map.put(resource.toString() + suffix, "true");
MapPropertySource propertySource = new MapPropertySource("loaded" + suffix, map);
return new ConfigData(Collections.singleton(propertySource));
}
}
static class TestConfigDataResource extends ConfigDataResource {
private final ConfigDataLocation location;
private boolean profileSpecific;
TestConfigDataResource(ConfigDataLocation location, boolean profileSpecific) {
super(location.toString().contains("optionalresult"));
this.location = location;
this.profileSpecific = profileSpecific;
}
boolean isProfileSpecific() {
return this.profileSpecific;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TestConfigDataResource other = (TestConfigDataResource) obj;
return ObjectUtils.nullSafeEquals(this.location, other.location)
&& this.profileSpecific == other.profileSpecific;
}
@Override
public int hashCode() {
return 0;
}
@Override
public String toString() {
return this.location.toString();
}
}
}