KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > ChildrenKeysGarbageCollectTest


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.nodes;
21
22 import java.lang.ref.*;
23 import java.util.*;
24 import org.openide.ErrorManager;
25 import junit.framework.*;
26 import org.netbeans.junit.*;
27
28 public class ChildrenKeysGarbageCollectTest extends NbTestCase
29 implements NodeListener {
30     private Creator creator;
31     private int nodeDestroyed;
32
33     public ChildrenKeysGarbageCollectTest(java.lang.String JavaDoc testName) {
34         super(testName);
35     }
36
37     public static Test suite () {
38         NbTestSuite general = new NbTestSuite ("General GC test suite");
39
40         general.addTest (createSuite ("Unlimited keys", new Creator () {
41             public Children.Keys createChildren () {
42                 return new ChildrenKeysTest.Keys ();
43             }
44         }));
45         general.addTest (createSuite ("1-1 keys", new Creator () {
46             public Children.Keys createChildren () {
47                 return new ChildrenKeysTest.Keys (1, 1);
48             }
49         }));
50         general.addTest (createSuite ("0-1 keys", new Creator () {
51             public Children.Keys createChildren () {
52                 return new ChildrenKeysTest.Keys (0, 1);
53             }
54         }));
55         
56         return general;
57     }
58     
59     private static NbTestSuite createSuite (String JavaDoc n, Creator c) {
60         NbTestSuite s = new NbTestSuite (ChildrenKeysGarbageCollectTest.class);
61         s.setName (n);
62         Enumeration e = s.tests ();
63         while (e.hasMoreElements ()) {
64             Object JavaDoc o = e.nextElement ();
65             if (o instanceof ChildrenKeysGarbageCollectTest) {
66                 ChildrenKeysGarbageCollectTest t = (ChildrenKeysGarbageCollectTest)o;
67                 t.creator = c;
68             } else {
69                 fail ("o is supposed to be of correct instance: " + o);
70             }
71         }
72         return s;
73     }
74
75     protected void setUp () throws Exception JavaDoc {
76         /*
77         System.setProperty("org.openide.util.Lookup", "org.openide.nodes.ChildrenGarbageCollectTest$Lkp");
78         assertNotNull ("ErrManager has to be in lookup", org.openide.util.Lookup.getDefault ().lookup (ErrManager.class));
79         ErrManager.messages.delete (0, ErrManager.messages.length ());
80          */

81     }
82
83     
84     public void testUsuallyAllNodesAreGCedAtOnce () throws Exception JavaDoc {
85         Children.Keys k = creator.createChildren ();
86         
87         
88         k.setKeys (new String JavaDoc[] { "1", "3", "4" });
89         
90         doFullOperationOnNodes (k);
91     }
92     
93     private void doFullOperationOnNodes (Children.Keys k) {
94         Node[] arr = k.getNodes ();
95         
96         assertEquals ("Three", 3, arr.length);
97         
98         WeakReference[] refs = new WeakReference[arr.length];
99         for (int i = 0; i < arr.length; i++) {
100             refs[i] = new WeakReference (arr[i]);
101             arr[i].addNodeListener (this);
102         }
103         
104         arr[0] = null;
105         try {
106             assertGC ("Try to gc only one node", refs[0]);
107             fail ("This should not succeed, as others are still referenced");
108         } catch (Throwable JavaDoc t) {
109             // ok
110
}
111         arr[1] = arr[2] = null;
112         
113         assertGC ("Now we gc all", refs[0]);
114         assertGC ("#2", refs[1]);
115         assertGC ("#3", refs[2]);
116         
117         assertEquals ("Still 3 children", 3, k.getNodesCount ());
118         assertEquals ("No nodes notified to be destoryed", 0, nodeDestroyed);
119
120         arr = k.getNodes ();
121         assertEquals ("Again three", 3, arr.length);
122     }
123     
124     
125     //
126
// Listener methods
127
//
128

129     public void childrenAdded (NodeMemberEvent ev) {
130     }
131
132     public void childrenRemoved (NodeMemberEvent ev) {
133     }
134
135     public void childrenReordered (NodeReorderEvent ev) {
136     }
137
138     public void nodeDestroyed (NodeEvent ev) {
139         nodeDestroyed++;
140     }
141     
142     public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
143     }
144     
145     /** Factory for creating
146      */

147     private interface Creator {
148         public Children.Keys createChildren ();
149     }
150 }
151
Popular Tags