1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.lang.ref.WeakReference ; 24 import java.util.Enumeration ; 25 import javax.swing.JButton ; 26 import org.netbeans.junit.MockServices; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.filesystems.Repository; 31 import org.openide.util.Enumerations; 32 import org.openide.util.Lookup; 33 import org.openide.util.SharedClassObject; 34 35 39 public class CanYouCreateFolderLookupFromHandleFindTest extends NbTestCase { 40 41 public CanYouCreateFolderLookupFromHandleFindTest(String s) { 42 super(s); 43 } 44 45 protected void setUp() { 46 MockServices.setServices(new Class [] {Pool.class}); 47 } 48 49 public void testCreateAndImmediatelyQueryTheLookup() throws Exception { 50 MyLoader m = (MyLoader)MyLoader.getLoader(MyLoader.class); 51 m.button = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), "FolderLookup"); 52 DataObject instance = InstanceDataObject.create(DataFolder.findFolder(m.button), "SomeName", JButton .class); 53 m.instanceFile = instance.getPrimaryFile(); 54 55 WeakReference ref = new WeakReference (instance); 56 instance = null; 57 assertGC("Object must disappear first", ref); 58 59 FileObject any = Repository.getDefault().getDefaultFileSystem().getRoot().createData("Ahoj.txt"); 60 DataObject obj = DataObject.find(any); 61 62 assertEquals("The right object found", m, obj.getLoader()); 63 assertNotNull("Value found", m.v); 64 assertEquals("Button", JButton .class, m.v.getClass()); 65 assertNotNull("Lookup created", m.lookup); 66 } 67 68 72 public static final class Pool extends DataLoaderPool { 73 protected Enumeration loaders() { 74 return Enumerations.singleton(SharedClassObject.findObject(MyLoader.class, true)); 75 } 76 } 77 78 public static final class MyLoader extends UniFileLoader { 79 public FileObject button; 80 public Object v; 81 public Lookup lookup; 82 83 public InstanceDataObject created; 84 85 private FileObject instanceFile; 86 87 private DataObject middleCreation; 88 89 public MyLoader() throws IOException { 90 super("org.openide.loaders.MultiDataObject"); 91 } 92 93 protected FileObject findPrimaryFile(FileObject fo) { 94 if (!fo.hasExt("txt")) { 95 return null; 96 } 97 98 assertNull("First invocation", lookup); 99 100 FolderLookup l = new FolderLookup(DataFolder.findFolder(button)); 101 lookup = l.getLookup(); 102 v = lookup.lookup(JButton .class); 103 assertNotNull("The instance computed", v); 104 105 return fo; 106 } 107 108 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 109 return new MultiDataObject(primaryFile, this); 110 } 111 } 112 113 } 114 | Popular Tags |