ResourceBundleMessageSource supports "defaultEncoding", "fallbackToSystemLocale", "cacheSeconds"

These features require Java 6 or higher due to their dependency on the ResourceBundle.Control class. To some degree, ResourceBundleMessageSource catches up with ReloadableResourceBundleMessageSource now. However, as noted in the javadoc, there are still severe limitations in the standard ResourceBundle class that justify an ongoing investment in our own ReloadableResourceBundleMessageSource (based on the Spring resource abstraction, with manual parsing of properties files).

Issue: SPR-7392
This commit is contained in:
Juergen Hoeller
2012-09-26 16:46:25 +02:00
parent 437ce9bb0e
commit c81543e1a4
3 changed files with 276 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.context.support;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import junit.framework.TestCase;
@@ -46,6 +47,20 @@ public class ResourceBundleMessageSourceTests extends TestCase {
doTestMessageAccess(false, true, true, true, false);
}
public void testMessageAccessWithDefaultMessageSourceAndFallbackTurnedOff() {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16) {
return;
}
doTestMessageAccess(false, false, false, false, false);
}
public void testMessageAccessWithDefaultMessageSourceAndFallbackTurnedOffAndFallbackToGerman() {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16) {
return;
}
doTestMessageAccess(false, false, true, true, false);
}
public void testMessageAccessWithReloadableMessageSource() {
doTestMessageAccess(true, true, false, false, false);
}
@@ -82,13 +97,13 @@ public class ResourceBundleMessageSourceTests extends TestCase {
String[] basenames = null;
if (reloadable) {
basenames = new String[] {
"classpath:" + basepath + "messages",
"classpath:" + basepath + "more-messages"};
"classpath:" + basepath + "messages",
"classpath:" + basepath + "more-messages"};
}
else {
basenames = new String[] {
basepath + "messages",
basepath + "more-messages"};
basepath + "messages",
basepath + "more-messages"};
}
pvs.add("basenames", basenames);
if (!fallbackToSystemLocale) {
@@ -202,6 +217,31 @@ public class ResourceBundleMessageSourceTests extends TestCase {
assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN));
}
public void testResourceBundleMessageSourceWithDefaultCharset() {
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
ms.setBasename("org/springframework/context/support/messages");
ms.setDefaultEncoding("ISO-8859-1");
assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH));
assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN));
}
public void testResourceBundleMessageSourceWithInappropriateDefaultCharset() {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16) {
return;
}
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
ms.setBasename("org/springframework/context/support/messages");
ms.setDefaultEncoding("argh");
ms.setFallbackToSystemLocale(false);
try {
ms.getMessage("code1", null, Locale.ENGLISH);
fail("Should have thrown NoSuchMessageException");
}
catch (NoSuchMessageException ex) {
// expected
}
}
public void testReloadableResourceBundleMessageSourceStandalone() {
ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
ms.setBasename("org/springframework/context/support/messages");
@@ -300,4 +340,11 @@ public class ResourceBundleMessageSourceTests extends TestCase {
assertEquals(0, filenames.size());
}
@Override
protected void tearDown() throws Exception {
if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_16) {
ResourceBundle.clearCache();
}
}
}