]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed memory leak in Tree #11053
authorJohn Ahlroos <john@vaadin.com>
Thu, 21 Feb 2013 13:55:57 +0000 (15:55 +0200)
committerJohn Ahlroos <john@vaadin.com>
Thu, 21 Feb 2013 13:55:57 +0000 (15:55 +0200)
Change-Id: I4c58367d4041b5e61989fca5be6b037f7f8e0c01

server/src/com/vaadin/ui/Tree.java
server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java

index 608c947d5944ee8a8b28b82d3125a60b10140537..219b39020b1b9a8986f1dfc18482b4d1910dd146 100644 (file)
@@ -1670,4 +1670,21 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
         return itemDescriptionGenerator;
     }
 
+    @Override
+    public void containerItemSetChange(
+            com.vaadin.data.Container.ItemSetChangeEvent event) {
+
+        // Ensure removed items are cleaned up from expanded list
+        for (Object expandedItemId : expanded) {
+            if (getItem(expandedItemId) == null) {
+                expanded.remove(expandedItemId);
+                if (this.expandedItemId == expandedItemId) {
+                    this.expandedItemId = null;
+                }
+            }
+        }
+
+        super.containerItemSetChange(event);
+    }
+
 }
index 16a7091947a0b09673a9b3d40922d61ed4490a97..f467a42339ebf0db2cd44d33df07999b956311a7 100644 (file)
@@ -1,5 +1,8 @@
 package com.vaadin.tests.server.component.tree;
 
+import java.lang.reflect.Field;
+import java.util.HashSet;
+
 import junit.framework.TestCase;
 
 import com.vaadin.data.Container;
@@ -71,4 +74,42 @@ public class TreeTest extends TestCase {
         assertTrue(Container.Hierarchical.class.isAssignableFrom(tree4
                 .getContainerDataSource().getClass()));
     }
+
+    public void testRemoveExpandedItems() throws Exception {
+        tree.expandItem("parent");
+        tree.expandItem("child");
+
+        Field expandedField = tree.getClass()
+                .getDeclaredField("expanded");
+        Field expandedItemIdField = tree.getClass().getDeclaredField(
+                "expandedItemId");
+
+        expandedField.setAccessible(true);
+        expandedItemIdField.setAccessible(true);
+
+        HashSet<Object> expanded = (HashSet<Object>) expandedField.get(tree);
+        Object expandedItemId = expandedItemIdField.get(tree);
+
+        assertEquals(2, expanded.size());
+        assertTrue("Contains parent", expanded.contains("parent"));
+        assertTrue("Contains child", expanded.contains("child"));
+        assertEquals("child", expandedItemId);
+
+        tree.removeItem("parent");
+
+        expanded = (HashSet<Object>) expandedField.get(tree);
+        expandedItemId = expandedItemIdField.get(tree);
+
+        assertEquals(1, expanded.size());
+        assertTrue("Contains child", expanded.contains("child"));
+        assertEquals("child", expandedItemId);
+
+        tree.removeItem("child");
+
+        expanded = (HashSet<Object>) expandedField.get(tree);
+        expandedItemId = expandedItemIdField.get(tree);
+
+        assertEquals(0, expanded.size());
+        assertNull(expandedItemId);
+    }
 }