KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.textui.TestRunner;
23 import org.openide.ErrorManager;
24
25 import org.openide.filesystems.*;
26 import org.openide.util.Lookup;
27 import java.io.IOException JavaDoc;
28 import java.util.*;
29 import org.netbeans.junit.*;
30 import java.beans.PropertyChangeListener JavaDoc;
31
32 /** Simulates the deadlock from issue 35847.
33  * @author Jaroslav Tulach
34  */

35 public class Deadlock35847Test extends LoggingTestCaseHid {
36     private ErrorManager err;
37
38     public Deadlock35847Test(String JavaDoc name) {
39         super(name);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43         clearWorkDir();
44         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
45         registerIntoLookup(new Pool());
46     }
47     
48     public void testLoaderThatStopsToRecognizeWhatItHasRecognized () throws Exception JavaDoc {
49         ForgetableLoader l = (ForgetableLoader)ForgetableLoader.getLoader(ForgetableLoader.class);
50         
51         FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), new String JavaDoc[] {
52             "folder/f.forget",
53             "folder/f.keep"
54         });
55
56         // do not recognize anything
57
l.forget = true;
58
59         FileObject fo = lfs.findResource("folder");
60         DataFolder f = DataFolder.findFolder(fo);
61
62
63         DataObject[] arr = f.getChildren ();
64         assertEquals ("Two child there", 2, arr.length);
65
66         DataObject keep;
67         java.lang.ref.WeakReference JavaDoc forget;
68         if (arr[0].getPrimaryFile().hasExt ("keep")) {
69             keep = arr[0];
70             forget = new java.lang.ref.WeakReference JavaDoc (arr[1]);
71         } else {
72             keep = arr[1];
73             forget = new java.lang.ref.WeakReference JavaDoc (arr[0]);
74         }
75
76         org.openide.nodes.Node theDelegate = new org.openide.nodes.FilterNode (keep.getNodeDelegate());
77
78         arr = null;
79         assertGC ("Forgetable object can be forgeted", forget);
80
81         class P extends org.openide.nodes.NodeAdapter
82         implements java.beans.PropertyChangeListener JavaDoc {
83             int cnt;
84             String JavaDoc name;
85
86             public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
87                 name = ev.getPropertyName();
88                 cnt++;
89                 err.log("Event arrived: " + ev.getPropertyName());
90             }
91         }
92         P listener = new P ();
93         keep.addPropertyChangeListener (listener);
94         // in order to trigger listening on the original node and cause deadlock
95
theDelegate.addNodeListener(listener);
96
97         // now recognize
98
l.forget = false;
99
100         // this will trigger invalidation of keep from Folder Recognizer Thread
101
err.log("Beging to get children");
102         DataObject[] newArr = f.getChildren ();
103         err.log("End of get children");
104
105         assertEquals ("Keep is Invalidated", 1, listener.cnt);
106         assertEquals ("Property is PROP_VALID", DataObject.PROP_VALID, listener.name);
107     }
108     
109     public void testLoaderThatStopsToRecognizeWhatItHasRecognizedAndDoesItWhileHoldingChildrenMutex () throws Exception JavaDoc {
110         org.openide.nodes.Children.MUTEX.readAccess (new org.openide.util.Mutex.ExceptionAction () {
111             public Object JavaDoc run () throws Exception JavaDoc {
112                 testLoaderThatStopsToRecognizeWhatItHasRecognized ();
113                 return null;
114             }
115         });
116     }
117     
118
119     public static final class ForgetableLoader extends MultiFileLoader {
120         public boolean forget;
121         
122         public ForgetableLoader () {
123             super(MultiDataObject.class);
124         }
125         protected String JavaDoc displayName() {
126             return "ForgetableLoader";
127         }
128         /** Recognizes just two files - .forget and .keep at once, only in non-forgetable mode
129          */

130         protected FileObject findPrimaryFile(FileObject fo) {
131             if (forget) {
132                 return null;
133             }
134             if (fo.hasExt ("forget")) {
135                 return FileUtil.findBrother (fo, "keep");
136             }
137             if (fo.hasExt ("keep")) {
138                 return fo;
139             }
140             return null;
141         }
142         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
143             return new MultiDataObject (primaryFile, this);
144         }
145         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
146             return new FileEntry (obj, primaryFile);
147         }
148         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
149             return new FileEntry(obj, secondaryFile);
150         }
151     }
152     private static final class Pool extends DataLoaderPool {
153         public Pool() {
154         }
155         
156         public Enumeration loaders() {
157             ForgetableLoader l = (ForgetableLoader)ForgetableLoader.getLoader(ForgetableLoader.class);
158             return org.openide.util.Enumerations.singleton(l);
159         }
160     }
161 }
162
Popular Tags