From 91a80e6138920c2731caf16566948f68b1fcf16f Mon Sep 17 00:00:00 2001 From: Konstantin Salikhov Date: Wed, 14 Sep 2016 13:43:36 +0400 Subject: [PATCH] Fix date coercion for non-long numeric types --- .../yahoo/elide/utils/coerce/CoerceUtil.java | 2 +- .../converters/EpochToDateConverter.java | 16 +++++----- .../elide/utils/coerce/CoerceUtilTest.java | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java index 9d7bb2e8e8..b2efe1274d 100644 --- a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java +++ b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java @@ -74,7 +74,7 @@ public Converter lookup(Class sourceType, Class targetType) { return TO_ENUM_CONVERTER; } else if (Map.class.isAssignableFrom(sourceType)) { return FROM_MAP_CONVERTER; - } else if ((String.class.isAssignableFrom(sourceType) || Long.class.isAssignableFrom(sourceType)) + } else if ((String.class.isAssignableFrom(sourceType) || Number.class.isAssignableFrom(sourceType)) && ClassUtils.isAssignable(targetType, Date.class)) { return EPOCH_TO_DATE_CONVERTER; } else { diff --git a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/EpochToDateConverter.java b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/EpochToDateConverter.java index 2aa777d9c8..188c864cb3 100644 --- a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/EpochToDateConverter.java +++ b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/EpochToDateConverter.java @@ -24,8 +24,8 @@ public T convert(Class cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToDate(cls, (String) value); - } else if (ClassUtils.isAssignable(value.getClass(), Long.class, true)) { - return longToDate(cls, (Long) value); + } else if (ClassUtils.isAssignable(value.getClass(), Number.class, true)) { + return numberToDate(cls, (Number) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " is not a valid epoch"); } @@ -35,21 +35,21 @@ public T convert(Class cls, Object value) { } } - private static T longToDate(Class cls, Long epoch) throws ReflectiveOperationException { + private static T numberToDate(Class cls, Number epoch) throws ReflectiveOperationException { if (ClassUtils.isAssignable(cls, java.sql.Date.class)) { - return (T) new java.sql.Date(epoch); + return (T) new java.sql.Date(epoch.longValue()); } else if (ClassUtils.isAssignable(cls, Timestamp.class)) { - return (T) new Timestamp(epoch); + return (T) new Timestamp(epoch.longValue()); } else if (ClassUtils.isAssignable(cls, Time.class)) { - return (T) new Time(epoch); + return (T) new Time(epoch.longValue()); } else if (ClassUtils.isAssignable(cls, Date.class)) { - return (T) new Date(epoch); + return (T) new Date(epoch.longValue()); } else { throw new UnsupportedOperationException("Cannot convert to " + cls.getSimpleName()); } } private static T stringToDate(Class cls, String epoch) throws ReflectiveOperationException { - return longToDate(cls, Long.parseLong(epoch)); + return numberToDate(cls, Long.parseLong(epoch)); } } diff --git a/elide-core/src/test/java/com/yahoo/elide/utils/coerce/CoerceUtilTest.java b/elide-core/src/test/java/com/yahoo/elide/utils/coerce/CoerceUtilTest.java index 110e302ac0..09ff25cf62 100644 --- a/elide-core/src/test/java/com/yahoo/elide/utils/coerce/CoerceUtilTest.java +++ b/elide-core/src/test/java/com/yahoo/elide/utils/coerce/CoerceUtilTest.java @@ -123,4 +123,34 @@ public void testStringToDate() throws Exception { Time time = CoerceUtil.coerce("1", Time.class); assertEquals(time, new Time(1)); } + + @Test + public void testLongToDate() { + Date dateLong = CoerceUtil.coerce(0L, Date.class); + assertEquals(dateLong, new Date(0)); + + java.sql.Date date1Long = CoerceUtil.coerce(0L, java.sql.Date.class); + assertEquals(date1Long, new java.sql.Date(0)); + + Timestamp timestampLong = CoerceUtil.coerce(0L, Timestamp.class); + assertEquals(timestampLong, new Timestamp(0)); + + Time timeLong = CoerceUtil.coerce(0L, Time.class); + assertEquals(timeLong, new Time(0)); + } + + @Test + public void testIntToDate() throws Exception { + Date date = CoerceUtil.coerce(0, Date.class); + assertEquals(date, new Date(0)); + + java.sql.Date date1 = CoerceUtil.coerce(0, java.sql.Date.class); + assertEquals(date1, new java.sql.Date(0)); + + Timestamp timestamp = CoerceUtil.coerce(0, Timestamp.class); + assertEquals(timestamp, new Timestamp(0)); + + Time time = CoerceUtil.coerce(0, Time.class); + assertEquals(time, new Time(0)); + } }