|
27 | 27 | import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource;
|
28 | 28 | import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
|
29 | 29 | import com.ctrip.framework.apollo.util.ConfigUtil;
|
| 30 | +import jdk.jfr.Event; |
30 | 31 | import org.junit.After;
|
31 | 32 | import org.junit.Before;
|
32 | 33 | import org.junit.Test;
|
33 | 34 | import org.springframework.core.env.ConfigurableEnvironment;
|
34 | 35 | import org.springframework.core.env.MutablePropertySources;
|
| 36 | +import org.springframework.core.env.PropertiesPropertySource; |
| 37 | +import org.springframework.core.env.StandardEnvironment; |
| 38 | + |
| 39 | +import java.util.Properties; |
35 | 40 |
|
36 | 41 | public class ApolloApplicationContextInitializerTest {
|
37 | 42 |
|
@@ -143,4 +148,24 @@ public void testPropertyNamesCacheEnabled() {
|
143 | 148 | assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
|
144 | 149 | assertTrue(propertySources.iterator().next() instanceof CachedCompositePropertySource);
|
145 | 150 | }
|
| 151 | + |
| 152 | + @Test |
| 153 | + public void testOverrideSystemProperties() { |
| 154 | + Properties properties = new Properties(); |
| 155 | + properties.setProperty("server.port", "8080"); |
| 156 | + ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); |
| 157 | + |
| 158 | + MutablePropertySources propertySources = new MutablePropertySources(); |
| 159 | + propertySources.addLast(new PropertiesPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, properties)); |
| 160 | + |
| 161 | + when(environment.getPropertySources()).thenReturn(propertySources); |
| 162 | + when(environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true)).thenReturn(false); |
| 163 | + when(environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, |
| 164 | + ConfigConsts.NAMESPACE_APPLICATION)).thenReturn(""); |
| 165 | + |
| 166 | + apolloApplicationContextInitializer.initialize(environment); |
| 167 | + |
| 168 | + assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)); |
| 169 | + assertEquals(propertySources.iterator().next().getName(), StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); |
| 170 | + } |
146 | 171 | }
|
0 commit comments