aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java
blob: 0aa9bb80ba11c1ed82bccc52527472176a39ad62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.vaadin.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;

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 void setSortOrder(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<>(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());
        setSortOrder(Sort.asc("value").thenDesc("id").build(), comp);

        List<StrBean> list = dataProvider
                .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() {
        setSortOrder(Sort.asc("value").build(),
                Comparator.comparing(StrBean::getValue));

        List<StrBean> list = dataProvider.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 filteringListDataProvider_convertFilter() {
        DataProvider<StrBean, String> strFilterDataProvider = dataProvider
                .withConvertedFilter(
                        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)));
    }

    protected long sizeWithUnfilteredQuery() {
        return dataProvider.fetch(new Query<>()).count();
    }

    protected static <F> void assertSizeWithFilter(int expectedSize,
            DataProvider<?, F> dataProvider, F filterValue) {
        Assert.assertEquals(expectedSize,
                dataProvider.size(new Query<>(filterValue)));
    }

}