|
| 1 | +package com.ctrip.framework.apollo.spring.boot; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import com.ctrip.framework.apollo.core.ConfigConsts; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 12 | + |
| 13 | +public class ApolloApplicationContextInitializerTest { |
| 14 | + |
| 15 | + private ApolloApplicationContextInitializer apolloApplicationContextInitializer; |
| 16 | + |
| 17 | + @Before |
| 18 | + public void setUp() throws Exception { |
| 19 | + apolloApplicationContextInitializer = new ApolloApplicationContextInitializer(); |
| 20 | + } |
| 21 | + |
| 22 | + @After |
| 23 | + public void tearDown() throws Exception { |
| 24 | + System.clearProperty("app.id"); |
| 25 | + System.clearProperty(ConfigConsts.APOLLO_CLUSTER_KEY); |
| 26 | + System.clearProperty("apollo.cacheDir"); |
| 27 | + System.clearProperty(ConfigConsts.APOLLO_META_KEY); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testFillFromEnvironment() throws Exception { |
| 32 | + String someAppId = "someAppId"; |
| 33 | + String someCluster = "someCluster"; |
| 34 | + String someCacheDir = "someCacheDir"; |
| 35 | + String someApolloMeta = "someApolloMeta"; |
| 36 | + |
| 37 | + ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); |
| 38 | + |
| 39 | + when(environment.getProperty("app.id")).thenReturn(someAppId); |
| 40 | + when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(someCluster); |
| 41 | + when(environment.getProperty("apollo.cacheDir")).thenReturn(someCacheDir); |
| 42 | + when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(someApolloMeta); |
| 43 | + |
| 44 | + apolloApplicationContextInitializer.initializeSystemProperty(environment); |
| 45 | + |
| 46 | + assertEquals(someAppId, System.getProperty("app.id")); |
| 47 | + assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); |
| 48 | + assertEquals(someCacheDir, System.getProperty("apollo.cacheDir")); |
| 49 | + assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testFillFromEnvironmentWithSystemPropertyAlreadyFilled() throws Exception { |
| 54 | + String someAppId = "someAppId"; |
| 55 | + String someCluster = "someCluster"; |
| 56 | + String someCacheDir = "someCacheDir"; |
| 57 | + String someApolloMeta = "someApolloMeta"; |
| 58 | + |
| 59 | + System.setProperty("app.id", someAppId); |
| 60 | + System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, someCluster); |
| 61 | + System.setProperty("apollo.cacheDir", someCacheDir); |
| 62 | + System.setProperty(ConfigConsts.APOLLO_META_KEY, someApolloMeta); |
| 63 | + |
| 64 | + String anotherAppId = "anotherAppId"; |
| 65 | + String anotherCluster = "anotherCluster"; |
| 66 | + String anotherCacheDir = "anotherCacheDir"; |
| 67 | + String anotherApolloMeta = "anotherApolloMeta"; |
| 68 | + |
| 69 | + ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); |
| 70 | + |
| 71 | + when(environment.getProperty("app.id")).thenReturn(anotherAppId); |
| 72 | + when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(anotherCluster); |
| 73 | + when(environment.getProperty("apollo.cacheDir")).thenReturn(anotherCacheDir); |
| 74 | + when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(anotherApolloMeta); |
| 75 | + |
| 76 | + apolloApplicationContextInitializer.initializeSystemProperty(environment); |
| 77 | + |
| 78 | + assertEquals(someAppId, System.getProperty("app.id")); |
| 79 | + assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); |
| 80 | + assertEquals(someCacheDir, System.getProperty("apollo.cacheDir")); |
| 81 | + assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testFillFromEnvironmentWithNoPropertyFromEnvironment() throws Exception { |
| 86 | + ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); |
| 87 | + |
| 88 | + apolloApplicationContextInitializer.initializeSystemProperty(environment); |
| 89 | + |
| 90 | + assertNull(System.getProperty("app.id")); |
| 91 | + assertNull(System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); |
| 92 | + assertNull(System.getProperty("apollo.cacheDir")); |
| 93 | + assertNull(System.getProperty(ConfigConsts.APOLLO_META_KEY)); |
| 94 | + } |
| 95 | +} |
0 commit comments