KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > FolderChildrenGCRaceConditionTest


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.loaders;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.*;
26 import java.lang.ref.WeakReference JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.openide.ErrorManager;
30
31 import org.openide.filesystems.*;
32
33 import org.netbeans.junit.*;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.Children;
36 import org.openide.util.RequestProcessor;
37
38
39 public class FolderChildrenGCRaceConditionTest extends LoggingTestCaseHid {
40     public FolderChildrenGCRaceConditionTest() {
41         super("");
42     }
43     
44     public FolderChildrenGCRaceConditionTest(java.lang.String JavaDoc testName) {
45         super(testName);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50         clearWorkDir();
51
52         FileObject[] arr = Repository.getDefault().getDefaultFileSystem().getRoot().getChildren();
53         for (int i = 0; i < arr.length; i++) {
54             arr[i].delete();
55         }
56     }
57     
58     public void testChildrenCanBeSetToNullIfGCKicksIn () throws Exception JavaDoc {
59         FileObject f = FileUtil.createData(Repository.getDefault().getDefaultFileSystem().getRoot(), "folder/node.txt");
60         
61         DataFolder df = DataFolder.findFolder(f.getParent());
62         Node n = df.getNodeDelegate();
63         
64         Node[] arr = n.getChildren().getNodes(true);
65         assertEquals("Ok, one", 1, arr.length);
66         final WeakReference JavaDoc ref = new WeakReference JavaDoc(arr[0]);
67         arr = null;
68         
69         class R implements Runnable JavaDoc {
70             public void run() {
71                 ErrorManager.getDefault().log("Ready to GC");
72                 assertGC("Node can go away in the worst possible moment", ref);
73                 ErrorManager.getDefault().log("Gone");
74             }
75         }
76         R r = new R();
77         RequestProcessor.Task t = new RequestProcessor("Inter", 1, true).post(r);
78         
79         registerSwitches(
80             "THREAD:FolderChildren_Refresh MSG:Children computed" +
81             "THREAD:FolderChildren_Refresh MSG:notifyFinished.*" +
82             "THREAD:Inter MSG:Gone.*" +
83             "THREAD:Finalizer MSG:RMV.*" +
84             "THREAD:FolderChildren_Refresh MSG:Clearing the ref.*" +
85             "", 200);
86         
87         int cnt = n.getChildren().getNodes(true).length;
88         
89         t.cancel();
90         
91         assertEquals("Count is really one", 1, cnt);
92     }
93    
94 }
Popular Tags