Commit 360abba2 by nobodyiam

add config injection with inheritance test

parent 86a5a651
......@@ -55,6 +55,22 @@ public class JavaConfigAnnotationTest extends AbstractSpringIntegrationTest {
}
@Test
public void testApolloConfigWithInheritance() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);
TestApolloChildConfigBean bean = getBean(TestApolloChildConfigBean.class, AppConfig6.class);
assertEquals(applicationConfig, bean.getConfig());
assertEquals(applicationConfig, bean.getAnotherConfig());
assertEquals(fxApolloConfig, bean.getYetAnotherConfig());
assertEquals(applicationConfig, bean.getSomeConfig());
}
@Test
public void testApolloConfigChangeListener() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
......@@ -127,6 +143,63 @@ public class JavaConfigAnnotationTest extends AbstractSpringIntegrationTest {
getBean(TestApolloConfigChangeListenerBean3.class, AppConfig5.class);
}
@Test
public void testApolloConfigChangeListenerWithInheritance() 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.getArgumentAt(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.getArgumentAt(0, ConfigChangeListener.class));
return Void.class;
}
}).when(fxApolloConfig).addChangeListener(any(ConfigChangeListener.class));
ConfigChangeEvent someEvent = mock(ConfigChangeEvent.class);
ConfigChangeEvent anotherEvent = mock(ConfigChangeEvent.class);
TestApolloChildConfigChangeListener bean = getBean(TestApolloChildConfigChangeListener.class, AppConfig7.class);
//PropertySourcesProcessor add listeners to listen config changed of all namespace
assertEquals(5, 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());
assertEquals(someEvent, bean.getSomeChangeEvent());
for (ConfigChangeListener listener : fxApolloListeners) {
listener.onChange(anotherEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(anotherEvent, bean.getChangeEvent3());
assertEquals(someEvent, bean.getSomeChangeEvent());
}
private <T> T getBean(Class<T> beanClass, Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);
......@@ -178,6 +251,24 @@ public class JavaConfigAnnotationTest extends AbstractSpringIntegrationTest {
}
}
@Configuration
@EnableApolloConfig
static class AppConfig6 {
@Bean
public TestApolloChildConfigBean bean() {
return new TestApolloChildConfigBean();
}
}
@Configuration
@EnableApolloConfig
static class AppConfig7 {
@Bean
public TestApolloChildConfigChangeListener bean() {
return new TestApolloChildConfigChangeListener();
}
}
static class TestApolloConfigBean1 {
@ApolloConfig
private Config config;
......@@ -199,11 +290,21 @@ public class JavaConfigAnnotationTest extends AbstractSpringIntegrationTest {
}
}
public static class TestApolloConfigBean2 {
static class TestApolloConfigBean2 {
@ApolloConfig
private String config;
}
static class TestApolloChildConfigBean extends TestApolloConfigBean1 {
@ApolloConfig
private Config someConfig;
public Config getSomeConfig() {
return someConfig;
}
}
static class TestApolloConfigChangeListenerBean1 {
private ConfigChangeEvent changeEvent1;
private ConfigChangeEvent changeEvent2;
......@@ -251,4 +352,17 @@ public class JavaConfigAnnotationTest extends AbstractSpringIntegrationTest {
}
}
static class TestApolloChildConfigChangeListener extends TestApolloConfigChangeListenerBean1 {
private ConfigChangeEvent someChangeEvent;
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
this.someChangeEvent = changeEvent;
}
public ConfigChangeEvent getSomeChangeEvent() {
return someChangeEvent;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment