KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > LookNodeGCTest


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.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 JavaDoc RED = "Red";
34     private static String JavaDoc GREEN = "Green";
35     private static String JavaDoc BLUE = "Blue";
36
37     private static List allObjects = Arrays.asList( new String JavaDoc[] { RED, GREEN, BLUE } );
38
39     protected void setUp() throws Exception JavaDoc {
40         super.setUp();
41         TestUtil.setUpRegistryToDefault();
42     }
43     
44     public LookNodeGCTest(java.lang.String JavaDoc testName) {
45         super(testName);
46     }
47     
48     public static void main(java.lang.String JavaDoc[] args) {
49         junit.textui.TestRunner.run(new NbTestSuite (LookNodeGCTest.class));
50     }
51     
52     /** Tests whether nodes are garbage collecte and detachFrom is called
53      */

54     public void testNodeGC() throws Exception JavaDoc {
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             // Substitutes should still exist
73
assertEquals( "No attached objects", 3, look.attached.size() );
74             
75             // Now forgot about the nodes
76
nodes = null;
77             
78             // Nodes should dissapear
79
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     /** Tests whether substitutes are not listed when they change look.
94      */

95     
96     public void testChangeOfLook () throws Exception JavaDoc {
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     // Private methods ---------------------------------------------------------
124

125     /** Waits until clear method on test reference is called
126      */

127     
128     private static void waitForTestRef() throws InterruptedException JavaDoc {
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     // Inner classes -----------------------------------------------------------
150

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 JavaDoc representedObject) {
160             attached.add( this );
161         }
162         
163         public void detachFrom(Object JavaDoc representedObject) {
164             assertNotNull( "Represented objec is null.", representedObject );
165             attached.remove( this );
166         }
167         
168         public String JavaDoc getDisplayName () {
169             return "MyLook";
170         }
171         
172     }
173     
174         
175     private static class TestReference extends WeakReference implements Runnable JavaDoc {
176         
177         boolean[] flag;
178         
179         public TestReference( Object JavaDoc 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