@@ -54,6 +54,22 @@ public void testApolloConfigWithWrongFieldType() throws Exception {
54
54
getBean (TestApolloConfigBean2 .class , AppConfig2 .class );
55
55
}
56
56
57
+ @ Test
58
+ public void testApolloConfigWithInheritance () throws Exception {
59
+ Config applicationConfig = mock (Config .class );
60
+ Config fxApolloConfig = mock (Config .class );
61
+
62
+ mockConfig (ConfigConsts .NAMESPACE_APPLICATION , applicationConfig );
63
+ mockConfig (FX_APOLLO_NAMESPACE , fxApolloConfig );
64
+
65
+ TestApolloChildConfigBean bean = getBean (TestApolloChildConfigBean .class , AppConfig6 .class );
66
+
67
+ assertEquals (applicationConfig , bean .getConfig ());
68
+ assertEquals (applicationConfig , bean .getAnotherConfig ());
69
+ assertEquals (fxApolloConfig , bean .getYetAnotherConfig ());
70
+ assertEquals (applicationConfig , bean .getSomeConfig ());
71
+ }
72
+
57
73
@ Test
58
74
public void testApolloConfigChangeListener () throws Exception {
59
75
Config applicationConfig = mock (Config .class );
@@ -127,6 +143,63 @@ public void testApolloConfigChangeListenerWithWrongParamCount() throws Exception
127
143
getBean (TestApolloConfigChangeListenerBean3 .class , AppConfig5 .class );
128
144
}
129
145
146
+ @ Test
147
+ public void testApolloConfigChangeListenerWithInheritance () throws Exception {
148
+ Config applicationConfig = mock (Config .class );
149
+ Config fxApolloConfig = mock (Config .class );
150
+
151
+ mockConfig (ConfigConsts .NAMESPACE_APPLICATION , applicationConfig );
152
+ mockConfig (FX_APOLLO_NAMESPACE , fxApolloConfig );
153
+
154
+ final List <ConfigChangeListener > applicationListeners = Lists .newArrayList ();
155
+ final List <ConfigChangeListener > fxApolloListeners = Lists .newArrayList ();
156
+
157
+ doAnswer (new Answer () {
158
+ @ Override
159
+ public Object answer (InvocationOnMock invocation ) throws Throwable {
160
+ applicationListeners .add (invocation .getArgumentAt (0 , ConfigChangeListener .class ));
161
+
162
+ return Void .class ;
163
+ }
164
+ }).when (applicationConfig ).addChangeListener (any (ConfigChangeListener .class ));
165
+
166
+ doAnswer (new Answer () {
167
+ @ Override
168
+ public Object answer (InvocationOnMock invocation ) throws Throwable {
169
+ fxApolloListeners .add (invocation .getArgumentAt (0 , ConfigChangeListener .class ));
170
+
171
+ return Void .class ;
172
+ }
173
+ }).when (fxApolloConfig ).addChangeListener (any (ConfigChangeListener .class ));
174
+
175
+ ConfigChangeEvent someEvent = mock (ConfigChangeEvent .class );
176
+ ConfigChangeEvent anotherEvent = mock (ConfigChangeEvent .class );
177
+
178
+ TestApolloChildConfigChangeListener bean = getBean (TestApolloChildConfigChangeListener .class , AppConfig7 .class );
179
+
180
+ //PropertySourcesProcessor add listeners to listen config changed of all namespace
181
+ assertEquals (5 , applicationListeners .size ());
182
+ assertEquals (1 , fxApolloListeners .size ());
183
+
184
+ for (ConfigChangeListener listener : applicationListeners ) {
185
+ listener .onChange (someEvent );
186
+ }
187
+
188
+ assertEquals (someEvent , bean .getChangeEvent1 ());
189
+ assertEquals (someEvent , bean .getChangeEvent2 ());
190
+ assertEquals (someEvent , bean .getChangeEvent3 ());
191
+ assertEquals (someEvent , bean .getSomeChangeEvent ());
192
+
193
+ for (ConfigChangeListener listener : fxApolloListeners ) {
194
+ listener .onChange (anotherEvent );
195
+ }
196
+
197
+ assertEquals (someEvent , bean .getChangeEvent1 ());
198
+ assertEquals (someEvent , bean .getChangeEvent2 ());
199
+ assertEquals (anotherEvent , bean .getChangeEvent3 ());
200
+ assertEquals (someEvent , bean .getSomeChangeEvent ());
201
+ }
202
+
130
203
private <T > T getBean (Class <T > beanClass , Class <?>... annotatedClasses ) {
131
204
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (annotatedClasses );
132
205
@@ -178,6 +251,24 @@ public TestApolloConfigChangeListenerBean3 bean() {
178
251
}
179
252
}
180
253
254
+ @ Configuration
255
+ @ EnableApolloConfig
256
+ static class AppConfig6 {
257
+ @ Bean
258
+ public TestApolloChildConfigBean bean () {
259
+ return new TestApolloChildConfigBean ();
260
+ }
261
+ }
262
+
263
+ @ Configuration
264
+ @ EnableApolloConfig
265
+ static class AppConfig7 {
266
+ @ Bean
267
+ public TestApolloChildConfigChangeListener bean () {
268
+ return new TestApolloChildConfigChangeListener ();
269
+ }
270
+ }
271
+
181
272
static class TestApolloConfigBean1 {
182
273
@ ApolloConfig
183
274
private Config config ;
@@ -199,11 +290,21 @@ public Config getYetAnotherConfig() {
199
290
}
200
291
}
201
292
202
- public static class TestApolloConfigBean2 {
293
+ static class TestApolloConfigBean2 {
203
294
@ ApolloConfig
204
295
private String config ;
205
296
}
206
297
298
+ static class TestApolloChildConfigBean extends TestApolloConfigBean1 {
299
+
300
+ @ ApolloConfig
301
+ private Config someConfig ;
302
+
303
+ public Config getSomeConfig () {
304
+ return someConfig ;
305
+ }
306
+ }
307
+
207
308
static class TestApolloConfigChangeListenerBean1 {
208
309
private ConfigChangeEvent changeEvent1 ;
209
310
private ConfigChangeEvent changeEvent2 ;
@@ -251,4 +352,17 @@ private void onChange(ConfigChangeEvent event, String someParam) {
251
352
}
252
353
}
253
354
355
+ static class TestApolloChildConfigChangeListener extends TestApolloConfigChangeListenerBean1 {
356
+
357
+ private ConfigChangeEvent someChangeEvent ;
358
+
359
+ @ ApolloConfigChangeListener
360
+ private void someOnChange (ConfigChangeEvent changeEvent ) {
361
+ this .someChangeEvent = changeEvent ;
362
+ }
363
+
364
+ public ConfigChangeEvent getSomeChangeEvent () {
365
+ return someChangeEvent ;
366
+ }
367
+ }
254
368
}
0 commit comments