Merge branch 'LANG-1127'
LANG-1127: Use JUnit rules to set and reset the default Locale and TimeZone.
This commit is contained in:
commit
cfe63beeac
|
@ -30,7 +30,7 @@
|
|||
<action issue="LANG-1131" type="fix" dev="britter">StrBuilder.equals(StrBuilder) doesn't check for null inputs</action>
|
||||
<action issue="LANG-1105" type="add" dev="britter" due-to="Hendrik Saly">Add ThreadUtils - A utility class which provides helper methods related to java.lang.Thread</action>
|
||||
<action issue="LANG-1031" type="add" dev="britter" due-to="Felipe Adorno">Add annotations to exclude fields from ReflectionEqualsBuilder, ReflectionToStringBuilder and ReflectionHashCodeBuilder</action>
|
||||
<action issue="LANG-1127" type="add" dev="chas">Unit test helpers which set and reset default Locale and TimeZone</action>
|
||||
<action issue="LANG-1127" type="add" dev="chas, britter">Use JUnit rules to set and reset the default Locale and TimeZone</action>
|
||||
<action issue="LANG-1128" type="fix" dev="britter" due-to="Jack Tan">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>
|
||||
|
|
|
@ -23,14 +23,20 @@ import static org.junit.Assert.assertTrue;
|
|||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.lang3.test.DefaultLocale;
|
||||
import org.apache.commons.lang3.test.SystemDefaultsSwitch;
|
||||
import org.apache.commons.lang3.test.SystemDefaults;
|
||||
import org.hamcrest.core.IsNot;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests {@link org.apache.commons.lang3.StringUtils} - Substring methods
|
||||
*/
|
||||
public class StringUtilsEqualsIndexOfTest {
|
||||
|
||||
@Rule
|
||||
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
|
||||
|
||||
private static final String BAR = "bar";
|
||||
/**
|
||||
* Supplementary character U+20000
|
||||
|
@ -232,6 +238,7 @@ public class StringUtilsEqualsIndexOfTest {
|
|||
assertFalse(StringUtils.containsAny("hello, null", new String[] { "Hello", null }));
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="de_DE")
|
||||
@Test
|
||||
public void testContainsIgnoreCase_LocaleIndependence() {
|
||||
final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
|
||||
|
@ -248,11 +255,8 @@ public class StringUtilsEqualsIndexOfTest {
|
|||
{ "\u00DF", "SS" },
|
||||
};
|
||||
|
||||
new DefaultLocale<RuntimeException>(Locale.ENGLISH) {
|
||||
@Override
|
||||
public void test() {
|
||||
for (final Locale locale : locales) {
|
||||
Locale.setDefault(locale);
|
||||
for (final Locale testLocale : locales) {
|
||||
Locale.setDefault(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]));
|
||||
|
@ -263,8 +267,6 @@ public class StringUtilsEqualsIndexOfTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsIgnoreCase_StringString() {
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.lang3.test;
|
||||
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* run a test with a different default TimeZone
|
||||
*/
|
||||
public abstract class DefaultTimeZone<E extends Throwable> {
|
||||
|
||||
public DefaultTimeZone(TimeZone targetZone) throws E {
|
||||
// only one test at a time may change default
|
||||
synchronized (getClass()) {
|
||||
TimeZone defaultZone = TimeZone.getDefault();
|
||||
try {
|
||||
TimeZone.setDefault(targetZone);
|
||||
test();
|
||||
} finally {
|
||||
TimeZone.setDefault(defaultZone);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement test in this method
|
||||
*/
|
||||
abstract public void test() throws E;
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.lang3.test;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* run a test with a different default TimeZone and Locale
|
||||
*/
|
||||
public abstract class DefaultTimeZoneAndLocale<E extends Throwable> {
|
||||
|
||||
public DefaultTimeZoneAndLocale(TimeZone targetZone, final Locale targetLocale) throws E {
|
||||
new DefaultTimeZone<E>(targetZone) {
|
||||
@Override
|
||||
public void test() throws E {
|
||||
new DefaultLocale<E>(targetLocale) {
|
||||
@Override
|
||||
public void test() throws E {
|
||||
DefaultTimeZoneAndLocale.this.test();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement test in this method
|
||||
*/
|
||||
abstract public void test() throws E;
|
||||
}
|
|
@ -14,30 +14,27 @@
|
|||
* 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.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* run a test with a different default Locale
|
||||
* Annotation used with {@link SystemDefaults} that specifies the
|
||||
* system default Locale and TimeZone to be used in a test method.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface SystemDefaults {
|
||||
/**
|
||||
* Implement test in this method
|
||||
* The name of the Locale to be used while running a test method
|
||||
*/
|
||||
abstract public void test() throws E;
|
||||
String locale() default "";
|
||||
/**
|
||||
* The name of the TimeZone to be used while running a test method
|
||||
*/
|
||||
String timezone() default "";
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.apache.commons.lang3.LocaleUtils;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* Test Rule used with {@link SystemDefaults} annotation that sets and restores the system default Locale and TimeZone.
|
||||
*
|
||||
* <p>
|
||||
* Set up tests to use alternate system default Locale and/or TimeZone by creating an instance of this rule
|
||||
* and annotating the test method with {@link SystemDefaults}
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* public class SystemDefaultsDependentTest {
|
||||
*
|
||||
* {@literal@}Rule
|
||||
* public SystemDefaultsSwitch locale = new SystemDefaultsSwitch();
|
||||
*
|
||||
* {@literal@}Test
|
||||
* public void testThatWillExecuteWithTheDefaultLocaleAndTimeZone() {
|
||||
* // nothing to do, just implement the test
|
||||
* }
|
||||
*
|
||||
* {@literal@}Test
|
||||
* {@literal@}SystemDefaults(local="zh_CN")
|
||||
* public void testWithSimplifiedChinaDefaultLocale() {
|
||||
* // Locale.getDefault() will return Locale.CHINA until the end of this test method
|
||||
* }
|
||||
*
|
||||
* {@literal@}Test
|
||||
* {@literal@}SystemDefaults(timezone="America/New_York")
|
||||
* public void testWithNorthAmericaEasternTimeZone() {
|
||||
* // TimeZone.getDefault() will equal TimeZone.getTimeZone("America/New_York") until the end of this method
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class SystemDefaultsSwitch 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());
|
||||
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
TimeZone save = TimeZone.getDefault();
|
||||
try {
|
||||
TimeZone.setDefault(newTimeZone);
|
||||
stmt.evaluate();
|
||||
} finally {
|
||||
TimeZone.setDefault(save);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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 {
|
||||
Locale save = Locale.getDefault();
|
||||
try {
|
||||
Locale.setDefault(newLocale);
|
||||
stmt.evaluate();
|
||||
} finally {
|
||||
Locale.setDefault(save);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -29,8 +29,9 @@ 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.apache.commons.lang3.test.SystemDefaultsSwitch;
|
||||
import org.apache.commons.lang3.test.SystemDefaults;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -38,6 +39,9 @@ import org.junit.Test;
|
|||
*/
|
||||
public class DateFormatUtilsTest {
|
||||
|
||||
@Rule
|
||||
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
|
@ -166,11 +170,9 @@ public class DateFormatUtilsTest {
|
|||
testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en")
|
||||
@Test
|
||||
public void testSMTP() {
|
||||
new DefaultLocale<RuntimeException>(Locale.ENGLISH) {
|
||||
@Override
|
||||
public void test() {
|
||||
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
|
||||
Calendar june = createJuneTestDate(timeZone);
|
||||
|
||||
|
@ -182,8 +184,6 @@ public class DateFormatUtilsTest {
|
|||
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
|
||||
timeZone, june);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
public void testLang312() {
|
||||
|
@ -219,19 +219,15 @@ public class DateFormatUtilsTest {
|
|||
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.parse(date);
|
||||
}
|
||||
|
||||
@SystemDefaults(timezone="UTC")
|
||||
@Test
|
||||
public void testLang530() throws ParseException {
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916),
|
||||
|
|
|
@ -38,9 +38,11 @@ import java.util.TimeZone;
|
|||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.apache.commons.lang3.test.DefaultLocale;
|
||||
import org.apache.commons.lang3.test.SystemDefaultsSwitch;
|
||||
import org.apache.commons.lang3.test.SystemDefaults;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -57,6 +59,9 @@ public class DateUtilsTest {
|
|||
BASE_DATE = cal.getTime();
|
||||
}
|
||||
|
||||
@Rule
|
||||
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
|
||||
|
||||
private DateFormat dateParser = null;
|
||||
private DateFormat dateTimeParser = null;
|
||||
private Date dateAmPm1 = null;
|
||||
|
@ -1560,60 +1565,40 @@ public class DateUtilsTest {
|
|||
dateParser.parse("December 2, 2001"));
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en")
|
||||
@Test
|
||||
public void testLANG799_EN_OK() throws ParseException {
|
||||
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
|
||||
@SystemDefaults(locale="en")
|
||||
@Test(expected = ParseException.class)
|
||||
public void testLANG799_EN_FAIL() throws ParseException {
|
||||
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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="de")
|
||||
@Test
|
||||
public void testLANG799_DE_OK() throws ParseException {
|
||||
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
|
||||
@SystemDefaults(locale="de")
|
||||
@Test(expected=ParseException.class)
|
||||
public void testLANG799_DE_FAIL() throws ParseException {
|
||||
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
|
||||
@SystemDefaults(locale="en")
|
||||
@Test
|
||||
public void testLANG799_EN_WITH_DE_LOCALE() throws ParseException {
|
||||
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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This checks that this is a 7 element iterator of Calendar objects
|
||||
|
|
|
@ -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,9 @@ 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.apache.commons.lang3.test.SystemDefaultsSwitch;
|
||||
import org.apache.commons.lang3.test.SystemDefaults;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -45,6 +47,9 @@ import org.junit.Test;
|
|||
*/
|
||||
public class FastDateFormatTest {
|
||||
|
||||
@Rule
|
||||
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
|
||||
|
||||
/*
|
||||
* Only the cache methods need to be tested here.
|
||||
* The print methods are tested by {@link FastDateFormat_PrinterTest}
|
||||
|
@ -70,11 +75,10 @@ public class FastDateFormatTest {
|
|||
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
|
||||
}
|
||||
|
||||
@SystemDefaults(timezone="America/New_York", locale="en_US")
|
||||
@Test
|
||||
public void test_getInstance_String_TimeZone() {
|
||||
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");
|
||||
|
@ -83,82 +87,66 @@ public class FastDateFormatTest {
|
|||
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);
|
||||
assertNotSame(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);
|
||||
}
|
||||
};
|
||||
assertNotSame(format3, format5);
|
||||
assertNotSame(format4, format6);
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en_US")
|
||||
@Test
|
||||
public void test_getInstance_String_Locale() {
|
||||
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);
|
||||
assertNotSame(format1, format2);
|
||||
assertSame(format1, format3);
|
||||
assertEquals(Locale.GERMANY, format1.getLocale());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en_US")
|
||||
@Test
|
||||
public void test_changeDefault_Locale_DateInstance() {
|
||||
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());
|
||||
assertEquals(Locale.US, format2.getLocale());
|
||||
assertSame(Locale.GERMANY, format3.getLocale());
|
||||
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
|
||||
assertTrue(format2 != format3);
|
||||
}
|
||||
};
|
||||
assertNotSame(format1, format2);
|
||||
assertNotSame(format2, format3);
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en_US")
|
||||
@Test
|
||||
public void test_changeDefault_Locale_DateTimeInstance() {
|
||||
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());
|
||||
assertEquals(Locale.US, format2.getLocale());
|
||||
assertSame(Locale.GERMANY, format3.getLocale());
|
||||
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
|
||||
assertTrue(format2 != format3);
|
||||
}
|
||||
};
|
||||
assertNotSame(format1, format2);
|
||||
assertNotSame(format2, format3);
|
||||
}
|
||||
|
||||
@SystemDefaults(locale="en_US", timezone="America/New_York")
|
||||
@Test
|
||||
public void test_getInstance_String_TimeZone_Locale() {
|
||||
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);
|
||||
assertNotSame(format1, format2);
|
||||
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
|
||||
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
|
||||
assertEquals(TimeZone.getDefault(), format3.getTimeZone());
|
||||
|
@ -166,8 +154,6 @@ public class FastDateFormatTest {
|
|||
assertEquals(Locale.GERMANY, format2.getLocale());
|
||||
assertEquals(Locale.GERMANY, format3.getLocale());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDefaults() {
|
||||
|
|
|
@ -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.apache.commons.lang3.test.SystemDefaultsSwitch;
|
||||
import org.apache.commons.lang3.test.SystemDefaults;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -73,12 +75,12 @@ public class FastDatePrinterTest {
|
|||
return new FastDatePrinter(format, timeZone, locale);
|
||||
}
|
||||
|
||||
@Rule
|
||||
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
|
||||
|
||||
@SystemDefaults(timezone="America/New_York", locale="en_US")
|
||||
@Test
|
||||
public void testFormat() {
|
||||
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();
|
||||
|
@ -122,8 +124,6 @@ public class FastDatePrinterTest {
|
|||
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));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}.
|
||||
|
@ -261,6 +261,7 @@ public class FastDatePrinterTest {
|
|||
assertEquals(NEW_YORK, printer.getTimeZone());
|
||||
}
|
||||
|
||||
@SystemDefaults(timezone="UTC")
|
||||
@Test
|
||||
public void testTimeZoneAsZ() throws Exception {
|
||||
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
|
|
Loading…
Reference in New Issue