use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigWithWrongFieldType.
@Test(expected = BeanCreationException.class)
public void testApolloConfigWithWrongFieldType() throws Exception {
Config applicationConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
getBean(TestApolloConfigBean2.class, AppConfig2.class);
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithYamlFile.
@Test
public void testApolloConfigChangeListenerWithYamlFile() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
String anotherValue = "anotherValue";
YamlConfigFile configFile = prepareYamlConfigFile(APPLICATION_YAML_NAMESPACE, readYamlContentAsConfigFileProperties("case9.yml"));
TestApolloConfigChangeListenerWithYamlFile bean = getBean(TestApolloConfigChangeListenerWithYamlFile.class, AppConfig9.class);
Config yamlConfig = bean.getYamlConfig();
SettableFuture<ConfigChangeEvent> future = bean.getConfigChangeEventFuture();
assertEquals(someValue, yamlConfig.getProperty(someKey, null));
assertFalse(future.isDone());
configFile.onRepositoryChange(APPLICATION_YAML_NAMESPACE, readYamlContentAsConfigFileProperties("case9-new.yml"));
ConfigChangeEvent configChangeEvent = future.get(100, TimeUnit.MILLISECONDS);
ConfigChange change = configChangeEvent.getChange(someKey);
assertEquals(someValue, change.getOldValue());
assertEquals(anotherValue, change.getNewValue());
assertEquals(anotherValue, yamlConfig.getProperty(someKey, null));
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithInterestedKeys.
@Test
public void testApolloConfigChangeListenerWithInterestedKeys() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);
TestApolloConfigChangeListenerWithInterestedKeysBean bean = getBean(TestApolloConfigChangeListenerWithInterestedKeysBean.class, AppConfig8.class);
final ArgumentCaptor<Set> applicationConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);
final ArgumentCaptor<Set> fxApolloConfigInterestedKeys = ArgumentCaptor.forClass(Set.class);
verify(applicationConfig, times(2)).addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), Mockito.nullable(Set.class));
verify(fxApolloConfig, times(1)).addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), Mockito.nullable(Set.class));
assertEquals(2, applicationConfigInterestedKeys.getAllValues().size());
Set<String> result = Sets.newHashSet();
for (Set interestedKeys : applicationConfigInterestedKeys.getAllValues()) {
result.addAll(interestedKeys);
}
assertEquals(Sets.newHashSet("someKey", "anotherKey"), result);
assertEquals(1, fxApolloConfigInterestedKeys.getAllValues().size());
assertEquals(Collections.singletonList(Sets.newHashSet("anotherKey")), fxApolloConfigInterestedKeys.getAllValues());
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListener.
@Test
public void testApolloConfigChangeListener() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);
final List<ConfigChangeListener> applicationListeners = Lists.newArrayList();
final List<ConfigChangeListener> fxApolloListeners = Lists.newArrayList();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(applicationConfig).addChangeListener(any(ConfigChangeListener.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(fxApolloConfig).addChangeListener(any(ConfigChangeListener.class));
ConfigChangeEvent someEvent = mock(ConfigChangeEvent.class);
ConfigChangeEvent anotherEvent = mock(ConfigChangeEvent.class);
TestApolloConfigChangeListenerBean1 bean = getBean(TestApolloConfigChangeListenerBean1.class, AppConfig3.class);
// PropertySourcesProcessor add listeners to listen config changed of all namespace
assertEquals(4, applicationListeners.size());
assertEquals(1, fxApolloListeners.size());
for (ConfigChangeListener listener : applicationListeners) {
listener.onChange(someEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(someEvent, bean.getChangeEvent3());
for (ConfigChangeListener listener : fxApolloListeners) {
listener.onChange(anotherEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(anotherEvent, bean.getChangeEvent3());
}
use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerResolveExpressionFromSystemProperty.
/**
* resolve namespace's name from system property.
*/
@Test
public void testApolloConfigChangeListenerResolveExpressionFromSystemProperty() {
Config applicationConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
final String namespaceName = "magicRedis";
System.setProperty(SystemPropertyKeyConstants.REDIS_NAMESPACE, namespaceName);
Config redisConfig = mock(Config.class);
mockConfig(namespaceName, redisConfig);
getSimpleBean(TestApolloConfigChangeListenerResolveExpressionFromSystemPropertyConfiguration.class);
// if config was used, it must be invoked on method addChangeListener 1 time
verify(redisConfig, times(1)).addChangeListener(any(ConfigChangeListener.class));
}
Aggregations