aboutsummaryrefslogtreecommitdiffstats
path: root/server/tests/src/com/vaadin/data/util/AbstractHierarchicalContainerTestBase.java
blob: 3bd00cce3c8186460488930cb20330c9786aab0e (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
package com.vaadin.data.util;

import java.util.Collection;

import com.vaadin.data.Container;
import com.vaadin.data.Container.Hierarchical;
import com.vaadin.data.Container.Sortable;
import com.vaadin.data.Item;

public abstract class AbstractHierarchicalContainerTestBase extends
        AbstractContainerTestBase {

    /**
     * @param container
     *            The container to validate
     * @param expectedFirstItemId
     *            Expected first item id
     * @param expectedLastItemId
     *            Expected last item id
     * @param itemIdInSet
     *            An item id that is in the container
     * @param itemIdNotInSet
     *            An item id that is not in the container
     * @param checkGetItemNull
     *            true if getItem() should return null for itemIdNotInSet, false
     *            to skip the check (container.containsId() is checked in any
     *            case)
     * @param expectedSize
     *            Expected number of items in the container. Not related to
     *            hierarchy.
     * @param expectedTraversalSize
     *            Expected number of items found when traversing from the roots
     *            down to all available nodes.
     * @param expectedRootSize
     *            Expected number of root items
     * @param rootsHaveChildren
     *            true if all roots have children, false otherwise (skips some
     *            asserts)
     */
    protected void validateHierarchicalContainer(Hierarchical container,
            Object expectedFirstItemId, Object expectedLastItemId,
            Object itemIdInSet, Object itemIdNotInSet,
            boolean checkGetItemNull, int expectedSize, int expectedRootSize,
            boolean rootsHaveChildren) {

        validateContainer(container, expectedFirstItemId, expectedLastItemId,
                itemIdInSet, itemIdNotInSet, checkGetItemNull, expectedSize);

        // rootItemIds
        Collection<?> rootIds = container.rootItemIds();
        assertEquals(expectedRootSize, rootIds.size());

        for (Object rootId : rootIds) {
            // All roots must be in container
            assertTrue(container.containsId(rootId));

            // All roots must have no parent
            assertNull(container.getParent(rootId));

            // all roots must be roots
            assertTrue(container.isRoot(rootId));

            if (rootsHaveChildren) {
                // all roots have children allowed in this case
                assertTrue(container.areChildrenAllowed(rootId));

                // all roots have children in this case
                Collection<?> children = container.getChildren(rootId);
                assertNotNull(rootId + " should have children", children);
                assertTrue(rootId + " should have children",
                        (children.size() > 0));
                // getParent
                for (Object childId : children) {
                    assertEquals(container.getParent(childId), rootId);
                }

            }
        }

        // isRoot should return false for unknown items
        assertFalse(container.isRoot(itemIdNotInSet));

        // hasChildren should return false for unknown items
        assertFalse(container.hasChildren(itemIdNotInSet));

        // areChildrenAllowed should return false for unknown items
        assertFalse(container.areChildrenAllowed(itemIdNotInSet));

        // removeItem of unknown items should return false
        assertFalse(container.removeItem(itemIdNotInSet));

        assertEquals(expectedSize, countNodes(container));

        validateHierarchy(container);
    }

    private int countNodes(Hierarchical container) {
        int totalNodes = 0;
        for (Object rootId : container.rootItemIds()) {
            totalNodes += countNodes(container, rootId);
        }

        return totalNodes;
    }

    private int countNodes(Hierarchical container, Object itemId) {
        int nodes = 1; // This
        Collection<?> children = container.getChildren(itemId);
        if (children != null) {
            for (Object id : children) {
                nodes += countNodes(container, id);
            }
        }

        return nodes;
    }

    private void validateHierarchy(Hierarchical container) {
        for (Object rootId : container.rootItemIds()) {
            validateHierarchy(container, rootId, null);
        }
    }

    private void validateHierarchy(Hierarchical container, Object itemId,
            Object parentId) {
        Collection<?> children = container.getChildren(itemId);

        // getParent
        assertEquals(container.getParent(itemId), parentId);

        if (!container.areChildrenAllowed(itemId)) {
            // If no children is allowed the item should have no children
            assertFalse(container.hasChildren(itemId));
            assertTrue(children == null || children.size() == 0);

            return;
        }
        if (children != null) {
            for (Object id : children) {
                validateHierarchy(container, id, itemId);
            }
        }
    }

    protected void testHierarchicalContainer(Container.Hierarchical container) {
        initializeContainer(container);

        int packages = 21 + 3;
        int expectedSize = sampleData.length + packages;
        validateHierarchicalContainer(container, "com",
                "org.vaadin.test.LastClass",
                "com.vaadin.server.ApplicationResource", "blah", true,
                expectedSize, 2, true);

    }

    protected void testHierarchicalSorting(Container.Hierarchical container) {
        Container.Sortable sortable = (Sortable) container;

        initializeContainer(container);

        // Must be able to sort based on PROP1 and PROP2 for this test
        assertTrue(sortable.getSortableContainerPropertyIds().contains(
                FULLY_QUALIFIED_NAME));
        assertTrue(sortable.getSortableContainerPropertyIds().contains(
                REVERSE_FULLY_QUALIFIED_NAME));

        sortable.sort(new Object[] { FULLY_QUALIFIED_NAME },
                new boolean[] { true });

        int packages = 21 + 3;
        int expectedSize = sampleData.length + packages;
        validateHierarchicalContainer(container, "com",
                "org.vaadin.test.LastClass",
                "com.vaadin.server.ApplicationResource", "blah", true,
                expectedSize, 2, true);

        sortable.sort(new Object[] { REVERSE_FULLY_QUALIFIED_NAME },
                new boolean[] { true });

        validateHierarchicalContainer(container,
                "com.vaadin.server.ApplicationPortlet2",
                "com.vaadin.data.util.ObjectProperty",
                "com.vaadin.server.ApplicationResource", "blah", true,
                expectedSize, 2, true);

    }

    protected void initializeContainer(Container.Hierarchical container) {
        container.removeAllItems();
        Object[] propertyIds = container.getContainerPropertyIds().toArray();
        for (Object propertyId : propertyIds) {
            container.removeContainerProperty(propertyId);
        }

        container.addContainerProperty(FULLY_QUALIFIED_NAME, String.class, "");
        container.addContainerProperty(SIMPLE_NAME, String.class, "");
        container.addContainerProperty(REVERSE_FULLY_QUALIFIED_NAME,
                String.class, null);
        container.addContainerProperty(ID_NUMBER, Integer.class, null);

        for (int i = 0; i < sampleData.length; i++) {
            String id = sampleData[i];

            // Add path as parent
            String paths[] = id.split("\\.");
            String path = paths[0];
            // Adds "com" and other items multiple times so should return null
            // for all but the first time
            if (container.addItem(path) != null) {
                assertTrue(container.setChildrenAllowed(path, false));
                Item item = container.getItem(path);
                item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path);
                item.getItemProperty(SIMPLE_NAME).setValue(getSimpleName(path));
                item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME).setValue(
                        reverse(path));
                item.getItemProperty(ID_NUMBER).setValue(1);
            }
            for (int j = 1; j < paths.length; j++) {
                String parent = path;
                path = path + "." + paths[j];

                // Adds "com" and other items multiple times so should return
                // null for all but the first time
                if (container.addItem(path) != null) {
                    assertTrue(container.setChildrenAllowed(path, false));

                    Item item = container.getItem(path);
                    item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path);
                    item.getItemProperty(SIMPLE_NAME).setValue(
                            getSimpleName(path));
                    item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME)
                            .setValue(reverse(path));
                    item.getItemProperty(ID_NUMBER).setValue(1);

                }
                assertTrue(container.setChildrenAllowed(parent, true));
                assertTrue(
                        "Failed to set " + parent + " as parent for " + path,
                        container.setParent(path, parent));
            }

            Item item = container.getItem(id);
            assertNotNull(item);
            String parent = id.substring(0, id.lastIndexOf('.'));
            assertTrue(container.setParent(id, parent));
            item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(sampleData[i]);
            item.getItemProperty(SIMPLE_NAME).setValue(
                    getSimpleName(sampleData[i]));
            item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME).setValue(
                    reverse(sampleData[i]));
            item.getItemProperty(ID_NUMBER).setValue(i % 2);
        }
    }

}