Applying Sebb's test and fix from LANG-368 - fixing it so that FastDateFormat getDateInstance and getDateTimeInstance continue to work if Locale.getDefault() changes

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@590551 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-10-31 03:58:52 +00:00
parent 43cf3f491e
commit 127e6e338f
2 changed files with 48 additions and 12 deletions

View File

@ -281,16 +281,15 @@ public static synchronized FastDateFormat getDateInstance(int style, TimeZone ti
if (timeZone != null) {
key = new Pair(key, timeZone);
}
if (locale != null) {
key = new Pair(key, locale);
}
FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key);
if (format == null) {
if (locale == null) {
locale = Locale.getDefault();
}
key = new Pair(key, locale);
FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key);
if (format == null) {
try {
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
String pattern = formatter.toPattern();
@ -461,16 +460,13 @@ public static synchronized FastDateFormat getDateTimeInstance(int dateStyle, int
if (timeZone != null) {
key = new Pair(key, timeZone);
}
if (locale != null) {
key = new Pair(key, locale);
}
FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key);
if (format == null) {
if (locale == null) {
locale = Locale.getDefault();
}
key = new Pair(key, locale);
FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key);
if (format == null) {
try {
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyle, timeStyle,
locale);

View File

@ -132,6 +132,46 @@ public void test_getInstance_String_Locale() {
}
}
public void test_changeDefault_Locale_DateInstance() {
Locale realDefaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
FastDateFormat format3 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
assertSame(Locale.GERMANY, format1.getLocale());
assertSame(Locale.US, format2.getLocale());
assertSame(Locale.GERMANY, format3.getLocale());
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertTrue(format2 != format3);
} finally {
Locale.setDefault(realDefaultLocale);
}
}
public void test_changeDefault_Locale_DateTimeInstance() {
Locale realDefaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
FastDateFormat format3 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
assertSame(Locale.GERMANY, format1.getLocale());
assertSame(Locale.US, format2.getLocale());
assertSame(Locale.GERMANY, format3.getLocale());
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertTrue(format2 != format3);
} finally {
Locale.setDefault(realDefaultLocale);
}
}
public void test_getInstance_String_TimeZone_Locale() {
Locale realDefaultLocale = Locale.getDefault();
TimeZone realDefaultZone = TimeZone.getDefault();