Commit ede385d1 authored by Dave Syer's avatar Dave Syer

Modify @Condition for detecting existing message bundle resource

The problem with the old code is that it worces a ResourceBundle to
initialize with the default encoding (and that is then cached in the
JDK) during @Condition evaluation (so before the encoding is known).

Includes test for swedish messages

Fixes gh-1228
parent f7a5ee3b
...@@ -16,9 +16,7 @@ ...@@ -16,9 +16,7 @@
package org.springframework.boot.autoconfigure; package org.springframework.boot.autoconfigure;
import java.util.Locale; import java.io.IOException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition; import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
...@@ -34,6 +32,8 @@ import org.springframework.context.annotation.Configuration; ...@@ -34,6 +32,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -102,23 +102,28 @@ public class MessageSourceAutoConfiguration { ...@@ -102,23 +102,28 @@ public class MessageSourceAutoConfiguration {
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
String basename = context.getEnvironment().getProperty( String basename = context.getEnvironment().getProperty(
"spring.messages.basename", "messages"); "spring.messages.basename", "messages");
if (!StringUtils.hasText(basename)) {
return ConditionOutcome.noMatch("Empty spring.messages.basename");
}
for (String name : commaDelimitedListToStringArray(trimAllWhitespace(basename))) { for (String name : commaDelimitedListToStringArray(trimAllWhitespace(basename))) {
Resource[] resources;
try { try {
ResourceBundle.getBundle(name, Locale.getDefault(), resources = new PathMatchingResourcePatternResolver(
context.getClassLoader()); context.getClassLoader()).getResources("classpath*:" + name
+ "*.properties");
}
catch (IOException e) {
continue;
} }
catch (MissingResourceException e) { for (Resource resource : resources) {
return ConditionOutcome
.noMatch("Bundle found for spring.messages.basename: " + name); if (resource.exists()) {
return ConditionOutcome
.match("Bundle found for spring.messages.basename: "
+ name);
}
} }
} }
return ConditionOutcome.match("Bundle found for spring.messages.basename: " return ConditionOutcome
+ basename); .noMatch("No bundle found for spring.messages.basename: " + basename);
} }
} }
} }
...@@ -58,6 +58,18 @@ public class MessageSourceAutoConfigurationTests { ...@@ -58,6 +58,18 @@ public class MessageSourceAutoConfigurationTests {
this.context.getMessage("foo", null, "Foo message", Locale.UK)); this.context.getMessage("foo", null, "Foo message", Locale.UK));
} }
@Test
public void testEncodingWorks() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/swedish");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertEquals("Some text with some swedish öäå!",
this.context.getMessage("foo", null, "Foo message", Locale.UK));
}
@Test @Test
public void testMultipleMessageSourceCreated() throws Exception { public void testMultipleMessageSourceCreated() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
......
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