LANG-1127 Create a base test for the time package, which sets and resets default Locales and TimeZones

This commit is contained in:
Chas Honton 2015-05-02 11:30:32 -07:00
parent 0add1e8975
commit b37837ce63
9 changed files with 326 additions and 217 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1127" type="add" dev="chas">Unit test helpers which set and reset default Locale and TimeZone</action>
<action issue="LANG-1128" type="fix" dev="britter" due-to="jacktan1991">JsonToStringStyle doesn't handle chars and objects correctly</action>
<action issue="LANG-456" type="fix" dev="britter" due-to="Bob Fields, Woosan Ko, Bruno P. Kinoshita">HashCodeBuilder throws StackOverflowError in bidirectional navigable association</action>
<action issue="LANG-1126" type="fix" dev="britter">DateFormatUtilsTest.testSMTP depends on the default Locale</action>

View File

@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue;
import java.util.Locale;
import org.apache.commons.lang3.test.DefaultLocale;
import org.hamcrest.core.IsNot;
import org.junit.Test;
@ -231,8 +232,6 @@ public class StringUtilsEqualsIndexOfTest {
@Test
public void testContainsIgnoreCase_LocaleIndependence() {
final Locale orig = Locale.getDefault();
final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
final String[][] tdata = {
@ -247,21 +246,22 @@ public class StringUtilsEqualsIndexOfTest {
{ "\u00DF", "SS" },
};
try {
for (final Locale locale : locales) {
Locale.setDefault(locale);
for (int j = 0; j < tdata.length; j++) {
assertTrue(Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1], StringUtils
.containsIgnoreCase(tdata[j][0], tdata[j][1]));
}
for (int j = 0; j < fdata.length; j++) {
assertFalse(Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1], StringUtils
.containsIgnoreCase(fdata[j][0], fdata[j][1]));
new DefaultLocale<RuntimeException>(Locale.ENGLISH) {
@Override
public void test() {
for (final Locale locale : locales) {
Locale.setDefault(locale);
for (int j = 0; j < tdata.length; j++) {
assertTrue(Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1], StringUtils
.containsIgnoreCase(tdata[j][0], tdata[j][1]));
}
for (int j = 0; j < fdata.length; j++) {
assertFalse(Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1], StringUtils
.containsIgnoreCase(fdata[j][0], fdata[j][1]));
}
}
}
} finally {
Locale.setDefault(orig);
}
};
}
@Test

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.test;
import java.util.Locale;
/**
* run a test with a different default Locale
*/
public abstract class DefaultLocale<E extends Throwable> {
public DefaultLocale(Locale targetLocale) throws E {
// only one test at a time may change default
synchronized (getClass()) {
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(targetLocale);
test();
} finally {
Locale.setDefault(defaultLocale);
}
}
}
/**
* Implement test in this method
*/
abstract public void test() throws E;
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.test;
import java.util.TimeZone;
/**
* run a test with a different default TimeZone
*/
public abstract class DefaultTimeZone<E extends Throwable> {
public DefaultTimeZone(TimeZone targetZone) throws E {
// only one test at a time may change default
synchronized (getClass()) {
TimeZone defaultZone = TimeZone.getDefault();
try {
TimeZone.setDefault(targetZone);
test();
} finally {
TimeZone.setDefault(defaultZone);
}
}
}
/**
* Implement test in this method
*/
abstract public void test() throws E;
}

View File

@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.test;
import java.util.Locale;
import java.util.TimeZone;
/**
* run a test with a different default TimeZone and Locale
*/
public abstract class DefaultTimeZoneAndLocale<E extends Throwable> {
public DefaultTimeZoneAndLocale(TimeZone targetZone, final Locale targetLocale) throws E {
new DefaultTimeZone<E>(targetZone) {
@Override
public void test() throws E {
new DefaultLocale<E>(targetLocale) {
@Override
public void test() throws E {
DefaultTimeZoneAndLocale.this.test();
}
};
}
};
}
/**
* Implement test in this method
*/
abstract public void test() throws E;
}

View File

@ -29,6 +29,8 @@ import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang3.test.DefaultLocale;
import org.apache.commons.lang3.test.DefaultTimeZone;
import org.junit.Test;
/**
@ -167,22 +169,21 @@ public class DateFormatUtilsTest {
@Test
public void testSMTP() {
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
timeZone = TimeZone.getTimeZone("UTC");
june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
} finally {
Locale.setDefault(defaultLocale);
}
new DefaultLocale<RuntimeException>(Locale.ENGLISH) {
@Override
public void test() {
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
timeZone = TimeZone.getTimeZone("UTC");
june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
}
};
}
/*
@ -221,18 +222,16 @@ public class DateFormatUtilsTest {
@Test
public void testLang530() throws ParseException {
TimeZone save = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
final Date d = new Date();
final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
final Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
// the format loses milliseconds so have to reintroduce them
assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
}
finally {
TimeZone.setDefault(save);
}
new DefaultTimeZone<ParseException>(TimeZone.getTimeZone("UTC")) {
@Override
public void test() throws ParseException {
final Date d = new Date();
final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
final Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
// the format loses milliseconds so have to reintroduce them
assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
}
};
}
/**

View File

@ -37,6 +37,8 @@ import java.util.NoSuchElementException;
import java.util.TimeZone;
import junit.framework.AssertionFailedError;
import org.apache.commons.lang3.test.DefaultLocale;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -1561,62 +1563,57 @@ public class DateUtilsTest {
@Test
public void testLANG799_EN_OK() throws ParseException {
final Locale dflt = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
try {
DateUtils.parseDate("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
DateUtils.parseDateStrictly("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} finally {
Locale.setDefault(dflt);
}
new DefaultLocale<ParseException>(Locale.ENGLISH){
@Override
public void test() throws ParseException {
DateUtils.parseDate("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
DateUtils.parseDateStrictly("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
}
};
}
// Parse German date with English Locale
@Test(expected=ParseException.class)
public void testLANG799_EN_FAIL() throws ParseException {
final Locale dflt = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
try {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} finally {
Locale.setDefault(dflt);
}
new DefaultLocale<ParseException>(Locale.ENGLISH){
@Override
public void test() throws ParseException {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
}
};
}
@Test
public void testLANG799_DE_OK() throws ParseException {
final Locale dflt = Locale.getDefault();
Locale.setDefault(Locale.GERMAN);
try {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
DateUtils.parseDateStrictly("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} finally {
Locale.setDefault(dflt);
}
new DefaultLocale<ParseException>(Locale.GERMAN){
@Override
public void test() throws ParseException {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
DateUtils.parseDateStrictly("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
}
};
}
// Parse English date with German Locale
@Test(expected=ParseException.class)
public void testLANG799_DE_FAIL() throws ParseException {
final Locale dflt = Locale.getDefault();
Locale.setDefault(Locale.GERMAN);
try {
DateUtils.parseDate("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} finally {
Locale.setDefault(dflt);
}
new DefaultLocale<ParseException>(Locale.GERMAN){
@Override
public void test() throws ParseException {
DateUtils.parseDate("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
}
};
}
// Parse German date with English Locale, specifying German Locale override
@Test
public void testLANG799_EN_WITH_DE_LOCALE() throws ParseException {
final Locale dflt = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
try {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", Locale.GERMAN, "EEE, dd MMM yyyy HH:mm:ss zzz");
} finally {
Locale.setDefault(dflt);
}
new DefaultLocale<ParseException>(Locale.ENGLISH){
@Override
public void test() throws ParseException {
DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", Locale.GERMAN, "EEE, dd MMM yyyy HH:mm:ss zzz");
}
};
}
/**

View File

@ -34,6 +34,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.test.DefaultLocale;
import org.apache.commons.lang3.test.DefaultTimeZoneAndLocale;
import org.junit.Test;
/**
@ -71,119 +73,101 @@ public class FastDateFormatTest {
@Test
public void test_getInstance_String_TimeZone() {
final Locale realDefaultLocale = Locale.getDefault();
final TimeZone realDefaultZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
new DefaultTimeZoneAndLocale<RuntimeException>(TimeZone.getTimeZone("America/New_York"), Locale.US) {
@Override
public void test() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"));
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format4 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format5 = FastDateFormat.getInstance("MM-DD-yyyy", TimeZone.getDefault());
final FastDateFormat format6 = FastDateFormat.getInstance("MM-DD-yyyy");
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"));
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format4 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format5 = FastDateFormat.getInstance("MM-DD-yyyy", TimeZone.getDefault());
final FastDateFormat format6 = FastDateFormat.getInstance("MM-DD-yyyy");
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertSame(format3, format4);
assertTrue(format3 != format5); // -- junit 3.8 version -- assertFalse(format3 == format5);
assertTrue(format4 != format6); // -- junit 3.8 version -- assertFalse(format3 == format5);
} finally {
Locale.setDefault(realDefaultLocale);
TimeZone.setDefault(realDefaultZone);
}
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertSame(format3, format4);
assertTrue(format3 != format5); // -- junit 3.8 version -- assertFalse(format3 == format5);
assertTrue(format4 != format6); // -- junit 3.8 version -- assertFalse(format3 == format5);
}
};
}
@Test
public void test_getInstance_String_Locale() {
final Locale realDefaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
new DefaultLocale<RuntimeException>(Locale.US) {
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertSame(format1, format3);
assertEquals(Locale.GERMANY, format1.getLocale());
} finally {
Locale.setDefault(realDefaultLocale);
}
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
assertSame(format1, format3);
assertEquals(Locale.GERMANY, format1.getLocale());
}
};
}
@Test
public void test_changeDefault_Locale_DateInstance() {
final Locale realDefaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
final FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
final FastDateFormat format3 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
new DefaultLocale<RuntimeException>(Locale.US) {
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
final 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);
}
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);
}
};
}
@Test
public void test_changeDefault_Locale_DateTimeInstance() {
final Locale realDefaultLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
final FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
final FastDateFormat format3 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
new DefaultLocale<RuntimeException>(Locale.US) {
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY);
final 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);
}
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);
}
};
}
@Test
public void test_getInstance_String_TimeZone_Locale() {
final Locale realDefaultLocale = Locale.getDefault();
final TimeZone realDefaultZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
new DefaultTimeZoneAndLocale<RuntimeException>(TimeZone.getTimeZone("America/New_York"), Locale.US) {
@Override
public void test() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"), Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getDefault(), Locale.GERMANY);
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"), Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getDefault(), Locale.GERMANY);
assertTrue(format1 != format2); // -- junit 3.8 version -- assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertEquals(TimeZone.getDefault(), format3.getTimeZone());
assertEquals(Locale.GERMANY, format1.getLocale());
assertEquals(Locale.GERMANY, format2.getLocale());
assertEquals(Locale.GERMANY, format3.getLocale());
} finally {
Locale.setDefault(realDefaultLocale);
TimeZone.setDefault(realDefaultZone);
}
assertTrue(format1 != format2); // -- junit 3.8 version -- assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertEquals(TimeZone.getDefault(), format3.getTimeZone());
assertEquals(Locale.GERMANY, format1.getLocale());
assertEquals(Locale.GERMANY, format2.getLocale());
assertEquals(Locale.GERMANY, format3.getLocale());
}
};
}
@Test

View File

@ -30,6 +30,7 @@ import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.test.DefaultTimeZoneAndLocale;
import org.junit.Test;
/**
@ -75,58 +76,54 @@ public class FastDatePrinterTest {
@Test
public void testFormat() {
final Locale realDefaultLocale = Locale.getDefault();
final TimeZone realDefaultZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.US);
TimeZone.setDefault(NEW_YORK);
new DefaultTimeZoneAndLocale<RuntimeException>(NEW_YORK, Locale.US) {
@Override
public void test() {
final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
final GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 0, 0);
final Date date1 = cal1.getTime();
final Date date2 = cal2.getTime();
final long millis1 = date1.getTime();
final long millis2 = date2.getTime();
final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
final GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 0, 0);
final Date date1 = cal1.getTime();
final Date date2 = cal2.getTime();
final long millis1 = date1.getTime();
final long millis2 = date2.getTime();
DatePrinter fdf = getInstance("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
assertEquals(sdf.format(date1), fdf.format(date1));
assertEquals("2003-01-10T15:33:20", fdf.format(date1));
assertEquals("2003-01-10T15:33:20", fdf.format(cal1));
assertEquals("2003-01-10T15:33:20", fdf.format(millis1));
assertEquals("2003-07-10T09:00:00", fdf.format(date2));
assertEquals("2003-07-10T09:00:00", fdf.format(cal2));
assertEquals("2003-07-10T09:00:00", fdf.format(millis2));
DatePrinter fdf = getInstance("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
assertEquals(sdf.format(date1), fdf.format(date1));
assertEquals("2003-01-10T15:33:20", fdf.format(date1));
assertEquals("2003-01-10T15:33:20", fdf.format(cal1));
assertEquals("2003-01-10T15:33:20", fdf.format(millis1));
assertEquals("2003-07-10T09:00:00", fdf.format(date2));
assertEquals("2003-07-10T09:00:00", fdf.format(cal2));
assertEquals("2003-07-10T09:00:00", fdf.format(millis2));
fdf = getInstance("Z");
assertEquals("-0500", fdf.format(date1));
assertEquals("-0500", fdf.format(cal1));
assertEquals("-0500", fdf.format(millis1));
fdf = getInstance("Z");
assertEquals("-0500", fdf.format(date1));
assertEquals("-0500", fdf.format(cal1));
assertEquals("-0500", fdf.format(millis1));
assertEquals("-0400", fdf.format(date2));
assertEquals("-0400", fdf.format(cal2));
assertEquals("-0400", fdf.format(millis2));
assertEquals("-0400", fdf.format(date2));
assertEquals("-0400", fdf.format(cal2));
assertEquals("-0400", fdf.format(millis2));
fdf = getInstance("ZZ");
assertEquals("-05:00", fdf.format(date1));
assertEquals("-05:00", fdf.format(cal1));
assertEquals("-05:00", fdf.format(millis1));
fdf = getInstance("ZZ");
assertEquals("-05:00", fdf.format(date1));
assertEquals("-05:00", fdf.format(cal1));
assertEquals("-05:00", fdf.format(millis1));
assertEquals("-04:00", fdf.format(date2));
assertEquals("-04:00", fdf.format(cal2));
assertEquals("-04:00", fdf.format(millis2));
assertEquals("-04:00", fdf.format(date2));
assertEquals("-04:00", fdf.format(cal2));
assertEquals("-04:00", fdf.format(millis2));
final String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M" +
" dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
fdf = getInstance(pattern);
sdf = new SimpleDateFormat(pattern);
// SDF bug fix starting with Java 7
assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1));
assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
} finally {
Locale.setDefault(realDefaultLocale);
TimeZone.setDefault(realDefaultZone);
}
final String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M" +
" dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
fdf = getInstance(pattern);
sdf = new SimpleDateFormat(pattern);
// SDF bug fix starting with Java 7
assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1));
assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
}
};
}
/**