diff options
author | Filippo <f92dev@gmail.com> | 2017-02-23 08:46:39 +0100 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-02-23 09:46:39 +0200 |
commit | 2c58ba7c5a4b3f573de3140f7b516b3329d54359 (patch) | |
tree | 99ecd16d3209044da9aafa6af0ccf395205df1d8 | |
parent | 6783bca88d4cf0c8944e84a0fef0a219c0b9a4d0 (diff) | |
download | vaadin-framework-2c58ba7c5a4b3f573de3140f7b516b3329d54359.tar.gz vaadin-framework-2c58ba7c5a4b3f573de3140f7b516b3329d54359.zip |
Fix NPE sorting columns with null values (#8610)
* Fix NPE sorting columns with null values
Sorting a column in Grid throws a NullPointerException if there are null values and we are using the default TextRenderer
* Test for #8610
* Update Grid.java
* Update GridNullValueSortTest.java
* Update GridNullValueSortTest.java
-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; + } + } +} |