From 18b3437c15d78c54510964728486831822809d9c Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 7 May 2015 20:49:52 +0200 Subject: [PATCH 1/3] Fix whitespaces and indentation --- .../commons/lang3/test/SwitchDefaults.java | 58 +++++++++---------- .../commons/lang3/test/SystemDefaults.java | 16 ++--- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java b/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java index d48bd3770e0..cacf37d2fd6 100644 --- a/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java +++ b/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java @@ -58,46 +58,44 @@ * */ public class SwitchDefaults implements TestRule { + + @Override + public Statement apply(Statement stmt, Description description) { + SystemDefaults defaults = description.getAnnotation(SystemDefaults.class); + if (defaults == null) { + return stmt; + } + return applyTimeZone(defaults, applyLocale(defaults, stmt)); + } + private Statement applyTimeZone(SystemDefaults defaults, final Statement stmt) { + if (defaults.timezone().isEmpty()) { + return stmt; + } + final TimeZone newTimeZone = TimeZone.getTimeZone(defaults.timezone()); - @Override - public Statement apply(Statement stmt, Description description) { - SystemDefaults defaults = description.getAnnotation(SystemDefaults.class); - if(defaults == null) { - return stmt; - } - return applyTimeZone(defaults, applyLocale(defaults, stmt)); - } - - private Statement applyTimeZone(SystemDefaults defaults, final Statement stmt) { - if(defaults.timezone().isEmpty()) { - return stmt; - } - - final TimeZone newTimeZone = TimeZone.getTimeZone(defaults.timezone()); - return new Statement() { @Override public void evaluate() throws Throwable { - TimeZone save = TimeZone.getDefault(); + TimeZone save = TimeZone.getDefault(); try { - TimeZone.setDefault(newTimeZone); + TimeZone.setDefault(newTimeZone); stmt.evaluate(); } finally { - TimeZone.setDefault(save); + TimeZone.setDefault(save); } } - }; - } + }; + } + + private Statement applyLocale(SystemDefaults defaults, final Statement stmt) { + if (defaults.locale().isEmpty()) { + return stmt; + } + + final Locale newLocale = LocaleUtils.toLocale(defaults.locale()); - private Statement applyLocale(SystemDefaults defaults, final Statement stmt) { - if(defaults.locale().isEmpty()) { - return stmt; - } - - final Locale newLocale = LocaleUtils.toLocale(defaults.locale()); - return new Statement() { @Override public void evaluate() throws Throwable { @@ -109,7 +107,7 @@ public void evaluate() throws Throwable { Locale.setDefault(save); } } - }; - } + }; + } } diff --git a/src/test/java/org/apache/commons/lang3/test/SystemDefaults.java b/src/test/java/org/apache/commons/lang3/test/SystemDefaults.java index 1c95f28a552..e5893a2c642 100644 --- a/src/test/java/org/apache/commons/lang3/test/SystemDefaults.java +++ b/src/test/java/org/apache/commons/lang3/test/SystemDefaults.java @@ -29,12 +29,12 @@ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SystemDefaults { - /** - * The name of the Locale to be used while running a test method - */ - String locale() default ""; - /** - * The name of the TimeZone to be used while running a test method - */ - String timezone() default ""; + /** + * The name of the Locale to be used while running a test method + */ + String locale() default ""; + /** + * The name of the TimeZone to be used while running a test method + */ + String timezone() default ""; } From bcb33ec1c7c4aa98758d1561c8b695737d0a15d5 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 7 May 2015 20:57:12 +0200 Subject: [PATCH 2/3] Rename SwitchDefaults to SystemDefaultsSwitch to better describe it's role --- .../apache/commons/lang3/StringUtilsEqualsIndexOfTest.java | 4 ++-- .../test/{SwitchDefaults.java => SystemDefaultsSwitch.java} | 4 ++-- .../org/apache/commons/lang3/time/DateFormatUtilsTest.java | 4 ++-- .../java/org/apache/commons/lang3/time/DateUtilsTest.java | 4 ++-- .../org/apache/commons/lang3/time/FastDateFormatTest.java | 4 ++-- .../org/apache/commons/lang3/time/FastDatePrinterTest.java | 6 ++---- 6 files changed, 12 insertions(+), 14 deletions(-) rename src/test/java/org/apache/commons/lang3/test/{SwitchDefaults.java => SystemDefaultsSwitch.java} (96%) diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index fb3a9590a36..a50229ef4ff 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -23,7 +23,7 @@ import java.util.Locale; -import org.apache.commons.lang3.test.SwitchDefaults; +import org.apache.commons.lang3.test.SystemDefaultsSwitch; import org.apache.commons.lang3.test.SystemDefaults; import org.hamcrest.core.IsNot; import org.junit.Rule; @@ -37,7 +37,7 @@ public class StringUtilsEqualsIndexOfTest { @Rule - public SwitchDefaults defaults = new SwitchDefaults(); + public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch(); private static final String BAR = "bar"; /** diff --git a/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java b/src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitch.java similarity index 96% rename from src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java rename to src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitch.java index cacf37d2fd6..5e5cd6c87f1 100644 --- a/src/test/java/org/apache/commons/lang3/test/SwitchDefaults.java +++ b/src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitch.java @@ -36,7 +36,7 @@ * public class SystemDefaultsDependentTest { * * {@literal@}Rule - * public SwitchDefaults locale = new SwitchDefaults(); + * public SystemDefaultsSwitch locale = new SystemDefaultsSwitch(); * * {@literal@}Test * public void testThatWillExecuteWithTheDefaultLocaleAndTimeZone() { @@ -57,7 +57,7 @@ * } * */ -public class SwitchDefaults implements TestRule { +public class SystemDefaultsSwitch implements TestRule { @Override public Statement apply(Statement stmt, Description description) { 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 af0cf8d9123..66c3d1e6c58 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java @@ -29,7 +29,7 @@ import java.util.Locale; import java.util.TimeZone; -import org.apache.commons.lang3.test.SwitchDefaults; +import org.apache.commons.lang3.test.SystemDefaultsSwitch; import org.apache.commons.lang3.test.SystemDefaults; import org.junit.Rule; import org.junit.Test; @@ -41,7 +41,7 @@ public class DateFormatUtilsTest { @Rule - public SwitchDefaults defaults = new SwitchDefaults(); + public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch(); //----------------------------------------------------------------------- @Test 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 44dbac5d6d0..4617ea9af4f 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -38,7 +38,7 @@ import junit.framework.AssertionFailedError; -import org.apache.commons.lang3.test.SwitchDefaults; +import org.apache.commons.lang3.test.SystemDefaultsSwitch; import org.apache.commons.lang3.test.SystemDefaults; import org.junit.Before; import org.junit.BeforeClass; @@ -61,7 +61,7 @@ public static void classSetup() { } @Rule - public SwitchDefaults defaults = new SwitchDefaults(); + public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch(); private DateFormat dateParser = null; private DateFormat dateTimeParser = null; 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 ee6d505afc2..ba036c4d37b 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java @@ -35,7 +35,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import org.apache.commons.lang3.test.SwitchDefaults; +import org.apache.commons.lang3.test.SystemDefaultsSwitch; import org.apache.commons.lang3.test.SystemDefaults; import org.junit.Rule; import org.junit.Test; @@ -49,7 +49,7 @@ public class FastDateFormatTest { @Rule - public SwitchDefaults defaults = new SwitchDefaults(); + public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch(); /* * Only the cache methods need to be tested here. 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 e0e44dcf0b9..7674b56df7b 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java @@ -30,10 +30,8 @@ import java.util.TimeZone; import org.apache.commons.lang3.SerializationUtils; -import org.apache.commons.lang3.test.SwitchDefaults; +import org.apache.commons.lang3.test.SystemDefaultsSwitch; import org.apache.commons.lang3.test.SystemDefaults; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; @@ -79,7 +77,7 @@ protected DatePrinter getInstance(final String format, final TimeZone timeZone, } @Rule - public SwitchDefaults defaults = new SwitchDefaults(); + public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch(); @SystemDefaults(timezone="America/New_York", locale="en_US") @Test From d68f7f5f44924ddee14428e16b7458e2878baef2 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 7 May 2015 21:15:37 +0200 Subject: [PATCH 3/3] Write basic unit test for SystemDefaultsSwitch --- .../lang3/test/SystemDefaultsSwitchTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitchTest.java diff --git a/src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitchTest.java b/src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitchTest.java new file mode 100644 index 00000000000..2d3c2f55e1f --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/test/SystemDefaultsSwitchTest.java @@ -0,0 +1,88 @@ +/* + * 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 static org.junit.Assert.assertEquals; + +import java.util.Locale; +import java.util.TimeZone; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +public class SystemDefaultsSwitchTest { + + private static Locale TEST_DEFAULT_LOCALE; + private static Locale DEFAULT_LOCALE_BEFORE_TEST; + + private static TimeZone DEFAULT_TIMEZONE_BEFORE_TEST; + private static TimeZone TEST_DEFAULT_TIMEZONE; + + @BeforeClass + public static void classSetUp() { + DEFAULT_LOCALE_BEFORE_TEST = Locale.getDefault(); + if (!DEFAULT_LOCALE_BEFORE_TEST.equals(Locale.CANADA)) { + Locale.setDefault(Locale.CANADA); + } else { + // you seem to be from Canada... + Locale.setDefault(Locale.CHINESE); + } + TEST_DEFAULT_LOCALE = Locale.getDefault(); + + DEFAULT_TIMEZONE_BEFORE_TEST = TimeZone.getDefault(); + TimeZone utc = TimeZone.getTimeZone("UTC"); + if (!DEFAULT_TIMEZONE_BEFORE_TEST.equals(utc)) { + TimeZone.setDefault(utc); + } else { + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + } + TEST_DEFAULT_TIMEZONE = TimeZone.getDefault(); + } + + @Rule + public SystemDefaultsSwitch defaultsSwitch = new SystemDefaultsSwitch(); + + @Test + public void testDefaultLocaleNoAnnotation() throws Exception { + assertEquals(TEST_DEFAULT_LOCALE, Locale.getDefault()); + } + + @Test + @SystemDefaults(locale = "en_EN") + public void testUseDifferentLocale() throws Exception { + assertEquals(new Locale("en", "EN"), Locale.getDefault()); + } + + @Test + public void testDefaultTimeZoneNoAnnotation() throws Exception { + assertEquals(TEST_DEFAULT_TIMEZONE, TimeZone.getDefault()); + } + + @Test + @SystemDefaults(timezone = "CET") + public void testUseDifferentTimeZone() throws Exception { + assertEquals(TimeZone.getTimeZone("CET"), TimeZone.getDefault()); + } + + @AfterClass + public static void classTearDown() { + Locale.setDefault(DEFAULT_LOCALE_BEFORE_TEST); + TimeZone.setDefault(DEFAULT_TIMEZONE_BEFORE_TEST); + } +}