KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 import org.openide.filesystems.*;
31
32 import org.netbeans.junit.*;
33 import org.openide.nodes.Node;
34 import org.openide.nodes.Children;
35
36
37 public class FolderChildrenTest extends LoggingTestCaseHid {
38     public FolderChildrenTest() {
39         super("");
40     }
41
42     public FolderChildrenTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45     
46     private static void setSystemProp(String JavaDoc key, String JavaDoc value) {
47         java.util.Properties JavaDoc prop = System.getProperties();
48         if (prop.get(key) != null) return;
49         prop.put(key, value);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54         clearWorkDir();
55         setSystemProp("netbeans.security.nocheck","true");
56         
57         FileObject[] arr = Repository.getDefault().getDefaultFileSystem().getRoot().getChildren();
58         for (int i = 0; i < arr.length; i++) {
59             arr[i].delete();
60         }
61     }
62     
63     public void testSimulateADeadlockThatWillBeFixedByIssue49459 () throws Exception JavaDoc {
64         FileSystem fs = Repository.getDefault ().getDefaultFileSystem();
65         FileObject a = FileUtil.createData (fs.getRoot (), "XYZ49459/org-openide-loaders-FolderChildrenTest$N1.instance");
66         FileObject bb = fs.findResource("/XYZ49459");
67         assertNotNull (bb);
68         
69         class Run implements Runnable JavaDoc {
70             private boolean read;
71             private DataFolder folder;
72             
73             public Node[] children;
74             
75             public Run (DataFolder folder) {
76                 this.folder = folder;
77             }
78             
79             public void run () {
80                 if (!read) {
81                     read = true;
82                     Children.MUTEX.readAccess (this);
83                     return;
84                 }
85         
86                 
87                 // this will deadlock without fix #49459
88
children = folder.getNodeDelegate ().getChildren ().getNodes (true);
89                 
90             }
91         }
92         
93         Run r = new Run (DataFolder.findFolder (bb));
94         Children.MUTEX.writeAccess (r);
95         
96         assertNotNull ("Children filled", r.children);
97         assertEquals ("But are empty as cannot wait under getNodes", 0, r.children.length);
98         
99         // try once more without the locks
100
r.children = null;
101         r.run ();
102         assertNotNull ("But running without mutexs works better - children filled", r.children);
103         assertEquals ("One child", 1, r.children.length);
104         DataObject obj = (DataObject)r.children[0].getCookie (DataObject.class);
105         assertNotNull ("There is data object", obj);
106         assertEquals ("It belongs to our file", a, obj.getPrimaryFile ());
107     }
108     
109     public void testAdditionOfNewFileDoesNotInfluenceAlreadyExistingLoaders ()
110     throws Exception JavaDoc {
111         FileSystem fs = Repository.getDefault ().getDefaultFileSystem();
112         FileUtil.createData (fs.getRoot (), "AA/org-openide-loaders-FolderChildrenTest$N1.instance");
113         FileUtil.createData (fs.getRoot (), "AA/org-openide-loaders-FolderChildrenTest$N2.instance");
114         
115         FileObject bb = fs.findResource("/AA");
116         
117         DataFolder folder = DataFolder.findFolder (bb);
118         Node node = folder.getNodeDelegate();
119         
120         Node[] arr = node.getChildren ().getNodes (true);
121         assertEquals ("There is a nodes for both", 2, arr.length);
122         assertNotNull ("First one is our node", arr[0].getCookie (N1.class));
123         
124         FileObject n = bb.createData ("A.txt");
125         Node[] newarr = node.getChildren ().getNodes (true);
126         assertEquals ("There is new node", 3, newarr.length);
127         
128         n.delete ();
129         
130         Node[] last = node.getChildren ().getNodes (true);
131         assertEquals ("Again they are two", 2, last.length);
132         
133         assertEquals ("First one is the same", last[0], arr[0]);
134         assertEquals ("Second one is the same", last[1], arr[1]);
135         
136     }
137     
138     public void testChangeableDataFilter() throws Exception JavaDoc {
139         FileSystem fs = Repository.getDefault ().getDefaultFileSystem();
140         FileUtil.createData (fs.getRoot (), "BB/A.txt");
141         FileUtil.createData (fs.getRoot (), "BB/B.txt");
142         FileUtil.createData (fs.getRoot (), "BB/AA.txt");
143         FileUtil.createData (fs.getRoot (), "BB/BA.txt");
144         
145         
146         FileObject bb = fs.findResource("/BB");
147         
148         Filter filter = new Filter();
149         DataFolder folder = DataFolder.findFolder (bb);
150         
151         Children ch = folder.createNodeChildren( filter );
152         Node[] arr = ch.getNodes (true);
153         
154         assertNodes( arr, new String JavaDoc[] { "A.txt", "AA.txt" } );
155         filter.fire();
156         arr = ch.getNodes (true);
157         assertNodes( arr, new String JavaDoc[] { "B.txt", "BA.txt" } );
158         
159     }
160     
161     public void testChildrenCanGC () throws Exception JavaDoc {
162         Filter filter = new Filter ();
163         
164         FileSystem fs = Repository.getDefault ().getDefaultFileSystem();
165         FileObject bb = FileUtil.createFolder(fs.getRoot(), "/BB");
166     bb.createData("Ahoj.txt");
167     bb.createData("Hi.txt");
168         DataFolder folder = DataFolder.findFolder (bb);
169            
170         Children ch = folder.createNodeChildren( filter );
171         Node[] arr = ch.getNodes (true);
172     assertEquals("Accepts only Ahoj", 1, arr.length);
173         
174         WeakReference JavaDoc ref = new WeakReference JavaDoc (ch);
175         ch = null;
176         arr = null;
177         
178         assertGC ("Children can disappear even we hold the filter", ref);
179     }
180     
181     public void testSeemsLikeTheAbilityToRefreshIsBroken() throws Exception JavaDoc {
182         FileSystem fs = Repository.getDefault ().getDefaultFileSystem();
183         FileObject bb = FileUtil.createFolder(fs.getRoot(), "/BB");
184     bb.createData("Ahoj.txt");
185     bb.createData("Hi.txt");
186     
187         DataFolder folder = DataFolder.findFolder (bb);
188     
189     Node n = folder.getNodeDelegate();
190     Node[] arr = n.getChildren().getNodes(true);
191     assertEquals("Both are visible", 2, arr.length);
192     
193     WeakReference JavaDoc ref = new WeakReference JavaDoc(arr[0]);
194     arr = null;
195     assertGC("Nodes can disappear", ref);
196     
197     
198     bb.createData("Third.3rd");
199     
200     arr = n.getChildren().getNodes(true);
201     assertEquals("All are visbile ", 3, arr.length);
202     }
203
204     
205     public static class N1 extends org.openide.nodes.AbstractNode
206     implements Node.Cookie {
207         public N1 () {
208             this (true);
209         }
210         
211         private N1 (boolean doGc) {
212             super (org.openide.nodes.Children.LEAF);
213             
214             if (doGc) {
215                 for (int i = 0; i < 5; i++) {
216                     System.gc ();
217                 }
218             }
219         }
220         
221         public Node cloneNode () {
222             return new N1 (false);
223         }
224         
225         public Node.Cookie getCookie (Class JavaDoc c) {
226             if (c == getClass ()) {
227                 return this;
228             }
229             return null;
230         }
231     }
232     
233     public static final class N2 extends N1 {
234     }
235
236     
237     private void assertNodes( Node[] nodes, String JavaDoc names[] ) {
238         
239         assertEquals( "Wrong number of nodes.", names.length, nodes.length );
240         
241         for( int i = 0; i < nodes.length; i++ ) {
242             assertEquals( "Wrong name at index " + i + ".", names[i], nodes[i].getName() );
243         }
244         
245     }
246     
247     private static class Filter implements ChangeableDataFilter {
248
249         private boolean selectA = true;
250                     
251         ArrayList listeners = new ArrayList();
252         
253         public boolean acceptDataObject (DataObject obj) {
254             String JavaDoc fileName = obj.getPrimaryFile().getName();
255             boolean select = fileName.startsWith( "A" );
256             select = selectA ? select : !select;
257             return select;
258         }
259         
260         public void addChangeListener( ChangeListener JavaDoc listener ) {
261             listeners.add( listener );
262         }
263         
264         public void removeChangeListener( ChangeListener JavaDoc listener ) {
265             listeners.remove( listener );
266         }
267         
268         public void fire( ) {
269         
270             selectA = !selectA;
271             
272             ChangeEvent JavaDoc che = new ChangeEvent JavaDoc( this );
273             
274             for( Iterator it = listeners.iterator(); it.hasNext(); ) {
275                 ChangeListener JavaDoc chl = (ChangeListener JavaDoc)it.next();
276                 chl.stateChanged( che );
277             }
278         }
279         
280     }
281     
282     public void testChildrenListenToFilesystemByABadea () throws Exception JavaDoc {
283         doChildrenListenToFilesystem (false);
284     }
285     public void testChildrenListenToFileByABadea () throws Exception JavaDoc {
286         doChildrenListenToFilesystem (true);
287     }
288         
289     private void doChildrenListenToFilesystem (boolean useFileObject) throws Exception JavaDoc {
290         
291         final Object JavaDoc waitObj = new Object JavaDoc();
292         
293         class MyFileChangeListener implements FileChangeListener {
294             boolean created;
295             
296             public void fileFolderCreated(FileEvent fe) {}
297             public void fileChanged(FileEvent fe) {}
298             public void fileDeleted(FileEvent fe) {}
299             public void fileRenamed(FileRenameEvent fe) {}
300             public void fileAttributeChanged(FileAttributeEvent fe) {}
301             public void fileDataCreated(FileEvent e) {
302                 synchronized (waitObj) {
303                     created = true;
304                     waitObj.notify();
305                 }
306             }
307         }
308         
309         final String JavaDoc FILE_NAME = "C.txt";
310         
311         MyFileChangeListener fcl = new MyFileChangeListener();
312         
313         
314         
315         LocalFileSystem fs = new LocalFileSystem();
316         fs.setRootDirectory(getWorkDir());
317         Repository.getDefault().addFileSystem(fs);
318         final FileObject workDir = FileUtil.createFolder (fs.getRoot(), "workFolder");
319         final FileObject sibling = FileUtil.createFolder (fs.getRoot (), "unimportantSibling");
320         
321         workDir.addFileChangeListener(fcl);
322         
323         DataFolder workDirDo = DataFolder.findFolder(workDir);
324         FolderChildren fc = new FolderChildren(workDirDo);
325         
326         // init the FolderChildren
327
fc.getNodes();
328         
329         File JavaDoc newFile;
330         
331         if (useFileObject) {
332             FileObject newFo = FileUtil.createData (workDir, FILE_NAME);
333             newFile = FileUtil.toFile(newFo);
334         } else {
335             newFile = new File JavaDoc(FileUtil.toFile(workDir), FILE_NAME);
336             new FileOutputStream JavaDoc(newFile).close();
337         }
338         
339         // first or second run (second run is after caling workDir.refresh())
340
boolean firstRun = true;
341         
342         synchronized (waitObj) {
343             
344             for(;;) {
345                 // wait for create notification
346
if (!fcl.created)
347                     waitObj.wait(5000);
348
349                 if (!fcl.created) {
350                     System.out.println("Not received file create notification, can't test.");
351                     if (firstRun) {
352                         // didn't get a notification, we should get one by calling refresh()
353
firstRun = false;
354                         workDir.refresh();
355                         continue;
356                     }
357                     else {
358                         // didn't get a notification even after second run
359
// FolderChildren probably didn't get a notification neither
360
// so it doesn't know anything about the new file => nothing to test
361
return;
362                     }
363                 } else {
364                     break;
365                 }
366             }
367             
368             // wait for FolderChildren to receive and process the create notification
369
int cnt = 10;
370             while (cnt-- > 0 && fc.getNodes ().length < 1) {
371                 try {
372                     Thread.sleep(300);
373                 }
374                 catch (InterruptedException JavaDoc e) {}
375             }
376             
377             assertEquals("FolderChildren doesn't contain " + newFile, 1, fc.getNodes().length);
378         }
379     }
380     
381 }
Popular Tags