summaryrefslogtreecommitdiffstats
path: root/tests/server-side/com/vaadin/data/util/TestHierarchicalContainer.java
blob: 9e0558a122c13cefae9e00db5767cda006b683e5 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.vaadin.data.util;

import com.vaadin.data.Container.Filter;
import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;

public class TestHierarchicalContainer extends
        AbstractHierarchicalContainerTest {

    public void testBasicOperations() {
        testBasicContainerOperations(new HierarchicalContainer());
    }

    public void testFiltering() {
        testContainerFiltering(new HierarchicalContainer());
    }

    public void testSorting() {
        testContainerSorting(new HierarchicalContainer());
    }

    public void testOrdered() {
        testContainerOrdered(new HierarchicalContainer());
    }

    public void testHierarchicalSorting() {
        testHierarchicalSorting(new HierarchicalContainer());
    }

    public void testSortingAndFiltering() {
        testContainerSortingAndFiltering(new HierarchicalContainer());
    }

    public void testRemovingItemsFromFilteredContainer() {
        HierarchicalContainer container = new HierarchicalContainer();
        initializeContainer(container);
        container.setIncludeParentsWhenFiltering(true);
        container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false);
        Object p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com.vaadin.ui", p1);

        container.removeItem("com.vaadin.ui.TabSheet");
        // Parent for the removed item must be null because the item is no
        // longer in the container
        p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertNull("Parent should be null, is " + p1, p1);

        container.removeAllItems();
        p1 = container.getParent("com.vaadin.terminal.gwt.client.Focusable");
        assertNull("Parent should be null, is " + p1, p1);

    }

    public void testParentWhenRemovingFilterFromContainer() {
        HierarchicalContainer container = new HierarchicalContainer();
        initializeContainer(container);
        container.setIncludeParentsWhenFiltering(true);
        container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false);
        Object p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com.vaadin.ui", p1);
        p1 = container
                .getParent("com.vaadin.terminal.gwt.client.ui.VPopupCalendar");
        assertNull(p1);
        container.removeAllContainerFilters();
        p1 = container
                .getParent("com.vaadin.terminal.gwt.client.ui.VPopupCalendar");
        assertEquals("com.vaadin.terminal.gwt.client.ui", p1);

    }

    public void testChangeParentInFilteredContainer() {
        HierarchicalContainer container = new HierarchicalContainer();
        initializeContainer(container);
        container.setIncludeParentsWhenFiltering(true);
        container.addContainerFilter(FULLY_QUALIFIED_NAME, "Tab", false, false);

        // Change parent of filtered item
        Object p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com.vaadin.ui", p1);
        container.setParent("com.vaadin.ui.TabSheet", "com.vaadin");
        p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com.vaadin", p1);
        container.setParent("com.vaadin.ui.TabSheet", "com");
        p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com", p1);
        container.setParent("com.vaadin.ui.TabSheet", null);
        p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertNull(p1);

        // root -> non-root
        container.setParent("com.vaadin.ui.TabSheet", "com");
        p1 = container.getParent("com.vaadin.ui.TabSheet");
        assertEquals("com", p1);

    }

    public void testHierarchicalFilteringWithParents() {
        HierarchicalContainer container = new HierarchicalContainer();
        initializeContainer(container);
        container.setIncludeParentsWhenFiltering(true);

        // Filter by "contains ab"
        container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false);

        // 20 items match the filters and the have 8 parents that should also be
        // included
        // only one root "com" should exist
        // filtered
        int expectedSize = 29;
        int expectedRoots = 1;

        validateHierarchicalContainer(container, "com",
                "com.vaadin.ui.TabSheet",
                "com.vaadin.terminal.gwt.client.Focusable", "blah", true,
                expectedSize, expectedRoots, true);

        // only include .gwt.client classes
        container.removeAllContainerFilters();
        container.addContainerFilter(FULLY_QUALIFIED_NAME, ".gwt.client.",
                false, false);

        int packages = 6;
        int classes = 112;

        expectedSize = packages + classes;
        expectedRoots = 1;

        validateHierarchicalContainer(container, "com",
                "com.vaadin.terminal.gwt.client.WidgetSet",
                "com.vaadin.terminal.gwt.client.ui.VSplitPanelVertical",
                "blah", true, expectedSize, expectedRoots, true);

        // Additionally remove all without 'm' in the simple name.
        container.addContainerFilter(SIMPLE_NAME, "m", false, false);

        expectedSize = 7 + 18;
        expectedRoots = 1;

        validateHierarchicalContainer(
                container,
                "com",
                "com.vaadin.terminal.gwt.client.ui.VUriFragmentUtility",
                "com.vaadin.terminal.gwt.client.ui.layout.ChildComponentContainer",
                "blah", true, expectedSize, expectedRoots, true);

    }

    public void testRemoveLastChild() {
        HierarchicalContainer c = new HierarchicalContainer();

        c.addItem("root");
        assertEquals(false, c.hasChildren("root"));

        c.addItem("child");
        c.setParent("child", "root");
        assertEquals(true, c.hasChildren("root"));

        c.removeItem("child");
        assertFalse(c.containsId("child"));
        assertNull(c.getChildren("root"));
        assertNull(c.getChildren("child"));
        assertFalse(c.hasChildren("child"));
        assertFalse(c.hasChildren("root"));
    }

    public void testRemoveLastChildFromFiltered() {
        HierarchicalContainer c = new HierarchicalContainer();

        c.addItem("root");
        assertEquals(false, c.hasChildren("root"));

        c.addItem("child");
        c.setParent("child", "root");
        assertEquals(true, c.hasChildren("root"));

        // Dummy filter that does not remove any items
        c.addContainerFilter(new Filter() {

            @Override
            public boolean passesFilter(Object itemId, Item item)
                    throws UnsupportedOperationException {
                return true;
            }

            @Override
            public boolean appliesToProperty(Object propertyId) {
                return true;
            }
        });
        c.removeItem("child");

        assertFalse(c.containsId("child"));
        assertNull(c.getChildren("root"));
        assertNull(c.getChildren("child"));
        assertFalse(c.hasChildren("child"));
        assertFalse(c.hasChildren("root"));
    }

    public void testHierarchicalFilteringWithoutParents() {
        HierarchicalContainer container = new HierarchicalContainer();

        initializeContainer(container);
        container.setIncludeParentsWhenFiltering(false);

        // Filter by "contains ab"
        container.addContainerFilter(SIMPLE_NAME, "ab", false, false);

        // 20 items match the filter.
        // com.vaadin.data.BufferedValidatable
        // com.vaadin.data.Validatable
        // com.vaadin.terminal.gwt.client.Focusable
        // com.vaadin.terminal.gwt.client.Paintable
        // com.vaadin.terminal.gwt.client.ui.Table
        // com.vaadin.terminal.gwt.client.ui.VLabel
        // com.vaadin.terminal.gwt.client.ui.VScrollTable
        // com.vaadin.terminal.gwt.client.ui.VTablePaging
        // com.vaadin.terminal.gwt.client.ui.VTabsheet
        // com.vaadin.terminal.gwt.client.ui.VTabsheetBase
        // com.vaadin.terminal.gwt.client.ui.VTabsheetPanel
        // com.vaadin.terminal.gwt.server.ChangeVariablesErrorEvent
        // com.vaadin.terminal.Paintable
        // com.vaadin.terminal.Scrollable
        // com.vaadin.terminal.Sizeable
        // com.vaadin.terminal.VariableOwner
        // com.vaadin.ui.Label
        // com.vaadin.ui.Table
        // com.vaadin.ui.TableFieldFactory
        // com.vaadin.ui.TabSheet
        // all become roots.
        int expectedSize = 20;
        int expectedRoots = 20;

        validateHierarchicalContainer(container,
                "com.vaadin.data.BufferedValidatable",
                "com.vaadin.ui.TabSheet",
                "com.vaadin.terminal.gwt.client.ui.VTabsheetBase", "blah",
                true, expectedSize, expectedRoots, false);

        // only include .gwt.client classes
        container.removeAllContainerFilters();
        container.addContainerFilter(FULLY_QUALIFIED_NAME, ".gwt.client.",
                false, false);

        int packages = 3;
        int classes = 110;

        expectedSize = packages + classes;
        expectedRoots = 35 + 1; // com.vaadin.terminal.gwt.client.ui +
        // com.vaadin.terminal.gwt.client.*

        // Sorting is case insensitive
        validateHierarchicalContainer(container,
                "com.vaadin.terminal.gwt.client.ApplicationConfiguration",
                "com.vaadin.terminal.gwt.client.WidgetSet",
                "com.vaadin.terminal.gwt.client.ui.VOptionGroup", "blah", true,
                expectedSize, expectedRoots, false);

        // Additionally remove all without 'P' in the simple name.
        container.addContainerFilter(SIMPLE_NAME, "P", false, false);

        expectedSize = 13;
        expectedRoots = expectedSize;

        validateHierarchicalContainer(container,
                "com.vaadin.terminal.gwt.client.Paintable",
                "com.vaadin.terminal.gwt.client.ui.VTabsheetPanel",
                "com.vaadin.terminal.gwt.client.ui.VPopupCalendar", "blah",
                true, expectedSize, expectedRoots, false);

    }
}