1 19 20 package org.openide.filesystems; 21 22 import org.netbeans.junit.NbTestCase; 23 import org.openide.util.Lookup; 24 25 83 public class URLMapper50984Test extends NbTestCase { 84 public URLMapper50984Test(String name) { 85 super(name); 86 } 87 88 protected void setUp() throws Exception { 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 { 97 class DoubleEntry implements Runnable { 98 private org.openide.util.RequestProcessor RP = new org.openide.util.RequestProcessor ("Deadlock processor"); 99 public Exception 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 ex) { 113 this.e = ex; 114 } 115 } 116 117 public void closeToLookup () throws Exception { 118 java.net.URL url = new java.net.URL ("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 queried; 137 138 public org.openide.filesystems.FileObject[] getFileObjects(java.net.URL url) { 139 queried = url; 140 return null; 141 } 142 143 public java.net.URL getURL(org.openide.filesystems.FileObject fo, int type) { 144 return null; 145 } 146 } 147 148 150 @SuppressWarnings ("unchecked") 151 private static class QueryingPair extends org.openide.util.lookup.AbstractLookup.Pair { 152 public static boolean beBroken; 153 154 public java.lang.String getId() { 155 return getType ().toString(); 156 } 157 158 public java.lang.String getDisplayName() { 159 return getId (); 160 } 161 162 public java.lang.Class getType() { 163 return getClass (); 164 } 165 166 protected boolean creatorOf(java.lang.Object obj) { 167 return obj == this; 168 } 169 170 protected boolean instanceOf(java.lang.Class c) { 171 if (beBroken) { 172 beBroken = false; 173 try { 174 assertNull ("is still null", MyUM.queried); 175 java.net.URL url = new java.net.URL ("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 ex) { 179 ex.printStackTrace(); 180 fail ("No exceptions: " + ex.getMessage ()); 181 } 182 } 183 return c.isAssignableFrom(getType ()); 184 } 185 186 public java.lang.Object 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 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 for (int i = 0; i < 1000; i++) { 211 ic.add (new Integer (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 r = runnable; 221 runnable = null; 222 if (r != null) { 223 r.run (); 224 } 225 super.beforeLookup(template); 226 } 227 228 } } 230 | Popular Tags |