]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix issues when using java.sql.Date as DateField range (#15342)
authorTeemu Pöntelin <teemu@vaadin.com>
Mon, 8 Dec 2014 21:21:08 +0000 (23:21 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 9 Dec 2014 08:59:06 +0000 (08:59 +0000)
Change-Id: I656cc0600f929239605e17ab9cf71dc1ba96582f

server/src/com/vaadin/ui/DateField.java
uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDate.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDateTest.java [new file with mode: 0644]

index 030bd5f6c29b36c194ef682d2c7bb054129d9ed3..d5700c4b65dc2d02e0bf1343f6a771093e1ab791 100644 (file)
@@ -316,10 +316,10 @@ public class DateField extends AbstractField<Date> implements
             throw new IllegalStateException(
                     "startDate cannot be later than endDate");
         }
-        getState().rangeStart = startDate;
-        // rangeStart = startDate;
-        // This has to be done to correct for the resolution
-        // updateRangeState();
+
+        // Create a defensive copy against issues when using java.sql.Date (and
+        // also against mutable Date).
+        getState().rangeStart = new Date(startDate.getTime());
         updateRangeValidator();
     }
 
@@ -436,8 +436,10 @@ public class DateField extends AbstractField<Date> implements
             throw new IllegalStateException(
                     "endDate cannot be earlier than startDate");
         }
-        // rangeEnd = endDate;
-        getState().rangeEnd = endDate;
+
+        // Create a defensive copy against issues when using java.sql.Date (and
+        // also against mutable Date).
+        getState().rangeEnd = new Date(endDate.getTime());
         updateRangeValidator();
     }
 
diff --git a/uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDate.java b/uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDate.java
new file mode 100644 (file)
index 0000000..74d3e85
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.datefield;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.DateField;
+import com.vaadin.ui.InlineDateField;
+
+public class DateRangeWithSqlDate extends AbstractTestUI {
+
+    // 2014-12-01
+    private static final java.sql.Date startDate = new java.sql.Date(
+            1417467822699L);
+
+    // 2014-12-02
+    private static final java.sql.Date endDate = new java.sql.Date(
+            1417554763317L);
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        DateField df = new InlineDateField();
+        df.setRangeStart(startDate);
+        df.setRangeEnd(endDate);
+
+        df.setValue(startDate);
+
+        addComponent(df);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Test that java.sql.Date can be given to specify date range start and end dates.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 15342;
+    }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDateTest.java b/uitest/src/com/vaadin/tests/components/datefield/DateRangeWithSqlDateTest.java
new file mode 100644 (file)
index 0000000..2352011
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.datefield;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class DateRangeWithSqlDateTest extends MultiBrowserTest {
+
+    @Test
+    public void testDateRange() {
+        openTestURL();
+
+        // Get all cells of the inline datefield.
+        List<WebElement> cells = driver.findElements(By
+                .className("v-inline-datefield-calendarpanel-day"));
+
+        // Verify the range is rendered correctly.
+        assertCell(cells.get(0), "30", true);
+        assertCell(cells.get(1), "1", false);
+        assertCell(cells.get(2), "2", false);
+        assertCell(cells.get(3), "3", true);
+    }
+
+    private void assertCell(WebElement cell, String text, boolean outsideRange) {
+        assertEquals(text, cell.getText());
+        assertEquals(outsideRange,
+                cell.getAttribute("class").contains("outside-range"));
+    }
+
+}