1 19 20 package org.openide.explorer.view; 21 22 import java.awt.Dimension ; 23 import java.awt.EventQueue ; 24 import java.awt.event.ActionEvent ; 25 import java.beans.PropertyVetoException ; 26 import java.lang.ref.WeakReference ; 27 import java.lang.reflect.InvocationTargetException ; 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.JFrame ; 31 import javax.swing.JOptionPane ; 32 import javax.swing.JScrollBar ; 33 import javax.swing.JScrollPane ; 34 import org.netbeans.junit.NbTestCase; 35 import org.openide.explorer.ExplorerManager; 36 import org.openide.nodes.*; 37 38 42 public final class TreeNodeLeakTest extends NbTestCase { 43 44 private TreeView treeView; 45 private ExplorerWindow testWindow; 46 private Node toSelect[] = new Node[6]; 47 48 public TreeNodeLeakTest(String testName) { 49 super(testName); 50 } 51 52 53 private static Node createNode(String name, Node ... sub) { 54 Children ch = Children.LEAF; 55 if (sub != null) { 56 ch = new Children.Array(); 57 ch.add(sub); 58 } 59 AbstractNode n = new AbstractNode(ch); 60 n.setName(name); 61 return n; 62 } 63 64 67 public void testNodesLeak() throws Exception { 68 assert !EventQueue.isDispatchThread(); 69 final Node root = createNode("Root", 70 toSelect[0] = createNode("ch1", 71 toSelect[1] = createNode("A", (Node[])null), 72 toSelect[2] = createNode("B", (Node[])null)), 73 toSelect[3] = createNode("ch2", 74 toSelect[4] = createNode("A", (Node[])null), 75 toSelect[5] = createNode("B", (Node[])null)), 76 createNode("ch3", 77 createNode("A", (Node[])null), 78 createNode("B", (Node[])null)) 79 ); 80 EventQueue.invokeAndWait(new Runnable () { public void run() { 81 treeView = new BeanTreeView(); 82 testWindow = new ExplorerWindow(); 83 testWindow.getContentPane().add(treeView); 84 testWindow.pack(); 85 testWindow.setVisible(true); 86 testWindow.getExplorerManager().setRootContext(root); 87 try { 88 testWindow.getExplorerManager().setSelectedNodes(toSelect); 89 } catch (PropertyVetoException pve) { 90 fail(pve.getMessage()); 91 } 92 root.getChildren().remove( new Node[] {toSelect[0], toSelect[3]}); 93 }}); 94 EventQueue.invokeAndWait(new Runnable () { public void run() {}}); 95 96 WeakReference wr = new WeakReference (toSelect[0]); 97 toSelect = null; 98 assertGC("Node freed", wr); 99 } 100 101 102 private static final class ExplorerWindow extends JFrame 103 implements ExplorerManager.Provider { 104 105 private final ExplorerManager explManager = new ExplorerManager(); 106 107 ExplorerWindow() { 108 super("TreeView test"); } 110 111 public ExplorerManager getExplorerManager() { 112 return explManager; 113 } 114 115 } 116 } 117 | Popular Tags |