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