diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-11-29 14:16:23 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-11-29 13:55:10 +0000 |
commit | 7ff852f9a96c1264b5b2d8613be70ad0b5fc2dc4 (patch) | |
tree | ccefef4767bd567574e2246755444239be66752d | |
parent | 8ba529503b6caee2d7755f292fb1f9f985943e78 (diff) | |
download | vaadin-framework-7ff852f9a96c1264b5b2d8613be70ad0b5fc2dc4.tar.gz vaadin-framework-7ff852f9a96c1264b5b2d8613be70ad0b5fc2dc4.zip |
Fix sorted data provider data change event propagation
This patch adds a test set for BackEndDataProvider.
Change-Id: I32dfedb692ba71247d74a682f79afdfaf8751de3
6 files changed, 555 insertions, 226 deletions
diff --git a/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java b/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java index 9c6433bc4d..50e104adc1 100644 --- a/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java +++ b/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java @@ -21,6 +21,7 @@ import java.util.Objects; import java.util.stream.Stream; import com.vaadin.server.SerializableFunction; +import com.vaadin.shared.Registration; /** * A {@link DataProvider} for any back end. @@ -71,16 +72,30 @@ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> { * directions * @return new data provider with modified sorting */ + @SuppressWarnings("serial") public BackEndDataProvider<T, F> sortingBy( List<SortOrder<String>> sortOrders) { - return new BackEndDataProvider<>(query -> { + BackEndDataProvider<T, F> parent = this; + return new BackEndDataProvider<T, F>(query -> { List<SortOrder<String>> queryOrder = new ArrayList<>( query.getSortOrders()); queryOrder.addAll(sortOrders); - return request.apply(new Query<>(query.getLimit(), - query.getOffset(), queryOrder, query.getInMemorySorting(), + return parent.fetch(new Query<>(query.getOffset(), query.getLimit(), + queryOrder, query.getInMemorySorting(), query.getFilter().orElse(null))); - }, sizeCallback); + }, sizeCallback) { + + @Override + public Registration addDataProviderListener( + DataProviderListener listener) { + return parent.addDataProviderListener(listener); + } + + @Override + public void refreshAll() { + parent.refreshAll(); + } + }; } @Override diff --git a/server/src/main/java/com/vaadin/server/data/ListDataProvider.java b/server/src/main/java/com/vaadin/server/data/ListDataProvider.java index 5665ac5f29..b295f3375d 100644 --- a/server/src/main/java/com/vaadin/server/data/ListDataProvider.java +++ b/server/src/main/java/com/vaadin/server/data/ListDataProvider.java @@ -23,6 +23,7 @@ import java.util.function.Function; import java.util.stream.Stream; import com.vaadin.server.SerializablePredicate; +import com.vaadin.shared.Registration; /** * {@link DataProvider} wrapper for {@link Collection}s. This class does not @@ -73,7 +74,7 @@ public class ListDataProvider<T> .filter(t -> query.getFilter().orElse(p -> true).test(t)); Optional<Comparator<T>> comparing = Stream - .of(sortOrder, query.getInMemorySorting()) + .of(query.getInMemorySorting(), sortOrder) .filter(c -> c != null) .reduce((c1, c2) -> c1.thenComparing(c2)); @@ -94,8 +95,22 @@ public class ListDataProvider<T> * a {@link Comparator} providing the needed sorting order * @return new data provider with modified sorting */ + @SuppressWarnings("serial") public ListDataProvider<T> sortingBy(Comparator<T> sortOrder) { - return new ListDataProvider<>(backend, sortOrder); + ListDataProvider<T> parent = this; + return new ListDataProvider<T>(backend, sortOrder) { + + @Override + public Registration addDataProviderListener( + DataProviderListener listener) { + return parent.addDataProviderListener(listener); + } + + @Override + public void refreshAll() { + parent.refreshAll(); + } + }; } /** diff --git a/server/src/main/java/com/vaadin/server/data/Sort.java b/server/src/main/java/com/vaadin/server/data/Sort.java new file mode 100644 index 0000000000..7afa017157 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/Sort.java @@ -0,0 +1,132 @@ +/* + * 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.server.data; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.vaadin.shared.data.sort.SortDirection; + +/** + * Helper class for constructing SortOrders. + * + * @author Vaadin Ltd + * @since 8.0 + */ +public abstract class Sort implements Serializable { + + /** + * SortBuilder is a helper class with fluent API for constructing sort order + * lists. When the sort order is ready to be passed on, calling + * {@link #build()} will create the list of sort orders + * + * @param <S> + * sort order data type + * + * @see Sort + * @see Sort#asc(Object) + * @see Sort#desc(Object) + * @see #build() + */ + public static class SortBuilder<S> implements Serializable { + private List<SortOrder<S>> sortOrder = new ArrayList<>(); + + /** + * Constructs an empty SortBuilder. + */ + protected SortBuilder() { + } + + /** + * Appends sorting with ascending sort direction. + * + * @param by + * the object to sort by + * @return this sort builder + */ + public SortBuilder<S> thenAsc(S by) { + return append(by, SortDirection.ASCENDING); + } + + /** + * Appends sorting with descending sort direction. + * + * @param by + * the object to sort by + * @return this sort builder + */ + public SortBuilder<S> thenDesc(S by) { + return append(by, SortDirection.DESCENDING); + } + + /** + * Appends sorting with given sort direction. + * + * @param by + * the object to sort by + * @param direction + * the sort direction + * + * @return this sort builder + */ + protected SortBuilder<S> append(S by, SortDirection direction) { + sortOrder.add(new SortOrder<>(by, direction)); + return this; + } + + /** + * Returns an unmodifiable list of the current sort order in this sort + * builder. + * + * @return the unmodifiable sort order list + */ + public List<SortOrder<S>> build() { + return Collections.unmodifiableList(sortOrder); + } + } + + /** + * Creates a new sort builder with given sorting using ascending sort + * direction. + * + * @param by + * the object to sort by + * @param <S> + * sort order data type + * + * @return the sort builder + */ + public static <S> SortBuilder<S> asc(S by) { + return new SortBuilder<S>().thenAsc(by); + } + + /** + * Creates a new sort builder with given sorting using descending sort + * direction. + * + * @param by + * the object to sort by + * @param <S> + * sort order data type + * + * @return the sort builder + */ + public static <S> SortBuilder<S> desc(S by) { + return new SortBuilder<S>().thenDesc(by); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/BackendDataProviderTest.java b/server/src/test/java/com/vaadin/server/data/provider/BackendDataProviderTest.java new file mode 100644 index 0000000000..3a70f94b9d --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/BackendDataProviderTest.java @@ -0,0 +1,62 @@ +package com.vaadin.server.data.provider; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.vaadin.server.SerializablePredicate; +import com.vaadin.server.data.BackEndDataProvider; +import com.vaadin.server.data.SortOrder; +import com.vaadin.shared.data.sort.SortDirection; + +public class BackendDataProviderTest extends + DataProviderTestBase<BackEndDataProvider<StrBean, SerializablePredicate<StrBean>>> { + + private static Map<String, Comparator<StrBean>> propertyToComparatorMap = new HashMap<>(); + + static { + propertyToComparatorMap.put("value", + Comparator.comparing(StrBean::getValue)); + propertyToComparatorMap.put("id", Comparator.comparing(StrBean::getId)); + propertyToComparatorMap.put("randomNumber", + Comparator.comparing(StrBean::getRandomNumber)); + } + + private Comparator<StrBean> getComparator(SortOrder<String> so) { + Comparator<StrBean> comparator = propertyToComparatorMap + .get(so.getSorted()); + if (so.getDirection() == SortDirection.DESCENDING) { + comparator = comparator.reversed(); + } + return comparator; + } + + @Override + protected BackEndDataProvider<StrBean, SerializablePredicate<StrBean>> createDataProvider() { + return dataProvider = new BackEndDataProvider<>(query -> { + Stream<StrBean> stream = data.stream() + .filter(t -> query.getFilter().orElse(s -> true).test(t)); + if (!query.getSortOrders().isEmpty()) { + Comparator<StrBean> sorting = query.getSortOrders().stream() + .map(this::getComparator) + .reduce((c1, c2) -> c1.thenComparing(c2)).get(); + stream = stream.sorted(sorting); + } + List<StrBean> list = stream.skip(query.getOffset()) + .limit(query.getLimit()).collect(Collectors.toList()); + list.forEach(s -> System.err.println(s.toString())); + return list.stream(); + }, query -> (int) data.stream() + .filter(t -> query.getFilter().orElse(s -> true).test(t)) + .count()); + } + + @Override + protected BackEndDataProvider<StrBean, SerializablePredicate<StrBean>> sortingBy( + List<SortOrder<String>> sortOrder, Comparator<StrBean> comp) { + return getDataProvider().sortingBy(sortOrder); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/DataProviderTestBase.java b/server/src/test/java/com/vaadin/server/data/provider/DataProviderTestBase.java new file mode 100644 index 0000000000..d930162016 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/DataProviderTestBase.java @@ -0,0 +1,313 @@ +package com.vaadin.server.data.provider; + +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.SerializablePredicate; +import com.vaadin.server.data.DataChangeEvent; +import com.vaadin.server.data.DataProvider; +import com.vaadin.server.data.DataProviderListener; +import com.vaadin.server.data.Query; +import com.vaadin.server.data.Sort; +import com.vaadin.server.data.SortOrder; + +public abstract class DataProviderTestBase<D extends DataProvider<StrBean, SerializablePredicate<StrBean>>> { + + protected static class CountingListener implements DataProviderListener { + + private int counter = 0; + + @Override + public void onDataChange(DataChangeEvent event) { + ++counter; + } + + public int getCounter() { + return counter; + } + } + + protected final SerializablePredicate<StrBean> fooFilter = s -> s.getValue() + .equals("Foo"); + protected final SerializablePredicate<StrBean> gt5Filter = s -> s + .getRandomNumber() > 5; + + protected D dataProvider; + protected List<StrBean> data = StrBean.generateRandomBeans(100); + + @Before + public void setUp() { + dataProvider = createDataProvider(); + } + + protected abstract D createDataProvider(); + + protected final D getDataProvider() { + return dataProvider; + } + + protected abstract D sortingBy(List<SortOrder<String>> sortOrder, + Comparator<StrBean> comp); + + private Query<StrBean, SerializablePredicate<StrBean>> createQuery( + List<SortOrder<String>> sortOrder, Comparator<StrBean> comp) { + return createQuery(sortOrder, comp, null); + } + + private Query<StrBean, SerializablePredicate<StrBean>> createQuery( + List<SortOrder<String>> sortOrder, Comparator<StrBean> comp, + SerializablePredicate<StrBean> filter) { + return new Query<StrBean, SerializablePredicate<StrBean>>(0, + Integer.MAX_VALUE, sortOrder, comp, filter); + } + + private Query<StrBean, SerializablePredicate<StrBean>> createQuery( + SerializablePredicate<StrBean> filter) { + return createQuery(Collections.emptyList(), null, filter); + } + + // Tests start here. + + @Test + public void testListContainsAllData() { + List<StrBean> list = new LinkedList<>(data); + dataProvider.fetch(new Query<>()) + .forEach(str -> assertTrue( + "Data provider contained values not in original data", + list.remove(str))); + assertTrue("Not all values from original data were in data provider", + list.isEmpty()); + } + + @Test + public void testSortByComparatorListsDiffer() { + Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue) + .thenComparing(StrBean::getRandomNumber) + .thenComparing(StrBean::getId); + + List<StrBean> list = dataProvider + .fetch(createQuery(Sort.asc("value").thenAsc("randomNumber") + .thenAsc("id").build(), comp)) + .collect(Collectors.toList()); + + // First value in data is { Xyz, 10, 100 } which should be last in list + Assert.assertNotEquals("First value should not match", data.get(0), + list.get(0)); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + data.sort(comp); + for (int i = 0; i < data.size(); ++i) { + Assert.assertEquals("Sorting result differed", data.get(i), + list.get(i)); + } + } + + @Test + public void testDefaultSortWithSpecifiedPostSort() { + Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue) + .thenComparing(Comparator.comparing(StrBean::getId).reversed()); + List<StrBean> list = sortingBy( + Sort.asc("value").thenDesc("id").build(), comp) + .fetch(createQuery(Sort.asc("randomNumber").build(), + Comparator.comparing(StrBean::getRandomNumber))) + .collect(Collectors.toList()); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + for (int i = 1; i < list.size(); ++i) { + StrBean prev = list.get(i - 1); + StrBean cur = list.get(i); + // Test specific sort + Assert.assertTrue( + "Failure: " + prev.getRandomNumber() + " > " + + cur.getRandomNumber(), + prev.getRandomNumber() <= cur.getRandomNumber()); + + if (prev.getRandomNumber() == cur.getRandomNumber()) { + // Test default sort + Assert.assertTrue( + prev.getValue().compareTo(cur.getValue()) <= 0); + if (prev.getValue().equals(cur.getValue())) { + Assert.assertTrue(prev.getId() > cur.getId()); + } + } + } + } + + @Test + public void testDefaultSortWithFunction() { + List<StrBean> list = sortingBy(Sort.asc("value").build(), + Comparator.comparing(StrBean::getValue)).fetch(new Query<>()) + .collect(Collectors.toList()); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + for (int i = 1; i < list.size(); ++i) { + StrBean prev = list.get(i - 1); + StrBean cur = list.get(i); + + // Test default sort + Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); + } + } + + @Test + public void refreshAll_changeBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + int size = dataProvider.size(new Query<>()); + + data.set(0, bean); + dataProvider.refreshAll(); + + List<StrBean> list = dataProvider.fetch(new Query<>()) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size, dataProvider.size(new Query<>())); + } + + @Test + public void refreshAll_updateBean() { + int size = dataProvider.size(new Query<>()); + + StrBean bean = data.get(0); + bean.setValue("foo"); + dataProvider.refreshAll(); + + List<StrBean> list = dataProvider.fetch(new Query<>()) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals("foo", first.getValue()); + + Assert.assertEquals(size, dataProvider.size(new Query<>())); + } + + @Test + public void refreshAll_sortingBy_changeBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + int size = dataProvider.size(new Query<>()); + + data.set(0, bean); + + D dSource = sortingBy(Sort.asc("id").build(), + Comparator.comparing(StrBean::getId)); + dSource.refreshAll(); + + List<StrBean> list = dSource.fetch(new Query<>()) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size, dataProvider.size(new Query<>())); + } + + @Test + public void refreshAll_addBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + + int size = dataProvider.size(new Query<>()); + + data.add(0, bean); + dataProvider.refreshAll(); + + List<StrBean> list = dataProvider.fetch(new Query<>()) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size + 1, dataProvider.size(new Query<>())); + } + + @Test + public void refreshAll_removeBeanInstance() { + int size = dataProvider.size(new Query<>()); + + data.remove(0); + dataProvider.refreshAll(); + + Assert.assertEquals(size - 1, dataProvider.size(new Query<>())); + } + + @Test + public void refreshAll_fromParentToSortedBy() { + D sortedDataProvider = sortingBy(Sort.asc("randomNumber").build(), + Comparator.comparing(StrBean::getRandomNumber)); + + CountingListener listener = new CountingListener(); + sortedDataProvider.addDataProviderListener(listener); + + Assert.assertEquals("Listener was not called prematurely", 0, + listener.getCounter()); + + dataProvider.refreshAll(); + + Assert.assertEquals("Listener was not called correctly", 1, + listener.getCounter()); + + sortedDataProvider.refreshAll(); + + Assert.assertEquals("Listener was not called correctly", 2, + listener.getCounter()); + } + + @Test + public void filteringListDataProvider_convertFilter() { + DataProvider<StrBean, String> strFilterDataProvider = dataProvider + .convertFilter( + text -> strBean -> strBean.getValue().contains(text)); + Assert.assertEquals("Only one item should match 'Xyz'", 1, + strFilterDataProvider.size(new Query<>("Xyz"))); + Assert.assertEquals("No item should match 'Zyx'", 0, + strFilterDataProvider.size(new Query<>("Zyx"))); + Assert.assertEquals("Unexpected number of matches for 'Foo'", 36, + strFilterDataProvider.size(new Query<>("Foo"))); + + Assert.assertEquals("No items should've been filtered out", data.size(), + strFilterDataProvider.size(new Query<>())); + } + + @Test + public void filteringListDataProvider_defaultFilterType() { + Assert.assertEquals("Only one item should match 'Xyz'", 1, + dataProvider.size(new Query<>( + strBean -> strBean.getValue().contains("Xyz")))); + Assert.assertEquals("No item should match 'Zyx'", 0, dataProvider.size( + new Query<>(strBean -> strBean.getValue().contains("Zyx")))); + Assert.assertEquals("Unexpected number of matches for 'Foo'", 36, + dataProvider.size(new Query<>(fooFilter))); + } + + @Test + public void filteringListDataProvider_defaultFilter() { + // Intentionally lost filter type. Not actually filterable anymore. + DataProvider<StrBean, ?> filtered = dataProvider.setFilter(gt5Filter); + + Assert.assertEquals("Filter not applied, unexpected item count", + dataProvider.size(new Query<>(gt5Filter)), + filtered.size(new Query<>())); + + Assert.assertEquals("Further filtering succeeded", + filtered.size(new Query<>()), + filtered.size((Query) new Query<>(fooFilter))); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java index ec0f054da3..a018148dfa 100644 --- a/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java +++ b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java @@ -1,240 +1,26 @@ package com.vaadin.server.data.provider; -import static org.junit.Assert.assertTrue; - import java.util.Comparator; -import java.util.LinkedList; import java.util.List; -import java.util.stream.Collectors; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import com.vaadin.server.SerializablePredicate; import com.vaadin.server.data.DataProvider; import com.vaadin.server.data.ListDataProvider; import com.vaadin.server.data.Query; +import com.vaadin.server.data.SortOrder; -public class ListDataProviderTest { - - private ListDataProvider<StrBean> dataProvider; - private List<StrBean> data; - private SerializablePredicate<StrBean> fooFilter = s -> s.getValue() - .equals("Foo"); - private SerializablePredicate<StrBean> gt5Filter = s -> s - .getRandomNumber() > 5; - - @Before - public void setUp() { - data = StrBean.generateRandomBeans(100); - dataProvider = DataProvider.create(data); - } - - @Test - public void testListContainsAllData() { - List<StrBean> list = new LinkedList<>(data); - dataProvider.fetch(new Query<>()) - .forEach(str -> assertTrue( - "Data provider contained values not in original data", - list.remove(str))); - assertTrue("Not all values from original data were in data provider", - list.isEmpty()); - } - - @Test - public void testSortByComparatorListsDiffer() { - Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue) - .thenComparing(StrBean::getRandomNumber) - .thenComparing(StrBean::getId); - List<StrBean> list = dataProvider.sortingBy(comp).fetch(new Query<>()) - .collect(Collectors.toList()); - - // First value in data is { Xyz, 10, 100 } which should be last in list - Assert.assertNotEquals("First value should not match", data.get(0), - list.get(0)); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - data.sort(comp); - for (int i = 0; i < data.size(); ++i) { - Assert.assertEquals("Sorting result differed", data.get(i), - list.get(i)); - } - } - - @Test - public void testDefatulSortWithSpecifiedPostSort() { - Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue) - .thenComparing(Comparator.comparing(StrBean::getId).reversed()); - List<StrBean> list = dataProvider.sortingBy(comp).fetch(new Query<>()) - // The sort here should come e.g from a Component - .sorted(Comparator.comparing(StrBean::getRandomNumber)) - .collect(Collectors.toList()); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - for (int i = 1; i < list.size(); ++i) { - StrBean prev = list.get(i - 1); - StrBean cur = list.get(i); - // Test specific sort - Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber()); - - if (prev.getRandomNumber() == cur.getRandomNumber()) { - // Test default sort - Assert.assertTrue( - prev.getValue().compareTo(cur.getValue()) <= 0); - if (prev.getValue().equals(cur.getValue())) { - Assert.assertTrue(prev.getId() > cur.getId()); - } - } - } - } - - @Test - public void testDefatulSortWithFunction() { - List<StrBean> list = dataProvider.sortingBy(StrBean::getValue) - .fetch(new Query<>()).collect(Collectors.toList()); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - for (int i = 1; i < list.size(); ++i) { - StrBean prev = list.get(i - 1); - StrBean cur = list.get(i); - - // Test default sort - Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); - } - } - - @Test - public void refreshAll_changeBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - int size = dataProvider.size(new Query<>()); - - data.set(0, bean); - dataProvider.refreshAll(); - - List<StrBean> list = dataProvider.fetch(new Query<>()) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size, dataProvider.size(new Query<>())); - } - - @Test - public void refreshAll_updateBean() { - int size = dataProvider.size(new Query<>()); - - StrBean bean = data.get(0); - bean.setValue("foo"); - dataProvider.refreshAll(); - - List<StrBean> list = dataProvider.fetch(new Query<>()) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals("foo", first.getValue()); - - Assert.assertEquals(size, dataProvider.size(new Query<>())); - } - - @Test - public void refreshAll_sortingBy_changeBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - int size = dataProvider.size(new Query<>()); - - data.set(0, bean); - - ListDataProvider<StrBean> dSource = dataProvider - .sortingBy(Comparator.comparing(StrBean::getId)); - dSource.refreshAll(); - - List<StrBean> list = dSource.fetch(new Query<>()) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size, dataProvider.size(new Query<>())); - } - - @Test - public void refreshAll_addBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - - int size = dataProvider.size(new Query<>()); - - data.add(0, bean); - dataProvider.refreshAll(); - - List<StrBean> list = dataProvider.fetch(new Query<>()) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size + 1, dataProvider.size(new Query<>())); - } - - @Test - public void refreshAll_removeBeanInstance() { - int size = dataProvider.size(new Query<>()); +public class ListDataProviderTest + extends DataProviderTestBase<ListDataProvider<StrBean>> { - data.remove(0); - dataProvider.refreshAll(); - - Assert.assertEquals(size - 1, dataProvider.size(new Query<>())); + @Override + protected ListDataProvider<StrBean> createDataProvider() { + return DataProvider.create(data); } @Test - public void filteringListDataProvider_convertFilter() { - DataProvider<StrBean, String> strFilterDataProvider = dataProvider - .convertFilter( - text -> strBean -> strBean.getValue().contains(text)); - Assert.assertEquals("Only one item should match 'Xyz'", 1, - strFilterDataProvider.size(new Query<>("Xyz"))); - Assert.assertEquals("No item should match 'Zyx'", 0, - strFilterDataProvider.size(new Query<>("Zyx"))); - Assert.assertEquals("Unexpected number of matches for 'Foo'", 36, - strFilterDataProvider.size(new Query<>("Foo"))); - - Assert.assertEquals("No items should've been filtered out", data.size(), - strFilterDataProvider.size(new Query<>())); - } - - @Test - public void filteringListDataProvider_defaultFilterType() { - Assert.assertEquals("Only one item should match 'Xyz'", 1, - dataProvider.size(new Query<>( - strBean -> strBean.getValue().contains("Xyz")))); - Assert.assertEquals("No item should match 'Zyx'", 0, dataProvider.size( - new Query<>(strBean -> strBean.getValue().contains("Zyx")))); - Assert.assertEquals("Unexpected number of matches for 'Foo'", 36, - dataProvider.size(new Query<>(fooFilter))); - } - - @Test - public void filteringListDataProvider_defaultFilter() { - // Intentionally lost filter type. Not actually filterable anymore. - DataProvider<StrBean, ?> filtered = dataProvider.setFilter(gt5Filter); - - Assert.assertEquals("Filter not applied, unexpected item count", - dataProvider.size(new Query<>(gt5Filter)), - filtered.size(new Query<>())); - - Assert.assertEquals("Further filtering succeeded", - filtered.size(new Query<>()), - filtered.size((Query) new Query<>(fooFilter))); - } - public void filteringListDataProvider_appliedFilters() { Assert.assertEquals("Filtering result differ", data.stream().filter(fooFilter).count(), @@ -293,4 +79,10 @@ public class ListDataProviderTest { .size(new Query<>("Zyx"))); } + @Override + protected ListDataProvider<StrBean> sortingBy( + List<SortOrder<String>> sortOrder, Comparator<StrBean> comp) { + return getDataProvider().sortingBy(comp); + } + } |