KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectOutputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.filesystems.FileLock;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32 import org.openide.util.Lookup;
33 import org.openide.util.Lookup.Result;
34 import org.openide.util.lookup.AbstractLookup;
35 import org.openide.util.lookup.InstanceContent;
36 import org.openide.util.lookup.Lookups;
37 import org.openide.util.lookup.ProxyLookup;
38
39 public class FolderLookupTest extends NbTestCase {
40     
41     public FolderLookupTest(java.lang.String JavaDoc testName) {
42         super(testName);
43     }
44     
45     static {
46         System.setProperty ("org.openide.util.Lookup", GLkp.class.getName());
47     }
48     
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         clearWorkDir();
52     }
53     
54     /** Test of the lookup method. Creates a file under Services directory
55      * and tries to lookup it. The object should be immediatelly found.
56      *
57      */

58     public void testFolderLookupIsUpdatedQuickly () throws Exception JavaDoc {
59         String JavaDoc fsstruct [] = new String JavaDoc [] {
60             "AA/",
61         };
62         
63         TestUtilHid.destroyLocalFileSystem (getName());
64         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
65
66         FileObject bb = lfs.findResource("/AA");
67         
68         DataFolder folder = DataFolder.findFolder (bb);
69         
70         
71         Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();
72         try {
73             checkTheLookup (lookup, folder);
74         } finally {
75             folder.delete ();
76         }
77     }
78
79
80    private void checkTheLookup (Lookup lookup, DataFolder folder) throws Exception JavaDoc {
81         Class JavaDoc toFind = java.util.Dictionary JavaDoc.class;
82         Class JavaDoc toCreate = java.util.Hashtable JavaDoc.class;
83         
84         Object JavaDoc wrongResult = lookup.lookup (toFind);
85         DataObject obj = InstanceDataObject.create (folder, "Test", toCreate);
86         
87         if (lookup.lookup (toFind) == null) {
88             fail ("Lookup has not found the class");
89         }
90         obj.delete ();
91         
92         if (lookup.lookup (toFind) != null) {
93             fail ("Still it is possible to find the class");
94         }
95         if (wrongResult != null) {
96             // report the original mistake, means the a previous test forgot
97
// to clean after itself
98
fail ("There is uncleaned environment: " + wrongResult);
99         }
100         
101     }
102    
103     /** Test of the lookup method. Creates files under different levels of directory
104      * hierarchy and tries to lookup it. The objects should be immediatelly found.
105      *
106      */

107     public void testFolderLookupIsUpdatedQuicklyForSubfolders () throws Exception JavaDoc {
108         String JavaDoc fsstruct [] = new String JavaDoc [] {
109             "AA/",
110         };
111         
112         TestUtilHid.destroyLocalFileSystem (getName());
113         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
114
115         FileObject bb = lfs.findResource("/AA");
116         assertNotNull(bb + " not found", bb);
117         
118         DataFolder folder = DataFolder.findFolder (bb);
119         
120         
121         Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();
122         checkTheLookupForSubfolders (lookup, folder);
123     }
124     
125     private void checkTheLookupForSubfolders (Lookup lookup, DataFolder folder) throws Exception JavaDoc {
126         Class JavaDoc toFind = java.awt.Component JavaDoc.class;
127         Class JavaDoc toCreate = javax.swing.JButton JavaDoc.class;
128
129         Lookup.Result res = lookup.lookupResult(toFind);
130         assertEquals("no Component's in " + res.allInstances(), 0, res.allInstances().size());
131
132         DataObject obj = InstanceDataObject.create (folder, "Test", toCreate);
133         assertNotNull(obj.getPrimaryFile() + " not found",
134             folder.getPrimaryFile().getFileSystem().findResource(obj.getPrimaryFile().getPath()));
135         assertEquals("just one Component in " + res.allInstances(), 1, res.allInstances().size());
136
137         DataFolder subfolder = DataFolder.create(folder, "BB");
138         assertNotNull(subfolder.getPrimaryFile() + " not found",
139             folder.getPrimaryFile().getFileSystem().findResource(subfolder.getPrimaryFile().getPath()));
140         
141         obj = InstanceDataObject.create (subfolder, "Test", toCreate);
142         assertNotNull(obj.getPrimaryFile() + " not found",
143             folder.getPrimaryFile().getFileSystem().findResource(obj.getPrimaryFile().getPath()));
144         assertEquals("now two Component's in " + res.allInstances(), 2, res.allInstances().size());
145     }
146
147     /** Tests delegation stuff.
148      */

149     public void testProxyLookups () throws Exception JavaDoc {
150         String JavaDoc fsstruct [] = new String JavaDoc [] {
151             "AA/",
152         };
153         
154         TestUtilHid.destroyLocalFileSystem (getName());
155         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
156
157         FileObject bb = lfs.findResource("/AA");
158         
159         DataFolder folder = DataFolder.findFolder (bb);
160
161         InstanceDataObject obj = InstanceDataObject.create (folder, null, ALkp.class);
162         
163         Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();
164         
165         if (lookup.lookup (Integer JavaDoc.class) == null) {
166             fail ("Integer not found in delegating lookup");
167         }
168         
169     }
170     
171     public void testFindInstanceNotCreatedByYouIssue24986 () throws Exception JavaDoc {
172         String JavaDoc fsstruct [] = new String JavaDoc [] {
173             "AA/",
174         };
175         TestUtilHid.destroyLocalFileSystem (getName());
176         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
177
178         FileObject bb = lfs.findResource("/AA");
179         
180         String JavaDoc inst = "My instnace";
181         
182         DataFolder folder = DataFolder.findFolder (bb);
183         FileObject fo = FileUtil.createData (folder.getPrimaryFile (), "test.ser");
184         FileLock lock = fo.lock ();
185         ObjectOutputStream JavaDoc oss = new ObjectOutputStream JavaDoc (fo.getOutputStream (lock));
186         oss.writeObject (inst);
187         oss.close ();
188         lock.releaseLock ();
189         DataObject o = DataObject.find (fo);
190         assertTrue ("Is IDO: " + o, o instanceof InstanceDataObject);
191         InstanceDataObject obj = (InstanceDataObject)o;
192
193         assertEquals ("The instance is created", inst, obj.instanceCreate ());
194         assertNotSame ("But is not the same", inst, obj.instanceCreate ());
195         inst = (String JavaDoc)obj.instanceCreate ();
196         
197         Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();
198         
199         Lookup.Template t = new Lookup.Template (null, null, inst);
200         Collection JavaDoc found = lookup.lookup (t).allInstances ();
201         
202         assertEquals ("Lookup finds it as well", 1, found.size ());
203         assertEquals ("Lookup finds it as well", inst, found.iterator ().next());
204     }
205     
206     public void testDeadlockWhileWaitingForFolderRecognizerToFinish50768 () throws Exception JavaDoc {
207         /*
208          * Used to produce deadlock between main thread and folder recognizer.
209          * Similar is described in issue 50768.
210
211 "Folder recognizer" daemon prio=1 tid=0x08218478 nid=0x4f4b in Object.wait() [0x4e640000..0x4e640790]
212     at java.lang.Object.wait(Native Method)
213     - waiting on <0x45e41eb8> (a java.lang.Object)
214     at java.lang.Object.wait(Object.java:474)
215     at org.openide.util.lookup.AbstractLookup.enterStorage(AbstractLookup.java:102)
216     - locked <0x45e41eb8> (a java.lang.Object)
217     at org.openide.util.lookup.AbstractLookup.setPairsAndCollectListeners(AbstractLookup.java:223)
218     at org.openide.util.lookup.AbstractLookup.setPairs(AbstractLookup.java:213)
219     at org.openide.util.lookup.AbstractLookup$Content.setPairs(AbstractLookup.java:895)
220     at org.openide.loaders.FolderLookup$ProxyLkp.update(FolderLookup.java:336)
221     at org.openide.loaders.FolderLookup.createInstance(FolderLookup.java:170)
222     at org.openide.loaders.FolderInstance.defaultProcessObjects(FolderInstance.java:715)
223     at org.openide.loaders.FolderInstance.access$000(FolderInstance.java:68)
224     at org.openide.loaders.FolderInstance$2.run(FolderInstance.java:601)
225     at org.openide.loaders.FolderLookup.postCreationTask(FolderLookup.java:234)
226     at org.openide.loaders.FolderInstance.processObjects(FolderInstance.java:599)
227     at org.openide.loaders.FolderInstance$Listener.finished(FolderInstance.java:880)
228     at org.openide.loaders.FolderList.createObjects(FolderList.java:644)
229     at org.openide.loaders.FolderList.getObjects(FolderList.java:512)
230     at org.openide.loaders.FolderList.access$200(FolderList.java:50)
231     at org.openide.loaders.FolderList$ListTask.run(FolderList.java:877)
232     at org.openide.util.Task.run(Task.java:136)
233     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330)
234     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:686)
235
236
237 "main" prio=1 tid=0x0805c538 nid=0x4f35 in Object.wait() [0xbfffc000..0xbfffcf48]
238     at java.lang.Object.wait(Native Method)
239     - waiting on <0x459691d8> (a org.openide.util.RequestProcessor$Task)
240     at java.lang.Object.wait(Object.java:474)
241     at org.openide.util.Task.waitFinished(Task.java:85)
242     - locked <0x459691d8> (a org.openide.util.RequestProcessor$Task)
243     at org.openide.util.RequestProcessor$Task.waitFinished(RequestProcessor.java:447)
244     at org.openide.loaders.FolderList.waitProcessingFinished(FolderList.java:247)
245     at org.openide.loaders.FolderInstance.waitProcessingFinished(FolderInstance.java:555)
246     at org.openide.loaders.FolderInstance.waitFinished(FolderInstance.java:274)
247     at org.openide.loaders.FolderInstance.instanceFinished(FolderInstance.java:262)
248     at org.openide.loaders.FolderLookup$ProxyLkp.beforeLookup(FolderLookup.java:355)
249     at org.openide.util.lookup.ProxyLookup.lookup(ProxyLookup.java:135)
250     at org.openide.loaders.FolderLookupTest$1R.operationPostCreate(FolderLookupTest.java:239)
251     at org.openide.loaders.DataLoaderPool.fireOperationEvent(DataLoaderPool.java:205)
252     at org.openide.loaders.DataObjectPool.notifyCreation(DataObjectPool.java:423)
253     at org.openide.loaders.DataObjectPool.notifyCreationAll(DataObjectPool.java:438)
254     at org.openide.loaders.DataObjectPool.exitAllowConstructor(DataObjectPool.java:96)
255     at org.openide.loaders.DataObjectPool.handleFindDataObject(DataObjectPool.java:113)
256     at org.openide.loaders.DataLoader.findDataObject(DataLoader.java:233)
257     at org.openide.loaders.DataLoaderPool.findDataObject(DataLoaderPool.java:378)
258     at org.openide.loaders.DataLoaderPool.findDataObject(DataLoaderPool.java:338)
259     at org.openide.loaders.DataObject.find(DataObject.java:456)
260     at org.openide.loaders.FolderLookup$ICItem.init(FolderLookup.java:400)
261     at org.openide.loaders.FolderLookup$ICItem.instanceOf(FolderLookup.java:451)
262     at org.openide.util.lookup.InheritanceTree$1TwoJobs.before(InheritanceTree.java:424)
263     at org.openide.util.lookup.InheritanceTree.classToNode(InheritanceTree.java:494)
264     at org.openide.util.lookup.InheritanceTree.searchClass(InheritanceTree.java:513)
265     at org.openide.util.lookup.InheritanceTree.lookup(InheritanceTree.java:197)
266     at org.openide.util.lookup.DelegatingStorage.lookup(DelegatingStorage.java:128)
267     at org.openide.util.lookup.AbstractLookup.lookupItem(AbstractLookup.java:324)
268     at org.openide.util.lookup.AbstractLookup.lookup(AbstractLookup.java:307)
269     at org.openide.util.lookup.ProxyLookup.lookup(ProxyLookup.java:140)
270     at org.openide.loaders.FolderLookupTest.testDeadlockWhileWaitingForFolderRecognizerToFinish50768(FolderLookupTest.java:309)
271          */

272         
273         
274         String JavaDoc fsstruct [] = new String JavaDoc [] {
275             "AA/X/Y/Z//java-lang-StringBuffer.instance",
276         };
277         
278         TestUtilHid.destroyLocalFileSystem (getName());
279         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
280         Repository.getDefault ().addFileSystem (lfs);
281
282         final FileObject bb = lfs.findResource("/AA");
283         final FileObject file = lfs.findResource ("/AA/X/Y/Z/java-lang-StringBuffer.instance");
284         final DataFolder folder = DataFolder.findFolder (bb);
285         final Thread JavaDoc t = Thread.currentThread ();
286         DataLoaderPool pool = DataLoaderPool.getDefault ();
287         assertNotNull (pool);
288         
289         
290         
291         class R implements OperationListener, FolderListListener {
292             public int cnt;
293             public int created;
294             public Exception JavaDoc ex;
295             public Lookup lookup;
296             public boolean doWrongThing;
297             
298             /* Empty implementation */
299             public void operationPostCreate (OperationEvent ev) {
300                 if (doWrongThing && file.equals (ev.getObject ().getPrimaryFile ())) {
301                     assertSame ("The right thread", t, Thread.currentThread ());
302                     
303                     doWrongThing = false;
304                     cnt++;
305                     
306                     
307                     try {
308                         synchronized (this) {
309                             if (!alreadyBlocked) {
310                                 wait ();
311                             }
312                         }
313                         // this initializes the rescan
314
FileUtil.createData (bb, "java-lang-Object.instance");
315                     } catch (Exception JavaDoc ex) {
316                         this.ex = ex;
317                     }
318                     // and this will deadlock
319
lookup.lookup (String JavaDoc.class);
320                 }
321             }
322
323             /* Empty implementation */
324             public void operationCopy (OperationEvent.Copy ev) {
325             }
326
327             /* Empty implementation */
328             public void operationMove (OperationEvent.Move ev) {
329             }
330
331             /* Empty implementation */
332             public void operationDelete (OperationEvent ev) {
333             }
334
335             /* Empty implementation */
336             public void operationRename (OperationEvent.Rename ev) {
337             }
338
339             /* Empty implementation */
340             public void operationCreateShadow (OperationEvent.Copy ev) {
341             }
342
343             /* Empty implementation */
344             public void operationCreateFromTemplate (OperationEvent.Copy ev) {
345             }
346             
347             private boolean alreadyBlocked;
348             public synchronized void process (DataObject obj, java.util.List JavaDoc arr) {
349                 if (!alreadyBlocked) {
350                     try {
351                         wait (1000);
352                     } catch (InterruptedException JavaDoc ex) {
353                         fail ("No exceptions");
354                     }
355                     alreadyBlocked = true;
356                 }
357             }
358
359             public synchronized void finished (java.util.List JavaDoc arr) {
360                 notifyAll ();
361             }
362         }
363         
364         R r = new R ();
365         pool.addOperationListener (r);
366         
367         r.lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();
368         
369         Object JavaDoc o = r.lookup.lookup (StringBuffer JavaDoc.class);
370         assertNotNull ("StringBuffer found", o);
371         
372         org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (r.lookup);
373         
374         DataObject obj = DataObject.find (file);
375         assertEquals ("IDO", InstanceDataObject.class, obj.getClass ());
376         java.lang.ref.WeakReference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc (obj);
377         obj = null;
378         r.lookup = null;
379         assertGC ("Make sure the object goes away", ref);
380         
381         // this will block Folder Recognizer for a while
382
FolderList l = FolderList.find (bb, true);
383         org.openide.util.RequestProcessor.Task task = l.computeChildrenList (r);
384         
385         r.doWrongThing = true;
386         r.lookup = (Lookup)mar.get ();
387         
388         // one of the next two lines was causing the deadlock
389
o = r.lookup.lookup (StringBuffer JavaDoc.class);
390         o = r.lookup.lookup (Runnable JavaDoc.class);
391         
392         assertEquals ("Called once", 1, r.cnt);
393         
394         if (r.ex != null) {
395             throw r.ex;
396         }
397         
398         pool.removeOperationListener (r);
399     }
400     
401     
402     public void testDeserializationOnFolder () throws Exception JavaDoc {
403         doDeser (true);
404     }
405     
406     public void testDeserializationOnSubFolder () throws Exception JavaDoc {
407         doDeser (false);
408     }
409     
410     public void testGcWhenHoldingOnlyResult () throws Exception JavaDoc {
411         String JavaDoc fsstruct [] = new String JavaDoc [] {
412             "AA/BB/A.simple"
413         };
414         
415         TestUtilHid.destroyLocalFileSystem (getName());
416         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
417         Repository.getDefault ().addFileSystem (lfs);
418
419         DataFolder folder = DataFolder.findFolder (lfs.findResource("/AA"));
420         DataFolder subfolder = DataFolder.findFolder (lfs.findResource("/AA/BB/"));
421         DataObject tmp = InstanceDataObject.create (subfolder, null, Hashtable JavaDoc.class);
422
423         FolderLookup lkp = new FolderLookup (folder);
424         Lookup.Result res = lkp.getLookup ().lookup (new Lookup.Template(Hashtable JavaDoc.class));
425         java.lang.ref.WeakReference JavaDoc ref2 = new java.lang.ref.WeakReference JavaDoc (lkp);
426         
427         lkp = null;
428         folder = null;
429         subfolder = null;
430         tmp = null;
431         boolean collected;
432         try {
433             assertGC("XXX", ref2);
434             collected = true;
435         } catch (junit.framework.AssertionFailedError x) {
436             collected = false;
437         }
438         assertEquals(res.allInstances().size(), 1);
439         if (collected) {
440             fail("Lookup got GCed when holding only result..");
441         }
442     }
443     
444     private void doDeser (boolean root) throws Exception JavaDoc {
445         String JavaDoc fsstruct [] = new String JavaDoc [] {
446             "AA/BB/A.simple"
447         };
448         
449         TestUtilHid.destroyLocalFileSystem (getName());
450         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
451         Repository.getDefault ().addFileSystem (lfs);
452
453         DataFolder folder = DataFolder.findFolder (lfs.findResource("/AA"));
454         DataFolder subfolder = DataFolder.findFolder (lfs.findResource("/AA/BB/"));
455         DataObject tmp = InstanceDataObject.create (subfolder, null, Hashtable JavaDoc.class);
456
457         FolderLookup lkp = new FolderLookup (folder);
458         Object JavaDoc res = lkp.getLookup ().lookup (Hashtable JavaDoc.class);
459         assertNotNull ("The table is obtained", res);
460         
461         org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (lkp.getLookup ());
462         
463         java.lang.ref.WeakReference JavaDoc ref1 = new java.lang.ref.WeakReference JavaDoc (subfolder);
464         java.lang.ref.WeakReference JavaDoc ref2 = new java.lang.ref.WeakReference JavaDoc (lkp);
465         subfolder = null;
466         tmp = null;
467         folder = null;
468         lkp = null;
469         assertGC ("Lookup can disappear", ref1);
470         assertGC ("Folder can disappear", ref2);
471
472         
473         Lookup lookup = (Lookup)mar.get ();
474         ((FolderLookup.ProxyLkp)lookup).waitFinished ();
475         res = lookup.lookup (Hashtable JavaDoc.class);
476         assertNotNull ("A table is there", res);
477         res = lookup.lookup (ArrayList JavaDoc.class);
478         assertNull ("No array list", res);
479
480         DataFolder my = DataFolder.findFolder (lfs.findResource(root ? "/AA/" : "/AA/BB/"));
481         tmp = InstanceDataObject.create (my, null, ArrayList JavaDoc.class);
482         res = lookup.lookup (ArrayList JavaDoc.class);
483         assertNotNull ("array list is there", res);
484         
485         Repository.getDefault ().removeFileSystem (lfs);
486     }
487     
488     
489     
490    
491     
492    public static class ALkp extends AbstractLookup {
493        public InstanceContent ic;
494        
495        public ALkp () {
496            this (new InstanceContent ());
497        }
498        
499        private ALkp (InstanceContent ic) {
500            super (ic);
501            ic.add (new Integer JavaDoc (1));
502            this.ic = ic;
503        }
504        
505    }
506    
507    public static final class GLkp extends ProxyLookup {
508        public GLkp() {
509            super(new Lookup[] {
510                new ALkp(),
511                Lookups.metaInfServices(GLkp.class.getClassLoader()),
512            });
513        }
514    }
515    
516 }
517
Popular Tags