diff options
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 6 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/components/grid/GridNullValueSortTest.java | 45 |
2 files changed, 50 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index c3456075c3..c464acb8fc 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -887,7 +887,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, if (hasCommonComparableBaseType(a, b)) { return compareComparables(a, b); } else { - return compareComparables(a.toString(), b.toString()); + return compareComparables(Objects.toString(a, ""), Objects.toString(b, "")); } } @@ -906,6 +906,10 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, return true; } } + if ((a == null && b instanceof Comparable<?>) + || (b == null && a instanceof Comparable<?>)) { + return true; + } return false; } diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridNullValueSortTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridNullValueSortTest.java new file mode 100644 index 0000000000..ddf4844490 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridNullValueSortTest.java @@ -0,0 +1,45 @@ +package com.vaadin.tests.components.grid; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.VaadinSession; +import com.vaadin.tests.util.MockUI; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.Column; + +public class GridNullValueSortTest { + + private Grid<TestClass> grid; + private Column<TestClass, String> stringColumn; + private Column<TestClass, Object> nonComparableColumn; + + @Test + public void sortWithNullValues() { + this.grid.sort(this.stringColumn); + this.grid.sort(this.nonComparableColumn); + this.grid.getDataCommunicator().beforeClientResponse(true); + } + + @Before + public void setup() { + VaadinSession.setCurrent(null); + this.grid = new Grid<TestClass>(); + this.stringColumn = this.grid.addColumn(bean -> bean.stringField); + this.nonComparableColumn = this.grid.addColumn(bean -> bean.nonComparableField); + + this.grid.setItems(new TestClass(null, new Object()), new TestClass("something", null)); + new MockUI().setContent(grid); + } + + private static class TestClass { + + private final String stringField; + private final Object nonComparableField; + + TestClass(final String stringField, final Object nonComparableField) { + this.stringField = stringField; + this.nonComparableField = nonComparableField; + } + } +} |