Search in sources :

Example 41 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class JavaConfigPlaceholderTest method testMultiplePropertiesCompatiblePropertySourcesWithSameProperties.

@Test
public void testMultiplePropertiesCompatiblePropertySourcesWithSameProperties() throws Exception {
    int someTimeout = 1000;
    int anotherTimeout = someTimeout + 1;
    int someBatch = 2000;
    Properties properties = mock(Properties.class);
    when(properties.getProperty(TIMEOUT_PROPERTY)).thenReturn(String.valueOf(someTimeout));
    when(properties.getProperty(BATCH_PROPERTY)).thenReturn(String.valueOf(someBatch));
    PropertiesCompatibleConfigFile configFile = mock(PropertiesCompatibleConfigFile.class);
    when(configFile.asProperties()).thenReturn(properties);
    mockConfigFile("application.yml", configFile);
    Config fxApollo = mock(Config.class);
    when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(anotherTimeout));
    mockConfig(FX_APOLLO_NAMESPACE, fxApollo);
    check(someTimeout, someBatch, AppConfig11.class);
}
Also used : EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) Config(com.ctrip.framework.apollo.Config) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) PropertiesCompatibleConfigFile(com.ctrip.framework.apollo.PropertiesCompatibleConfigFile) Test(org.junit.Test)

Example 42 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class JavaConfigPlaceholderTest method testApolloJsonValueWithInvalidJson.

@Test(expected = BeanCreationException.class)
public void testApolloJsonValueWithInvalidJson() throws Exception {
    String someInvalidJson = "someInvalidJson";
    Config config = mock(Config.class);
    when(config.getProperty(eq(JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(someInvalidJson);
    when(config.getProperty(eq(OTHER_JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(someInvalidJson);
    when(config.getProperty(eq("a"), Mockito.nullable(String.class))).thenReturn(JSON_PROPERTY);
    mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
    new AnnotationConfigApplicationContext(AppConfig8.class).getBean(TestJsonPropertyBean.class);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) Config(com.ctrip.framework.apollo.Config) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 43 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class JavaConfigPlaceholderTest method testApplicationPropertySourceWithValueInjectedAsParameter.

@Test
public void testApplicationPropertySourceWithValueInjectedAsParameter() throws Exception {
    int someTimeout = 1000;
    int someBatch = 2000;
    Config config = mock(Config.class);
    when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout));
    when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch));
    mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig5.class);
    TestJavaConfigBean2 bean = context.getBean(TestJavaConfigBean2.class);
    assertEquals(someTimeout, bean.getTimeout());
    assertEquals(someBatch, bean.getBatch());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) Config(com.ctrip.framework.apollo.Config) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 44 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class XMLConfigAnnotationTest 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() {

        @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() {

        @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("spring/XmlConfigAnnotationTest3.xml", TestApolloConfigChangeListenerBean1.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 : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfigChangeListener(com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener) ApolloConfig(com.ctrip.framework.apollo.spring.annotation.ApolloConfig) Config(com.ctrip.framework.apollo.Config) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 45 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class XMLConfigAnnotationTest 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("spring/XmlConfigAnnotationTest6.xml", TestApolloConfigChangeListenerWithInterestedKeysBean.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) ApolloConfig(com.ctrip.framework.apollo.spring.annotation.ApolloConfig) Config(com.ctrip.framework.apollo.Config) 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