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

[Dubbo-2499-2.6.x]fix PojoUtil realize type convert not support subclasses of 'java.util.date' #2499 #2502

Merged
merged 1 commit into from
Sep 14, 2018
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 @@ -79,9 +79,18 @@ public static Object compatibleTypeConvert(Object value, Class<?> type) {
return new Byte(string);
} else if (type == Boolean.class || type == boolean.class) {
return new Boolean(string);
} else if (type == Date.class) {
} else if (type == Date.class || type == java.sql.Date.class || type == java.sql.Timestamp.class || type == java.sql.Time.class) {
try {
return new SimpleDateFormat(DATE_FORMAT).parse((String) value);
Date date = new SimpleDateFormat(DATE_FORMAT).parse((String) value);
if (type == java.sql.Date.class) {
return new java.sql.Date(date.getTime());
} else if (type == java.sql.Timestamp.class) {
return new java.sql.Timestamp(date.getTime());
} else if (type == java.sql.Time.class) {
return new java.sql.Time(date.getTime());
} else {
return date;
}
} catch (ParseException e) {
throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public void testCompatibleTypeConvert() throws Exception {
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", Date.class);
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-11 12:24:12"), (Date) result);

result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Date.class);
assertEquals(new SimpleDateFormat("yyyy-MM-dd").format((java.sql.Date) result), "2011-12-11");

result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Time.class);
assertEquals(new SimpleDateFormat("HH:mm:ss").format((java.sql.Time) result), "12:24:12");

result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Timestamp.class);
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.sql.Timestamp) result), "2011-12-11 12:24:12");

result = CompatibleTypeUtils.compatibleTypeConvert("ab", char[].class);
assertEquals(2, ((char[]) result).length);
assertEquals('a', ((char[]) result)[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -559,6 +561,34 @@ public void testListPojoListPojo() throws Exception {
Assert.assertEquals(parent.getAge(), realizeParent.getAge());
}

@Test
public void testDateTimeTimestamp() throws Exception {
String dateStr = "2018-09-12";
String timeStr = "10:12:33";
String dateTimeStr = "2018-09-12 10:12:33";
String[] dateFormat = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"};

//java.util.Date
Object date = PojoUtils.realize(dateTimeStr, Date.class, (Type) Date.class);
assertEquals(Date.class, date.getClass());
assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(date));

//java.sql.Time
Object time = PojoUtils.realize(dateTimeStr, java.sql.Time.class, (Type) java.sql.Time.class);
assertEquals(java.sql.Time.class, time.getClass());
assertEquals(timeStr, new SimpleDateFormat(dateFormat[2]).format(time));

//java.sql.Date
Object sqlDate = PojoUtils.realize(dateTimeStr, java.sql.Date.class, (Type) java.sql.Date.class);
assertEquals(java.sql.Date.class, sqlDate.getClass());
assertEquals(dateStr, new SimpleDateFormat(dateFormat[1]).format(sqlDate));

//java.sql.Timestamp
Object timestamp = PojoUtils.realize(dateTimeStr, java.sql.Timestamp.class, (Type) java.sql.Timestamp.class);
assertEquals(java.sql.Timestamp.class, timestamp.getClass());
assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp));
}

public static class Parent {
public String gender;
public String email;
Expand Down