KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.cookies.InstanceCookie;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32 import org.openide.util.Enumerations;
33 import org.openide.util.Lookup;
34 import org.openide.util.lookup.AbstractLookup;
35 import org.openide.util.lookup.InstanceContent;
36
37
38 /** It must be possible to create lookup anytime, if there is no deadlock,
39  * even if the recognition of FolderLookup takes really long time.
40  *
41  * @author Jaroslav Tulach
42  */

43 public class CanYouCreateFolderLookupFromHandleFindSlowVersionTest extends LoggingTestCaseHid {
44     
45     /** Creates a new instance of CanYouQueryFolderLookupFromHandleFindTest */
46     public CanYouCreateFolderLookupFromHandleFindSlowVersionTest(String JavaDoc s) {
47         super(s);
48     }
49     
50     protected void setUp() {
51         registerIntoLookup(new Pool());
52     }
53     
54     public void testCreateAndImmediatellyQueryWhenThereIsALotfSlowDataObjectsTheLookup() throws Exception JavaDoc {
55         MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class);
56         m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup");
57         DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JButton JavaDoc.class);
58         m.instanceFile = instance.getPrimaryFile();
59         for (int i = 0; i < 15; i++) {
60             m.button.createData("slow" + i + ".slow");
61         }
62         
63         
64         WeakReference JavaDoc ref = new WeakReference JavaDoc(instance);
65         instance = null;
66         assertGC("Object must disappear first", ref);
67         
68         FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt");
69         DataObject obj = DataObject.find(any);
70         
71         assertEquals("The right object found", m, obj.getLoader());
72         assertNotNull("Value found", m.v);
73         assertEquals("Button", JButton JavaDoc.class, m.v.getClass());
74         assertNotNull("Lookup created", m.lookup);
75         assertEquals("All slow files recognized", 15, m.slowCnt);
76     }
77     
78     
79     public static final class MyLoader extends UniFileLoader {
80         public FileObject button;
81         public Object JavaDoc v;
82         public Lookup lookup;
83         
84         public InstanceDataObject created;
85         
86         private FileObject instanceFile;
87         
88         private DataObject middleCreation;
89
90         private int slowCnt;
91         
92         public MyLoader() throws IOException JavaDoc {
93             super("org.openide.loaders.MultiDataObject");
94         }
95         
96         protected FileObject findPrimaryFile(FileObject fo) {
97             if (fo.hasExt("slow")) {
98                 slowCnt++;
99                 try {
100                     Thread.sleep(1000);
101                 } catch (InterruptedException JavaDoc ex) {
102                     ex.printStackTrace();
103                     fail("No failures, please");
104                 }
105                 return null;
106             }
107             
108             if (!fo.hasExt("txt")) {
109                 return null;
110             }
111             
112             assertNull("First invocation", lookup);
113             
114             FolderLookup l = new FolderLookup(DataFolder.findFolder(button));
115             lookup = l.getLookup();
116             v = lookup.lookup(JButton JavaDoc.class);
117             assertNotNull("The instance computed", v);
118             
119             return fo;
120         }
121         
122         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
123             return new MultiDataObject(primaryFile, this);
124         }
125     }
126     
127     private static final class Pool extends DataLoaderPool {
128         static List JavaDoc loaders;
129         
130         public Pool() {
131         }
132         
133         public Enumeration JavaDoc loaders() {
134             return Enumerations.singleton(DataLoader.getLoader(MyLoader.class));
135         }
136     }
137     
138 }
139
Popular Tags