1 19 20 package org.netbeans.api.nodes2looks; 21 22 import java.lang.ref.*; 23 import java.util.*; 24 25 import org.netbeans.junit.*; 26 27 import org.netbeans.spi.looks.*; 28 import org.openide.nodes.Node; 29 import org.openide.util.Utilities; 30 31 public class LookNodeGCTest extends NbTestCase { 32 33 private static String RED = "Red"; 34 private static String GREEN = "Green"; 35 private static String BLUE = "Blue"; 36 37 private static List allObjects = Arrays.asList( new String [] { RED, GREEN, BLUE } ); 38 39 protected void setUp() throws Exception { 40 super.setUp(); 41 TestUtil.setUpRegistryToDefault(); 42 } 43 44 public LookNodeGCTest(java.lang.String testName) { 45 super(testName); 46 } 47 48 public static void main(java.lang.String [] args) { 49 junit.textui.TestRunner.run(new NbTestSuite (LookNodeGCTest.class)); 50 } 51 52 54 public void testNodeGC() throws Exception { 55 56 for (int cnt = 0; cnt < 5; cnt++) { 57 58 MyLook look = new MyLook (); 59 60 Node nodes[] = { 61 Nodes.node ( RED, look), 62 Nodes.node ( GREEN, look), 63 Nodes.node ( BLUE, look) 64 }; 65 66 WeakReference nodesWeak[] = { 67 new WeakReference( nodes[0] ), 68 new WeakReference( nodes[1] ), 69 new WeakReference( nodes[2] ), 70 }; 71 72 assertEquals( "No attached objects", 3, look.attached.size() ); 74 75 nodes = null; 77 78 assertGC( "Reference 0 not cleared", nodesWeak[0] ); 80 assertGC( "Reference 1 not cleared", nodesWeak[1] ); 81 assertGC( "Reference 2 not cleared", nodesWeak[2] ); 82 83 84 waitForTestRef(); 85 86 assertEquals( "Substitutes not cleared", 0, look.attached.size() ); 87 88 } 89 90 } 91 92 93 95 96 public void testChangeOfLook () throws Exception { 97 98 MyLook lookOne = new MyLook (); 99 MyLook lookTwo = new MyLook (); 100 101 Node ln = Nodes.node ( RED, lookOne ); 102 103 assertEquals( "Look one should have one assertion", 1, lookOne.attached.size() ); 104 assertEquals( "Look one should have zero assertions", 0, lookTwo.attached.size() ); 105 106 TestUtil.setLook( ln, lookTwo ); 107 108 waitForTestRef(); 109 110 assertEquals( "Look one should have zero assertion", 0, lookOne.attached.size() ); 111 assertEquals( "Look one should have one assertions", 1, lookTwo.attached.size() ); 112 113 TestUtil.setLook( ln, lookOne ); 114 115 waitForTestRef(); 116 117 assertEquals( "Look one should have one assertion", 1, lookOne.attached.size() ); 118 assertEquals( "Look one should have zero assertions", 0, lookTwo.attached.size() ); 119 120 } 121 122 123 125 127 128 private static void waitForTestRef() throws InterruptedException { 129 boolean flag[] = { false }; 130 Reference tRef = new TestReference( "Ahoj", Utilities.activeReferenceQueue(), flag ); 131 132 tRef.enqueue(); 133 tRef.clear(); 134 135 for( long startTime = System.currentTimeMillis(); System.currentTimeMillis() - startTime < 3000; ) { 136 System.gc(); 137 if ( flag[0] ) { 138 break; 139 } 140 } 141 142 Thread.sleep( 100 ); 143 144 if ( !flag[0] ) { 145 fail( "Test reference not cleared" ); 146 } 147 } 148 149 151 private static class MyLook extends DefaultLook { 152 153 List attached = new ArrayList(); 154 155 public MyLook() { 156 super( "MyLook" ); 157 } 158 159 public void attachTo(Object representedObject) { 160 attached.add( this ); 161 } 162 163 public void detachFrom(Object representedObject) { 164 assertNotNull( "Represented objec is null.", representedObject ); 165 attached.remove( this ); 166 } 167 168 public String getDisplayName () { 169 return "MyLook"; 170 } 171 172 } 173 174 175 private static class TestReference extends WeakReference implements Runnable { 176 177 boolean[] flag; 178 179 public TestReference( Object object, ReferenceQueue q, boolean flag[] ) { 180 super( object, q ); 181 this.flag = flag; 182 this.flag[0] = false; 183 } 184 185 public void run() { 186 flag[0] = true; 187 } 188 189 } 190 191 } 192 | Popular Tags |