use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class PureApolloConfigTest method testPureApolloConfigWithEnvironmentVariables.
@Test
public void testPureApolloConfigWithEnvironmentVariables() throws Exception {
SystemLambda.withEnvironmentVariable("SPRING_PROFILES_ACTIVE", "test-env").execute(() -> {
ApolloMockInjectorCustomizer.register(ConfigFactory.class, PureApolloConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assert.assertNull(config.getProperty("SPRING_PROFILES_ACTIVE", null));
});
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class PropertySourcesProcessor method initializePropertySources.
private void initializePropertySources() {
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) {
// already initialized
return;
}
CompositePropertySource composite;
if (configUtil.isPropertyNamesCacheEnabled()) {
composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);
} else {
composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);
}
// sort by order asc
ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet());
Iterator<Integer> iterator = orders.iterator();
while (iterator.hasNext()) {
int order = iterator.next();
for (String namespace : NAMESPACE_NAMES.get(order)) {
Config config = ConfigService.getConfig(namespace);
composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
}
}
// clean up
NAMESPACE_NAMES.clear();
// add after the bootstrap property source or to the first
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
// ensure ApolloBootstrapPropertySources is still the first
ensureBootstrapPropertyPrecedence(environment);
environment.getPropertySources().addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
} else {
environment.getPropertySources().addFirst(composite);
}
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class DefaultConfigManagerTest method testGetConfig.
@Test
public void testGetConfig() throws Exception {
String someNamespace = "someName";
String anotherNamespace = "anotherName";
String someKey = "someKey";
Config config = defaultConfigManager.getConfig(someNamespace);
Config anotherConfig = defaultConfigManager.getConfig(anotherNamespace);
assertEquals(someNamespace + ":" + someKey, config.getProperty(someKey, null));
assertEquals(anotherNamespace + ":" + someKey, anotherConfig.getProperty(someKey, null));
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testUpdateProperties.
@Test
public void testUpdateProperties() throws Exception {
String someNewValue = "someNewValue";
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
future.set(changeEvent);
}
});
assertEquals("otherValue1", otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
embeddedApollo.addOrModifyProperty(anotherNamespace, "key1", someNewValue);
ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
assertEquals(someNewValue, otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
assertTrue(changeEvent.isChanged("key1"));
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testDeleteSamePropertyTwice.
@Test
public void testDeleteSamePropertyTwice() throws Exception {
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final Semaphore changes = new Semaphore(0);
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
changes.release();
}
});
assertEquals("otherValue6", otherConfig.getProperty("key6", null));
embeddedApollo.deleteProperty(anotherNamespace, "key6");
embeddedApollo.deleteProperty(anotherNamespace, "key6");
assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
assertNull(otherConfig.getProperty("key6", null));
assertEquals(0, changes.availablePermits());
}
Aggregations