1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Enumeration ; 27 import java.util.List ; 28 import java.util.logging.Handler ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogRecord ; 31 import java.util.logging.Logger ; 32 import javax.swing.JButton ; 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 103 public class CanYouQueryFolderLookupFromHandleFindTest extends NbTestCase { 104 static { 105 Logger l = Logger.getLogger(""); 106 Handler [] 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 s) { 115 super(s); 116 } 117 118 protected void setUp() { 119 MockServices.setServices(new Class [] {Pool.class, ErrManager.class}); 120 } 121 122 public void testTheDeadlock() throws Exception { 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 .class); 126 m.instanceFile = instance.getPrimaryFile(); 127 128 WeakReference ref = new WeakReference (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 v = m.lookup.lookup(JButton .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 { 158 public FileObject button; 159 public Object 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 { 169 super("org.openide.loaders.MultiDataObject"); 170 } 171 172 protected FileObject findPrimaryFile(FileObject fo) { 173 if (fo.hasExt("instance") && !FolderList.isFolderRecognizerThread()) { 174 try { 176 created = new InstanceDataObject(fo, this); 177 synchronized (this) { 178 try { 179 notifyAll(); 180 wait(); 181 } catch (InterruptedException ex) { 182 ex.printStackTrace(); 183 } 184 } 185 } catch (DataObjectExistsException ex) { 186 ex.printStackTrace(); 187 fail("Now exception now"); 188 } 189 190 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 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 .class); 213 assertEquals("We cannot create the instance currently", null, v); 214 215 synchronized (this) { 216 notifyAll(); 217 } 218 t.waitFinished(); 220 221 return fo; 222 } 223 224 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 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 loaders; 239 240 public Pool() { 241 } 242 243 public Enumeration loaders() { 244 return Enumerations.singleton(DataLoader.getLoader(MyLoader.class)); 245 } 246 } 247 public static final class ErrManager extends Handler { 248 static final StringBuffer messages = new StringBuffer (); 249 static int nOfMessages; 250 static final String DELIMITER = ": "; 251 252 static void resetMessages() { 253 messages.delete(0, ErrManager.messages.length()); 254 nOfMessages = 0; 255 } 256 257 public void publish(LogRecord rec) { 258 nOfMessages++; 259 messages.append(rec.getLevel() + DELIMITER + rec.getMessage()); 260 messages.append('\n'); 261 262 Throwable t = rec.getThrown(); 263 if (t == null) { 264 return; 265 } 266 267 StringWriter w = new StringWriter (); 268 t.printStackTrace(new PrintWriter (w)); 269 messages.append(w.toString()); 270 } 271 272 public void flush() { 273 } 274 275 public void close() throws SecurityException { 276 } 277 } 278 279 } 280 | Popular Tags |