FIXED - task 586: Only one parse date format is attempted from list in toDate(format1,format2)

http://code.google.com/p/google-refine/issues/detail?id=586

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2520 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Tom Morris 2012-08-03 18:01:01 +00:00
parent 4319314675
commit b2ae74d23f
2 changed files with 16 additions and 11 deletions

View File

@ -106,12 +106,17 @@ public class ToDate implements Function {
continue; continue;
} }
String format = (String) args[i]; String format = (String) args[i];
SimpleDateFormat formatter = new SimpleDateFormat(format); SimpleDateFormat formatter;
try {
formatter = new SimpleDateFormat(format);
} catch (IllegalArgumentException e) {
return new EvalError("Unknown date format");
}
Date date = null; Date date = null;
try { try {
date = formatter.parse(o1); date = formatter.parse(o1);
} catch (java.text.ParseException e) { } catch (java.text.ParseException e) {
return new EvalError("Cannot parse to date"); continue;
} }
if (date != null) { if (date != null) {
GregorianCalendar c = new GregorianCalendar(); GregorianCalendar c = new GregorianCalendar();
@ -119,6 +124,7 @@ public class ToDate implements Function {
return c; return c;
} }
} }
return new EvalError("Unable to parse as date");
} }
return null; return null;

View File

@ -44,6 +44,8 @@ import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.util.CalendarParser;
import com.google.refine.expr.util.CalendarParserException;
import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
@ -92,13 +94,6 @@ public class ToFromConversionTests extends RefineTest {
} }
} }
@Test
public void testFromError() {
Assert.assertTrue(invoke("fromError") instanceof EvalError);
Assert.assertTrue(invoke("fromError", (Object)null) instanceof EvalError);
Assert.assertTrue(invoke("fromError", "string") instanceof EvalError);
Assert.assertEquals(invoke("fromError", new EvalError("message")),"message");
}
@Test @Test
public void testToNumber() { public void testToNumber() {
@ -127,13 +122,17 @@ public class ToFromConversionTests extends RefineTest {
} }
@Test @Test
public void testToDate() { public void testToDate() throws CalendarParserException {
// Assert.assertTrue(invoke("toDate") instanceof EvalError); // Assert.assertTrue(invoke("toDate") instanceof EvalError);
Assert.assertNull(invoke("toDate")); Assert.assertNull(invoke("toDate"));
Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError); Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError);
Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError); Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError);
Assert.assertTrue(invoke("toDate", "2012-03-01","xxx") instanceof EvalError); // bad format string
Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar); Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar);
// Assert.assertEquals(invoke("toDate", "2012-03-01"),new GregorianCalendar(2012,3,1)); Assert.assertEquals(invoke("toDate", "2012-03-01"),CalendarParser.parse("2012-03-01"));
Assert.assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalendarParser.parse("2012-03-01"));
// Multiple format strings should get tried sequentially until one succeeds or all are exhausted
Assert.assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalendarParser.parse("2012-03-01"));
// Date // Date
// Calendar // Calendar
// String // String