diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-02-07 09:48:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-07 09:48:33 +0200 |
commit | 909c7e56d8d57be45210424850e904149546d738 (patch) | |
tree | 2e016c94cfe6baf9bcb552058766f30d65653412 /server/src/test/java | |
parent | e16c0ed3c191dcd9ee3ac3c5abc4a9e3f9e53be6 (diff) | |
download | vaadin-framework-909c7e56d8d57be45210424850e904149546d738.tar.gz vaadin-framework-909c7e56d8d57be45210424850e904149546d738.zip |
Make one-arg addColumn use natural sort for mutually comparable values (#8458)
Fixes #8371
Fixes #8128
Diffstat (limited to 'server/src/test/java')
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java | 45 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/util/ReflectToolsTest.java | 53 |
2 files changed, 96 insertions, 2 deletions
diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java index f216593c2f..57c4107a6b 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java @@ -6,10 +6,13 @@ import static org.junit.Assert.assertNotNull; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Random; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -25,6 +28,7 @@ import com.vaadin.data.ValueProvider; import com.vaadin.data.provider.GridSortOrder; import com.vaadin.data.provider.bov.Person; import com.vaadin.event.selection.SelectionEvent; +import com.vaadin.server.SerializableComparator; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.tests.util.MockUI; @@ -41,8 +45,8 @@ public class GridTest { private Grid<String> grid; private Column<String, String> fooColumn; - private Column<String, Number> lengthColumn; - private Column<String, String> objectColumn; + private Column<String, Integer> lengthColumn; + private Column<String, Object> objectColumn; private Column<String, String> randomColumn; @Before @@ -453,6 +457,43 @@ public class GridTest { grid.setColumnOrder("randomColumnId", "length"); } + @Test + public void defaultSorting_comparableTypes() { + testValueProviderSorting(1, 2, 3); + } + + @Test + public void defaultSorting_strings() { + testValueProviderSorting("a", "b", "c"); + } + + @Test + public void defaultSorting_notComparable() { + assert !Comparable.class.isAssignableFrom(AtomicInteger.class); + + testValueProviderSorting(new AtomicInteger(10), new AtomicInteger(8), + new AtomicInteger(9)); + } + + @Test + public void defaultSorting_differentComparables() { + testValueProviderSorting(10.1, 200, 3000.1, 4000); + } + + private static void testValueProviderSorting(Object... expectedOrder) { + SerializableComparator<Object> comparator = new Grid<>() + .addColumn(ValueProvider.identity()) + .getComparator(SortDirection.ASCENDING); + + Assert.assertNotNull(comparator); + + List<Object> values = new ArrayList<>(Arrays.asList(expectedOrder)); + Collections.shuffle(values, new Random(42)); + + Assert.assertArrayEquals(expectedOrder, + values.stream().sorted(comparator).toArray()); + } + private static <T> JsonObject getRowData(Grid<T> grid, T row) { JsonObject json = Json.createObject(); if (grid.getColumns().isEmpty()) { diff --git a/server/src/test/java/com/vaadin/util/ReflectToolsTest.java b/server/src/test/java/com/vaadin/util/ReflectToolsTest.java new file mode 100644 index 0000000000..353bf0972c --- /dev/null +++ b/server/src/test/java/com/vaadin/util/ReflectToolsTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2016 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.util; + +import java.io.Serializable; + +import org.junit.Assert; +import org.junit.Test; + +public class ReflectToolsTest implements Serializable { + @Test + public void findCommonBaseType_sameType() { + Assert.assertSame(Number.class, + ReflectTools.findCommonBaseType(Number.class, Number.class)); + } + + @Test + public void findCommonBaseType_aExtendsB() { + Assert.assertSame(Number.class, + ReflectTools.findCommonBaseType(Integer.class, Number.class)); + } + + @Test + public void findCommonBaseType_bExtendsA() { + Assert.assertSame(Number.class, + ReflectTools.findCommonBaseType(Number.class, Integer.class)); + } + + @Test + public void findCommonBaseType_commonBase() { + Assert.assertSame(Number.class, + ReflectTools.findCommonBaseType(Double.class, Integer.class)); + } + + @Test + public void findCommonBaseType_noCommonBase() { + Assert.assertSame(Object.class, + ReflectTools.findCommonBaseType(String.class, Number.class)); + } +} |