KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > URLMapper50984Test


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.filesystems;
21
22 import org.netbeans.junit.NbTestCase;
23 import org.openide.util.Lookup;
24
25 /**
26  * Trying to mimic IZ 50984. Used to create following deadlock:
27  *
28
29
30  "Deadlock processor" daemon prio=1 tid=0x081f3370 nid=0x61b9 in Object.wait() [0x465af000..0x465af610]
31     at java.lang.Object.wait(Native Method)
32     - waiting on <0x69917db0> (a java.lang.Class)
33     at java.lang.Object.wait(Object.java:474)
34     at org.openide.filesystems.URLMapper.getInstances(URLMapper.java:223)
35     - locked <0x69917db0> (a java.lang.Class)
36     at org.openide.filesystems.URLMapper.findFileObject(URLMapper.java:177)
37     at org.openide.filesystems.URLMapper50984Test$QueryingPair.instanceOf(URLMapper50984Test.java:117)
38     at org.openide.util.lookup.InheritanceTree$1TwoJobs.before(InheritanceTree.java:424)
39     at org.openide.util.lookup.InheritanceTree.classToNode(InheritanceTree.java:494)
40     at org.openide.util.lookup.InheritanceTree.searchClass(InheritanceTree.java:513)
41     at org.openide.util.lookup.InheritanceTree.lookup(InheritanceTree.java:197)
42     at org.openide.util.lookup.DelegatingStorage.lookup(DelegatingStorage.java:128)
43     at org.openide.util.lookup.AbstractLookup.lookupItem(AbstractLookup.java:314)
44     at org.openide.util.lookup.AbstractLookup.lookup(AbstractLookup.java:297)
45     at org.openide.filesystems.URLMapper50984Test$1DoubleEntry.run(URLMapper50984Test.java:52)
46     at org.openide.util.Task.run(Task.java:136)
47     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330)
48     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:686)
49
50 "main" prio=1 tid=0x0805c248 nid=0x61ad in Object.wait() [0xbfffc000..0xbfffcd98]
51     at java.lang.Object.wait(Native Method)
52     - waiting on <0x659601e8> (a java.lang.Object)
53     at java.lang.Object.wait(Object.java:474)
54     at org.openide.util.lookup.AbstractLookup.enterStorage(AbstractLookup.java:102)
55     - locked <0x659601e8> (a java.lang.Object)
56     at org.openide.util.lookup.AbstractLookup.access$400(AbstractLookup.java:35)
57     at org.openide.util.lookup.AbstractLookup$R.allItemsWithoutBeforeLookup(AbstractLookup.java:746)
58     at org.openide.util.lookup.AbstractLookup$R.allInstances(AbstractLookup.java:691)
59     at org.openide.filesystems.URLMapper.getInstances(URLMapper.java:238)
60     at org.openide.filesystems.URLMapper.findFileObject(URLMapper.java:177)
61     at org.openide.filesystems.URLMapper50984Test$1DoubleEntry.closeToLookup(URLMapper50984Test.java:61)
62     at org.openide.filesystems.URLMapper50984Test.testDeadlockInIssue50984(URLMapper50984Test.java:70)
63     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
64     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
65     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
66     at java.lang.reflect.Method.invoke(Method.java:585)
67     at junit.framework.TestCase.runTest(TestCase.java:154)
68     at junit.framework.TestCase.runBare(TestCase.java:127)
69     at junit.framework.TestResult$1.protect(TestResult.java:106)
70     at junit.framework.TestResult.runProtected(TestResult.java:124)
71     at junit.framework.TestResult.run(TestResult.java:109)
72     at junit.framework.TestCase.run(TestCase.java:118)
73     at org.netbeans.junit.NbTestCase.run(NbTestCase.java:119)
74     at junit.framework.TestSuite.runTest(TestSuite.java:208)
75     at junit.framework.TestSuite.run(TestSuite.java:203)
76     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:289)
77     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:656)
78     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:558)
79  
80  
81  * @author Jaroslav Tulach
82  */

83 public class URLMapper50984Test extends NbTestCase {
84     public URLMapper50984Test(String JavaDoc name) {
85         super(name);
86     }
87
88     protected void setUp() throws Exception JavaDoc {
89         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.URLMapper50984Test$Lkp");
90         
91         super.setUp();
92         
93         assertEquals ("Our lookup is registered", Lkp.class, org.openide.util.Lookup.getDefault().getClass());
94     }
95     
96     public void testDeadlockInIssue50984 () throws Exception JavaDoc {
97         class DoubleEntry implements Runnable JavaDoc {
98             private org.openide.util.RequestProcessor RP = new org.openide.util.RequestProcessor ("Deadlock processor");
99             public Exception JavaDoc e;
100             
101             public void run () {
102                 try {
103                     if (!RP.isRequestProcessorThread ()) {
104                         RP.post (this);
105                         synchronized (this) {
106                             wait (200);
107                         }
108                     } else {
109                         QueryingPair.beBroken = true;
110                         Lookup.getDefault ().lookup (QueryingPair.class);
111                     }
112                 } catch (Exception JavaDoc ex) {
113                     this.e = ex;
114                 }
115             }
116             
117             public void closeToLookup () throws Exception JavaDoc {
118                 java.net.URL JavaDoc url = new java.net.URL JavaDoc ("http://www.netbeans.org");
119                 URLMapper.findFileObject(url);
120                 synchronized (this) {
121                     notifyAll ();
122                 }
123             }
124         }
125         
126         DoubleEntry d = new DoubleEntry ();
127         Lkp.runnable = d;
128         d.closeToLookup ();
129         
130         if (d.e != null) {
131             throw d.e;
132         }
133     }
134
135     private static final class MyUM extends URLMapper {
136         public static java.net.URL JavaDoc queried;
137         
138         public org.openide.filesystems.FileObject[] getFileObjects(java.net.URL JavaDoc url) {
139             queried = url;
140             return null;
141         }
142
143         public java.net.URL JavaDoc getURL(org.openide.filesystems.FileObject fo, int type) {
144             return null;
145         }
146     }
147     
148     /** This is a pair that as a part of its instanceOf method queries the URL resolver.
149      */

150     @SuppressWarnings JavaDoc("unchecked")
151     private static class QueryingPair extends org.openide.util.lookup.AbstractLookup.Pair {
152         public static boolean beBroken;
153         
154         public java.lang.String JavaDoc getId() {
155             return getType ().toString();
156         }
157
158         public java.lang.String JavaDoc getDisplayName() {
159             return getId ();
160         }
161
162         public java.lang.Class JavaDoc getType() {
163             return getClass ();
164         }
165
166        protected boolean creatorOf(java.lang.Object JavaDoc obj) {
167             return obj == this;
168         }
169
170         protected boolean instanceOf(java.lang.Class JavaDoc c) {
171             if (beBroken) {
172                 beBroken = false;
173                 try {
174                     assertNull ("is still null", MyUM.queried);
175                     java.net.URL JavaDoc url = new java.net.URL JavaDoc ("http://www.netbeans.org");
176                     URLMapper.findFileObject(url);
177                     fail("Lookup is not reentrant so this line should never be called.");
178                 } catch (java.net.MalformedURLException JavaDoc ex) {
179                     ex.printStackTrace();
180                     fail ("No exceptions: " + ex.getMessage ());
181                 }
182             }
183             return c.isAssignableFrom(getType ());
184         }
185
186         public java.lang.Object JavaDoc getInstance() {
187             return this;
188         }
189     }
190     
191      
192
193     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
194         private static org.openide.util.lookup.InstanceContent ic;
195         static volatile Runnable JavaDoc runnable;
196         
197         public Lkp () {
198             this (new org.openide.util.lookup.InstanceContent ());
199         }
200         
201         private Lkp (org.openide.util.lookup.InstanceContent ic) {
202             super (ic);
203             this.ic = ic;
204         }
205
206         protected void initialize() {
207             // a small trick to make the InheritanceTree storage to be used
208
// because if the amount of elements in small, the ArrayStorage is
209
// used and it does not have the same problems like InheritanceTree
210
for (int i = 0; i < 1000; i++) {
211                 ic.add (new Integer JavaDoc (i));
212             }
213
214             QueryingPair qp = new QueryingPair();
215             ic.addPair (qp);
216             ic.add (new MyUM ());
217         }
218
219         protected void beforeLookup (org.openide.util.Lookup.Template template) {
220             Runnable JavaDoc r = runnable;
221             runnable = null;
222             if (r != null) {
223                 r.run ();
224             }
225             super.beforeLookup(template);
226         }
227
228     } // end of Lkp
229
}
230
Popular Tags