]> source.dussan.org Git - vaadin-framework.git/commitdiff
NullPointerException in DateToSqlDateConverter (#12284)
authorEdoardo Vacchi <uncommonnonsense@gmail.com>
Fri, 13 Sep 2013 08:25:29 +0000 (10:25 +0200)
committerVaadin Code Review <review@vaadin.com>
Fri, 13 Sep 2013 13:00:11 +0000 (13:00 +0000)
DateToSqlDateConverter throws a NullPointerException when the provided
value is null, thus violating the interface contract. If the provided
value is null, then the methods should return null.

Missing test case included

Change-Id: If08225c2a6ae7c3103e47d3817a5d45469c7bf4f

server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java
server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java [new file with mode: 0644]

index cddf2d8a421a7a95c15e2adff37f66c495772c6f..7c252d78f24bdc672370a4b59f8fe04d21db1199 100644 (file)
@@ -44,6 +44,10 @@ public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {
                     + targetType.getName() + ")");
         }
 
+        if (value == null) {
+            return null;
+        }
+
         return new java.sql.Date(value.getTime());
     }
 
@@ -56,6 +60,11 @@ public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {
                     + getPresentationType().getName() + " (targetType was "
                     + targetType.getName() + ")");
         }
+
+        if (value == null) {
+            return null;
+        }
+
         return new Date(value.getTime());
     }
 
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java
new file mode 100644 (file)
index 0000000..685404d
--- /dev/null
@@ -0,0 +1,25 @@
+package com.vaadin.tests.data.converter;
+
+import java.util.Date;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+import com.vaadin.data.util.converter.DateToSqlDateConverter;
+
+public class TestDateToSqlDateConverter extends TestCase {
+
+    DateToSqlDateConverter converter = new DateToSqlDateConverter();
+
+    public void testNullConversion() {
+        assertEquals(null,
+                converter.convertToModel(null, java.sql.Date.class, null));
+    }
+
+    public void testValueConversion() {
+        Date testDate = new Date(100, 0, 1);
+        long time = testDate.getTime();
+        assertEquals(testDate, converter.convertToModel(new java.sql.Date(time),
+                java.sql.Date.class, Locale.ENGLISH));
+    }
+}