This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 98d5fe1 Sort methods.
98d5fe1 is described below
commit 98d5fe190cb7164b1862d2a8ef288f13ecdc1830
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Feb 24 10:06:39 2021 -0500
Sort methods.
---
.../core/util/datetime/FastDateParserSDFTest.java | 60 +-
.../core/util/datetime/FastDateParserTest.java | 764 ++++++++++-----------
.../datetime/FastDateParser_MoreOrLessTest.java | 60 +-
.../FastDateParser_TimeZoneStrategyTest.java | 18 +-
.../core/util/datetime/FixedDateFormatTest.java | 320 ++++-----
5 files changed, 611 insertions(+), 611 deletions(-)
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserSDFTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserSDFTest.java
index 55102a2..7803995 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserSDFTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserSDFTest.java
@@ -121,36 +121,6 @@ public class FastDateParserSDFTest {
this.valid = valid;
}
- @Test
- public void testOriginal() throws Exception {
- checkParse(input);
- }
-
- @Test
- public void testOriginalPP() throws Exception {
- checkParsePosition(input);
- }
-
- @Test
- public void testUpperCase() throws Exception {
- checkParse(input.toUpperCase(locale));
- }
-
- @Test
- public void testUpperCasePP() throws Exception {
- checkParsePosition(input.toUpperCase(locale));
- }
-
- @Test
- public void testLowerCase() throws Exception {
- checkParse(input.toLowerCase(locale));
- }
-
- @Test
- public void testLowerCasePP() throws Exception {
- checkParsePosition(input.toLowerCase(locale));
- }
-
private void checkParse(final String formattedDate) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
@@ -191,6 +161,7 @@ public class FastDateParserSDFTest {
assertEquals(locale.toString()+" "+formattedDate + " expected same
Exception ", sdfE, fdfE);
}
}
+
private void checkParsePosition(final String formattedDate) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
@@ -229,4 +200,33 @@ public class FastDateParserSDFTest {
sdferrorIndex - fdferrorIndex <= 4);
}
}
+
+ @Test
+ public void testLowerCase() throws Exception {
+ checkParse(input.toLowerCase(locale));
+ }
+
+ @Test
+ public void testLowerCasePP() throws Exception {
+ checkParsePosition(input.toLowerCase(locale));
+ }
+
+ @Test
+ public void testOriginal() throws Exception {
+ checkParse(input);
+ }
+
+ @Test
+ public void testOriginalPP() throws Exception {
+ checkParsePosition(input);
+ }
+
+ @Test
+ public void testUpperCase() throws Exception {
+ checkParse(input.toUpperCase(locale));
+ }
+ @Test
+ public void testUpperCasePP() throws Exception {
+ checkParsePosition(input.toUpperCase(locale));
+ }
}
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserTest.java
index 00588df..412d56f 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParserTest.java
@@ -43,32 +43,98 @@ import org.junit.Test;
* Copied from Apache Commons Lang 3 on 2016-11-16.
*/
public class FastDateParserTest {
+ private static enum Expected1806 {
+ India(INDIA, "+05", "+0530", "+05:30", true),
+ Greenwich(GMT, "Z", "Z", "Z", false),
+ NewYork(NEW_YORK, "-05", "-0500", "-05:00", false);
+
+ final TimeZone zone;
+
+ final String one;
+ final String two;
+ final String three;
+ final long offset;
+ private Expected1806(final TimeZone zone, final String one, final
String two, final String three, final boolean hasHalfHourOffset) {
+ this.zone = zone;
+ this.one = one;
+ this.two = two;
+ this.three = three;
+ this.offset = hasHalfHourOffset ?30*60*1000 :0;
+ }
+ }
private static final String SHORT_FORMAT_NOERA = "y/M/d/h/a/m/s/E";
private static final String LONG_FORMAT_NOERA =
"yyyy/MMMM/dddd/hhhh/mmmm/ss/aaaa/EEEE";
private static final String SHORT_FORMAT = "G/" + SHORT_FORMAT_NOERA;
- private static final String LONG_FORMAT = "GGGG/" + LONG_FORMAT_NOERA;
+ private static final String LONG_FORMAT = "GGGG/" + LONG_FORMAT_NOERA;
private static final String yMdHmsSZ = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
private static final String DMY_DOT = "dd.MM.yyyy";
private static final String YMD_SLASH = "yyyy/MM/dd";
private static final String MDY_DASH = "MM-DD-yyyy";
- private static final String MDY_SLASH = "MM/DD/yyyy";
+ private static final String MDY_SLASH = "MM/DD/yyyy";
private static final TimeZone REYKJAVIK =
TimeZone.getTimeZone("Atlantic/Reykjavik");
private static final TimeZone NEW_YORK =
TimeZone.getTimeZone("America/New_York");
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
+
private static final TimeZone INDIA =
TimeZone.getTimeZone("Asia/Calcutta");
private static final Locale SWEDEN = new Locale("sv", "SE");
- DateParser getInstance(final String format) {
- return getInstance(format, TimeZone.getDefault(), Locale.getDefault());
+ private static Calendar initializeCalendar(final TimeZone tz) {
+ final Calendar cal = Calendar.getInstance(tz);
+ cal.set(Calendar.YEAR, 2001);
+ cal.set(Calendar.MONTH, 1); // not daylight savings
+ cal.set(Calendar.DAY_OF_MONTH, 4);
+ cal.set(Calendar.HOUR_OF_DAY, 12);
+ cal.set(Calendar.MINUTE, 8);
+ cal.set(Calendar.SECOND, 56);
+ cal.set(Calendar.MILLISECOND, 235);
+ return cal;
+ }
+
+ private void checkParse(final Locale locale, final Calendar cal, final
SimpleDateFormat sdf, final DateParser fdf) throws ParseException {
+ final String formattedDate= sdf.format(cal.getTime());
+ checkParse(locale, sdf, fdf, formattedDate);
+ checkParse(locale, sdf, fdf, formattedDate.toLowerCase(locale));
+ checkParse(locale, sdf, fdf, formattedDate.toUpperCase(locale));
+ }
+
+ private void checkParse(final Locale locale, final SimpleDateFormat sdf,
final DateParser fdf, final String formattedDate) throws ParseException {
+ final Date expectedTime = sdf.parse(formattedDate);
+ final Date actualTime = fdf.parse(formattedDate);
+ assertEquals(locale.toString()+" "+formattedDate +"\n",expectedTime,
actualTime);
}
private DateParser getDateInstance(final int dateStyle, final Locale
locale) {
return
getInstance(FormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null,
locale), TimeZone.getDefault(), Locale.getDefault());
}
+ private Calendar getEraStart(int year, final TimeZone zone, final Locale
locale) {
+ final Calendar cal = Calendar.getInstance(zone, locale);
+ cal.clear();
+
+ //
http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
+ if (locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
+ if(year < 1868) {
+ cal.set(Calendar.ERA, 0);
+ cal.set(Calendar.YEAR, 1868-year);
+ }
+ }
+ else {
+ if (year < 0) {
+ cal.set(Calendar.ERA, GregorianCalendar.BC);
+ year= -year;
+ }
+ cal.set(Calendar.YEAR, year/100 * 100);
+ }
+ return cal;
+ }
+
+ DateParser getInstance(final String format) {
+ return getInstance(format, TimeZone.getDefault(), Locale.getDefault());
+ }
+
private DateParser getInstance(final String format, final Locale locale) {
return getInstance(format, TimeZone.getDefault(), locale);
}
@@ -115,52 +181,32 @@ public class FastDateParserTest {
}
@Test
- public void testParseZone() throws ParseException {
- final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
- cal.clear();
- cal.set(2003, Calendar.JULY, 10, 16, 33, 20);
-
- final DateParser fdf = getInstance(yMdHmsSZ, NEW_YORK, Locale.US);
+ public void test1806() throws ParseException {
+ final String formatStub = "yyyy-MM-dd'T'HH:mm:ss.SSS";
+ final String dateStub = "2001-02-04T12:08:56.235";
- assertEquals(cal.getTime(), fdf.parse("2003-07-10T15:33:20.000
-0500"));
- assertEquals(cal.getTime(), fdf.parse("2003-07-10T15:33:20.000
GMT-05:00"));
- assertEquals(cal.getTime(), fdf.parse("2003-07-10T16:33:20.000 Eastern
Daylight Time"));
- assertEquals(cal.getTime(), fdf.parse("2003-07-10T16:33:20.000 EDT"));
+ for (final Expected1806 trial : Expected1806.values()) {
+ final Calendar cal = initializeCalendar(trial.zone);
- cal.setTimeZone(TimeZone.getTimeZone("GMT-3"));
- cal.set(2003, Calendar.FEBRUARY, 10, 9, 0, 0);
+ final String message = trial.zone.getDisplayName()+";";
- assertEquals(cal.getTime(), fdf.parse("2003-02-10T09:00:00.000
-0300"));
+ DateParser parser = getInstance(formatStub+"X", trial.zone);
+ assertEquals(message+trial.one, cal.getTime().getTime(),
parser.parse(dateStub+trial.one).getTime()-trial.offset);
- cal.setTimeZone(TimeZone.getTimeZone("GMT+5"));
- cal.set(2003, Calendar.FEBRUARY, 10, 15, 5, 6);
+ parser = getInstance(formatStub+"XX", trial.zone);
+ assertEquals(message+trial.two, cal.getTime(),
parser.parse(dateStub+trial.two));
- assertEquals(cal.getTime(), fdf.parse("2003-02-10T15:05:06.000
+0500"));
+ parser = getInstance(formatStub+"XXX", trial.zone);
+ assertEquals(message+trial.three, cal.getTime(),
parser.parse(dateStub+trial.three));
+ }
}
- @Test
- public void testParseLongShort() throws ParseException {
- final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
- cal.clear();
- cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20);
- cal.set(Calendar.MILLISECOND, 989);
- cal.setTimeZone(NEW_YORK);
-
- DateParser fdf = getInstance("yyyy GGGG MMMM dddd aaaa EEEE HHHH mmmm
ssss SSSS ZZZZ", NEW_YORK, Locale.US);
-
- assertEquals(cal.getTime(), fdf.parse("2003 AD February 0010 PM Monday
0015 0033 0020 0989 GMT-05:00"));
- cal.set(Calendar.ERA, GregorianCalendar.BC);
-
- final Date parse = fdf.parse("2003 BC February 0010 PM Saturday 0015
0033 0020 0989 GMT-05:00");
- assertEquals(cal.getTime(), parse);
-
- fdf = getInstance("y G M d a E H m s S Z", NEW_YORK, Locale.US);
- assertEquals(cal.getTime(), fdf.parse("03 BC 2 10 PM Sat 15 33 20 989
-0500"));
-
- cal.set(Calendar.ERA, GregorianCalendar.AD);
- assertEquals(cal.getTime(), fdf.parse("03 AD 2 10 PM Saturday 15 33 20
989 -0500"));
+ @Test(expected = IllegalArgumentException.class)
+ public void test1806Argument() {
+ getInstance("XXXX");
}
+
@Test
public void testAmPm() throws ParseException {
final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
@@ -196,127 +242,143 @@ public class FastDateParserTest {
assertEquals(cal.getTime(), H.parse("2010-08-01 12:33:20"));
}
- private Calendar getEraStart(int year, final TimeZone zone, final Locale
locale) {
- final Calendar cal = Calendar.getInstance(zone, locale);
- cal.clear();
+ @Test
+ public void testDayNumberOfWeek() throws ParseException {
+ final DateParser parser = getInstance("u");
+ final Calendar calendar = Calendar.getInstance();
- //
http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
- if (locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
- if(year < 1868) {
- cal.set(Calendar.ERA, 0);
- cal.set(Calendar.YEAR, 1868-year);
- }
- }
- else {
- if (year < 0) {
- cal.set(Calendar.ERA, GregorianCalendar.BC);
- year= -year;
- }
- cal.set(Calendar.YEAR, year/100 * 100);
- }
- return cal;
+ calendar.setTime(parser.parse("1"));
+ Assert.assertEquals(Calendar.MONDAY,
calendar.get(Calendar.DAY_OF_WEEK));
+
+ calendar.setTime(parser.parse("6"));
+ Assert.assertEquals(Calendar.SATURDAY,
calendar.get(Calendar.DAY_OF_WEEK));
+
+ calendar.setTime(parser.parse("7"));
+ Assert.assertEquals(Calendar.SUNDAY,
calendar.get(Calendar.DAY_OF_WEEK));
}
- private void validateSdfFormatFdpParseEquality(final String format, final
Locale locale, final TimeZone tz, final DateParser fdp, final Date in, final
int year, final Date cs) throws ParseException {
- final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
- sdf.setTimeZone(tz);
- if (format.equals(SHORT_FORMAT)) {
- sdf.set2DigitYearStart( cs );
- }
- final String fmt = sdf.format(in);
- try {
- final Date out = fdp.parse(fmt);
- assertEquals(locale.toString()+" "+in+" "+ format+ " "+tz.getID(),
in, out);
- } catch (final ParseException pe) {
- if (year >= 1868 || !locale.getCountry().equals("JP")) {// LANG-978
- throw pe;
- }
- }
+ @Test
+ public void testDayOf() throws ParseException {
+ final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
+ cal.clear();
+ cal.set(2003, Calendar.FEBRUARY, 10);
+
+ final DateParser fdf = getInstance("W w F D y", NEW_YORK, Locale.US);
+ assertEquals(cal.getTime(), fdf.parse("3 7 2 41 03"));
}
@Test
- // Check that all Locales can parse the formats we use
- public void testParses() throws Exception {
- for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
- for(final Locale locale : Locale.getAvailableLocales()) {
- for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK,
GMT}) {
- for(final int year : new int[]{2003, 1940, 1868, 1867, 1,
-1, -1940}) {
- final Calendar cal= getEraStart(year, tz, locale);
- final Date centuryStart= cal.getTime();
+ public void testEquals() {
+ final DateParser parser1= getInstance(YMD_SLASH);
+ final DateParser parser2= getInstance(YMD_SLASH);
- cal.set(Calendar.MONTH, 1);
- cal.set(Calendar.DAY_OF_MONTH, 10);
- final Date in= cal.getTime();
+ assertEquals(parser1, parser2);
+ assertEquals(parser1.hashCode(), parser2.hashCode());
- final FastDateParser fdp= new FastDateParser(format,
tz, locale, centuryStart);
- validateSdfFormatFdpParseEquality(format, locale, tz,
fdp, in, year, centuryStart);
- }
- }
- }
- }
+ assertFalse(parser1.equals(new Object()));
}
- // we cannot use historic dates to test timezone parsing, some timezones
have second offsets
- // as well as hours and minutes which makes the z formats a low fidelity
round trip
@Test
- public void testTzParses() throws Exception {
- // Check that all Locales can parse the time formats we use
- for(final Locale locale : Locale.getAvailableLocales()) {
- final FastDateParser fdp= new FastDateParser("yyyy/MM/dd z",
TimeZone.getDefault(), locale);
+ public void testJpLocales() {
- for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK, GMT})
{
- final Calendar cal= Calendar.getInstance(tz, locale);
- cal.clear();
- cal.set(Calendar.YEAR, 2000);
- cal.set(Calendar.MONTH, 1);
- cal.set(Calendar.DAY_OF_MONTH, 10);
- final Date expected= cal.getTime();
+ final Calendar cal= Calendar.getInstance(GMT);
+ cal.clear();
+ cal.set(2003, Calendar.FEBRUARY, 10);
+ cal.set(Calendar.ERA, GregorianCalendar.BC);
- final Date actual = fdp.parse("2000/02/10
"+tz.getDisplayName(locale));
- Assert.assertEquals("tz:"+tz.getID()+"
locale:"+locale.getDisplayName(), expected, actual);
- }
- }
- }
+ final Locale locale = LocaleUtils.toLocale("zh"); {
+ // ja_JP_JP cannot handle dates before 1868 properly
+ final SimpleDateFormat sdf = new SimpleDateFormat(LONG_FORMAT,
locale);
+ final DateParser fdf = getInstance(LONG_FORMAT, locale);
- @Test
- public void testLocales_Long_AD() throws Exception {
- testLocales(LONG_FORMAT, false);
+ try {
+ checkParse(locale, cal, sdf, fdf);
+ } catch(final ParseException ex) {
+ Assert.fail("Locale "+locale+ " failed with "+LONG_FORMAT+"\n"
+ trimMessage(ex.toString()));
+ }
+ }
}
@Test
- public void testLocales_Long_BC() throws Exception {
- testLocales(LONG_FORMAT, true);
+ public void testLANG_831() throws Exception {
+ testSdfAndFdp("M E","3 Tue", true);
}
@Test
- public void testLocales_Short_AD() throws Exception {
- testLocales(SHORT_FORMAT, false);
+ public void testLANG_832() throws Exception {
+ testSdfAndFdp("'d'd" ,"d3", false); // OK
+ testSdfAndFdp("'d'd'","d3", true); // should fail (unterminated quote)
}
@Test
- public void testLocales_Short_BC() throws Exception {
- testLocales(SHORT_FORMAT, true);
- }
+ public void testLang1121() throws ParseException {
+ final TimeZone kst = TimeZone.getTimeZone("KST");
+ final DateParser fdp = getInstance("yyyyMMdd", kst, Locale.KOREA);
- @Test
- public void testLocales_LongNoEra_AD() throws Exception {
- testLocales(LONG_FORMAT_NOERA, false);
+ try {
+ fdp.parse("2015");
+ Assert.fail("expected parse exception");
+ } catch (final ParseException pe) {
+ // expected parse exception
+ }
+
+ // Wed Apr 29 00:00:00 KST 2015
+ Date actual = fdp.parse("20150429");
+ final Calendar cal = Calendar.getInstance(kst, Locale.KOREA);
+ cal.clear();
+ cal.set(2015, 3, 29);
+ Date expected = cal.getTime();
+ Assert.assertEquals(expected, actual);
+
+ final SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
Locale.KOREA);
+ df.setTimeZone(kst);
+ expected = df.parse("20150429113100");
+
+ // Thu Mar 16 00:00:00 KST 81724
+ actual = fdp.parse("20150429113100");
+ Assert.assertEquals(expected, actual);
}
@Test
- public void testLocales_LongNoEra_BC() throws Exception {
- testLocales(LONG_FORMAT_NOERA, true);
+ public void testLang303() throws ParseException {
+ DateParser parser = getInstance(YMD_SLASH);
+ final Calendar cal = Calendar.getInstance();
+ cal.set(2004, Calendar.DECEMBER, 31);
+
+ final Date date = parser.parse("2004/11/31");
+
+ parser =
SerializationUtils.deserialize(SerializationUtils.serialize((Serializable)
parser));
+ assertEquals(date, parser.parse("2004/11/31"));
}
@Test
- public void testLocales_ShortNoEra_AD() throws Exception {
- testLocales(SHORT_FORMAT_NOERA, false);
+ public void testLang538() throws ParseException {
+ final DateParser parser = getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
GMT);
+
+ final Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("GMT-8"));
+ cal.clear();
+ cal.set(2009, Calendar.OCTOBER, 16, 8, 42, 16);
+
+ assertEquals(cal.getTime(), parser.parse("2009-10-16T16:42:16.000Z"));
}
@Test
- public void testLocales_ShortNoEra_BC() throws Exception {
- testLocales(SHORT_FORMAT_NOERA, true);
+ public void testLang996() throws ParseException {
+ final Calendar expected = Calendar.getInstance(NEW_YORK, Locale.US);
+ expected.clear();
+ expected.set(2014, Calendar.MAY, 14);
+
+ final DateParser fdp = getInstance("ddMMMyyyy", NEW_YORK, Locale.US);
+ assertEquals(expected.getTime(), fdp.parse("14may2014"));
+ assertEquals(expected.getTime(), fdp.parse("14MAY2014"));
+ assertEquals(expected.getTime(), fdp.parse("14May2014"));
+ }
+
+ @Test
+ public void testLocaleMatches() {
+ final DateParser parser= getInstance(yMdHmsSZ, SWEDEN);
+ assertEquals(SWEDEN, parser.getLocale());
}
private void testLocales(final String format, final boolean eraBC) throws
Exception {
@@ -345,100 +407,181 @@ public class FastDateParserTest {
}
@Test
- public void testJpLocales() {
+ public void testLocales_Long_AD() throws Exception {
+ testLocales(LONG_FORMAT, false);
+ }
- final Calendar cal= Calendar.getInstance(GMT);
- cal.clear();
- cal.set(2003, Calendar.FEBRUARY, 10);
- cal.set(Calendar.ERA, GregorianCalendar.BC);
+ @Test
+ public void testLocales_Long_BC() throws Exception {
+ testLocales(LONG_FORMAT, true);
+ }
- final Locale locale = LocaleUtils.toLocale("zh"); {
- // ja_JP_JP cannot handle dates before 1868 properly
+ @Test
+ public void testLocales_LongNoEra_AD() throws Exception {
+ testLocales(LONG_FORMAT_NOERA, false);
+ }
- final SimpleDateFormat sdf = new SimpleDateFormat(LONG_FORMAT,
locale);
- final DateParser fdf = getInstance(LONG_FORMAT, locale);
+ @Test
+ public void testLocales_LongNoEra_BC() throws Exception {
+ testLocales(LONG_FORMAT_NOERA, true);
+ }
- try {
- checkParse(locale, cal, sdf, fdf);
- } catch(final ParseException ex) {
- Assert.fail("Locale "+locale+ " failed with "+LONG_FORMAT+"\n"
+ trimMessage(ex.toString()));
- }
- }
+ @Test
+ public void testLocales_Short_AD() throws Exception {
+ testLocales(SHORT_FORMAT, false);
}
- private String trimMessage(final String msg) {
- if (msg.length() < 100) {
- return msg;
- }
- final int gmt = msg.indexOf("(GMT");
- if (gmt > 0) {
- return msg.substring(0, gmt+4)+"...)";
- }
- return msg.substring(0, 100)+"...";
+ @Test
+ public void testLocales_Short_BC() throws Exception {
+ testLocales(SHORT_FORMAT, true);
}
- private void checkParse(final Locale locale, final Calendar cal, final
SimpleDateFormat sdf, final DateParser fdf) throws ParseException {
- final String formattedDate= sdf.format(cal.getTime());
- checkParse(locale, sdf, fdf, formattedDate);
- checkParse(locale, sdf, fdf, formattedDate.toLowerCase(locale));
- checkParse(locale, sdf, fdf, formattedDate.toUpperCase(locale));
+ @Test
+ public void testLocales_ShortNoEra_AD() throws Exception {
+ testLocales(SHORT_FORMAT_NOERA, false);
}
- private void checkParse(final Locale locale, final SimpleDateFormat sdf,
final DateParser fdf, final String formattedDate) throws ParseException {
- final Date expectedTime = sdf.parse(formattedDate);
- final Date actualTime = fdf.parse(formattedDate);
- assertEquals(locale.toString()+" "+formattedDate +"\n",expectedTime,
actualTime);
+ @Test
+ public void testLocales_ShortNoEra_BC() throws Exception {
+ testLocales(SHORT_FORMAT_NOERA, true);
}
+ /**
+ * Tests that pre-1000AD years get padded with yyyy
+ * @throws ParseException so we don't have to catch it
+ */
@Test
- public void testParseNumerics() throws ParseException {
+ public void testLowYearPadding() throws ParseException {
+ final DateParser parser = getInstance(YMD_SLASH);
+ final Calendar cal = Calendar.getInstance();
+ cal.clear();
+
+ cal.set(1, Calendar.JANUARY, 1);
+ assertEquals(cal.getTime(), parser.parse("0001/01/01"));
+ cal.set(10, Calendar.JANUARY, 1);
+ assertEquals(cal.getTime(), parser.parse("0010/01/01"));
+ cal.set(100, Calendar.JANUARY, 1);
+ assertEquals(cal.getTime(), parser.parse("0100/01/01"));
+ cal.set(999, Calendar.JANUARY, 1);
+ assertEquals(cal.getTime(), parser.parse("0999/01/01"));
+ }
+
+ @Test
+ public void testMilleniumBug() throws ParseException {
+ final DateParser parser = getInstance(DMY_DOT);
+ final Calendar cal = Calendar.getInstance();
+ cal.clear();
+
+ cal.set(1000, Calendar.JANUARY, 1);
+ assertEquals(cal.getTime(), parser.parse("01.01.1000"));
+ }
+
+ @Test
+ public void testParseLongShort() throws ParseException {
final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
cal.clear();
cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20);
cal.set(Calendar.MILLISECOND, 989);
+ cal.setTimeZone(NEW_YORK);
- final DateParser fdf = getInstance("yyyyMMddHHmmssSSS", NEW_YORK,
Locale.US);
- assertEquals(cal.getTime(), fdf.parse("20030210153320989"));
+ DateParser fdf = getInstance("yyyy GGGG MMMM dddd aaaa EEEE HHHH mmmm
ssss SSSS ZZZZ", NEW_YORK, Locale.US);
+
+ assertEquals(cal.getTime(), fdf.parse("2003 AD February 0010 PM Monday
0015 0033 0020 0989 GMT-05:00"));
+ cal.set(Calendar.ERA, GregorianCalendar.BC);
+
+ final Date parse = fdf.parse("2003 BC February 0010 PM Saturday 0015
0033 0020 0989 GMT-05:00");
+ assertEquals(cal.getTime(), parse);
+
+ fdf = getInstance("y G M d a E H m s S Z", NEW_YORK, Locale.US);
+ assertEquals(cal.getTime(), fdf.parse("03 BC 2 10 PM Sat 15 33 20 989
-0500"));
+
+ cal.set(Calendar.ERA, GregorianCalendar.AD);
+ assertEquals(cal.getTime(), fdf.parse("03 AD 2 10 PM Saturday 15 33 20
989 -0500"));
}
@Test
- public void testQuotes() throws ParseException {
+ public void testParseNumerics() throws ParseException {
final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
cal.clear();
cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20);
cal.set(Calendar.MILLISECOND, 989);
- final DateParser fdf = getInstance("''yyyyMMdd'A''B'HHmmssSSS''",
NEW_YORK, Locale.US);
- assertEquals(cal.getTime(), fdf.parse("'20030210A'B153320989'"));
+ final DateParser fdf = getInstance("yyyyMMddHHmmssSSS", NEW_YORK,
Locale.US);
+ assertEquals(cal.getTime(), fdf.parse("20030210153320989"));
}
@Test
- public void testSpecialCharacters() throws Exception {
- testSdfAndFdp("q" ,"", true); // bad pattern character (at present)
- testSdfAndFdp("Q" ,"", true); // bad pattern character
- testSdfAndFdp("$" ,"$", false); // OK
- testSdfAndFdp("?.d" ,"?.12", false); // OK
- testSdfAndFdp("''yyyyMMdd'A''B'HHmmssSSS''", "'20030210A'B153320989'",
false); // OK
- testSdfAndFdp("''''yyyyMMdd'A''B'HHmmssSSS''",
"''20030210A'B153320989'", false); // OK
- testSdfAndFdp("'$\\Ed'" ,"$\\Ed", false); // OK
+ public void testParseOffset() {
+ final DateParser parser = getInstance(YMD_SLASH);
+ final Date date = parser.parse("Today is 2015/07/04", new
ParsePosition(9));
- // quoted charaters are case sensitive
- testSdfAndFdp("'QED'", "QED", false);
- testSdfAndFdp("'QED'", "qed", true);
- // case sensitive after insensitive Month field
- testSdfAndFdp("yyyy-MM-dd 'QED'", "2003-02-10 QED", false);
- testSdfAndFdp("yyyy-MM-dd 'QED'", "2003-02-10 qed", true);
+ final Calendar cal = Calendar.getInstance();
+ cal.clear();
+ cal.set(2015, Calendar.JULY, 4);
+ Assert.assertEquals(cal.getTime(), date);
}
@Test
- public void testLANG_832() throws Exception {
- testSdfAndFdp("'d'd" ,"d3", false); // OK
- testSdfAndFdp("'d'd'","d3", true); // should fail (unterminated quote)
+ // Check that all Locales can parse the formats we use
+ public void testParses() throws Exception {
+ for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
+ for(final Locale locale : Locale.getAvailableLocales()) {
+ for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK,
GMT}) {
+ for(final int year : new int[]{2003, 1940, 1868, 1867, 1,
-1, -1940}) {
+ final Calendar cal= getEraStart(year, tz, locale);
+ final Date centuryStart= cal.getTime();
+
+ cal.set(Calendar.MONTH, 1);
+ cal.set(Calendar.DAY_OF_MONTH, 10);
+ final Date in= cal.getTime();
+
+ final FastDateParser fdp= new FastDateParser(format,
tz, locale, centuryStart);
+ validateSdfFormatFdpParseEquality(format, locale, tz,
fdp, in, year, centuryStart);
+ }
+ }
+ }
+ }
}
@Test
- public void testLANG_831() throws Exception {
- testSdfAndFdp("M E","3 Tue", true);
+ public void testParseZone() throws ParseException {
+ final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
+ cal.clear();
+ cal.set(2003, Calendar.JULY, 10, 16, 33, 20);
+
+ final DateParser fdf = getInstance(yMdHmsSZ, NEW_YORK, Locale.US);
+
+ assertEquals(cal.getTime(), fdf.parse("2003-07-10T15:33:20.000
-0500"));
+ assertEquals(cal.getTime(), fdf.parse("2003-07-10T15:33:20.000
GMT-05:00"));
+ assertEquals(cal.getTime(), fdf.parse("2003-07-10T16:33:20.000 Eastern
Daylight Time"));
+ assertEquals(cal.getTime(), fdf.parse("2003-07-10T16:33:20.000 EDT"));
+
+ cal.setTimeZone(TimeZone.getTimeZone("GMT-3"));
+ cal.set(2003, Calendar.FEBRUARY, 10, 9, 0, 0);
+
+ assertEquals(cal.getTime(), fdf.parse("2003-02-10T09:00:00.000
-0300"));
+
+ cal.setTimeZone(TimeZone.getTimeZone("GMT+5"));
+ cal.set(2003, Calendar.FEBRUARY, 10, 15, 5, 6);
+
+ assertEquals(cal.getTime(), fdf.parse("2003-02-10T15:05:06.000
+0500"));
+ }
+
+ @Test
+ public void testPatternMatches() {
+ final DateParser parser= getInstance(yMdHmsSZ);
+ assertEquals(yMdHmsSZ, parser.getPattern());
+ }
+
+ @Test
+ public void testQuotes() throws ParseException {
+ final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
+ cal.clear();
+ cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20);
+ cal.set(Calendar.MILLISECOND, 989);
+
+ final DateParser fdf = getInstance("''yyyyMMdd'A''B'HHmmssSSS''",
NEW_YORK, Locale.US);
+ assertEquals(cal.getTime(), fdf.parse("'20030210A'B153320989'"));
}
private void testSdfAndFdp(final String format, final String date, final
boolean shouldFail)
@@ -479,16 +622,6 @@ public class FastDateParserTest {
assertEquals("Parsed dates should be equal", dsdf, dfdp);
}
- @Test
- public void testDayOf() throws ParseException {
- final Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);
- cal.clear();
- cal.set(2003, Calendar.FEBRUARY, 10);
-
- final DateParser fdf = getInstance("W w F D y", NEW_YORK, Locale.US);
- assertEquals(cal.getTime(), fdf.parse("3 7 2 41 03"));
- }
-
/**
* Test case for {@link FastDateParser#FastDateParser(String, TimeZone,
Locale)}.
* @throws ParseException so we don't have to catch it
@@ -506,68 +639,28 @@ public class FastDateParserTest {
assertEquals(cal.getTime(), fdf.parse("2004-02-03"));
}
- /**
- * Tests that pre-1000AD years get padded with yyyy
- * @throws ParseException so we don't have to catch it
- */
- @Test
- public void testLowYearPadding() throws ParseException {
- final DateParser parser = getInstance(YMD_SLASH);
- final Calendar cal = Calendar.getInstance();
- cal.clear();
-
- cal.set(1, Calendar.JANUARY, 1);
- assertEquals(cal.getTime(), parser.parse("0001/01/01"));
- cal.set(10, Calendar.JANUARY, 1);
- assertEquals(cal.getTime(), parser.parse("0010/01/01"));
- cal.set(100, Calendar.JANUARY, 1);
- assertEquals(cal.getTime(), parser.parse("0100/01/01"));
- cal.set(999, Calendar.JANUARY, 1);
- assertEquals(cal.getTime(), parser.parse("0999/01/01"));
- }
-
- @Test
- public void testMilleniumBug() throws ParseException {
- final DateParser parser = getInstance(DMY_DOT);
- final Calendar cal = Calendar.getInstance();
- cal.clear();
-
- cal.set(1000, Calendar.JANUARY, 1);
- assertEquals(cal.getTime(), parser.parse("01.01.1000"));
- }
-
- @Test
- public void testLang303() throws ParseException {
- DateParser parser = getInstance(YMD_SLASH);
- final Calendar cal = Calendar.getInstance();
- cal.set(2004, Calendar.DECEMBER, 31);
-
- final Date date = parser.parse("2004/11/31");
-
- parser =
SerializationUtils.deserialize(SerializationUtils.serialize((Serializable)
parser));
- assertEquals(date, parser.parse("2004/11/31"));
- }
-
@Test
- public void testLang538() throws ParseException {
- final DateParser parser = getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
GMT);
-
- final Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("GMT-8"));
- cal.clear();
- cal.set(2009, Calendar.OCTOBER, 16, 8, 42, 16);
+ public void testSpecialCharacters() throws Exception {
+ testSdfAndFdp("q" ,"", true); // bad pattern character (at present)
+ testSdfAndFdp("Q" ,"", true); // bad pattern character
+ testSdfAndFdp("$" ,"$", false); // OK
+ testSdfAndFdp("?.d" ,"?.12", false); // OK
+ testSdfAndFdp("''yyyyMMdd'A''B'HHmmssSSS''", "'20030210A'B153320989'",
false); // OK
+ testSdfAndFdp("''''yyyyMMdd'A''B'HHmmssSSS''",
"''20030210A'B153320989'", false); // OK
+ testSdfAndFdp("'$\\Ed'" ,"$\\Ed", false); // OK
- assertEquals(cal.getTime(), parser.parse("2009-10-16T16:42:16.000Z"));
+ // quoted charaters are case sensitive
+ testSdfAndFdp("'QED'", "QED", false);
+ testSdfAndFdp("'QED'", "qed", true);
+ // case sensitive after insensitive Month field
+ testSdfAndFdp("yyyy-MM-dd 'QED'", "2003-02-10 QED", false);
+ testSdfAndFdp("yyyy-MM-dd 'QED'", "2003-02-10 qed", true);
}
@Test
- public void testEquals() {
- final DateParser parser1= getInstance(YMD_SLASH);
- final DateParser parser2= getInstance(YMD_SLASH);
-
- assertEquals(parser1, parser2);
- assertEquals(parser1.hashCode(), parser2.hashCode());
-
- assertFalse(parser1.equals(new Object()));
+ public void testTimeZoneMatches() {
+ final DateParser parser= getInstance(yMdHmsSZ, REYKJAVIK);
+ assertEquals(REYKJAVIK, parser.getTimeZone());
}
@Test
@@ -576,146 +669,53 @@ public class FastDateParserTest {
assertTrue(parser.toString().startsWith("FastDate"));
}
+ // we cannot use historic dates to test timezone parsing, some timezones
have second offsets
+ // as well as hours and minutes which makes the z formats a low fidelity
round trip
@Test
- public void testPatternMatches() {
- final DateParser parser= getInstance(yMdHmsSZ);
- assertEquals(yMdHmsSZ, parser.getPattern());
- }
-
- @Test
- public void testLocaleMatches() {
- final DateParser parser= getInstance(yMdHmsSZ, SWEDEN);
- assertEquals(SWEDEN, parser.getLocale());
- }
-
- @Test
- public void testTimeZoneMatches() {
- final DateParser parser= getInstance(yMdHmsSZ, REYKJAVIK);
- assertEquals(REYKJAVIK, parser.getTimeZone());
- }
-
- @Test
- public void testLang996() throws ParseException {
- final Calendar expected = Calendar.getInstance(NEW_YORK, Locale.US);
- expected.clear();
- expected.set(2014, Calendar.MAY, 14);
-
- final DateParser fdp = getInstance("ddMMMyyyy", NEW_YORK, Locale.US);
- assertEquals(expected.getTime(), fdp.parse("14may2014"));
- assertEquals(expected.getTime(), fdp.parse("14MAY2014"));
- assertEquals(expected.getTime(), fdp.parse("14May2014"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void test1806Argument() {
- getInstance("XXXX");
- }
-
- private static Calendar initializeCalendar(final TimeZone tz) {
- final Calendar cal = Calendar.getInstance(tz);
- cal.set(Calendar.YEAR, 2001);
- cal.set(Calendar.MONTH, 1); // not daylight savings
- cal.set(Calendar.DAY_OF_MONTH, 4);
- cal.set(Calendar.HOUR_OF_DAY, 12);
- cal.set(Calendar.MINUTE, 8);
- cal.set(Calendar.SECOND, 56);
- cal.set(Calendar.MILLISECOND, 235);
- return cal;
- }
+ public void testTzParses() throws Exception {
+ // Check that all Locales can parse the time formats we use
+ for(final Locale locale : Locale.getAvailableLocales()) {
+ final FastDateParser fdp= new FastDateParser("yyyy/MM/dd z",
TimeZone.getDefault(), locale);
- private static enum Expected1806 {
- India(INDIA, "+05", "+0530", "+05:30", true),
- Greenwich(GMT, "Z", "Z", "Z", false),
- NewYork(NEW_YORK, "-05", "-0500", "-05:00", false);
+ for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK, GMT})
{
+ final Calendar cal= Calendar.getInstance(tz, locale);
+ cal.clear();
+ cal.set(Calendar.YEAR, 2000);
+ cal.set(Calendar.MONTH, 1);
+ cal.set(Calendar.DAY_OF_MONTH, 10);
+ final Date expected= cal.getTime();
- private Expected1806(final TimeZone zone, final String one, final
String two, final String three, final boolean hasHalfHourOffset) {
- this.zone = zone;
- this.one = one;
- this.two = two;
- this.three = three;
- this.offset = hasHalfHourOffset ?30*60*1000 :0;
+ final Date actual = fdp.parse("2000/02/10
"+tz.getDisplayName(locale));
+ Assert.assertEquals("tz:"+tz.getID()+"
locale:"+locale.getDisplayName(), expected, actual);
+ }
}
-
- final TimeZone zone;
- final String one;
- final String two;
- final String three;
- final long offset;
}
- @Test
- public void test1806() throws ParseException {
- final String formatStub = "yyyy-MM-dd'T'HH:mm:ss.SSS";
- final String dateStub = "2001-02-04T12:08:56.235";
-
- for (final Expected1806 trial : Expected1806.values()) {
- final Calendar cal = initializeCalendar(trial.zone);
-
- final String message = trial.zone.getDisplayName()+";";
-
- DateParser parser = getInstance(formatStub+"X", trial.zone);
- assertEquals(message+trial.one, cal.getTime().getTime(),
parser.parse(dateStub+trial.one).getTime()-trial.offset);
-
- parser = getInstance(formatStub+"XX", trial.zone);
- assertEquals(message+trial.two, cal.getTime(),
parser.parse(dateStub+trial.two));
-
- parser = getInstance(formatStub+"XXX", trial.zone);
- assertEquals(message+trial.three, cal.getTime(),
parser.parse(dateStub+trial.three));
+ private String trimMessage(final String msg) {
+ if (msg.length() < 100) {
+ return msg;
+ }
+ final int gmt = msg.indexOf("(GMT");
+ if (gmt > 0) {
+ return msg.substring(0, gmt+4)+"...)";
}
+ return msg.substring(0, 100)+"...";
}
- @Test
- public void testLang1121() throws ParseException {
- final TimeZone kst = TimeZone.getTimeZone("KST");
- final DateParser fdp = getInstance("yyyyMMdd", kst, Locale.KOREA);
-
+ private void validateSdfFormatFdpParseEquality(final String format, final
Locale locale, final TimeZone tz, final DateParser fdp, final Date in, final
int year, final Date cs) throws ParseException {
+ final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
+ sdf.setTimeZone(tz);
+ if (format.equals(SHORT_FORMAT)) {
+ sdf.set2DigitYearStart( cs );
+ }
+ final String fmt = sdf.format(in);
try {
- fdp.parse("2015");
- Assert.fail("expected parse exception");
+ final Date out = fdp.parse(fmt);
+ assertEquals(locale.toString()+" "+in+" "+ format+ " "+tz.getID(),
in, out);
} catch (final ParseException pe) {
- // expected parse exception
+ if (year >= 1868 || !locale.getCountry().equals("JP")) {// LANG-978
+ throw pe;
+ }
}
-
- // Wed Apr 29 00:00:00 KST 2015
- Date actual = fdp.parse("20150429");
- final Calendar cal = Calendar.getInstance(kst, Locale.KOREA);
- cal.clear();
- cal.set(2015, 3, 29);
- Date expected = cal.getTime();
- Assert.assertEquals(expected, actual);
-
- final SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
Locale.KOREA);
- df.setTimeZone(kst);
- expected = df.parse("20150429113100");
-
- // Thu Mar 16 00:00:00 KST 81724
- actual = fdp.parse("20150429113100");
- Assert.assertEquals(expected, actual);
- }
-
- @Test
- public void testParseOffset() {
- final DateParser parser = getInstance(YMD_SLASH);
- final Date date = parser.parse("Today is 2015/07/04", new
ParsePosition(9));
-
- final Calendar cal = Calendar.getInstance();
- cal.clear();
- cal.set(2015, Calendar.JULY, 4);
- Assert.assertEquals(cal.getTime(), date);
- }
-
- @Test
- public void testDayNumberOfWeek() throws ParseException {
- final DateParser parser = getInstance("u");
- final Calendar calendar = Calendar.getInstance();
-
- calendar.setTime(parser.parse("1"));
- Assert.assertEquals(Calendar.MONDAY,
calendar.get(Calendar.DAY_OF_WEEK));
-
- calendar.setTime(parser.parse("6"));
- Assert.assertEquals(Calendar.SATURDAY,
calendar.get(Calendar.DAY_OF_WEEK));
-
- calendar.setTime(parser.parse("7"));
- Assert.assertEquals(Calendar.SUNDAY,
calendar.get(Calendar.DAY_OF_WEEK));
}
}
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_MoreOrLessTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_MoreOrLessTest.java
index e95631a..e6a05d5 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_MoreOrLessTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_MoreOrLessTest.java
@@ -33,6 +33,27 @@ public class FastDateParser_MoreOrLessTest {
private static final TimeZone NEW_YORK =
TimeZone.getTimeZone("America/New_York");
@Test
+ public void testInputHasLessCharacters() {
+ final FastDateParser parser = new FastDateParser("MM/dd/yyy",
TimeZone.getDefault(), Locale.getDefault());
+ final ParsePosition parsePosition = new ParsePosition(0);
+ Assert.assertNull(parser.parse("03/23", parsePosition));
+ Assert.assertEquals(5, parsePosition.getErrorIndex());
+ }
+
+ @Test
+ public void testInputHasMoreCharacters() {
+ final FastDateParser parser = new FastDateParser("MM/dd",
TimeZone.getDefault(), Locale.getDefault());
+ final ParsePosition parsePosition = new ParsePosition(0);
+ final Date date = parser.parse("3/23/61", parsePosition);
+ Assert.assertEquals(4, parsePosition.getIndex());
+
+ final Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ Assert.assertEquals(2, calendar.get(Calendar.MONTH));
+ Assert.assertEquals(23, calendar.get(Calendar.DATE));
+ }
+
+ @Test
public void testInputHasPrecedingCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd",
TimeZone.getDefault(), Locale.getDefault());
final ParsePosition parsePosition = new ParsePosition(0);
@@ -58,19 +79,6 @@ public class FastDateParser_MoreOrLessTest {
}
@Test
- public void testInputHasMoreCharacters() {
- final FastDateParser parser = new FastDateParser("MM/dd",
TimeZone.getDefault(), Locale.getDefault());
- final ParsePosition parsePosition = new ParsePosition(0);
- final Date date = parser.parse("3/23/61", parsePosition);
- Assert.assertEquals(4, parsePosition.getIndex());
-
- final Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- Assert.assertEquals(2, calendar.get(Calendar.MONTH));
- Assert.assertEquals(23, calendar.get(Calendar.DATE));
- }
-
- @Test
public void testInputHasWrongCharacters() {
final FastDateParser parser = new FastDateParser("MM-dd-yyy",
TimeZone.getDefault(), Locale.getDefault());
final ParsePosition parsePosition = new ParsePosition(0);
@@ -79,11 +87,16 @@ public class FastDateParser_MoreOrLessTest {
}
@Test
- public void testInputHasLessCharacters() {
- final FastDateParser parser = new FastDateParser("MM/dd/yyy",
TimeZone.getDefault(), Locale.getDefault());
+ public void testInputHasWrongDay() {
+ final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy",
NEW_YORK, Locale.US);
+ final String input = "Thursday, 03/23/61";
final ParsePosition parsePosition = new ParsePosition(0);
- Assert.assertNull(parser.parse("03/23", parsePosition));
- Assert.assertEquals(5, parsePosition.getErrorIndex());
+ Assert.assertNotNull(parser.parse(input, parsePosition));
+ Assert.assertEquals(input.length(), parsePosition.getIndex());
+
+ parsePosition.setIndex(0);
+ Assert.assertNull(parser.parse( "Thorsday, 03/23/61", parsePosition));
+ Assert.assertEquals(0, parsePosition.getErrorIndex());
}
@Test
@@ -99,17 +112,4 @@ public class FastDateParser_MoreOrLessTest {
Assert.assertNull(parser.parse( "11:23 Pacific Standard ",
parsePosition));
Assert.assertEquals(6, parsePosition.getErrorIndex());
}
-
- @Test
- public void testInputHasWrongDay() {
- final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy",
NEW_YORK, Locale.US);
- final String input = "Thursday, 03/23/61";
- final ParsePosition parsePosition = new ParsePosition(0);
- Assert.assertNotNull(parser.parse(input, parsePosition));
- Assert.assertEquals(input.length(), parsePosition.getIndex());
-
- parsePosition.setIndex(0);
- Assert.assertNull(parser.parse( "Thorsday, 03/23/61", parsePosition));
- Assert.assertEquals(0, parsePosition.getErrorIndex());
- }
}
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_TimeZoneStrategyTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_TimeZoneStrategyTest.java
index 64d5ed3..ce4a86e 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_TimeZoneStrategyTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FastDateParser_TimeZoneStrategyTest.java
@@ -31,6 +31,15 @@ import org.junit.Test;
public class FastDateParser_TimeZoneStrategyTest {
@Test
+ public void testLang1219() throws ParseException {
+ final FastDateParser parser = new FastDateParser("dd.MM.yyyy HH:mm:ss
z", TimeZone.getDefault(), Locale.GERMAN);
+
+ final Date summer = parser.parse("26.10.2014 02:00:00 MESZ");
+ final Date standard = parser.parse("26.10.2014 02:00:00 MEZ");
+ Assert.assertNotEquals(summer.getTime(), standard.getTime());
+ }
+
+ @Test
public void testTimeZoneStrategyPattern() {
for(final Locale locale : Locale.getAvailableLocales()) {
final FastDateParser parser = new FastDateParser("z",
TimeZone.getDefault(), locale);
@@ -57,13 +66,4 @@ public class FastDateParser_TimeZoneStrategyTest {
}
}
}
-
- @Test
- public void testLang1219() throws ParseException {
- final FastDateParser parser = new FastDateParser("dd.MM.yyyy HH:mm:ss
z", TimeZone.getDefault(), Locale.GERMAN);
-
- final Date summer = parser.parse("26.10.2014 02:00:00 MESZ");
- final Date standard = parser.parse("26.10.2014 02:00:00 MEZ");
- Assert.assertNotEquals(summer.getTime(), standard.getTime());
- }
}
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java
index dc53b9c..43c710a 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java
@@ -38,70 +38,32 @@ import org.junit.Test;
*/
public class FixedDateFormatTest {
- @Test
- public void testFixedFormat_getDatePatternNullIfNoDateInPattern() {
- assertNull(FixedFormat.ABSOLUTE.getDatePattern());
- assertNull(FixedFormat.ABSOLUTE_PERIOD.getDatePattern());
- }
-
- @Test
- public void testFixedFormat_getDatePatternLengthZeroIfNoDateInPattern() {
- assertEquals(0, FixedFormat.ABSOLUTE.getDatePatternLength());
- assertEquals(0, FixedFormat.ABSOLUTE_PERIOD.getDatePatternLength());
- }
-
- @Test
- public void testFixedFormat_getFastDateFormatNullIfNoDateInPattern() {
- assertNull(FixedFormat.ABSOLUTE.getFastDateFormat());
- assertNull(FixedFormat.ABSOLUTE_PERIOD.getFastDateFormat());
- }
-
- @Test
- public void testFixedFormat_getDatePatternReturnsDatePatternIfExists() {
- assertEquals("yyyyMMdd", FixedFormat.COMPACT.getDatePattern());
- assertEquals("yyyy-MM-dd ", DEFAULT.getDatePattern());
- }
-
- @Test
- public void testFixedFormat_getDatePatternLengthReturnsDatePatternLength()
{
- assertEquals("yyyyMMdd".length(),
FixedFormat.COMPACT.getDatePatternLength());
- assertEquals("yyyy-MM-dd ".length(), DEFAULT.getDatePatternLength());
- }
-
- @Test
- public void testFixedFormat_getFastDateFormatNonNullIfDateInPattern() {
- assertNotNull(FixedFormat.COMPACT.getFastDateFormat());
- assertNotNull(DEFAULT.getFastDateFormat());
- assertEquals("yyyyMMdd",
FixedFormat.COMPACT.getFastDateFormat().getPattern());
- assertEquals("yyyy-MM-dd ", DEFAULT.getFastDateFormat().getPattern());
+ private boolean containsNanos(final FixedFormat fixedFormat) {
+ final String pattern = fixedFormat.getPattern();
+ return pattern.endsWith("n") || pattern.matches(".+n+X*") ||
pattern.matches(".+n+Z*");
}
- @Test
- public void testCreateIfSupported_nonNullIfNameMatches() {
- for (final FixedDateFormat.FixedFormat format :
FixedDateFormat.FixedFormat.values()) {
- final String[] options = {format.name()};
- assertNotNull(format.name(),
FixedDateFormat.createIfSupported(options));
- }
+ @Test(expected = NullPointerException.class)
+ public void testConstructorDisallowsNullFormat() {
+ new FixedDateFormat(null, TimeZone.getDefault());
}
- @Test
- public void testCreateIfSupported_nonNullIfPatternMatches() {
- for (final FixedDateFormat.FixedFormat format :
FixedDateFormat.FixedFormat.values()) {
- final String[] options = {format.getPattern()};
- assertNotNull(format.name(),
FixedDateFormat.createIfSupported(options));
- }
+ @Test(expected = NullPointerException.class)
+ public void testConstructorDisallowsNullTimeZone() {
+ new FixedDateFormat(FixedFormat.ABSOLUTE, null);
}
@Test
- public void testCreateIfSupported_nullIfNameDoesNotMatch() {
- final String[] options = {"DEFAULT3"};
- assertNull("DEFAULT3", FixedDateFormat.createIfSupported(options));
+ public void
testCreateIfSupported_customTimeZoneIfOptionsArrayWithTimeZoneElement() {
+ final FixedDateFormat fmt =
FixedDateFormat.createIfSupported(DEFAULT.getPattern(), "+08:00", "");
+ assertEquals(DEFAULT.getPattern(), fmt.getFormat());
+ assertEquals(TimeZone.getTimeZone("+08:00"), fmt.getTimeZone());
}
@Test
- public void testCreateIfSupported_nullIfPatternDoesNotMatch() {
- final String[] options = {"y M d H m s"};
- assertNull("y M d H m s", FixedDateFormat.createIfSupported(options));
+ public void testCreateIfSupported_defaultIfOptionsArrayEmpty() {
+ final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new
String[0]);
+ assertEquals(DEFAULT.getPattern(), fmt.getFormat());
}
@Test
@@ -111,12 +73,6 @@ public class FixedDateFormatTest {
}
@Test
- public void testCreateIfSupported_defaultIfOptionsArrayEmpty() {
- final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new
String[0]);
- assertEquals(DEFAULT.getPattern(), fmt.getFormat());
- }
-
- @Test
public void
testCreateIfSupported_defaultIfOptionsArrayWithSingleNullElement() {
final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new
String[1]);
assertEquals(DEFAULT.getPattern(), fmt.getFormat());
@@ -131,112 +87,31 @@ public class FixedDateFormatTest {
}
@Test
- public void
testCreateIfSupported_customTimeZoneIfOptionsArrayWithTimeZoneElement() {
- final FixedDateFormat fmt =
FixedDateFormat.createIfSupported(DEFAULT.getPattern(), "+08:00", "");
- assertEquals(DEFAULT.getPattern(), fmt.getFormat());
- assertEquals(TimeZone.getTimeZone("+08:00"), fmt.getTimeZone());
- }
-
- @Test(expected = NullPointerException.class)
- public void testConstructorDisallowsNullFormat() {
- new FixedDateFormat(null, TimeZone.getDefault());
- }
-
- @Test(expected = NullPointerException.class)
- public void testConstructorDisallowsNullTimeZone() {
- new FixedDateFormat(FixedFormat.ABSOLUTE, null);
- }
-
- @Test
- public void testGetFormatReturnsConstructorFixedFormatPattern() {
- final FixedDateFormat format = new
FixedDateFormat(FixedDateFormat.FixedFormat.ABSOLUTE, TimeZone.getDefault());
- assertSame(FixedDateFormat.FixedFormat.ABSOLUTE.getPattern(),
format.getFormat());
- }
-
- @Test
- public void testFormatLong() {
- final long now = System.currentTimeMillis();
- final long start = now - TimeUnit.HOURS.toMillis(25);
- final long end = now + TimeUnit.HOURS.toMillis(25);
- for (final FixedFormat format : FixedFormat.values()) {
- final String pattern = format.getPattern();
- if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
- continue; // cannot compile precise timestamp formats with
SimpleDateFormat
- }
- final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
- final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
- for (long time = start; time < end; time += 12345) {
- final String actual = customTF.format(time);
- final String expected = simpleDF.format(new Date(time));
- assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
- }
+ public void testCreateIfSupported_nonNullIfNameMatches() {
+ for (final FixedDateFormat.FixedFormat format :
FixedDateFormat.FixedFormat.values()) {
+ final String[] options = {format.name()};
+ assertNotNull(format.name(),
FixedDateFormat.createIfSupported(options));
}
}
@Test
- public void testFormatLong_goingBackInTime() {
- final long now = System.currentTimeMillis();
- final long start = now - TimeUnit.HOURS.toMillis(25);
- final long end = now + TimeUnit.HOURS.toMillis(25);
- for (final FixedFormat format : FixedFormat.values()) {
- final String pattern = format.getPattern();
- if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
- continue; // cannot compile precise timestamp formats with
SimpleDateFormat
- }
- final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
- final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
- for (long time = end; time > start; time -= 12345) {
- final String actual = customTF.format(time);
- final String expected = simpleDF.format(new Date(time));
- assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
- }
+ public void testCreateIfSupported_nonNullIfPatternMatches() {
+ for (final FixedDateFormat.FixedFormat format :
FixedDateFormat.FixedFormat.values()) {
+ final String[] options = {format.getPattern()};
+ assertNotNull(format.name(),
FixedDateFormat.createIfSupported(options));
}
}
@Test
- public void testFormatLongCharArrayInt() {
- final long now = System.currentTimeMillis();
- final long start = now - TimeUnit.HOURS.toMillis(25);
- final long end = now + TimeUnit.HOURS.toMillis(25);
- final char[] buffer = new char[128];
- for (final FixedFormat format : FixedFormat.values()) {
- final String pattern = format.getPattern();
- if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
- // cannot compile precise timestamp formats with
SimpleDateFormat
- // This format() API not include the TZ
- continue;
- }
- final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
- final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
- for (long time = start; time < end; time += 12345) {
- final int length = customTF.format(time, buffer, 23);
- final String actual = new String(buffer, 23, length);
- final String expected = simpleDF.format(new Date(time));
- assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
- }
- }
+ public void testCreateIfSupported_nullIfNameDoesNotMatch() {
+ final String[] options = {"DEFAULT3"};
+ assertNull("DEFAULT3", FixedDateFormat.createIfSupported(options));
}
@Test
- public void testFormatLongCharArrayInt_goingBackInTime() {
- final long now = System.currentTimeMillis();
- final long start = now - TimeUnit.HOURS.toMillis(25);
- final long end = now + TimeUnit.HOURS.toMillis(25);
- final char[] buffer = new char[128];
- for (final FixedFormat format : FixedFormat.values()) {
- final String pattern = format.getPattern();
- if (pattern.endsWith("n") || pattern.matches(".+n+X*") ||
pattern.matches(".+n+Z*") || format.getFixedTimeZoneFormat() != null) {
- continue; // cannot compile precise timestamp formats with
SimpleDateFormat
- }
- final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
- final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
- for (long time = end; time > start; time -= 12345) {
- final int length = customTF.format(time, buffer, 23);
- final String actual = new String(buffer, 23, length);
- final String expected = simpleDF.format(new Date(time));
- assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
- }
- }
+ public void testCreateIfSupported_nullIfPatternDoesNotMatch() {
+ final String[] options = {"y M d H m s"};
+ assertNull("y M d H m s", FixedDateFormat.createIfSupported(options));
}
@Test
@@ -369,12 +244,85 @@ public class FixedDateFormatTest {
calendar.add(Calendar.HOUR_OF_DAY, 1);
}
}
-
- private boolean containsNanos(final FixedFormat fixedFormat) {
- final String pattern = fixedFormat.getPattern();
- return pattern.endsWith("n") || pattern.matches(".+n+X*") ||
pattern.matches(".+n+Z*");
+
+ @Test
+ public void testFixedFormat_getDatePatternLengthReturnsDatePatternLength()
{
+ assertEquals("yyyyMMdd".length(),
FixedFormat.COMPACT.getDatePatternLength());
+ assertEquals("yyyy-MM-dd ".length(), DEFAULT.getDatePatternLength());
}
-
+
+ @Test
+ public void testFixedFormat_getDatePatternLengthZeroIfNoDateInPattern() {
+ assertEquals(0, FixedFormat.ABSOLUTE.getDatePatternLength());
+ assertEquals(0, FixedFormat.ABSOLUTE_PERIOD.getDatePatternLength());
+ }
+
+ @Test
+ public void testFixedFormat_getDatePatternNullIfNoDateInPattern() {
+ assertNull(FixedFormat.ABSOLUTE.getDatePattern());
+ assertNull(FixedFormat.ABSOLUTE_PERIOD.getDatePattern());
+ }
+
+ @Test
+ public void testFixedFormat_getDatePatternReturnsDatePatternIfExists() {
+ assertEquals("yyyyMMdd", FixedFormat.COMPACT.getDatePattern());
+ assertEquals("yyyy-MM-dd ", DEFAULT.getDatePattern());
+ }
+
+ @Test
+ public void testFixedFormat_getFastDateFormatNonNullIfDateInPattern() {
+ assertNotNull(FixedFormat.COMPACT.getFastDateFormat());
+ assertNotNull(DEFAULT.getFastDateFormat());
+ assertEquals("yyyyMMdd",
FixedFormat.COMPACT.getFastDateFormat().getPattern());
+ assertEquals("yyyy-MM-dd ", DEFAULT.getFastDateFormat().getPattern());
+ }
+
+ @Test
+ public void testFixedFormat_getFastDateFormatNullIfNoDateInPattern() {
+ assertNull(FixedFormat.ABSOLUTE.getFastDateFormat());
+ assertNull(FixedFormat.ABSOLUTE_PERIOD.getFastDateFormat());
+ }
+
+ @Test
+ public void testFormatLong() {
+ final long now = System.currentTimeMillis();
+ final long start = now - TimeUnit.HOURS.toMillis(25);
+ final long end = now + TimeUnit.HOURS.toMillis(25);
+ for (final FixedFormat format : FixedFormat.values()) {
+ final String pattern = format.getPattern();
+ if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
+ continue; // cannot compile precise timestamp formats with
SimpleDateFormat
+ }
+ final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
+ final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
+ for (long time = start; time < end; time += 12345) {
+ final String actual = customTF.format(time);
+ final String expected = simpleDF.format(new Date(time));
+ assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
+ }
+ }
+ }
+
+ @Test
+ public void testFormatLong_goingBackInTime() {
+ final long now = System.currentTimeMillis();
+ final long start = now - TimeUnit.HOURS.toMillis(25);
+ final long end = now + TimeUnit.HOURS.toMillis(25);
+ for (final FixedFormat format : FixedFormat.values()) {
+ final String pattern = format.getPattern();
+ if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
+ continue; // cannot compile precise timestamp formats with
SimpleDateFormat
+ }
+ final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
+ final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
+ for (long time = end; time > start; time -= 12345) {
+ final String actual = customTF.format(time);
+ final String expected = simpleDF.format(new Date(time));
+ assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
+ }
+ }
+ }
+
/**
* This test case validates date pattern before and after DST
* Base Date : 12 Mar 2017
@@ -402,4 +350,56 @@ public class FixedDateFormatTest {
}
}
}
+
+ @Test
+ public void testFormatLongCharArrayInt() {
+ final long now = System.currentTimeMillis();
+ final long start = now - TimeUnit.HOURS.toMillis(25);
+ final long end = now + TimeUnit.HOURS.toMillis(25);
+ final char[] buffer = new char[128];
+ for (final FixedFormat format : FixedFormat.values()) {
+ final String pattern = format.getPattern();
+ if (containsNanos(format) || format.getFixedTimeZoneFormat() !=
null) {
+ // cannot compile precise timestamp formats with
SimpleDateFormat
+ // This format() API not include the TZ
+ continue;
+ }
+ final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
+ final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
+ for (long time = start; time < end; time += 12345) {
+ final int length = customTF.format(time, buffer, 23);
+ final String actual = new String(buffer, 23, length);
+ final String expected = simpleDF.format(new Date(time));
+ assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
+ }
+ }
+ }
+
+ @Test
+ public void testFormatLongCharArrayInt_goingBackInTime() {
+ final long now = System.currentTimeMillis();
+ final long start = now - TimeUnit.HOURS.toMillis(25);
+ final long end = now + TimeUnit.HOURS.toMillis(25);
+ final char[] buffer = new char[128];
+ for (final FixedFormat format : FixedFormat.values()) {
+ final String pattern = format.getPattern();
+ if (pattern.endsWith("n") || pattern.matches(".+n+X*") ||
pattern.matches(".+n+Z*") || format.getFixedTimeZoneFormat() != null) {
+ continue; // cannot compile precise timestamp formats with
SimpleDateFormat
+ }
+ final SimpleDateFormat simpleDF = new SimpleDateFormat(pattern,
Locale.getDefault());
+ final FixedDateFormat customTF = new FixedDateFormat(format,
TimeZone.getDefault());
+ for (long time = end; time > start; time -= 12345) {
+ final int length = customTF.format(time, buffer, 23);
+ final String actual = new String(buffer, 23, length);
+ final String expected = simpleDF.format(new Date(time));
+ assertEquals(format + "(" + pattern + ")" + "/" + time,
expected, actual);
+ }
+ }
+ }
+
+ @Test
+ public void testGetFormatReturnsConstructorFixedFormatPattern() {
+ final FixedDateFormat format = new
FixedDateFormat(FixedDateFormat.FixedFormat.ABSOLUTE, TimeZone.getDefault());
+ assertSame(FixedDateFormat.FixedFormat.ABSOLUTE.getPattern(),
format.getFormat());
+ }
}