aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/provider/hierarchical/HierarchicalCommunicatorTest.java
blob: 3475be55204e124a62dbcb644f4ebe5a7bab50c4 (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
package com.vaadin.data.provider.hierarchical;

import com.vaadin.data.TreeData;
import com.vaadin.data.provider.DataCommunicatorTest;
import com.vaadin.data.provider.HierarchicalDataCommunicator;
import com.vaadin.data.provider.TreeDataProvider;
import com.vaadin.server.AbstractClientConnector;
import com.vaadin.server.MockVaadinSession;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.List;

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

public class HierarchicalCommunicatorTest {

    private static final String ROOT = "ROOT";
    private static final String FOLDER = "FOLDER";
    private static final String LEAF = "LEAF";
    private TreeDataProvider<String> dataProvider;
    private TestHierarchicalDataCommunicator<String> communicator;
    private TreeData<String> treeData;

    @Before
    public void setUp() {
        VaadinSession session = new MockVaadinSession(
                Mockito.mock(VaadinService.class));
        session.lock();
        UI ui = new DataCommunicatorTest.TestUI(session);
        treeData = new TreeData<>();
        treeData.addItems(null, ROOT);
        treeData.addItems(ROOT, FOLDER);
        treeData.addItems(FOLDER, LEAF);
        dataProvider = new TreeDataProvider<>(treeData);
        communicator = new TestHierarchicalDataCommunicator<>();
        communicator.extend(ui);
        communicator.setDataProvider(dataProvider, null);
        communicator.attach();
    }

    @Test
    public void testFolderRemoveRefreshAll() {
        testItemRemove(FOLDER, true);
    }

    @Test
    public void testLeafRemoveRefreshAll() {
        testItemRemove(LEAF, true);
    }

    @Test
    public void testFolderRemove() {
        testItemRemove(FOLDER, false);
    }

    @Test
    public void testLeafRemove() {
        testItemRemove(LEAF, false);
    }

    private void testItemRemove(String item, boolean refreshAll) {
        communicator.pushData(1, Arrays.asList(ROOT, FOLDER, LEAF));
        communicator.expand(ROOT);
        communicator.expand(FOLDER);
        // Put the item into client queue
        communicator.refresh(item);
        treeData.removeItem(item);
        if (refreshAll) {
            dataProvider.refreshAll();
        } else {
            dataProvider.refreshItem(item);
        }
        communicator.beforeClientResponse(false);
    }

    @Test
    public void testReplaceAll() {
        communicator.pushData(1, Arrays.asList(ROOT, FOLDER, LEAF));
        // Some modifications
        communicator.expand(ROOT);
        communicator.expand(FOLDER);
        communicator.refresh(LEAF);
        // Replace dataprovider
        communicator.setDataProvider(new TreeDataProvider<>(new TreeData<>()),
                null);
        dataProvider.refreshAll();
        communicator.beforeClientResponse(false);
        assertFalse("Stalled object in KeyMapper",
                communicator.getKeyMapper().has(ROOT));
        assertEquals(-1, communicator.getParentIndex(FOLDER).longValue());
    }

    private static class TestHierarchicalDataCommunicator<T>
            extends HierarchicalDataCommunicator<T> {
        @Override
        public void extend(AbstractClientConnector target) {
            super.extend(target);
        }

        @Override
        public void pushData(int firstIndex, List<T> data) {
            super.pushData(firstIndex, data);
        }
    }
}