You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListDataSourceTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.vaadin.server.data.datasource;
  2. import static org.junit.Assert.assertTrue;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import org.junit.Assert;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import com.vaadin.server.data.DataSource;
  10. import com.vaadin.server.data.ListDataSource;
  11. import com.vaadin.server.data.Query;
  12. public class ListDataSourceTest {
  13. private ListDataSource<StrBean> dataSource;
  14. private List<StrBean> data;
  15. @Before
  16. public void setUp() {
  17. data = StrBean.generateRandomBeans(100);
  18. dataSource = DataSource.create(data);
  19. }
  20. @Test
  21. public void testListContainsAllData() {
  22. dataSource.apply(new Query())
  23. .forEach(str -> assertTrue(
  24. "Data source contained values not in original data",
  25. data.remove(str)));
  26. assertTrue("Not all values from original data were in data source",
  27. data.isEmpty());
  28. }
  29. @Test
  30. public void testSortByComparatorListsDiffer() {
  31. Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
  32. .thenComparing(StrBean::getRandomNumber)
  33. .thenComparing(StrBean::getId);
  34. List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
  35. .collect(Collectors.toList());
  36. // First value in data is { Xyz, 10, 100 } which should be last in list
  37. Assert.assertNotEquals("First value should not match", data.get(0),
  38. list.get(0));
  39. Assert.assertEquals("Sorted data and original data sizes don't match",
  40. data.size(), list.size());
  41. data.sort(comp);
  42. for (int i = 0; i < data.size(); ++i) {
  43. Assert.assertEquals("Sorting result differed", data.get(i),
  44. list.get(i));
  45. }
  46. }
  47. @Test
  48. public void testDefatulSortWithSpecifiedPostSort() {
  49. Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
  50. .thenComparing(Comparator.comparing(StrBean::getId).reversed());
  51. List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
  52. // The sort here should come e.g from a Component
  53. .sorted(Comparator.comparing(StrBean::getRandomNumber))
  54. .collect(Collectors.toList());
  55. Assert.assertEquals("Sorted data and original data sizes don't match",
  56. data.size(), list.size());
  57. for (int i = 1; i < list.size(); ++i) {
  58. StrBean prev = list.get(i - 1);
  59. StrBean cur = list.get(i);
  60. // Test specific sort
  61. Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber());
  62. if (prev.getRandomNumber() == cur.getRandomNumber()) {
  63. // Test default sort
  64. Assert.assertTrue(
  65. prev.getValue().compareTo(cur.getValue()) <= 0);
  66. if (prev.getValue().equals(cur.getValue())) {
  67. Assert.assertTrue(prev.getId() > cur.getId());
  68. }
  69. }
  70. }
  71. }
  72. @Test
  73. public void testDefatulSortWithFunction() {
  74. List<StrBean> list = dataSource.sortingBy(StrBean::getValue)
  75. .apply(new Query()).collect(Collectors.toList());
  76. Assert.assertEquals("Sorted data and original data sizes don't match",
  77. data.size(), list.size());
  78. for (int i = 1; i < list.size(); ++i) {
  79. StrBean prev = list.get(i - 1);
  80. StrBean cur = list.get(i);
  81. // Test default sort
  82. Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0);
  83. }
  84. }
  85. }