1 19 20 package org.openide.loaders; 21 import java.io.IOException ; 22 import java.lang.ref.WeakReference ; 23 import java.util.Enumeration ; 24 import java.util.List ; 25 import javax.swing.JButton ; 26 import javax.swing.SwingUtilities ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.cookies.InstanceCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.Repository; 32 import org.openide.util.Enumerations; 33 import org.openide.util.Lookup; 34 import org.openide.util.lookup.AbstractLookup; 35 import org.openide.util.lookup.InstanceContent; 36 37 38 43 public class CanYouCreateFolderLookupFromHandleFindSlowVersionTest extends LoggingTestCaseHid { 44 45 46 public CanYouCreateFolderLookupFromHandleFindSlowVersionTest(String s) { 47 super(s); 48 } 49 50 protected void setUp() { 51 registerIntoLookup(new Pool()); 52 } 53 54 public void testCreateAndImmediatellyQueryWhenThereIsALotfSlowDataObjectsTheLookup() throws Exception { 55 MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class); 56 m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup"); 57 DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JButton .class); 58 m.instanceFile = instance.getPrimaryFile(); 59 for (int i = 0; i < 15; i++) { 60 m.button.createData("slow" + i + ".slow"); 61 } 62 63 64 WeakReference ref = new WeakReference (instance); 65 instance = null; 66 assertGC("Object must disappear first", ref); 67 68 FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt"); 69 DataObject obj = DataObject.find(any); 70 71 assertEquals("The right object found", m, obj.getLoader()); 72 assertNotNull("Value found", m.v); 73 assertEquals("Button", JButton .class, m.v.getClass()); 74 assertNotNull("Lookup created", m.lookup); 75 assertEquals("All slow files recognized", 15, m.slowCnt); 76 } 77 78 79 public static final class MyLoader extends UniFileLoader { 80 public FileObject button; 81 public Object v; 82 public Lookup lookup; 83 84 public InstanceDataObject created; 85 86 private FileObject instanceFile; 87 88 private DataObject middleCreation; 89 90 private int slowCnt; 91 92 public MyLoader() throws IOException { 93 super("org.openide.loaders.MultiDataObject"); 94 } 95 96 protected FileObject findPrimaryFile(FileObject fo) { 97 if (fo.hasExt("slow")) { 98 slowCnt++; 99 try { 100 Thread.sleep(1000); 101 } catch (InterruptedException ex) { 102 ex.printStackTrace(); 103 fail("No failures, please"); 104 } 105 return null; 106 } 107 108 if (!fo.hasExt("txt")) { 109 return null; 110 } 111 112 assertNull("First invocation", lookup); 113 114 FolderLookup l = new FolderLookup(DataFolder.findFolder(button)); 115 lookup = l.getLookup(); 116 v = lookup.lookup(JButton .class); 117 assertNotNull("The instance computed", v); 118 119 return fo; 120 } 121 122 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 123 return new MultiDataObject(primaryFile, this); 124 } 125 } 126 127 private static final class Pool extends DataLoaderPool { 128 static List loaders; 129 130 public Pool() { 131 } 132 133 public Enumeration loaders() { 134 return Enumerations.singleton(DataLoader.getLoader(MyLoader.class)); 135 } 136 } 137 138 } 139 | Popular Tags |