1 19 20 package org.openide.loaders; 21 import java.io.IOException ; 22 import java.io.InterruptedIOException ; 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.Logger ; 31 import javax.swing.JButton ; 32 import javax.swing.JPanel ; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.cookies.InstanceCookie; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.util.Enumerations; 39 import org.openide.util.Lookup; 40 import org.openide.util.RequestProcessor; 41 import org.openide.util.lookup.AbstractLookup; 42 import org.openide.util.lookup.InstanceContent; 43 44 45 import org.openide.loaders.CanYouQueryFolderLookupFromHandleFindTest.ErrManager; 46 47 171 public class CanYouQueryFromRenameTest extends LoggingTestCaseHid { 172 static { 173 Logger l = Logger.getLogger(""); 174 Handler [] arr = l.getHandlers(); 175 for (int i = 0; i < arr.length; i++) { 176 l.removeHandler(arr[i]); 177 } 178 l.addHandler(new ErrManager()); 179 l.setLevel(Level.ALL); 180 } 181 182 183 public CanYouQueryFromRenameTest(String s) { 184 super(s); 185 } 186 187 protected int timeOut() { 188 return 57500; 189 } 190 191 192 protected void setUp() throws IOException { 193 registerIntoLookup(new Pool()); 194 clearWorkDir(); 195 } 196 197 public void testTheDeadlock() throws Exception { 198 MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class); 199 m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup"); 200 DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JPanel .class); 201 m.initLookup(instance.getPrimaryFile()); 202 203 assertNotNull("There is the lookup", m.lookup.lookup(JPanel .class)); 204 205 DataLoaderPool.setPreferredLoader(m.instanceFile, m); 206 207 FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt"); 208 DataObject obj = DataObject.find(any); 209 210 assertEquals("The right object found", m, obj.getLoader()); 211 assertEquals("Null value then", null, m.v); 212 213 obj.rename("SomeStrangeName"); 214 215 } 217 218 219 public static final class MyLoader extends UniFileLoader 220 implements Runnable { 221 public FileObject button; 222 public Object v; 223 public Lookup lookup; 224 225 public InstanceDataObject created; 226 227 private FileObject instanceFile; 228 229 private DataFolder middle; 230 private DataObject[] arr; 231 232 public MyLoader() throws IOException { 233 super("org.openide.loaders.MultiDataObject"); 234 235 } 236 237 protected FileObject findPrimaryFile(FileObject fo) { 238 if (fo.hasExt("txt") || fo.equals(instanceFile)) { 239 return fo; 240 } 241 return null; 242 } 243 244 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 245 return new MyObj(primaryFile, this); 246 } 247 248 public void initLookup(FileObject inst) { 249 instanceFile = inst; 250 DataFolder f = DataFolder.findFolder(instanceFile.getParent()); 251 FolderLookup l = new FolderLookup(f); 252 lookup = l.getLookup(); 253 254 assertNull("No button: ", lookup.lookup(JButton .class)); 255 } 256 257 public void run() { 258 assertNotNull("Lookup exists: ", lookup); 259 260 DataFolder f = DataFolder.findFolder(instanceFile.getParent()); 261 try { 262 f.getPrimaryFile().createData("javax-swing-JButton.instance"); 263 } catch (IOException ex) { 264 ex.printStackTrace(); 265 fail(ex.getMessage()); 266 } 267 268 middle = f; 269 270 arr = f.getChildren(); 272 } 273 274 } 275 276 private static final class MyObj extends MultiDataObject { 277 public MyObj(FileObject fo, MyLoader l) throws DataObjectExistsException { 278 super(fo, l); 279 } 280 281 protected FileObject handleRename(String name) throws IOException { 282 FileObject retValue; 283 284 MyLoader l = (MyLoader)getLoader(); 285 try { 286 RequestProcessor.getDefault().post(l).waitFinished(1000); 287 } catch (InterruptedException ex) { 288 throw (InterruptedIOException )new InterruptedIOException (ex.getMessage()).initCause(ex); 289 } 290 291 assertNotNull("In middle of creation", l.middle); 292 293 l.v = l.lookup.lookup(JButton .class); 294 295 retValue = super.handleRename(name); 296 return retValue; 297 } 298 } 299 300 private static final class Pool extends DataLoaderPool { 301 static List loaders; 302 303 public Pool() { 304 } 305 306 public Enumeration loaders() { 307 return Enumerations.singleton(DataLoader.getLoader(MyLoader.class)); 308 } 309 } 310 311 } 312 | Popular Tags |