From abfcf0a8cfb40cd30c83d30923bea6d412cc5a22 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sun, 3 May 2015 14:20:03 +0200 Subject: [PATCH] LANG-1127: Use JUnit rules to set and reset the default Locale and TimeZone. --- .../lang3/StringUtilsEqualsIndexOfTest.java | 32 ++-- .../commons/lang3/test/DefaultLocale.java | 43 ----- .../commons/lang3/test/DefaultTimeZone.java | 43 ----- .../lang3/test/DefaultTimeZoneAndLocale.java | 45 ----- .../lang3/time/DateFormatUtilsTest.java | 48 +++--- .../commons/lang3/time/DateUtilsTest.java | 57 +++--- .../lang3/time/FastDateFormatTest.java | 147 ++++++++-------- .../lang3/time/FastDatePrinterTest.java | 91 +++++----- .../apache/commons/lang3/time/TestLocale.java | 136 +++++++++++++++ .../commons/lang3/time/TestTimeZone.java | 163 ++++++++++++++++++ 10 files changed, 479 insertions(+), 326 deletions(-) delete mode 100644 src/test/java/org/apache/commons/lang3/test/DefaultLocale.java delete mode 100644 src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java delete mode 100644 src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java create mode 100644 src/test/java/org/apache/commons/lang3/time/TestLocale.java create mode 100644 src/test/java/org/apache/commons/lang3/time/TestTimeZone.java diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index c4dbcef74..f3aa79b1d 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -23,8 +23,9 @@ import static org.junit.Assert.assertTrue; 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.junit.Rule; import org.junit.Test; /** @@ -33,6 +34,10 @@ import org.junit.Test; * @version $Id$ */ public class StringUtilsEqualsIndexOfTest { + + @Rule + public TestLocale locale = TestLocale.usingDefaultLocale(); + private static final String BAR = "bar"; /** * Supplementary character U+20000 @@ -246,22 +251,17 @@ public class StringUtilsEqualsIndexOfTest { { "\u00DF", "SS" }, }; - new DefaultLocale(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])); - } - } + for (final Locale testLocale : locales) { + locale.setLocale(testLocale); + 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])); + } + } } @Test diff --git a/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java b/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java deleted file mode 100644 index 82a77bba5..000000000 --- a/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java +++ /dev/null @@ -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 { - - 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; -} diff --git a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java deleted file mode 100644 index f075ad58e..000000000 --- a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java +++ /dev/null @@ -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 { - - 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; -} diff --git a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java deleted file mode 100644 index bdc231221..000000000 --- a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java +++ /dev/null @@ -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 { - - public DefaultTimeZoneAndLocale(TimeZone targetZone, final Locale targetLocale) throws E { - new DefaultTimeZone(targetZone) { - @Override - public void test() throws E { - new DefaultLocale(targetLocale) { - @Override - public void test() throws E { - DefaultTimeZoneAndLocale.this.test(); - } - }; - } - }; - } - - /** - * Implement test in this method - */ - abstract public void test() throws E; -} diff --git a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java index 8468feccf..ead963534 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java @@ -29,8 +29,7 @@ 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.Rule; import org.junit.Test; /** @@ -39,6 +38,11 @@ import org.junit.Test; */ public class DateFormatUtilsTest { + @Rule + public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone(); + @Rule + public TestLocale locale = TestLocale.usingDefaultLocale(); + //----------------------------------------------------------------------- @Test public void testConstructor() { @@ -169,21 +173,18 @@ public class DateFormatUtilsTest { @Test public void testSMTP() { - new DefaultLocale(Locale.ENGLISH) { - @Override - public void test() { - TimeZone timeZone = TimeZone.getTimeZone("GMT-3"); - Calendar june = createJuneTestDate(timeZone); + locale.setLocale(Locale.ENGLISH); - assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), - timeZone, june); + TimeZone timeZone = TimeZone.getTimeZone("GMT-3"); + Calendar june = createJuneTestDate(timeZone); - timeZone = TimeZone.getTimeZone("UTC"); - june = createJuneTestDate(timeZone); - assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), - timeZone, june); - } - }; + 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); } /* @@ -222,16 +223,13 @@ public class DateFormatUtilsTest { @Test public void testLang530() throws ParseException { - new DefaultTimeZone(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); - } - }; + timeZone.setTimeZone("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); } /** diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java index a8f2e1a5e..0880fdf7a 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -37,10 +37,9 @@ 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.Rule; import org.junit.Test; /** @@ -58,6 +57,9 @@ public class DateUtilsTest { BASE_DATE = cal.getTime(); } + @Rule + public TestLocale locale = TestLocale.usingDefaultLocale(); + private DateFormat dateParser = null; private DateFormat dateTimeParser = null; private Date dateAmPm1 = null; @@ -1563,57 +1565,42 @@ public class DateUtilsTest { @Test public void testLANG799_EN_OK() throws ParseException { - new DefaultLocale(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"); - } - }; + locale.setLocale(Locale.ENGLISH); + + 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) + @Test(expected = ParseException.class) public void testLANG799_EN_FAIL() throws ParseException { - new DefaultLocale(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"); - } - }; + locale.setLocale(Locale.ENGLISH); + + 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 { - new DefaultLocale(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"); - } - }; + locale.setLocale(Locale.GERMAN); + + 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 { - new DefaultLocale(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"); - } - }; + locale.setLocale(Locale.GERMAN); + + 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 { - new DefaultLocale(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"); - } - }; + locale.setLocale(Locale.ENGLISH); + + DateUtils.parseDate("Mi, 09 Apr 2008 23:55:38 GMT", Locale.GERMAN, "EEE, dd MMM yyyy HH:mm:ss zzz"); } /** diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java index b9e3061bf..2e5a08479 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java @@ -18,6 +18,7 @@ package org.apache.commons.lang3.time; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; 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.AtomicLong; -import org.apache.commons.lang3.test.DefaultLocale; -import org.apache.commons.lang3.test.DefaultTimeZoneAndLocale; +import org.junit.Rule; import org.junit.Test; /** @@ -46,6 +46,12 @@ import org.junit.Test; */ public class FastDateFormatTest { + @Rule + public TestLocale locale = TestLocale.usingDefaultLocale(); + + @Rule + public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone(); + /* * Only the cache methods need to be tested here. * The print methods are tested by {@link FastDateFormat_PrinterTest} @@ -73,102 +79,89 @@ public class FastDateFormatTest { @Test public void test_getInstance_String_TimeZone() { - new DefaultTimeZoneAndLocale(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"); + locale.setLocale(Locale.US); + timeZone.setTimeZone("America/New_York"); - 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); - } - }; + 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"); + + assertNotSame(format1, format2); + assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone()); + assertEquals(TimeZone.getDefault(), format2.getTimeZone()); + assertSame(format3, format4); + assertNotSame(format3, format5); + assertNotSame(format4, format6); } @Test public void test_getInstance_String_Locale() { - new DefaultLocale(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); + locale.setLocale(Locale.US); - assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); - assertSame(format1, format3); - assertEquals(Locale.GERMANY, format1.getLocale()); - } - }; + 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); + + assertNotSame(format1, format2); + assertSame(format1, format3); + assertEquals(Locale.GERMANY, format1.getLocale()); } @Test public void test_changeDefault_Locale_DateInstance() { - new DefaultLocale(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); + locale.setLocale(Locale.US); - 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); - } - }; + 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()); + assertNotSame(format1, format2); + assertNotSame(format2, format3); } @Test public void test_changeDefault_Locale_DateTimeInstance() { - new DefaultLocale(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); + locale.setLocale(Locale.US); - 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); - } - }; + 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()); + assertNotSame(format1, format2); + assertNotSame(format2, format3); } @Test public void test_getInstance_String_TimeZone_Locale() { - new DefaultTimeZoneAndLocale(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); + locale.setLocale(Locale.US); + timeZone.setTimeZone("America/New_York"); - 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()); - } - }; - } + 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); + + 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 public void testCheckDefaults() { diff --git a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java index dbe7bbf6e..dc7d8d87a 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java @@ -30,7 +30,9 @@ import java.util.Locale; import java.util.TimeZone; 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; /** @@ -74,56 +76,59 @@ public class FastDatePrinterTest { return new FastDatePrinter(format, timeZone, locale); } + @Rule + public TestLocale locale = TestLocale.usingDefaultLocale(); + + @Rule + public TestTimeZone timeZone = TestTimeZone.usingDefaultTimeZone(); + @Test public void testFormat() { - new DefaultTimeZoneAndLocale(NEW_YORK, Locale.US) { - @Override - public void test() { + locale.setLocale(Locale.US); + timeZone.setTimeZone(NEW_YORK); - 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)); - } - }; + 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)); } /** @@ -264,6 +269,8 @@ public class FastDatePrinterTest { @Test public void testTimeZoneAsZ() throws Exception { + timeZone.setTimeZone("UTC"); + Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); FastDateFormat noColonFormat = FastDateFormat.getInstance("Z"); assertEquals("+0000", noColonFormat.format(c)); diff --git a/src/test/java/org/apache/commons/lang3/time/TestLocale.java b/src/test/java/org/apache/commons/lang3/time/TestLocale.java new file mode 100644 index 000000000..ad8bee0b4 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/time/TestLocale.java @@ -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. + * + *

+ * 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. + *

+ * + *
+ * 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
+ *     }
+ * }
+ * 
+ * + *

+ * 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. + *

+ * + *
+ * 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
+ *     }
+ * }
+ * 
+ * + * @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. + * + *

+ * The locale used for tests can be overridden using {@link #setLocale(Locale)}. + *

+ */ + public static TestLocale usingDefaultLocale() { + return new TestLocale(DEFAULT_LOCALE); + } + + /** + * Creates a new instance using the provided locale as default for tests. + * + *

+ * The locale used for tests can be overridden using {@link #setLocale(Locale)}. + *

+ * + * @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); + } + } + }; + } +} diff --git a/src/test/java/org/apache/commons/lang3/time/TestTimeZone.java b/src/test/java/org/apache/commons/lang3/time/TestTimeZone.java new file mode 100644 index 000000000..6d97aa60a --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/time/TestTimeZone.java @@ -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. + * + *

+ * 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. + *

+ * + *
+ * 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
+ *     }
+ * }
+ * 
+ * + *

+ * 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. + *

+ * + *
+ * 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
+ *     }
+ * }
+ * 
+ * + * @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. + * + *

+ * The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and + * {@link #setTimeZone(TimeZone)}. + *

+ */ + public static TestTimeZone usingDefaultTimeZone() { + return new TestTimeZone(DEFAULT_TIMEZONE); + } + + /** + * Creates a new instance using the provided TimeZone as default for tests. + * + *

+ * The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and + * {@link #setTimeZone(TimeZone)}. + *

+ * + * @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. + * + *

+ * The TimeZone used for tests can be overridden with {@link #setTimeZone(String)} and + * {@link #setTimeZone(TimeZone)}. + *

+ * + * @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); + } + } + }; + } +}