KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > TreeNodeLeakTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.view;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.JFrame JavaDoc;
31 import javax.swing.JOptionPane JavaDoc;
32 import javax.swing.JScrollBar JavaDoc;
33 import javax.swing.JScrollPane JavaDoc;
34 import org.netbeans.junit.NbTestCase;
35 import org.openide.explorer.ExplorerManager;
36 import org.openide.nodes.*;
37
38 /**
39  * A test covering JDK issue 6472844 and its NetBeans workaround
40  * @author Petr Nejedly
41  */

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 JavaDoc testName) {
49         super(testName);
50     }
51     
52
53     private static Node createNode(String JavaDoc 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     /**
65      * @see http://www.netbeans.org/issues/show_bug.cgi?id=84970
66      */

67     public void testNodesLeak() throws Exception JavaDoc {
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 JavaDoc() { 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 JavaDoc pve) {
90                 fail(pve.getMessage());
91             }
92             root.getChildren().remove( new Node[] {toSelect[0], toSelect[3]});
93         }});
94         EventQueue.invokeAndWait(new Runnable JavaDoc() { public void run() {}});
95
96         WeakReference JavaDoc wr = new WeakReference JavaDoc(toSelect[0]);
97         toSelect = null;
98         assertGC("Node freed", wr);
99     }
100     
101     
102     private static final class ExplorerWindow extends JFrame JavaDoc
103                                implements ExplorerManager.Provider {
104         
105         private final ExplorerManager explManager = new ExplorerManager();
106         
107         ExplorerWindow() {
108             super("TreeView test"); //NOI18N
109
}
110         
111         public ExplorerManager getExplorerManager() {
112             return explManager;
113         }
114         
115     }
116 }
117
Popular Tags