]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed the compare filter to handle subclasses (#17169)
authorPontus Boström <pontus@vaadin.com>
Tue, 26 Jul 2016 09:26:54 +0000 (12:26 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 1 Aug 2016 07:53:34 +0000 (07:53 +0000)
Earlier the comparision worked only if the item value was a subclass of
the value in comparator, but not in the other way around. Now the
compairision works if the one is a subclass of the other and both
implements Comparable.

Change-Id: I1bcbba94f1263915f838e948ed9d8b68a14aefd0

server/src/main/java/com/vaadin/data/util/filter/Compare.java
server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java [new file with mode: 0644]

index 1fcbe855800f21c883100a57cc9634ce40b7930c..60523f2d42de84711a9836f3dac036d4c77a1eee 100644 (file)
@@ -292,9 +292,15 @@ public abstract class Compare implements Filter {
             return null == value1 ? 0 : -1;
         } else if (null == value1) {
             return 1;
-        } else if (getValue() instanceof Comparable
-                && value1.getClass().isAssignableFrom(getValue().getClass())) {
-            return -((Comparable) getValue()).compareTo(value1);
+        } else if (getValue() instanceof Comparable) {
+            if (value1.getClass().isInstance(getValue())) {
+                return -((Comparable) getValue()).compareTo(value1);
+            } else if (getValue().getClass().isInstance(value1)) {
+                // isInstance returns true if value1 is a sub-type of
+                // getValue(), i.e. value1 must here be Comparable
+                return ((Comparable) value1).compareTo(getValue());
+            }
+
         }
         throw new IllegalArgumentException("Could not compare the arguments: "
                 + value1 + ", " + getValue());
diff --git a/server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java b/server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java
new file mode 100644 (file)
index 0000000..5908c7d
--- /dev/null
@@ -0,0 +1,137 @@
+package com.vaadin.data.util.filter;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.data.Container.Filter;
+import com.vaadin.data.Item;
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.util.PropertysetItem;
+import com.vaadin.data.util.filter.Compare.Equal;
+import com.vaadin.data.util.filter.Compare.Greater;
+import com.vaadin.data.util.filter.Compare.GreaterOrEqual;
+import com.vaadin.data.util.filter.Compare.Less;
+import com.vaadin.data.util.filter.Compare.LessOrEqual;
+
+
+public class CompareFilterDateTest extends AbstractFilterTestBase<Compare> {
+
+    protected Item itemNullUtilDate;
+    protected Item itemNullSqlDate;
+    protected Item itemUtilDate;
+    protected Item itemSqlDate;
+
+    protected SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
+
+    protected Filter equalCompUtilDate;
+    protected Filter greaterCompUtilDate;
+    protected Filter lessCompUtilDate;
+    protected Filter greaterEqualCompUtilDate;
+    protected Filter lessEqualCompUtilDate;
+
+    protected Filter equalCompSqlDate;
+    protected Filter greaterCompSqlDate;
+    protected Filter lessCompSqlDate;
+    protected Filter greaterEqualCompSqlDate;
+    protected Filter lessEqualCompSqlDate;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        equalCompUtilDate = new Equal(PROPERTY1, formatter.parse("26072016"));
+        greaterCompUtilDate = new Greater(PROPERTY1,
+                formatter.parse("26072016"));
+        lessCompUtilDate = new Less(PROPERTY1, formatter.parse("26072016"));
+        greaterEqualCompUtilDate = new GreaterOrEqual(PROPERTY1,
+                formatter.parse("26072016"));
+        lessEqualCompUtilDate = new LessOrEqual(PROPERTY1,
+                formatter.parse("26072016"));
+
+        equalCompSqlDate = new Equal(PROPERTY1, new java.sql.Date(formatter
+                .parse("26072016").getTime()));
+        greaterCompSqlDate = new Greater(PROPERTY1, new java.sql.Date(formatter
+                .parse("26072016").getTime()));
+        lessCompSqlDate = new Less(PROPERTY1, new java.sql.Date(formatter
+                .parse("26072016").getTime()));
+        greaterEqualCompSqlDate = new GreaterOrEqual(PROPERTY1,
+                new java.sql.Date(formatter.parse("26072016").getTime()));
+        lessEqualCompSqlDate = new LessOrEqual(PROPERTY1, new java.sql.Date(
+                formatter.parse("26072016").getTime()));
+
+        itemNullUtilDate = new PropertysetItem();
+        itemNullUtilDate.addItemProperty(PROPERTY1, new ObjectProperty<Date>(
+                null, Date.class));
+        itemNullSqlDate = new PropertysetItem();
+        itemNullSqlDate.addItemProperty(PROPERTY1,
+                new ObjectProperty<java.sql.Date>(null, java.sql.Date.class));
+        itemUtilDate = new PropertysetItem();
+        itemUtilDate.addItemProperty(PROPERTY1, new ObjectProperty<Date>(
+                formatter.parse("25072016"), Date.class));
+        itemSqlDate = new PropertysetItem();
+        itemSqlDate.addItemProperty(PROPERTY1,
+                new ObjectProperty<java.sql.Date>(new java.sql.Date(formatter
+                        .parse("25072016").getTime()), java.sql.Date.class));
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        itemNullUtilDate = null;
+        itemNullSqlDate = null;
+        itemUtilDate = null;
+        itemSqlDate = null;
+    }
+
+    @Test
+    public void testCompareUtilDatesAndUtilDates() {
+        Assert.assertFalse(equalCompUtilDate.passesFilter(null,
+                itemNullUtilDate));
+        Assert.assertFalse(equalCompUtilDate.passesFilter(null, itemUtilDate));
+        Assert.assertFalse(greaterCompUtilDate.passesFilter(null, itemUtilDate));
+        Assert.assertTrue(lessCompUtilDate.passesFilter(null, itemUtilDate));
+        Assert.assertFalse(greaterEqualCompUtilDate.passesFilter(null,
+                itemUtilDate));
+        Assert.assertTrue(lessEqualCompUtilDate
+                .passesFilter(null, itemUtilDate));
+    }
+
+    @Test
+    public void testCompareUtilDatesAndSqlDates() {
+        Assert.assertFalse(equalCompUtilDate
+                .passesFilter(null, itemNullSqlDate));
+        Assert.assertFalse(equalCompUtilDate.passesFilter(null, itemSqlDate));
+        Assert.assertFalse(greaterCompUtilDate.passesFilter(null, itemSqlDate));
+        Assert.assertTrue(lessCompUtilDate.passesFilter(null, itemSqlDate));
+        Assert.assertFalse(greaterEqualCompUtilDate.passesFilter(null,
+                itemSqlDate));
+        Assert.assertTrue(lessEqualCompUtilDate.passesFilter(null, itemSqlDate));
+    }
+
+    @Test
+    public void testCompareSqlDatesAndSqlDates() {
+        Assert.assertFalse(equalCompSqlDate.passesFilter(null, itemNullSqlDate));
+        Assert.assertFalse(equalCompSqlDate.passesFilter(null, itemSqlDate));
+        Assert.assertFalse(greaterCompSqlDate.passesFilter(null, itemSqlDate));
+        Assert.assertTrue(lessCompSqlDate.passesFilter(null, itemSqlDate));
+        Assert.assertFalse(greaterEqualCompSqlDate.passesFilter(null,
+                itemSqlDate));
+        Assert.assertTrue(lessEqualCompSqlDate.passesFilter(null, itemSqlDate));
+    }
+
+    @Test
+    public void testCompareSqlDatesAndUtilDates() {
+        Assert.assertFalse(equalCompSqlDate
+                .passesFilter(null, itemNullUtilDate));
+        Assert.assertFalse(equalCompSqlDate.passesFilter(null, itemUtilDate));
+        Assert.assertFalse(greaterCompSqlDate.passesFilter(null, itemUtilDate));
+        Assert.assertTrue(lessCompSqlDate.passesFilter(null, itemUtilDate));
+        Assert.assertFalse(greaterEqualCompSqlDate.passesFilter(null,
+                itemUtilDate));
+        Assert.assertTrue(lessEqualCompSqlDate.passesFilter(null, itemUtilDate));
+    }
+
+}