diff options
author | John Ahlroos <john@vaadin.com> | 2013-02-21 15:55:57 +0200 |
---|---|---|
committer | John Ahlroos <john@vaadin.com> | 2013-02-21 15:55:57 +0200 |
commit | 834275130dce0fee314e2c5b0b092579ba3905a4 (patch) | |
tree | 04afb32714682eec56248016f5cf072a5a9f626e | |
parent | c2cd910f53bd55e3472b3eb11c32f0cd28e5ee8e (diff) | |
download | vaadin-framework-834275130dce0fee314e2c5b0b092579ba3905a4.tar.gz vaadin-framework-834275130dce0fee314e2c5b0b092579ba3905a4.zip |
Fixed memory leak in Tree #11053
Change-Id: I4c58367d4041b5e61989fca5be6b037f7f8e0c01
-rw-r--r-- | server/src/com/vaadin/ui/Tree.java | 17 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java | 41 |
2 files changed, 58 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/Tree.java b/server/src/com/vaadin/ui/Tree.java index 608c947d59..219b39020b 100644 --- a/server/src/com/vaadin/ui/Tree.java +++ b/server/src/com/vaadin/ui/Tree.java @@ -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); + } + } diff --git a/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java b/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java index 16a7091947..f467a42339 100644 --- a/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java @@ -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); + } } |