From 329514c40edd8fa4553b41ec8f62bf20c96c6ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A8ne=20Martin?= Date: Tue, 1 Oct 2019 12:53:38 -0700 Subject: [PATCH] Explicitly identify the value that can't be converted to a date (#494) --- .../org/javarosa/xpath/expr/XPathFuncExpr.java | 14 +++++++------- .../org/javarosa/xpath/test/XPathEvalTest.java | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/javarosa/xpath/expr/XPathFuncExpr.java b/src/main/java/org/javarosa/xpath/expr/XPathFuncExpr.java index 1486e6f93..58c310e15 100644 --- a/src/main/java/org/javarosa/xpath/expr/XPathFuncExpr.java +++ b/src/main/java/org/javarosa/xpath/expr/XPathFuncExpr.java @@ -818,7 +818,7 @@ public static Object toDate(Object input, boolean preserveTime) { } if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) { - throw new XPathTypeMismatchException("converting out-of-range value to date"); + throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date."); } long timeMillis = (long) (n * DateUtils.DAY_IN_MS); @@ -833,7 +833,7 @@ public static Object toDate(Object input, boolean preserveTime) { } if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) { - throw new XPathTypeMismatchException("converting out-of-range value to date"); + throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date."); } return DateUtils.dateAdd(DateUtils.getDate(1970, 1, 1), n.intValue()); @@ -847,7 +847,7 @@ public static Object toDate(Object input, boolean preserveTime) { Date d = DateUtils.parseDateTime(s); if (d == null) { - throw new XPathTypeMismatchException("converting to date"); + throw new XPathTypeMismatchException("The value \"" + s + "\" can't be converted to a date."); } else { return d; } @@ -858,7 +858,7 @@ public static Object toDate(Object input, boolean preserveTime) { return DateUtils.roundDate((Date) input); } } else { - throw new XPathTypeMismatchException("converting to date"); + throw new XPathTypeMismatchException("The value \"" + input.toString() + "\" can't be converted to a date."); } } @@ -873,7 +873,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) { } if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) { - throw new XPathTypeMismatchException("converting out-of-range value to date"); + throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date."); } if (keepDate) { @@ -890,7 +890,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) { Date d = DateUtils.parseDateTime(s); if (d == null) { - throw new XPathTypeMismatchException("converting to date"); + throw new XPathTypeMismatchException("The value \"" + s + "\" can't be converted to a date."); } else { if (keepDate) { long milli = d.getTime(); @@ -910,7 +910,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) { return DateUtils.decimalTimeOfLocalDay(d); } } else { - throw new XPathTypeMismatchException("converting to date"); + throw new XPathTypeMismatchException("The value \"" + o.toString() + "\" can't be converted to a date."); } } diff --git a/src/test/java/org/javarosa/xpath/test/XPathEvalTest.java b/src/test/java/org/javarosa/xpath/test/XPathEvalTest.java index 1bc21e050..f6e263d48 100644 --- a/src/test/java/org/javarosa/xpath/test/XPathEvalTest.java +++ b/src/test/java/org/javarosa/xpath/test/XPathEvalTest.java @@ -318,6 +318,8 @@ should be tested (particularly DST changes), but it's just too hard and testEval("date-time('2000-01-01T10:20:30.000')", DateUtils.getDateTimeFromString("2000-01-01T10:20:30.000")); testEval("decimal-date-time('2000-01-01T10:20:30.000')", 10957.430902777778); testEval("decimal-time('2000-01-01T10:20:30.000+03:00')", .30590277777810115); + testEval("decimal-date-time('-1000')", new XPathTypeMismatchException()); + testEval("decimal-date-time('-01-2019')", new XPathTypeMismatchException()); } @Test