Search in sources :

Example 81 with Config

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);
}
Also used : 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) Test(org.junit.Test)

Example 82 with Config

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));
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) 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) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Matchers.anyString(org.mockito.Matchers.anyString) YamlConfigFile(com.ctrip.framework.apollo.internals.YamlConfigFile) Test(org.junit.Test)

Example 83 with Config

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());
}
Also used : Set(java.util.Set) 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) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 84 with Config

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());
}
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)

Example 85 with Config

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));
}
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) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

Config (com.ctrip.framework.apollo.Config)94 Test (org.junit.Test)79 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)39 Matchers.anyString (org.mockito.Matchers.anyString)31 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)26 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)22 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)20 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)16 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)13 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)13 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)10 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 Properties (java.util.Properties)6 ConfigFactory (com.ctrip.framework.apollo.spi.ConfigFactory)5 PureApolloConfigFactory (com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory)4 DefaultConfigFactory (com.ctrip.framework.apollo.spi.DefaultConfigFactory)4 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)4 Set (java.util.Set)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3