KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import org.netbeans.junit.MockServices;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.filesystems.Repository;
31 import org.openide.util.Enumerations;
32 import org.openide.util.Lookup;
33 import org.openide.util.SharedClassObject;
34
35 /** It must be possible to create lookup anytime, if there is no deadlock...
36  *
37  * @author Jaroslav Tulach
38  */

39 public class CanYouCreateFolderLookupFromHandleFindTest extends NbTestCase {
40     
41     public CanYouCreateFolderLookupFromHandleFindTest(String JavaDoc s) {
42         super(s);
43     }
44     
45     protected void setUp() {
46         MockServices.setServices(new Class JavaDoc[] {Pool.class});
47     }
48     
49     public void testCreateAndImmediatelyQueryTheLookup() throws Exception JavaDoc {
50         MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class);
51         m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup");
52         DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JButton JavaDoc.class);
53         m.instanceFile = instance.getPrimaryFile();
54         
55         WeakReference JavaDoc ref = new WeakReference JavaDoc(instance);
56         instance = null;
57         assertGC("Object must disappear first", ref);
58         
59         FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt");
60         DataObject obj = DataObject.find(any);
61         
62         assertEquals("The right object found", m, obj.getLoader());
63         assertNotNull("Value found", m.v);
64         assertEquals("Button", JButton JavaDoc.class, m.v.getClass());
65         assertNotNull("Lookup created", m.lookup);
66     }
67     
68     /**
69      * Registering directly MyLoader does not work since core's DLP is found
70      * and that one does not check META-INF/services.
71      */

72     public static final class Pool extends DataLoaderPool {
73         protected Enumeration JavaDoc loaders() {
74             return Enumerations.singleton(SharedClassObject.findObject(MyLoader.class, true));
75         }
76     }
77     
78     public static final class MyLoader extends UniFileLoader {
79         public FileObject button;
80         public Object JavaDoc v;
81         public Lookup lookup;
82         
83         public InstanceDataObject created;
84         
85         private FileObject instanceFile;
86         
87         private DataObject middleCreation;
88         
89         public MyLoader() throws IOException JavaDoc {
90             super("org.openide.loaders.MultiDataObject");
91         }
92         
93         protected FileObject findPrimaryFile(FileObject fo) {
94             if (!fo.hasExt("txt")) {
95                 return null;
96             }
97             
98             assertNull("First invocation", lookup);
99             
100             FolderLookup l = new FolderLookup(DataFolder.findFolder(button));
101             lookup = l.getLookup();
102             v = lookup.lookup(JButton JavaDoc.class);
103             assertNotNull("The instance computed", v);
104             
105             return fo;
106         }
107         
108         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
109             return new MultiDataObject(primaryFile, this);
110         }
111     }
112     
113 }
114
Popular Tags