aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/data/selection/StaleMultiSelectionTest.java
blob: 11918df0d680bf26c317d9fb3703d00659c197f9 (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
package com.vaadin.tests.data.selection;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

import com.vaadin.data.provider.StrBean;
import com.vaadin.ui.AbstractMultiSelect;
import com.vaadin.ui.CheckBoxGroup;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.TwinColSelect;

public class StaleMultiSelectionTest
        extends AbstractStaleSelectionTest<AbstractMultiSelect<StrBean>> {

    @Test
    public void testSelectionUpdateOnRefreshItem() {
        StrBean toReplace = data.get(0);
        assertNotStale(toReplace);

        select.select(toReplace);

        StrBean replacement = new StrBean("Replacement bean", toReplace.getId(),
                -1);
        dataProvider.refreshItem(replacement);

        assertIsStale(toReplace);
        select.getSelectedItems()
                .forEach(item -> assertFalse(
                        "Selection should not contain stale values",
                        dataProvider.isStale(item)));

        Object oldId = dataProvider.getId(toReplace);
        assertTrue("Selection did not contain an item with matching Id.",
                select.getSelectedItems().stream().map(dataProvider::getId)
                        .anyMatch(oldId::equals));
        assertTrue("Stale element is not considered selected.",
                select.isSelected(toReplace));
    }

    @Parameters(name = "{0}")
    public static Collection<Object[]> getParams() {
        return Stream
                .of(new ListSelect<>(), new TwinColSelect<>(),
                        new CheckBoxGroup<>())
                .map(component -> new Object[] {
                        component.getClass().getSimpleName(), component })
                .collect(Collectors.toList());
    }
}