LANG-831 FastDateParser does not handle white-space properly

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390779 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-27 00:09:15 +00:00
parent 192b1e1b6b
commit ad72b9f2bf
3 changed files with 47 additions and 9 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.2" date="TBA" description="Next release"> <release version="3.2" date="TBA" description="Next release">
<action issue="LANG-831" type="fix">FastDateParser does not handle white-space properly</action>
<action issue="LANG-828" type="fix">FastDateParser does not handle non-Gregorian calendars properly</action> <action issue="LANG-828" type="fix">FastDateParser does not handle non-Gregorian calendars properly</action>
<action issue="LANG-826" type="fix">FastDateParser does not handle non-ASCII digits correctly</action> <action issue="LANG-826" type="fix">FastDateParser does not handle non-ASCII digits correctly</action>
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action> <action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action>

View File

@ -301,17 +301,8 @@ public class FastDateParser implements DateParser, Serializable {
* @return The <code>StringBuilder</code> * @return The <code>StringBuilder</code>
*/ */
private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) { private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) {
boolean wasWhite= false;
for(int i= 0; i<value.length(); ++i) { for(int i= 0; i<value.length(); ++i) {
char c= value.charAt(i); char c= value.charAt(i);
if(Character.isWhitespace(c)) {
if(!wasWhite) {
wasWhite= true;
regex.append("\\s*+");
}
continue;
}
wasWhite= false;
switch(c) { switch(c) {
case '\'': case '\'':
if(unquote) { if(unquote) {

View File

@ -333,6 +333,52 @@ public class FastDateParserTest {
assertEquals(cal.getTime(), fdf.parse("'20030210A'B153320989'")); assertEquals(cal.getTime(), fdf.parse("'20030210A'B153320989'"));
} }
@Test
public void testLANG_831() throws Exception {
testSdfAndFdp("M E","3 Tue", true);
}
private void testSdfAndFdp(String format, String date, boolean shouldFail)
throws Exception {
Date dfdp = null;
Date dsdf = null;
Throwable f = null;
Throwable s = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(NEW_YORK);
dsdf = sdf.parse(date);
if (shouldFail) {
Assert.fail("Expected SDF failure, but got " + dsdf + " for ["+format+","+date+"]");
}
} catch (Exception e) {
s = e;
if (!shouldFail) {
throw e;
}
// System.out.println("sdf:"+format+"/"+date+"=>"+e);
}
try {
DateParser fdp = getInstance(format, NEW_YORK, Locale.US);
dfdp = fdp.parse(date);
if (shouldFail) {
Assert.fail("Expected FDF failure, but got " + dfdp + " for ["+format+","+date+"] using "+((FastDateParser)fdp).getParsePattern());
}
} catch (Exception e) {
f = e;
if (!shouldFail) {
throw e;
}
// System.out.println("fdf:"+format+"/"+date+"=>"+e);
}
// SDF and FDF should produce equivalent results
assertTrue("Should both or neither throw Exceptions", (f==null)==(s==null));
assertEquals("Parsed dates should be equal", dsdf, dfdp);
}
@Test @Test
public void testDayOf() throws ParseException { public void testDayOf() throws ParseException {
Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US); Calendar cal= Calendar.getInstance(NEW_YORK, Locale.US);