KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Handler JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogRecord JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import org.netbeans.junit.MockServices;
34 import org.netbeans.junit.NbTestCase;
35 import org.openide.cookies.InstanceCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.filesystems.Repository;
39 import org.openide.util.Enumerations;
40 import org.openide.util.Lookup;
41
42 /** To simulate and fix 65543.
43  * <pre>
44  "Folder recognizer" daemon prio=1 tid=0x08297dd0 nid=0x380b in Object.wait() [aa46e000..aa46f238]
45  at java.lang.Object.wait(Native Method)
46  - waiting on <0xabdb6e50> (a org.openide.loaders.DataObjectPool)
47  at java.lang.Object.wait(Object.java:429)
48  at org.openide.loaders.DataObjectPool.waitNotified(DataObjectPool.java:470)
49  - locked <0xabdb6e50> (a org.openide.loaders.DataObjectPool)
50  at org.openide.loaders.DataObjectExistsException.getDataObject(DataObjectExistsException.java:43)
51  at org.openide.loaders.MultiFileLoader.handleFindDataObject(MultiFileLoader.java:83)
52  at org.openide.loaders.DataObjectPool.handleFindDataObject(DataObjectPool.java:111)
53  at org.openide.loaders.DataLoader.findDataObject(DataLoader.java:362)
54  at org.openide.loaders.DataLoaderPool.findDataObject(DataLoaderPool.java:402)
55  at org.openide.loaders.FolderList.createBoth(FolderList.java:701)
56  at org.openide.loaders.FolderList.getObjects(FolderList.java:512)
57  at org.openide.loaders.FolderList.access$200(FolderList.java:50)
58  at org.openide.loaders.FolderList$ListTask.run(FolderList.java:880)
59  at org.openide.util.Task.run(Task.java:207)
60  at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:469)
61  at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:870)
62  
63  "main" prio=1 tid=0x0805cbc0 nid=0x380b in Object.wait() [bfffb000..bfffca5c]
64  at java.lang.Object.wait(Native Method)
65  - waiting on <0xab8a2530> (a org.openide.util.RequestProcessor$Task)
66  at java.lang.Object.wait(Object.java:429)
67  at org.openide.util.Task.waitFinished(Task.java:99)
68  - locked <0xab8a2530> (a org.openide.util.RequestProcessor$Task)
69  at org.openide.util.RequestProcessor$Task.waitFinished(RequestProcessor.java:629)
70  at org.openide.loaders.FolderInstance.waitFinished(FolderInstance.java:285)
71  at org.openide.loaders.FolderInstance.instanceFinished(FolderInstance.java:265)
72  at org.openide.loaders.FolderLookup$ProxyLkp.beforeLookup(FolderLookup.java:355)
73  at org.openide.util.lookup.ProxyLookup.lookup(ProxyLookup.java:175)
74  at org.openide.loaders.CanYouQueryFolderLookupFromHandleFindTest$MyLoader.findPrimaryFile(CanYouQueryFolderLookupFromHandleFindTest.java:140)
75  at org.openide.loaders.MultiFileLoader.findPrimaryFileImpl(MultiFileLoader.java:262)
76  at org.openide.loaders.MultiFileLoader.handleFindDataObject(MultiFileLoader.java:65)
77  at org.openide.loaders.DataObjectPool.handleFindDataObject(DataObjectPool.java:111)
78  at org.openide.loaders.DataLoader.findDataObject(DataLoader.java:362)
79  at org.openide.loaders.DataLoaderPool.findDataObject(DataLoaderPool.java:402)
80  at org.openide.loaders.DataLoaderPool.findDataObject(DataLoaderPool.java:362)
81  at org.openide.loaders.DataObject.find(DataObject.java:459)
82  at org.openide.loaders.CanYouQueryFolderLookupFromHandleFindTest.testTheDeadlock(CanYouQueryFolderLookupFromHandleFindTest.java:58)
83  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
84  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
85  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
86  at java.lang.reflect.Method.invoke(Method.java:324)
87  at junit.framework.TestCase.runTest(TestCase.java:154)
88  at org.netbeans.junit.NbTestCase.runBare(NbTestCase.java:135)
89  at junit.framework.TestResult$1.protect(TestResult.java:106)
90  at junit.framework.TestResult.runProtected(TestResult.java:124)
91  at junit.framework.TestResult.run(TestResult.java:109)
92  at junit.framework.TestCase.run(TestCase.java:118)
93  at org.netbeans.junit.NbTestCase.run(NbTestCase.java:122)
94  at junit.framework.TestSuite.runTest(TestSuite.java:208)
95  at junit.framework.TestSuite.run(TestSuite.java:203)
96  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:297)
97  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:672)
98  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:567)
99  </pre>
100  *
101  * @author Jaroslav Tulach
102  */

103 public class CanYouQueryFolderLookupFromHandleFindTest extends NbTestCase {
104     static {
105         Logger JavaDoc l = Logger.getLogger("");
106         Handler JavaDoc[] arr = l.getHandlers();
107         for (int i = 0; i < arr.length; i++) {
108             l.removeHandler(arr[i]);
109         }
110         l.addHandler(new ErrManager());
111         l.setLevel(Level.ALL);
112     }
113     
114     public CanYouQueryFolderLookupFromHandleFindTest(String JavaDoc s) {
115         super(s);
116     }
117     
118     protected void setUp() {
119         MockServices.setServices(new Class JavaDoc[] {Pool.class, ErrManager.class});
120     }
121     
122     public void testTheDeadlock() throws Exception JavaDoc {
123         MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class);
124         m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup");
125         DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JButton JavaDoc.class);
126         m.instanceFile = instance.getPrimaryFile();
127         
128         WeakReference JavaDoc ref = new WeakReference JavaDoc(instance);
129         instance = null;
130         assertGC("Object must disappear first", ref);
131         
132         DataLoaderPool.setPreferredLoader(m.instanceFile, m);
133         
134         FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt");
135         DataObject obj = DataObject.find(any);
136         
137         assertEquals("The right object found", m, obj.getLoader());
138         assertEquals("Null value then", null, m.v);
139         assertNotNull("Lookup created", m.lookup);
140         Object JavaDoc v = m.lookup.lookup(JButton JavaDoc.class);
141         assertNotNull("Now the value can be found", v);
142         
143         
144         instance = DataObject.find(m.instanceFile);
145         InstanceCookie ic = (InstanceCookie)instance.getCookie(InstanceCookie.class);
146         assertNotNull("InstanceCookie is there", ic);
147         assertEquals("Is the same as from instance", v, ic.instanceCreate());
148         
149         if (ErrManager.messages.indexOf("Preventing deadlock") == -1) {
150             fail("There should be a warning in the log: " + ErrManager.messages);
151         }
152         if (ErrManager.messages.indexOf("65543") == -1) {
153             fail("There should be a warning in the log: " + ErrManager.messages);
154         }
155     }
156     
157     public static final class MyLoader extends UniFileLoader implements Runnable JavaDoc {
158         public FileObject button;
159         public Object JavaDoc v;
160         public Lookup lookup;
161         
162         public InstanceDataObject created;
163         
164         private FileObject instanceFile;
165         
166         private DataObject middleCreation;
167         
168         public MyLoader() throws IOException JavaDoc {
169             super("org.openide.loaders.MultiDataObject");
170         }
171         
172         protected FileObject findPrimaryFile(FileObject fo) {
173             if (fo.hasExt("instance") && !FolderList.isFolderRecognizerThread()) {
174                 // this is the trick - it will cause DataObjectExistsException to be thrown later
175
try {
176                     created = new InstanceDataObject(fo, this);
177                     synchronized (this) {
178                         try {
179                             notifyAll();
180                             wait();
181                         } catch (InterruptedException JavaDoc ex) {
182                             ex.printStackTrace();
183                         }
184                     }
185                 } catch (DataObjectExistsException ex) {
186                     ex.printStackTrace();
187                     fail("Now exception now");
188                 }
189                 
190                 // we do not recognize it
191
return null;
192             }
193             if (!fo.hasExt("txt")) {
194                 return null;
195             }
196             
197             assertNull("First invocation", lookup);
198             
199             org.openide.util.RequestProcessor.Task t;
200             synchronized (this) {
201                 t = org.openide.util.RequestProcessor.getDefault().post(this);
202                 try {
203                     wait();
204                 } catch (InterruptedException JavaDoc ex) {
205                     ex.printStackTrace();
206                     fail("No exceptions please");
207                 }
208             }
209             
210             FolderLookup l = new FolderLookup(DataFolder.findFolder(button));
211             lookup = l.getLookup();
212             v = lookup.lookup(JButton JavaDoc.class);
213             assertEquals("We cannot create the instance currently", null, v);
214             
215             synchronized (this) {
216                 notifyAll();
217             }
218             // wait till the other task finishes
219
t.waitFinished();
220             
221             return fo;
222         }
223         
224         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
225             return new MultiDataObject(primaryFile, this);
226         }
227         
228         public void run() {
229             try {
230                 middleCreation = DataObject.find(instanceFile);
231             } catch (DataObjectNotFoundException ex) {
232                 fail("Exception: " + ex.getMessage());
233             }
234         }
235     }
236     
237     public static final class Pool extends DataLoaderPool {
238         static List JavaDoc loaders;
239         
240         public Pool() {
241         }
242         
243         public Enumeration JavaDoc loaders() {
244             return Enumerations.singleton(DataLoader.getLoader(MyLoader.class));
245         }
246     }
247     public static final class ErrManager extends Handler JavaDoc {
248         static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc();
249         static int nOfMessages;
250         static final String JavaDoc DELIMITER = ": ";
251         
252         static void resetMessages() {
253             messages.delete(0, ErrManager.messages.length());
254             nOfMessages = 0;
255         }
256
257         public void publish(LogRecord JavaDoc rec) {
258             nOfMessages++;
259             messages.append(rec.getLevel() + DELIMITER + rec.getMessage());
260             messages.append('\n');
261
262             Throwable JavaDoc t = rec.getThrown();
263             if (t == null) {
264                 return;
265             }
266
267             StringWriter JavaDoc w = new StringWriter JavaDoc();
268             t.printStackTrace(new PrintWriter JavaDoc(w));
269             messages.append(w.toString());
270         }
271
272         public void flush() {
273         }
274
275         public void close() throws SecurityException JavaDoc {
276         }
277     }
278     
279 }
280
Popular Tags