LANG-1127: Use JUnit rules to set and reset the default Locale and TimeZone.

This commit is contained in:
Benedikt Ritter 2015-05-03 14:20:03 +02:00
parent b37837ce63
commit abfcf0a8cf
10 changed files with 479 additions and 326 deletions

View File

@ -23,8 +23,9 @@ import static org.junit.Assert.assertTrue;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.lang3.test.DefaultLocale; import org.apache.commons.lang3.time.TestLocale;
import org.hamcrest.core.IsNot; import org.hamcrest.core.IsNot;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
/** /**
@ -33,6 +34,10 @@ import org.junit.Test;
* @version $Id$ * @version $Id$
*/ */
public class StringUtilsEqualsIndexOfTest { public class StringUtilsEqualsIndexOfTest {
@Rule
public TestLocale locale = TestLocale.usingDefaultLocale();
private static final String BAR = "bar"; private static final String BAR = "bar";
/** /**
* Supplementary character U+20000 * Supplementary character U+20000
@ -246,11 +251,8 @@ public class StringUtilsEqualsIndexOfTest {
{ "\u00DF", "SS" }, { "\u00DF", "SS" },
}; };
new DefaultLocale<RuntimeException>(Locale.ENGLISH) { for (final Locale testLocale : locales) {
@Override locale.setLocale(testLocale);
public void test() {
for (final Locale locale : locales) {
Locale.setDefault(locale);
for (int j = 0; j < tdata.length; j++) { for (int j = 0; j < tdata.length; j++) {
assertTrue(Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1], StringUtils assertTrue(Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1], StringUtils
.containsIgnoreCase(tdata[j][0], tdata[j][1])); .containsIgnoreCase(tdata[j][0], tdata[j][1]));
@ -261,8 +263,6 @@ public class StringUtilsEqualsIndexOfTest {
} }
} }
} }
};
}
@Test @Test
public void testContainsIgnoreCase_StringString() { public void testContainsIgnoreCase_StringString() {

View File

@ -1,43 +0,0 @@
/*
* 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

@ -1,43 +0,0 @@
/*
* 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

@ -1,45 +0,0 @@
/*
* 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,8 +29,7 @@ import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import org.apache.commons.lang3.test.DefaultLocale; import org.junit.Rule;
import org.apache.commons.lang3.test.DefaultTimeZone;
import org.junit.Test; import org.junit.Test;
/** /**
@ -39,6 +38,11 @@ import org.junit.Test;
*/ */
public class DateFormatUtilsTest { public class DateFormatUtilsTest {
@Rule
public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone();
@Rule
public TestLocale locale = TestLocale.usingDefaultLocale();
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Test @Test
public void testConstructor() { public void testConstructor() {
@ -169,9 +173,8 @@ public class DateFormatUtilsTest {
@Test @Test
public void testSMTP() { public void testSMTP() {
new DefaultLocale<RuntimeException>(Locale.ENGLISH) { locale.setLocale(Locale.ENGLISH);
@Override
public void test() {
TimeZone timeZone = TimeZone.getTimeZone("GMT-3"); TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar june = createJuneTestDate(timeZone); Calendar june = createJuneTestDate(timeZone);
@ -183,8 +186,6 @@ public class DateFormatUtilsTest {
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june); timeZone, june);
} }
};
}
/* /*
public void testLang312() { public void testLang312() {
@ -222,17 +223,14 @@ public class DateFormatUtilsTest {
@Test @Test
public void testLang530() throws ParseException { public void testLang530() throws ParseException {
new DefaultTimeZone<ParseException>(TimeZone.getTimeZone("UTC")) { timeZone.setTimeZone("UTC");
@Override
public void test() throws ParseException {
final Date d = new Date(); final Date d = new Date();
final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d); 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() }); final Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
// the format loses milliseconds so have to reintroduce them // 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); assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
} }
};
}
/** /**
* According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916), * According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916),

View File

@ -37,10 +37,9 @@ import java.util.NoSuchElementException;
import java.util.TimeZone; import java.util.TimeZone;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import org.apache.commons.lang3.test.DefaultLocale;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
/** /**
@ -58,6 +57,9 @@ public class DateUtilsTest {
BASE_DATE = cal.getTime(); BASE_DATE = cal.getTime();
} }
@Rule
public TestLocale locale = TestLocale.usingDefaultLocale();
private DateFormat dateParser = null; private DateFormat dateParser = null;
private DateFormat dateTimeParser = null; private DateFormat dateTimeParser = null;
private Date dateAmPm1 = null; private Date dateAmPm1 = null;
@ -1563,58 +1565,43 @@ public class DateUtilsTest {
@Test @Test
public void testLANG799_EN_OK() throws ParseException { public void testLANG799_EN_OK() throws ParseException {
new DefaultLocale<ParseException>(Locale.ENGLISH){ locale.setLocale(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.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"); DateUtils.parseDateStrictly("Wed, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} }
};
}
// Parse German date with English Locale // Parse German date with English Locale
@Test(expected = ParseException.class) @Test(expected = ParseException.class)
public void testLANG799_EN_FAIL() throws ParseException { public void testLANG799_EN_FAIL() throws ParseException {
new DefaultLocale<ParseException>(Locale.ENGLISH){ locale.setLocale(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"); DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} }
};
}
@Test @Test
public void testLANG799_DE_OK() throws ParseException { public void testLANG799_DE_OK() throws ParseException {
new DefaultLocale<ParseException>(Locale.GERMAN){ locale.setLocale(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.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"); DateUtils.parseDateStrictly("Mi, 09 Apr 2008 23:55:38 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz");
} }
};
}
// Parse English date with German Locale // Parse English date with German Locale
@Test(expected=ParseException.class) @Test(expected=ParseException.class)
public void testLANG799_DE_FAIL() throws ParseException { public void testLANG799_DE_FAIL() throws ParseException {
new DefaultLocale<ParseException>(Locale.GERMAN){ locale.setLocale(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"); 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 // Parse German date with English Locale, specifying German Locale override
@Test @Test
public void testLANG799_EN_WITH_DE_LOCALE() throws ParseException { public void testLANG799_EN_WITH_DE_LOCALE() throws ParseException {
new DefaultLocale<ParseException>(Locale.ENGLISH){ locale.setLocale(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"); DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", Locale.GERMAN, "EEE, dd MMM yyyy HH:mm:ss zzz");
} }
};
}
/** /**
* This checks that this is a 7 element iterator of Calendar objects * This checks that this is a 7 element iterator of Calendar objects

View File

@ -18,6 +18,7 @@ package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -34,8 +35,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.test.DefaultLocale; import org.junit.Rule;
import org.apache.commons.lang3.test.DefaultTimeZoneAndLocale;
import org.junit.Test; import org.junit.Test;
/** /**
@ -46,6 +46,12 @@ import org.junit.Test;
*/ */
public class FastDateFormatTest { public class FastDateFormatTest {
@Rule
public TestLocale locale = TestLocale.usingDefaultLocale();
@Rule
public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone();
/* /*
* Only the cache methods need to be tested here. * Only the cache methods need to be tested here.
* The print methods are tested by {@link FastDateFormat_PrinterTest} * The print methods are tested by {@link FastDateFormat_PrinterTest}
@ -73,9 +79,9 @@ public class FastDateFormatTest {
@Test @Test
public void test_getInstance_String_TimeZone() { public void test_getInstance_String_TimeZone() {
new DefaultTimeZoneAndLocale<RuntimeException>(TimeZone.getTimeZone("America/New_York"), Locale.US) { locale.setLocale(Locale.US);
@Override timeZone.setTimeZone("America/New_York");
public void test() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik")); TimeZone.getTimeZone("Atlantic/Reykjavik"));
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy"); final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
@ -84,37 +90,31 @@ public class FastDateFormatTest {
final FastDateFormat format5 = 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 format6 = FastDateFormat.getInstance("MM-DD-yyyy");
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone()); assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone()); assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertSame(format3, format4); assertSame(format3, format4);
assertTrue(format3 != format5); // -- junit 3.8 version -- assertFalse(format3 == format5); assertNotSame(format3, format5);
assertTrue(format4 != format6); // -- junit 3.8 version -- assertFalse(format3 == format5); assertNotSame(format4, format6);
}
};
} }
@Test @Test
public void test_getInstance_String_Locale() { public void test_getInstance_String_Locale() {
new DefaultLocale<RuntimeException>(Locale.US) { locale.setLocale(Locale.US);
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY); final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy"); final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY); final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); assertNotSame(format1, format2);
assertSame(format1, format3); assertSame(format1, format3);
assertEquals(Locale.GERMANY, format1.getLocale()); assertEquals(Locale.GERMANY, format1.getLocale());
} }
};
}
@Test @Test
public void test_changeDefault_Locale_DateInstance() { public void test_changeDefault_Locale_DateInstance() {
new DefaultLocale<RuntimeException>(Locale.US) { locale.setLocale(Locale.US);
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY); final FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL); final FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY); Locale.setDefault(Locale.GERMANY);
@ -123,17 +123,14 @@ public class FastDateFormatTest {
assertSame(Locale.GERMANY, format1.getLocale()); assertSame(Locale.GERMANY, format1.getLocale());
assertSame(Locale.US, format2.getLocale()); assertSame(Locale.US, format2.getLocale());
assertSame(Locale.GERMANY, format3.getLocale()); assertSame(Locale.GERMANY, format3.getLocale());
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); assertNotSame(format1, format2);
assertTrue(format2 != format3); assertNotSame(format2, format3);
}
};
} }
@Test @Test
public void test_changeDefault_Locale_DateTimeInstance() { public void test_changeDefault_Locale_DateTimeInstance() {
new DefaultLocale<RuntimeException>(Locale.US) { locale.setLocale(Locale.US);
@Override
public void test() throws RuntimeException {
final FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY); final FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL); final FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
Locale.setDefault(Locale.GERMANY); Locale.setDefault(Locale.GERMANY);
@ -142,24 +139,22 @@ public class FastDateFormatTest {
assertSame(Locale.GERMANY, format1.getLocale()); assertSame(Locale.GERMANY, format1.getLocale());
assertSame(Locale.US, format2.getLocale()); assertSame(Locale.US, format2.getLocale());
assertSame(Locale.GERMANY, format3.getLocale()); assertSame(Locale.GERMANY, format3.getLocale());
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); assertNotSame(format1, format2);
assertTrue(format2 != format3); assertNotSame(format2, format3);
}
};
} }
@Test @Test
public void test_getInstance_String_TimeZone_Locale() { public void test_getInstance_String_TimeZone_Locale() {
new DefaultTimeZoneAndLocale<RuntimeException>(TimeZone.getTimeZone("America/New_York"), Locale.US) { locale.setLocale(Locale.US);
@Override timeZone.setTimeZone("America/New_York");
public void test() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"), Locale.GERMANY); TimeZone.getTimeZone("Atlantic/Reykjavik"), Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY); final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getDefault(), Locale.GERMANY); TimeZone.getDefault(), Locale.GERMANY);
assertTrue(format1 != format2); // -- junit 3.8 version -- assertNotSame(format1, format2); assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone()); assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone()); assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertEquals(TimeZone.getDefault(), format3.getTimeZone()); assertEquals(TimeZone.getDefault(), format3.getTimeZone());
@ -167,8 +162,6 @@ public class FastDateFormatTest {
assertEquals(Locale.GERMANY, format2.getLocale()); assertEquals(Locale.GERMANY, format2.getLocale());
assertEquals(Locale.GERMANY, format3.getLocale()); assertEquals(Locale.GERMANY, format3.getLocale());
} }
};
}
@Test @Test
public void testCheckDefaults() { public void testCheckDefaults() {

View File

@ -30,7 +30,9 @@ import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.test.DefaultTimeZoneAndLocale; import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
/** /**
@ -74,11 +76,16 @@ public class FastDatePrinterTest {
return new FastDatePrinter(format, timeZone, locale); return new FastDatePrinter(format, timeZone, locale);
} }
@Rule
public TestLocale locale = TestLocale.usingDefaultLocale();
@Rule
public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone();
@Test @Test
public void testFormat() { public void testFormat() {
new DefaultTimeZoneAndLocale<RuntimeException>(NEW_YORK, Locale.US) { locale.setLocale(Locale.US);
@Override timeZone.setTimeZone(NEW_YORK);
public void test() {
final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20); final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
final GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 0, 0); final GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 0, 0);
@ -123,8 +130,6 @@ public class FastDatePrinterTest {
assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1)); 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)); assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
} }
};
}
/** /**
* Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}. * Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}.
@ -264,6 +269,8 @@ public class FastDatePrinterTest {
@Test @Test
public void testTimeZoneAsZ() throws Exception { public void testTimeZoneAsZ() throws Exception {
timeZone.setTimeZone("UTC");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
FastDateFormat noColonFormat = FastDateFormat.getInstance("Z"); FastDateFormat noColonFormat = FastDateFormat.getInstance("Z");
assertEquals("+0000", noColonFormat.format(c)); assertEquals("+0000", noColonFormat.format(c));

View File

@ -0,0 +1,136 @@
/*
* 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.time;
import java.util.Locale;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* Rule implementation that sets and resets the default Locale.
*
* <p>
* Set up tests to use {@code Locale.getDefault()} by creating a TestLocale rule using {@link #usingDefaultLocale()}.
* To override the default Locale for a single test method, use {@link #setLocale(Locale)}. The TestLocale rule will
* make sure, that the default Locale is restored after each test.
* </p>
*
* <pre>
* public class LocaleDependentTest {
*
* {@literal@}Rule
* public TestLocale locale = TestLocale.usingDefaultLocale();
*
* {@literal@}Test
* public void testThatWillExecuteWithTheDefaultLocale() {
* // nothing to do, just implement the test
* }
*
* {@literal@}Test
* public void testWithDifferentDefaultLocale() {
* locale.setLocale(Locale.CHINA);
* // Locale.getDefault() will return Locale.CHINA until the end of this test method
* }
* }
* </pre>
*
* <p>
* If all tests should use a different default Locale, use {@link #using(Locale)}. All tests will then have the given
* Locale set as default Locale. After each test method, the default Locale is restored to its initial value.
* </p>
*
* <pre>
* public class LocaleDependentTest {
*
* {@literal@}Rule
* public TestLocale locale = TestLocale.using(Locale.CHINA);
*
* {@literal@}Test
* public void testThatWillExecuteWithLocaleChina() {
* // nothing to do, just implement the test
* }
*
* {@literal@}Test
* public void testWithDifferentDefaultLocale() {
* locale.setLocale(Locale.US);
* // Locale.getDefault() will return Locale.US until the end of this test method
* }
* }
* </pre>
*
* @see TestTimeZone
*/
public class TestLocale implements TestRule {
private static final Locale DEFAULT_LOCALE = Locale.getDefault();
/**
* Creates a new instance using the default locale as default for tests.
*
* <p>
* The locale used for tests can be overridden using {@link #setLocale(Locale)}.
* </p>
*/
public static TestLocale usingDefaultLocale() {
return new TestLocale(DEFAULT_LOCALE);
}
/**
* Creates a new instance using the provided locale as default for tests.
*
* <p>
* The locale used for tests can be overridden using {@link #setLocale(Locale)}.
* </p>
*
* @param testLocale the locale to run tests with.
*/
public static TestLocale using(final Locale testLocale) {
return new TestLocale(testLocale);
}
private Locale testLocale;
private TestLocale(final Locale testLocale) {
this.testLocale = testLocale;
}
/**
* Override the configured test locale for this test execution.
*
* @param testLocale the locale to run this test with.
*/
public void setLocale(final Locale testLocale) {
Locale.setDefault(testLocale);
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Locale.setDefault(testLocale);
try {
base.evaluate();
} finally {
Locale.setDefault(DEFAULT_LOCALE);
}
}
};
}
}

View File

@ -0,0 +1,163 @@
/*
* 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.time;
import java.util.TimeZone;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* Rule implementation that sets and resets the default TimeZone.
*
* <p>
* Set up tests to use {@code TimeZone.getDefault()} by creating a TestTimeZone rule using {@link #usingDefaultTimeZone()}.
* To override the default TimeZone for a single test method, use {@link #setTimeZone(TimeZone)} or
* {@link #setTimeZone(String)}. The TestTimeZone rule will make sure, that the default TimeZone is restored after each
* test.
* </p>
*
* <pre>
* public class TimeZoneDependentTest {
*
* {@literal@}Rule
* public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone();
*
* {@literal@}Test
* public void testThatWillExecuteWithTheDefaultTimeZone() {
* // nothing to do, just implement the test
* }
*
* {@literal@}Test
* public void testWithDifferentDefaultTimeZone() {
* timeZone.setTimeZone("UTC");
* // TimeZone.getDefault() will return TimeZone.getTimeZone("UTC") until the end of this test method
* }
* }
* </pre>
*
* <p>
* If all tests should use a different default TimeZone, use {@link #using(TimeZone)} or {@link #using(String)}. All
* tests will then have the given TimeZone set as default TimeZone. After each test method, the default TimeZone is
* restored to its initial value.
* </p>
*
* <pre>
* public class TimeZoneDependentTest {
*
* {@literal@}Rule
* public TestTimeZone timeZone = TestTimeZone.using("UTC");
*
* {@literal@}Test
* public void testThatWillExecuteWithTimeZoneUTC() {
* // nothing to do, just implement the test
* }
*
* {@literal@}Test
* public void testWithDifferentDefaultTimeZone() {
* timeZone.setTimeZone("GMT")
* // TimeZone.getDefault() will return TimeZone.getTimeZone("GMT") until the end of this test method
* }
* }
* </pre>
*
* @see TestLocale
*/
public class TestTimeZone implements TestRule {
private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault();
/**
* Creates a new instance using the default TimeZone for tests.
*
* <p>
* The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and
* {@link #setTimeZone(TimeZone)}.
* </p>
*/
public static TestTimeZone usingDefaultTimeZone() {
return new TestTimeZone(DEFAULT_TIMEZONE);
}
/**
* Creates a new instance using the provided TimeZone as default for tests.
*
* <p>
* The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and
* {@link #setTimeZone(TimeZone)}.
* </p>
*
* @param testTimeZone the TimeZone to run tests with.
*/
public static TestTimeZone using(final TimeZone testTimeZone) {
return new TestTimeZone(testTimeZone);
}
/**
* Creates a new instance using the provided zone Id to set the default TimeZone for tests.
*
* <p>
* The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and
* {@link #setTimeZone(TimeZone)}.
* </p>
*
* @param testTimeZoneId the ID of the TimeZone to run tests with.
*/
public static TestTimeZone using(final String testTimeZoneId) {
return new TestTimeZone(TimeZone.getTimeZone(testTimeZoneId));
}
private TimeZone testTimeZone;
private TestTimeZone(final TimeZone testTimeZone) {
this.testTimeZone = testTimeZone;
}
/**
* Override the configured test locale for this test excution.
*
* @param testTimeZone the TimeZone to run this test with.
*/
public void setTimeZone(final TimeZone testTimeZone) {
TimeZone.setDefault(testTimeZone);
}
/**
* Override the configured test locale for this test excution.
*
* @param zoneId the ID of the TimeZone to run tests with.
*/
public void setTimeZone(final String zoneId) {
setTimeZone(TimeZone.getTimeZone(zoneId));
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
TimeZone.setDefault(testTimeZone);
try {
base.evaluate();
} finally {
TimeZone.setDefault(DEFAULT_TIMEZONE);
}
}
};
}
}