|
21 | 21 | import org.junit.Ignore;
|
22 | 22 | import org.junit.Test;
|
23 | 23 | import org.springframework.boot.test.EnvironmentTestUtils;
|
| 24 | +import org.springframework.context.ConfigurableApplicationContext; |
| 25 | +import org.springframework.context.MessageSource; |
| 26 | +import org.springframework.context.MessageSourceResolvable; |
| 27 | +import org.springframework.context.NoSuchMessageException; |
24 | 28 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 29 | +import org.springframework.context.annotation.Bean; |
25 | 30 | import org.springframework.context.annotation.Configuration;
|
26 | 31 | import org.springframework.context.annotation.PropertySource;
|
27 | 32 |
|
@@ -107,9 +112,69 @@ public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
|
107 | 112 | this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
108 | 113 | }
|
109 | 114 |
|
| 115 | + @Test |
| 116 | + public void existingMessageSourceIsPreferred() { |
| 117 | + this.context = new AnnotationConfigApplicationContext(); |
| 118 | + this.context.register(CustomMessageSource.class, |
| 119 | + MessageSourceAutoConfiguration.class, |
| 120 | + PropertyPlaceholderAutoConfiguration.class); |
| 121 | + this.context.refresh(); |
| 122 | + assertEquals("foo", this.context.getMessage("foo", null, null, null)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void existingMessageSourceInParentIsIgnored() { |
| 127 | + ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext(); |
| 128 | + parent.refresh(); |
| 129 | + try { |
| 130 | + this.context = new AnnotationConfigApplicationContext(); |
| 131 | + this.context.setParent(parent); |
| 132 | + EnvironmentTestUtils.addEnvironment(this.context, |
| 133 | + "spring.messages.basename:test/messages"); |
| 134 | + this.context.register(MessageSourceAutoConfiguration.class, |
| 135 | + PropertyPlaceholderAutoConfiguration.class); |
| 136 | + this.context.refresh(); |
| 137 | + assertEquals("bar", |
| 138 | + this.context.getMessage("foo", null, "Foo message", Locale.UK)); |
| 139 | + } |
| 140 | + finally { |
| 141 | + parent.close(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
110 | 145 | @Configuration
|
111 | 146 | @PropertySource("classpath:/switch-messages.properties")
|
112 | 147 | protected static class Config {
|
113 | 148 |
|
114 | 149 | }
|
| 150 | + |
| 151 | + @Configuration |
| 152 | + protected static class CustomMessageSource { |
| 153 | + |
| 154 | + @Bean |
| 155 | + public MessageSource messageSource() { |
| 156 | + return new MessageSource() { |
| 157 | + |
| 158 | + @Override |
| 159 | + public String getMessage(String code, Object[] args, |
| 160 | + String defaultMessage, Locale locale) { |
| 161 | + return code; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public String getMessage(String code, Object[] args, Locale locale) |
| 166 | + throws NoSuchMessageException { |
| 167 | + return code; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String getMessage(MessageSourceResolvable resolvable, |
| 172 | + Locale locale) throws NoSuchMessageException { |
| 173 | + return resolvable.getCodes()[0]; |
| 174 | + } |
| 175 | + |
| 176 | + }; |
| 177 | + } |
| 178 | + |
| 179 | + } |
115 | 180 | }
|
0 commit comments