1 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 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 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 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 { 76 81 } 82 83 84 public void testUsuallyAllNodesAreGCedAtOnce () throws Exception { 85 Children.Keys k = creator.createChildren (); 86 87 88 k.setKeys (new String [] { "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 t) { 109 } 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 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 ev) { 143 } 144 145 147 private interface Creator { 148 public Children.Keys createChildren (); 149 } 150 } 151 | Popular Tags |