You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/09/14 01:03:42 UTC

[GitHub] diecui1202 closed pull request #2500: [Dubbo-2499]fix PojoUtil realize type convert not support subclasses of 'java.util.date' #2499

diecui1202 closed pull request #2500: [Dubbo-2499]fix PojoUtil realize type convert not support subclasses of 'java.util.date' #2499
URL: https://github.com/apache/incubator-dubbo/pull/2500
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java
index 14a0a79b26..de96eebda1 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java
@@ -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);
                 }
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java
index fc18ac77e1..ff9a144830 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java
@@ -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]);
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
index 2feda7e3dd..fa93d7b341 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
@@ -28,8 +28,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;
@@ -646,6 +648,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 enum Day {
         SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org