1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.lang.ref.WeakReference ; 24 import java.util.Set ; 25 import junit.textui.TestRunner; 26 import org.openide.cookies.OpenCookie; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileStatusEvent; 29 import org.openide.filesystems.FileSystem; 30 import java.util.Enumeration ; 31 import org.openide.nodes.Children; 32 import org.openide.nodes.Node; 33 import org.openide.cookies.InstanceCookie; 34 import org.openide.filesystems.Repository; 35 import org.netbeans.junit.*; 36 import org.openide.util.Lookup; 37 import org.openide.util.lookup.Lookups; 38 39 45 public class DataNodeTest extends NbTestCase { 46 47 public DataNodeTest(String name) { 48 super(name); 49 } 50 51 static { 52 System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataNodeTest$Lkp"); 53 } 54 55 62 public void testDataNodeHasObjectAsCookie() throws Exception { 63 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 68 DataFolder top = DataFolder.findFolder(sfs.getRoot()); 69 Enumeration e = top.children(true); 70 while (e.hasMoreElements()) { 71 DataObject o = (DataObject)e.nextElement(); 72 Node n = o.getNodeDelegate(); 73 DataObject o2 = (DataObject)n.getCookie(DataObject.class); 74 assertEquals("Correct cookie from node delegate", o, o2); 75 } 76 } 77 78 79 public void testDataNodeGetHtmlNameDoesNotInitializeAllFiles () throws Exception { 80 org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 81 "F.java", "F.form" 82 }); 83 84 FSWithStatus fs = new FSWithStatus (); 85 fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot())); 86 87 DataObject obj = DataObject.find (fs.findResource("F.java")); 88 89 String n = obj.getNodeDelegate ().getHtmlDisplayName (); 90 assertNotNull ("FSWithStatus called", fs.lastFiles); 91 92 assertEquals ("Primary entry created", 1, TwoPartLoader.get ().primary); 93 if (TwoPartLoader.get ().secondary != 0) { 94 try { 95 assertEquals ("Secondary entry not", 0, TwoPartLoader.get ().secondary); 96 } catch (Error t1) { 97 Throwable t2 = TwoPartLoader.get ().whoCreatedSecondary; 98 if (t2 != null) { 99 t1.initCause (t2); 100 } 101 throw t1; 102 } 103 } 104 assertEquals ("Size is two", 2, fs.lastFiles.size ()); 105 assertEquals ("Now the secondary entry had to be created", 1, TwoPartLoader.get ().secondary); 106 } 107 108 109 public void testWhatIsAddedToNodeLookupIsByDefaultVisibleInDataObjectLookup() throws Exception { 110 org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 111 "F.java", "F.form" 112 }); 113 114 FSWithStatus fs = new FSWithStatus (); 115 fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot())); 116 117 DataObject obj = DataObject.find (fs.findResource("F.java")); 118 119 OpenCookie fromNode = obj.getNodeDelegate().getLookup().lookup(OpenCookie.class); 120 assertNotNull("There is some cookie in node", fromNode); 121 122 OpenCookie fromObject = obj.getLookup().lookup(OpenCookie.class); 123 assertNotNull("There is a cookie in data object too", fromObject); 124 125 } 126 127 public void testDataNodeGetDataFromLookup() throws Exception { 128 129 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 130 DataFolder rootFolder = DataFolder.findFolder(sfs.getRoot()); 131 132 class C implements Node.Cookie { 133 } 134 135 String objectToLookup = "I like my Lookup"; 136 C cookieToLookup = new C(); 137 Lookup lookup = Lookups.fixed(new Object [] { objectToLookup, cookieToLookup }); 138 139 DataNode node = new DataNode(rootFolder, Children.LEAF, lookup); 140 Object objectLookedUp = node.getLookup().lookup(String .class); 141 assertEquals("object is going to be found", objectToLookup, objectLookedUp); 142 assertEquals("cookie found.", cookieToLookup, node.getLookup().lookup(C.class)); 143 144 assertEquals("But C is found", cookieToLookup, node.getCookie(C.class)); 146 147 assertNull("Data object is not found in lookup", node.getLookup().lookup(DataObject.class)); 148 assertNull("DataObject not found in cookies as they delegate to lookup", node.getCookie(DataObject.class)); 149 } 150 151 158 public void testLeakAfterStatusChange() throws Exception { 159 org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 160 "F.java", "F.form" 161 }); 162 163 FSWithStatus fs = new FSWithStatus (); 164 fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot())); 165 166 FileObject fo = fs.findResource("F.java"); 167 DataObject obj = DataObject.find (fo); 168 Node n = obj.getNodeDelegate (); 169 fs.fireStatusChange(new FileStatusEvent(fs, fo, true, true)); 170 171 Thread.sleep(100); WeakReference refN = new WeakReference (n); 173 WeakReference refD = new WeakReference (obj); 174 n = null; 175 obj = null; 176 177 assertGC("Node released", refN); 178 assertGC("DataObject released", refD); 179 } 180 181 private static final class FSWithStatus extends org.openide.filesystems.LocalFileSystem 182 implements FileSystem.HtmlStatus { 183 public Set lastFiles; 184 185 186 public FileSystem.Status getStatus () { 187 return this; 188 } 189 190 private void checkFirst (Set files) { 191 lastFiles = files; 192 assertNotNull ("There is first file", files.iterator ().next ()); 193 } 194 195 public java.awt.Image annotateIcon(java.awt.Image icon, int iconType, java.util.Set files) { 196 checkFirst (files); 197 return icon; 198 } 199 200 public String annotateName(String name, java.util.Set files) { 201 checkFirst (files); 202 return name; 203 } 204 205 public String annotateNameHtml(String name, java.util.Set files) { 206 checkFirst (files); 207 return name; 208 } 209 210 void fireStatusChange(FileStatusEvent fse) { 211 fireFileStatusChanged(fse); 212 } 213 } 215 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 216 public Lkp () { 217 this (new org.openide.util.lookup.InstanceContent ()); 218 } 219 220 private Lkp (org.openide.util.lookup.InstanceContent ic) { 221 super (ic); 222 223 ic.add (new Pool ()); 224 } 225 226 } 228 private static final class Pool extends DataLoaderPool { 229 protected Enumeration loaders () { 230 return org.openide.util.Enumerations.singleton(TwoPartLoader.get ()); 231 } 232 } 233 234 public static final class TwoPartLoader extends MultiFileLoader { 235 public int primary; 236 public int secondary; 237 public Throwable whoCreatedSecondary; 238 239 public static TwoPartLoader get () { 240 return (TwoPartLoader)TwoPartLoader.findObject (TwoPartLoader.class, true); 241 } 242 243 public TwoPartLoader() { 244 super(TwoPartObject.class.getName ()); 245 } 246 protected String displayName() { 247 return "TwoPart"; 248 } 249 protected FileObject findPrimaryFile(FileObject fo) { 250 return org.openide.filesystems.FileUtil.findBrother(fo, "java"); 251 } 252 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 253 return new TwoPartObject(this, primaryFile); 254 } 255 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 256 primary++; 257 return new FileEntry.Folder(obj, primaryFile); 258 } 259 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 260 secondary++; 261 whoCreatedSecondary = new Throwable ("Secondary should not be created"); 262 return new FileEntry(obj, secondaryFile); 263 } 264 } 265 public static final class TwoPartObject extends MultiDataObject { 266 public TwoPartObject(TwoPartLoader l, FileObject folder) throws DataObjectExistsException { 267 super(folder, l); 268 } 269 270 protected Node createNodeDelegate() { 271 return new DN(this); 272 } 273 } 274 275 private static final class DN extends DataNode { 276 public DN(TwoPartObject obj) { 277 super(obj, Children.LEAF); 278 } 279 280 public <T extends Node.Cookie> T getCookie(Class <T> clazz) { 281 if (clazz.isAssignableFrom(OpenCookie.class)) { 282 return clazz.cast(new OpenCookie () { 283 public void open() { 284 } 285 }); 286 } 287 return super.getCookie(clazz); 288 } 289 } 290 291 } 292 | Popular Tags |