Search in sources :

Example 26 with ConfigChangeListener

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)));
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 27 with ConfigChangeListener

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());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfigChangeListener(com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener) EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) ApolloConfig(com.ctrip.framework.apollo.spring.annotation.ApolloConfig) SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) Config(com.ctrip.framework.apollo.Config) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Aggregations

ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)27 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)22 Test (org.junit.Test)21 Config (com.ctrip.framework.apollo.Config)14 ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)4 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 HashMap (java.util.HashMap)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 Answer (org.mockito.stubbing.Answer)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)3 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)2 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)2 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)2 Properties (java.util.Properties)2 Semaphore (java.util.concurrent.Semaphore)2