|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
49 | 49 |
|
50 | 50 | /**
|
51 | 51 | * @author Dave Syer
|
| 52 | + * @author Yanming Zhou |
52 | 53 | *
|
53 | 54 | */
|
54 | 55 | public class BootstrapConfigurationTests {
|
@@ -740,6 +741,78 @@ void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveAc
|
740 | 741 | .anyMatch("local"::equals)).isTrue();
|
741 | 742 | }
|
742 | 743 |
|
| 744 | + @Test |
| 745 | + void activatedOnProfile() { |
| 746 | + PropertySourceConfiguration.MAP.put("stage", "dev"); |
| 747 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev"); |
| 748 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 749 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 750 | + .properties(properties) |
| 751 | + .sources(BareConfiguration.class) |
| 752 | + .run("--spring.profiles.active=dev"); |
| 753 | + then(this.context.getEnvironment().getProperty("stage")).isEqualTo("dev"); |
| 754 | + } |
| 755 | + |
| 756 | + @Test |
| 757 | + void notActivatedOnNoActiveProfile() { |
| 758 | + PropertySourceConfiguration.MAP.put("stage", "dev"); |
| 759 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev"); |
| 760 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 761 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 762 | + .properties(properties) |
| 763 | + .sources(BareConfiguration.class) |
| 764 | + .run(); |
| 765 | + then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev"); |
| 766 | + } |
| 767 | + |
| 768 | + @Test |
| 769 | + void notActivatedOnMismatchedProfile() { |
| 770 | + PropertySourceConfiguration.MAP.put("stage", "dev"); |
| 771 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev"); |
| 772 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 773 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 774 | + .properties(properties) |
| 775 | + .sources(BareConfiguration.class) |
| 776 | + .run("--spring.profiles.active=prod"); |
| 777 | + then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev"); |
| 778 | + } |
| 779 | + |
| 780 | + @Test |
| 781 | + void activatedOnCloudPlatform() { |
| 782 | + PropertySourceConfiguration.MAP.put("cloud", "kubernetes"); |
| 783 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes"); |
| 784 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 785 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 786 | + .properties(properties) |
| 787 | + .sources(BareConfiguration.class) |
| 788 | + .run("--spring.main.cloud-platform=kubernetes"); |
| 789 | + then(this.context.getEnvironment().getProperty("cloud")).isEqualTo("kubernetes"); |
| 790 | + } |
| 791 | + |
| 792 | + @Test |
| 793 | + void notActivatedOnNoActiveCloudPlatform() { |
| 794 | + PropertySourceConfiguration.MAP.put("cloud", "kubernetes"); |
| 795 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes"); |
| 796 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 797 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 798 | + .properties(properties) |
| 799 | + .sources(BareConfiguration.class) |
| 800 | + .run(); |
| 801 | + then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes"); |
| 802 | + } |
| 803 | + |
| 804 | + @Test |
| 805 | + void notActivatedOnMismatchedCloudPlatform() { |
| 806 | + PropertySourceConfiguration.MAP.put("cloud", "kubernetes"); |
| 807 | + PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes"); |
| 808 | + String[] properties = new String[] { "spring.config.use-legacy-processing=true" }; |
| 809 | + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) |
| 810 | + .properties(properties) |
| 811 | + .sources(BareConfiguration.class) |
| 812 | + .run("--spring.main.cloud-platform=heroku"); |
| 813 | + then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes"); |
| 814 | + } |
| 815 | + |
743 | 816 | @Configuration(proxyBeanMethods = false)
|
744 | 817 | @EnableConfigurationProperties
|
745 | 818 | protected static class BareConfiguration {
|
|
0 commit comments