You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by th...@apache.org on 2019/11/02 19:56:31 UTC

[commons-dbutils] 01/04: Simplify logic to only convert to Timestamp if not already a timestamp.

This is an automated email from the ASF dual-hosted git repository.

thecarlhall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbutils.git

commit 324fef13bd7e1589c28df26ddd81080b76ee7a4c
Author: Carl Hall <th...@apache.org>
AuthorDate: Sun Oct 27 21:11:48 2019 -0700

    Simplify logic to only convert to Timestamp if not already a timestamp.
---
 .../handlers/properties/DatePropertyHandler.java   | 20 +++++++------
 .../properties/DatePropertyHandlerTest.java        | 34 +++++++++++++---------
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandler.java b/src/main/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandler.java
index e2f8d8b..24f8971 100644
--- a/src/main/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandler.java
+++ b/src/main/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandler.java
@@ -16,10 +16,11 @@
  */
 package org.apache.commons.dbutils.handlers.properties;
 
-import java.sql.Timestamp;
-
 import org.apache.commons.dbutils.PropertyHandler;
 
+import java.sql.Timestamp;
+import java.util.Date;
+
 public class DatePropertyHandler implements PropertyHandler {
     @Override
     public boolean match(final Class<?> parameter, final Object value) {
@@ -31,7 +32,8 @@ public class DatePropertyHandler implements PropertyHandler {
             if ("java.sql.Time".equals(targetType)) {
                 return true;
             } else
-            if ("java.sql.Timestamp".equals(targetType)) {
+            if ("java.sql.Timestamp".equals(targetType)
+                    && !Timestamp.class.isInstance(value)) {
                 return true;
             }
         }
@@ -42,17 +44,17 @@ public class DatePropertyHandler implements PropertyHandler {
     @Override
     public Object apply(final Class<?> parameter, Object value) {
         final String targetType = parameter.getName();
+        final Date dateValue = (java.util.Date) value;
+        final long time = dateValue.getTime();
+
         if ("java.sql.Date".equals(targetType)) {
-            value = new java.sql.Date(((java.util.Date) value).getTime());
+            value = new java.sql.Date(time);
         } else
         if ("java.sql.Time".equals(targetType)) {
-            value = new java.sql.Time(((java.util.Date) value).getTime());
+            value = new java.sql.Time(time);
         } else
         if ("java.sql.Timestamp".equals(targetType)) {
-            final Timestamp tsValue = (Timestamp) value;
-            final int nanos = tsValue.getNanos();
-            value = new Timestamp(tsValue.getTime());
-            ((Timestamp) value).setNanos(nanos);
+            value = new Timestamp(time);
         }
 
         return value;
diff --git a/src/test/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandlerTest.java
index 81380a0..b7ed03b 100644
--- a/src/test/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandlerTest.java
+++ b/src/test/java/org/apache/commons/dbutils/handlers/properties/DatePropertyHandlerTest.java
@@ -16,50 +16,58 @@
  */
 package org.apache.commons.dbutils.handlers.properties;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.junit.Before;
+import org.junit.Test;
 
 import java.sql.Time;
 import java.sql.Timestamp;
+import java.util.Date;
 
-import org.junit.Before;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 public class DatePropertyHandlerTest {
     private DatePropertyHandler handler;
+    private Date testValue;
 
     @Before
     public void setUp() {
         this.handler = new DatePropertyHandler();
+        this.testValue = new Date();
     }
 
     @Test
     public void testMatch() {
-        assertTrue(handler.match(java.sql.Date.class, new java.util.Date()));
-        assertTrue(handler.match(java.sql.Time.class, new java.util.Date()));
-        assertTrue(handler.match(java.sql.Timestamp.class, new java.util.Date()));
+        assertTrue(handler.match(java.sql.Date.class, testValue));
+        assertTrue(handler.match(java.sql.Time.class, testValue));
+        assertTrue(handler.match(java.sql.Timestamp.class, testValue));
+    }
+
+    @Test
+    public void testNotMatch() {
+        final Timestamp ts = new Timestamp(testValue.getTime());
+        assertFalse(handler.match(java.sql.Timestamp.class, ts));
     }
 
     @Test
     public void testMatchNegative() {
         assertFalse(handler.match(Float.class, null));
-        assertFalse(handler.match(Float.class, new java.util.Date()));
+        assertFalse(handler.match(Float.class, testValue));
     }
 
     @Test
     public void testApplyTypeOfDate() throws Exception {
-        assertEquals(java.sql.Date.class, handler.apply(java.sql.Date.class, new java.util.Date()).getClass());
+        assertEquals(java.sql.Date.class, handler.apply(java.sql.Date.class, testValue).getClass());
     }
 
     @Test
     public void testApplyTypeOfTime() throws Exception {
-        assertEquals(Time.class, handler.apply(java.sql.Time.class, new java.util.Date()).getClass());
+        assertEquals(Time.class, handler.apply(java.sql.Time.class, testValue).getClass());
     }
 
     @Test
     public void testApplyTypeOfTimestamp() throws Exception {
-        final Timestamp ts = new Timestamp(new java.util.Date().getTime());
-        assertEquals(Timestamp.class, handler.apply(java.sql.Timestamp.class, ts).getClass());
+        assertEquals(Timestamp.class, handler.apply(java.sql.Timestamp.class, testValue).getClass());
     }
 }