use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class ConfigPropertySourceTest method testAddChangeListener.
@Test
public void testAddChangeListener() throws Exception {
ConfigChangeListener someListener = mock(ConfigChangeListener.class);
ConfigChangeListener anotherListener = mock(ConfigChangeListener.class);
final List<ConfigChangeListener> listeners = Lists.newArrayList();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
listeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(someConfig).addChangeListener(any(ConfigChangeListener.class));
configPropertySource.addChangeListener(someListener);
configPropertySource.addChangeListener(anotherListener);
assertEquals(2, listeners.size());
assertTrue(listeners.containsAll(Lists.newArrayList(someListener, anotherListener)));
}
use of com.ctrip.framework.apollo.ConfigChangeListener 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());
}
Aggregations