forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support setting apollo.meta, app.id, apollo.cacheDir and apollo.clust…
…er from spring boot configuration files, e.g. application.properties, bootstrap.properties, etc
- Loading branch information
Showing
3 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
.../java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.ctrip.framework.apollo.spring.boot; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.ctrip.framework.apollo.core.ConfigConsts; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
public class ApolloApplicationContextInitializerTest { | ||
|
||
private ApolloApplicationContextInitializer apolloApplicationContextInitializer; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
apolloApplicationContextInitializer = new ApolloApplicationContextInitializer(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
System.clearProperty("app.id"); | ||
System.clearProperty(ConfigConsts.APOLLO_CLUSTER_KEY); | ||
System.clearProperty("apollo.cacheDir"); | ||
System.clearProperty(ConfigConsts.APOLLO_META_KEY); | ||
} | ||
|
||
@Test | ||
public void testFillFromEnvironment() throws Exception { | ||
String someAppId = "someAppId"; | ||
String someCluster = "someCluster"; | ||
String someCacheDir = "someCacheDir"; | ||
String someApolloMeta = "someApolloMeta"; | ||
|
||
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); | ||
|
||
when(environment.getProperty("app.id")).thenReturn(someAppId); | ||
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(someCluster); | ||
when(environment.getProperty("apollo.cacheDir")).thenReturn(someCacheDir); | ||
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(someApolloMeta); | ||
|
||
apolloApplicationContextInitializer.initializeSystemProperty(environment); | ||
|
||
assertEquals(someAppId, System.getProperty("app.id")); | ||
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); | ||
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir")); | ||
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY)); | ||
} | ||
|
||
@Test | ||
public void testFillFromEnvironmentWithSystemPropertyAlreadyFilled() throws Exception { | ||
String someAppId = "someAppId"; | ||
String someCluster = "someCluster"; | ||
String someCacheDir = "someCacheDir"; | ||
String someApolloMeta = "someApolloMeta"; | ||
|
||
System.setProperty("app.id", someAppId); | ||
System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, someCluster); | ||
System.setProperty("apollo.cacheDir", someCacheDir); | ||
System.setProperty(ConfigConsts.APOLLO_META_KEY, someApolloMeta); | ||
|
||
String anotherAppId = "anotherAppId"; | ||
String anotherCluster = "anotherCluster"; | ||
String anotherCacheDir = "anotherCacheDir"; | ||
String anotherApolloMeta = "anotherApolloMeta"; | ||
|
||
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); | ||
|
||
when(environment.getProperty("app.id")).thenReturn(anotherAppId); | ||
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(anotherCluster); | ||
when(environment.getProperty("apollo.cacheDir")).thenReturn(anotherCacheDir); | ||
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(anotherApolloMeta); | ||
|
||
apolloApplicationContextInitializer.initializeSystemProperty(environment); | ||
|
||
assertEquals(someAppId, System.getProperty("app.id")); | ||
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); | ||
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir")); | ||
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY)); | ||
} | ||
|
||
@Test | ||
public void testFillFromEnvironmentWithNoPropertyFromEnvironment() throws Exception { | ||
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); | ||
|
||
apolloApplicationContextInitializer.initializeSystemProperty(environment); | ||
|
||
assertNull(System.getProperty("app.id")); | ||
assertNull(System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); | ||
assertNull(System.getProperty("apollo.cacheDir")); | ||
assertNull(System.getProperty(ConfigConsts.APOLLO_META_KEY)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters