1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import org.openide.filesystems.*; 29 import org.netbeans.junit.*; 30 import org.openide.nodes.Children; 31 import org.openide.nodes.CookieSet; 32 import org.openide.nodes.Node; 33 import org.openide.text.DataEditorSupport; 34 import org.openide.util.Enumerations; 35 import org.openide.util.Lookup; 36 import org.openide.util.NbBundle; 37 38 40 public class FileObjectInLookupTest extends NbTestCase { 41 FileObject root; 42 43 public FileObjectInLookupTest(String name) { 44 super(name); 45 } 46 47 protected void setUp() throws Exception { 48 MockServices.setServices(OwnDataLoaderPool.class); 49 clearWorkDir (); 50 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir (), new String [] { 51 "adir/", 52 "adir/file.txt", 53 "adir/file.own" 54 }); 55 56 root = FileUtil.toFileObject(FileUtil.toFile(lfs.getRoot())); 57 58 Enumeration <?> en = DataLoaderPool.getDefault().allLoaders(); 59 while (en.hasMoreElements()) { 60 if (en.nextElement() instanceof OwnDataLoader) { 61 return; 62 } 63 } 64 fail("OwnDataLoader shall be registered"); 65 } 66 67 protected void tearDown() throws Exception { 68 } 69 70 protected boolean runInEQ() { 71 return true; 72 } 73 74 public void testFOInsideFolder() throws Exception { 75 DataFolder f = DataFolder.findFolder(root.getFileObject("adir")); 76 assertFileObjects(f); 77 f.rename("bdir"); 78 assertFileObjects(f); 79 } 80 81 public void testFOInsideADefaultDataObject() throws Exception { 82 DataObject obj = DataObject.find(root.getFileObject("adir/file.txt")); 83 assertFileObjects(obj); 84 obj.rename("kuk"); 85 assertFileObjects(obj); 86 obj.move(obj.getFolder().getFolder()); 87 assertFileObjects(obj); 88 } 89 90 public void testOwnLoader() throws Exception { 91 DataObject obj = DataObject.find(root.getFileObject("adir/file.own")); 92 assertEquals(OwnDataLoader.class, obj.getLoader().getClass()); 93 assertFileObjects(obj); 94 obj.rename("kuk"); 95 assertFileObjects(obj); 96 obj.move(obj.getFolder().getFolder()); 97 assertFileObjects(obj); 98 } 99 100 public void testShadow() throws Exception { 101 DataObject obj = DataObject.find(root.getFileObject("adir/file.own")); 102 DataShadow shadow = obj.createShadow(obj.getFolder().getFolder()); 103 assertEquals(OwnDataLoader.class, obj.getLoader().getClass()); 104 105 assertEquals("DataObject for the shadow is the shadow", shadow, shadow.getCookie(DataObject.class)); 106 107 assertFileObjects(obj); 108 assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files()); 109 obj.rename("kuk"); 110 assertFileObjects(obj); 111 assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files()); 112 obj.move(obj.getFolder().getFolder()); 113 assertFileObjects(obj); 114 assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files()); 115 shadow.rename("somenewshadow"); 116 assertFileObjects(obj); 117 assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files()); 118 obj.delete(); 119 127 } 128 129 private static void assertFileObjects(DataObject obj) { 130 assertFileObjects("", obj, obj.files()); 131 } 132 133 private static void assertFileObjects(String msg, DataObject obj, Collection <? extends FileObject> expect) { 134 Collection <? extends FileObject> allcol = obj.getNodeDelegate().getLookup().lookupAll(FileObject.class); 135 List <FileObject> all = new ArrayList <FileObject>(allcol); 136 Enumeration <? extends FileObject> files = Collections.enumeration(expect); 137 int i = 0; 138 while (files.hasMoreElements()) { 139 FileObject fo = files.nextElement(); 140 if (i >= all.size()) { 141 fail(msg + "\nThere should be more elements, but there is only " + all.size() + "\nAll: " + all + "\nCurrent: " + fo); 142 } 143 144 if (fo.equals(all.get(i))) { 145 i++; 146 continue; 147 } 148 fail(msg + "\nError at position " + i + " expected: " + fo + " but was: " + all.get(i) + "\nAll: " + all); 149 } 150 } 151 152 public static final class OwnDataLoaderPool extends DataLoaderPool { 153 protected Enumeration <? extends DataLoader> loaders() { 154 return Enumerations.singleton(OwnDataLoader.getLoader(OwnDataLoader.class)); 155 } 156 } 157 158 159 public static class OwnDataLoader extends UniFileLoader { 160 private static final long serialVersionUID = 1L; 161 162 public OwnDataLoader() { 163 super("org.openide.loaders.OwnDataObject"); 164 } 165 166 protected String defaultDisplayName() { 167 return NbBundle.getMessage(OwnDataLoader.class, "LBL_Own_loader_name"); 168 } 169 170 protected void initialize() { 171 super.initialize(); 172 getExtensions().addExtension("own"); 173 } 174 175 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 176 return new OwnDataObject(primaryFile, this); 177 } 178 } 179 static class OwnDataObject extends MultiDataObject implements Lookup.Provider { 180 181 public OwnDataObject(FileObject pf, OwnDataLoader loader) throws DataObjectExistsException, IOException { 182 super(pf, loader); 183 CookieSet cookies = getCookieSet(); 184 cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies)); 185 } 186 187 protected Node createNodeDelegate() { 188 return new OwnDataNode(this, getLookup()); 189 } 190 191 public Lookup getLookup() { 192 return getCookieSet().getLookup(); 193 } 194 } 195 196 static class OwnDataNode extends DataNode { 197 private static final String IMAGE_ICON_BASE = "SET/PATH/TO/ICON/HERE"; 198 199 public OwnDataNode(OwnDataObject obj, Lookup lookup) { 200 super(obj, Children.LEAF, lookup); 201 } 203 204 } 205 206 } 207 | Popular Tags |