commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benedikt Ritter <brit...@apache.org>
Subject Re: [lang] LANG-1127 Create a base test for the time package, which sets and resets default Locales and TimeZones
Date Sun, 03 May 2015 09:00:27 GMT
Hello Charles,

nice first implementation of LANG-1127. However it looks somehow
complicated to me.

When I wrote the ticket, I was thinking of a base class like:

public class LocaleAndTimeZoneBaseTest {

   private static final Locale DEFAULT_LOCALE = Locale.getDefault();
   private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault();

   @Before
   public void setUp() {
      Locale.setDefault(Locale.ENGLISH);
      TimeZone.setDefault("UTC")
   }

   @Before
   public void setUp() {
      Locale.setDefault(DEFAULT_LOCALE);
      TimeZone.setDefault(DEFAULT_TIMEZONE);
   }
}

All tests would then run with Locale.ENGLISH and the default Locale would
be restored after each test. However if a test needs a different setup, we
end up with the same try-finally code we had before. So this is not a good
solution either.

Then I thought about using JUnit test rules for this [1]. We could write a
Locale and a TimeZone test rule and the test code would look like the
following:

public class DateUtilsTest {

   @Rule
   public LocaleRule locale = new LocaleRule(Locale.ENGLISH);

   // ...

   @Test
   public void testDefaultLocaleSetup() {
      // test rule will take care of setting the locale to english and
resetting it to the default locale afterwards
      // do something
   }

   @Test
   public void testWithDifferentLocaleSetup() {
      // overwrite the Locale.ENGLISH as the default for tests and use
Locale.GERMAN instead
      locale.setLocale(Locale.GERMAN);
   }
}

This looks like a nice and clean solution to me. I'll implement it on a
branch so everybody can get a feeling how that would look like.

br,
Benedikt

[1] https://github.com/junit-team/junit/wiki/Rules

2015-05-02 20:35 GMT+02:00 <chas@apache.org>:

> Repository: commons-lang
> Updated Branches:
>   refs/heads/master 0add1e897 -> b37837ce6
>
>
> LANG-1127 Create a base test for the time package, which sets and resets
> default Locales and TimeZones
>
>
> Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
> Commit:
> http://git-wip-us.apache.org/repos/asf/commons-lang/commit/b37837ce
> Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/b37837ce
> Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/b37837ce
>
> Branch: refs/heads/master
> Commit: b37837ce638048384756850a3a6892519ddbc743
> Parents: 0add1e8
> Author: Chas Honton <chas@apache.org>
> Authored: Sat May 2 11:30:32 2015 -0700
> Committer: Chas Honton <chas@apache.org>
> Committed: Sat May 2 11:34:36 2015 -0700
>
> ----------------------------------------------------------------------
>  src/changes/changes.xml                         |   1 +
>  .../lang3/StringUtilsEqualsIndexOfTest.java     |  30 ++--
>  .../commons/lang3/test/DefaultLocale.java       |  43 +++++
>  .../commons/lang3/test/DefaultTimeZone.java     |  43 +++++
>  .../lang3/test/DefaultTimeZoneAndLocale.java    |  45 +++++
>  .../commons/lang3/time/DateFormatUtilsTest.java |  55 +++---
>  .../commons/lang3/time/DateUtilsTest.java       |  71 ++++----
>  .../commons/lang3/time/FastDateFormatTest.java  | 178 +++++++++----------
>  .../commons/lang3/time/FastDatePrinterTest.java | 101 +++++------
>  9 files changed, 338 insertions(+), 229 deletions(-)
> ----------------------------------------------------------------------
>
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index 4e5e15a..36fc671 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -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>
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
> ----------------------------------------------------------------------
> diff --git
> a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
> b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
> index 99c766a..c4dbcef 100644
> ---
> a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
> +++
> b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
> @@ -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
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java
> ----------------------------------------------------------------------
> diff --git
> a/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java
> b/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java
> new file mode 100644
> index 0000000..82a77bb
> --- /dev/null
> +++ b/src/test/java/org/apache/commons/lang3/test/DefaultLocale.java
> @@ -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;
> +}
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java
> ----------------------------------------------------------------------
> diff --git
> a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java
> b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java
> new file mode 100644
> index 0000000..f075ad5
> --- /dev/null
> +++ b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZone.java
> @@ -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;
> +}
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java
> ----------------------------------------------------------------------
> diff --git
> a/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java
> b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java
> new file mode 100644
> index 0000000..bdc2312
> --- /dev/null
> +++
> b/src/test/java/org/apache/commons/lang3/test/DefaultTimeZoneAndLocale.java
> @@ -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;
> +}
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
> ----------------------------------------------------------------------
> 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 8146002..8468fec 100644
> --- a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
> +++ b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
> @@ -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);
> +            }
> +        };
>      }
>
>      /**
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
> ----------------------------------------------------------------------
> 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 148f6cd..a8f2e1a 100644
> --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
> +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
> @@ -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");
> +            }
> +        };
>      }
>
>      /**
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
> ----------------------------------------------------------------------
> 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 fe110e9..b9e3061 100644
> --- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
> +++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
> @@ -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"));
> -
> -            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);
> -        }
> +        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");
> +
> +                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);
> -
> -            assertTrue(format1 != format2); // -- junit 3.8 version --
> assertFalse(format1 == format2);
> -            assertSame(format1, format3);
> -            assertEquals(Locale.GERMANY, format1.getLocale());
> -
> -        } finally {
> -            Locale.setDefault(realDefaultLocale);
> -        }
> +        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());
> +            }
> +        };
>      }
>
>      @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);
> -
> -            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);
> -        }
> +        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);
> +            }
> +        };
>      }
>
>      @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);
> -
> -            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);
> -        }
> +        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);
> +            }
> +        };
>      }
>
>      @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"));
> -
> -            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);
> -        }
> +        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);
> +
> +                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
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b37837ce/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
> ----------------------------------------------------------------------
> 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 5aaa670..dbe7bbf 100644
> --- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
> +++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
> @@ -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);
> -
> -            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));
> -
> -            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));
> -
> -            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));
> -
> -            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);
> -        }
> +        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();
> +
> +                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));
> +
> +                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));
> +
> +                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));
> +            }
> +        };
>      }
>
>      /**
>
>


-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message