Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date coercion for non-long numeric types #329

Merged
merged 1 commit into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public <T> T convert(Class<T> 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");
}
Expand All @@ -35,21 +35,21 @@ public <T> T convert(Class<T> cls, Object value) {
}
}

private static <T> T longToDate(Class<T> cls, Long epoch) throws ReflectiveOperationException {
private static <T> T numberToDate(Class<T> 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> T stringToDate(Class<T> cls, String epoch) throws ReflectiveOperationException {
return longToDate(cls, Long.parseLong(epoch));
return numberToDate(cls, Long.parseLong(epoch));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}