選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractHierarchicalContainerTestBase.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package com.vaadin.v7.data.util;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertNull;
  6. import static org.junit.Assert.assertTrue;
  7. import java.util.Collection;
  8. import com.vaadin.v7.data.Container;
  9. import com.vaadin.v7.data.Container.Hierarchical;
  10. import com.vaadin.v7.data.Container.Sortable;
  11. import com.vaadin.v7.data.Item;
  12. public abstract class AbstractHierarchicalContainerTestBase
  13. extends AbstractContainerTestBase {
  14. /**
  15. * @param container
  16. * The container to validate
  17. * @param expectedFirstItemId
  18. * Expected first item id
  19. * @param expectedLastItemId
  20. * Expected last item id
  21. * @param itemIdInSet
  22. * An item id that is in the container
  23. * @param itemIdNotInSet
  24. * An item id that is not in the container
  25. * @param checkGetItemNull
  26. * true if getItem() should return null for itemIdNotInSet, false
  27. * to skip the check (container.containsId() is checked in any
  28. * case)
  29. * @param expectedSize
  30. * Expected number of items in the container. Not related to
  31. * hierarchy.
  32. * @param expectedTraversalSize
  33. * Expected number of items found when traversing from the roots
  34. * down to all available nodes.
  35. * @param expectedRootSize
  36. * Expected number of root items
  37. * @param rootsHaveChildren
  38. * true if all roots have children, false otherwise (skips some
  39. * asserts)
  40. */
  41. protected void validateHierarchicalContainer(Hierarchical container,
  42. Object expectedFirstItemId, Object expectedLastItemId,
  43. Object itemIdInSet, Object itemIdNotInSet, boolean checkGetItemNull,
  44. int expectedSize, int expectedRootSize, boolean rootsHaveChildren) {
  45. validateContainer(container, expectedFirstItemId, expectedLastItemId,
  46. itemIdInSet, itemIdNotInSet, checkGetItemNull, expectedSize);
  47. // rootItemIds
  48. Collection<?> rootIds = container.rootItemIds();
  49. assertEquals(expectedRootSize, rootIds.size());
  50. for (Object rootId : rootIds) {
  51. // All roots must be in container
  52. assertTrue(container.containsId(rootId));
  53. // All roots must have no parent
  54. assertNull(container.getParent(rootId));
  55. // all roots must be roots
  56. assertTrue(container.isRoot(rootId));
  57. if (rootsHaveChildren) {
  58. // all roots have children allowed in this case
  59. assertTrue(container.areChildrenAllowed(rootId));
  60. // all roots have children in this case
  61. Collection<?> children = container.getChildren(rootId);
  62. assertNotNull(rootId + " should have children", children);
  63. assertTrue(rootId + " should have children",
  64. !children.isEmpty());
  65. // getParent
  66. for (Object childId : children) {
  67. assertEquals(container.getParent(childId), rootId);
  68. }
  69. }
  70. }
  71. // isRoot should return false for unknown items
  72. assertFalse(container.isRoot(itemIdNotInSet));
  73. // hasChildren should return false for unknown items
  74. assertFalse(container.hasChildren(itemIdNotInSet));
  75. // areChildrenAllowed should return false for unknown items
  76. assertFalse(container.areChildrenAllowed(itemIdNotInSet));
  77. // removeItem of unknown items should return false
  78. assertFalse(container.removeItem(itemIdNotInSet));
  79. assertEquals(expectedSize, countNodes(container));
  80. validateHierarchy(container);
  81. }
  82. private int countNodes(Hierarchical container) {
  83. int totalNodes = 0;
  84. for (Object rootId : container.rootItemIds()) {
  85. totalNodes += countNodes(container, rootId);
  86. }
  87. return totalNodes;
  88. }
  89. private int countNodes(Hierarchical container, Object itemId) {
  90. int nodes = 1; // This
  91. Collection<?> children = container.getChildren(itemId);
  92. if (children != null) {
  93. for (Object id : children) {
  94. nodes += countNodes(container, id);
  95. }
  96. }
  97. return nodes;
  98. }
  99. private void validateHierarchy(Hierarchical container) {
  100. for (Object rootId : container.rootItemIds()) {
  101. validateHierarchy(container, rootId, null);
  102. }
  103. }
  104. private void validateHierarchy(Hierarchical container, Object itemId,
  105. Object parentId) {
  106. Collection<?> children = container.getChildren(itemId);
  107. // getParent
  108. assertEquals(container.getParent(itemId), parentId);
  109. if (!container.areChildrenAllowed(itemId)) {
  110. // If no children is allowed the item should have no children
  111. assertFalse(container.hasChildren(itemId));
  112. assertTrue(children == null || children.isEmpty());
  113. return;
  114. }
  115. if (children != null) {
  116. for (Object id : children) {
  117. validateHierarchy(container, id, itemId);
  118. }
  119. }
  120. }
  121. protected void testHierarchicalContainer(Container.Hierarchical container) {
  122. initializeContainer(container);
  123. int packages = 21 + 3;
  124. int expectedSize = sampleData.length + packages;
  125. validateHierarchicalContainer(container, "com",
  126. "org.vaadin.test.LastClass",
  127. "com.vaadin.server.ApplicationResource", "blah", true,
  128. expectedSize, 2, true);
  129. }
  130. protected void testHierarchicalSorting(Container.Hierarchical container) {
  131. Container.Sortable sortable = (Sortable) container;
  132. initializeContainer(container);
  133. // Must be able to sort based on PROP1 and PROP2 for this test
  134. assertTrue(sortable.getSortableContainerPropertyIds()
  135. .contains(FULLY_QUALIFIED_NAME));
  136. assertTrue(sortable.getSortableContainerPropertyIds()
  137. .contains(REVERSE_FULLY_QUALIFIED_NAME));
  138. sortable.sort(new Object[] { FULLY_QUALIFIED_NAME },
  139. new boolean[] { true });
  140. int packages = 21 + 3;
  141. int expectedSize = sampleData.length + packages;
  142. validateHierarchicalContainer(container, "com",
  143. "org.vaadin.test.LastClass",
  144. "com.vaadin.server.ApplicationResource", "blah", true,
  145. expectedSize, 2, true);
  146. sortable.sort(new Object[] { REVERSE_FULLY_QUALIFIED_NAME },
  147. new boolean[] { true });
  148. validateHierarchicalContainer(container,
  149. "com.vaadin.server.ApplicationPortlet2",
  150. "com.vaadin.data.util.ObjectProperty",
  151. "com.vaadin.server.ApplicationResource", "blah", true,
  152. expectedSize, 2, true);
  153. }
  154. protected void initializeContainer(Container.Hierarchical container) {
  155. container.removeAllItems();
  156. Object[] propertyIds = container.getContainerPropertyIds().toArray();
  157. for (Object propertyId : propertyIds) {
  158. container.removeContainerProperty(propertyId);
  159. }
  160. container.addContainerProperty(FULLY_QUALIFIED_NAME, String.class, "");
  161. container.addContainerProperty(SIMPLE_NAME, String.class, "");
  162. container.addContainerProperty(REVERSE_FULLY_QUALIFIED_NAME,
  163. String.class, null);
  164. container.addContainerProperty(ID_NUMBER, Integer.class, null);
  165. for (int i = 0; i < sampleData.length; i++) {
  166. String id = sampleData[i];
  167. // Add path as parent
  168. String paths[] = id.split("\\.");
  169. String path = paths[0];
  170. // Adds "com" and other items multiple times so should return null
  171. // for all but the first time
  172. if (container.addItem(path) != null) {
  173. assertTrue(container.setChildrenAllowed(path, false));
  174. Item item = container.getItem(path);
  175. item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path);
  176. item.getItemProperty(SIMPLE_NAME).setValue(getSimpleName(path));
  177. item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME)
  178. .setValue(reverse(path));
  179. item.getItemProperty(ID_NUMBER).setValue(1);
  180. }
  181. for (int j = 1; j < paths.length; j++) {
  182. String parent = path;
  183. path = path + "." + paths[j];
  184. // Adds "com" and other items multiple times so should return
  185. // null for all but the first time
  186. if (container.addItem(path) != null) {
  187. assertTrue(container.setChildrenAllowed(path, false));
  188. Item item = container.getItem(path);
  189. item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path);
  190. item.getItemProperty(SIMPLE_NAME)
  191. .setValue(getSimpleName(path));
  192. item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME)
  193. .setValue(reverse(path));
  194. item.getItemProperty(ID_NUMBER).setValue(1);
  195. }
  196. assertTrue(container.setChildrenAllowed(parent, true));
  197. assertTrue("Failed to set " + parent + " as parent for " + path,
  198. container.setParent(path, parent));
  199. }
  200. Item item = container.getItem(id);
  201. assertNotNull(item);
  202. String parent = id.substring(0, id.lastIndexOf('.'));
  203. assertTrue(container.setParent(id, parent));
  204. item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(sampleData[i]);
  205. item.getItemProperty(SIMPLE_NAME)
  206. .setValue(getSimpleName(sampleData[i]));
  207. item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME)
  208. .setValue(reverse(sampleData[i]));
  209. item.getItemProperty(ID_NUMBER).setValue(i % 2);
  210. }
  211. }
  212. protected void testRemoveHierarchicalWrapperSubtree(
  213. Container.Hierarchical container) {
  214. initializeContainer(container);
  215. // remove root item
  216. removeItemRecursively(container, "org");
  217. int packages = 21 + 3 - 3;
  218. int expectedSize = sampleData.length + packages - 1;
  219. validateContainer(container, "com", "com.vaadin.util.SerializerHelper",
  220. "com.vaadin.server.ApplicationResource", "blah", true,
  221. expectedSize);
  222. // rootItemIds
  223. Collection<?> rootIds = container.rootItemIds();
  224. assertEquals(1, rootIds.size());
  225. }
  226. private void removeItemRecursively(Container.Hierarchical container,
  227. Object itemId) {
  228. if (container instanceof ContainerHierarchicalWrapper) {
  229. ((ContainerHierarchicalWrapper) container)
  230. .removeItemRecursively("org");
  231. } else {
  232. HierarchicalContainer.removeItemRecursively(container, itemId);
  233. }
  234. }
  235. }