Skip to content

Commit c3378fd

Browse files
committed
AbstractMessageSource properly interacts with non-AbstractMessageSource parent
Issue: SPR-16047
1 parent 950edf8 commit c3378fd

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ protected String getMessageFromParent(String code, @Nullable Object[] args, Loca
263263
}
264264
else {
265265
// Check parent MessageSource, returning null if not found there.
266-
return parent.getMessage(code, args, null, locale);
266+
// Covers custom MessageSource impls and DelegatingMessageSource.
267+
String msg = parent.getMessage(code, args, null, locale);
268+
return ("".equals(msg) ? null : msg);
267269
}
268270
}
269271
// Not found in parent either.

spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -210,6 +210,39 @@ public void testDefaultApplicationContextMessageSource() {
210210
assertEquals("default value", ac.getMessage("code1", new Object[] {"value"}, "default {0}", Locale.ENGLISH));
211211
}
212212

213+
@Test
214+
public void testDefaultApplicationContextMessageSourceWithParent() {
215+
GenericApplicationContext ac = new GenericApplicationContext();
216+
GenericApplicationContext parent = new GenericApplicationContext();
217+
parent.refresh();
218+
ac.setParent(parent);
219+
ac.refresh();
220+
assertEquals("default", ac.getMessage("code1", null, "default", Locale.ENGLISH));
221+
assertEquals("default value", ac.getMessage("code1", new Object[] {"value"}, "default {0}", Locale.ENGLISH));
222+
}
223+
224+
@Test
225+
public void testStaticApplicationContextMessageSourceWithStaticParent() {
226+
StaticApplicationContext ac = new StaticApplicationContext();
227+
StaticApplicationContext parent = new StaticApplicationContext();
228+
parent.refresh();
229+
ac.setParent(parent);
230+
ac.refresh();
231+
assertEquals("default", ac.getMessage("code1", null, "default", Locale.ENGLISH));
232+
assertEquals("default value", ac.getMessage("code1", new Object[] {"value"}, "default {0}", Locale.ENGLISH));
233+
}
234+
235+
@Test
236+
public void testStaticApplicationContextMessageSourceWithDefaultParent() {
237+
StaticApplicationContext ac = new StaticApplicationContext();
238+
GenericApplicationContext parent = new GenericApplicationContext();
239+
parent.refresh();
240+
ac.setParent(parent);
241+
ac.refresh();
242+
assertEquals("default", ac.getMessage("code1", null, "default", Locale.ENGLISH));
243+
assertEquals("default value", ac.getMessage("code1", new Object[] {"value"}, "default {0}", Locale.ENGLISH));
244+
}
245+
213246
@Test
214247
public void testResourceBundleMessageSourceStandalone() {
215248
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();

0 commit comments

Comments
 (0)