1 19 20 package org.openide.loaders; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.beans.PropertyChangeEvent ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import java.beans.PropertyChangeListener ; 28 import java.lang.ref.WeakReference ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import org.openide.ErrorManager; 32 33 import org.openide.filesystems.*; 34 import org.openide.nodes.*; 35 36 39 44 public class DataObjectInvalidationOneFileTest extends LoggingTestCaseHid { 45 Logger log; 46 47 public DataObjectInvalidationOneFileTest(String name) { 48 super(name); 49 } 50 51 protected Level logLevel() { 52 return Level.FINE; 53 } 54 55 protected void setUp() throws IOException { 56 clearWorkDir(); 57 58 log = Logger.getLogger("TEST-" + getName()); 59 registerIntoLookup(new Pool()); 60 } 61 62 protected void tearDown() throws Exception { 63 WeakReference ref = new WeakReference (DataLoader.getLoader(SlowDataLoader.class)); 64 Pool.setExtra(null); 65 assertGC("Let's cleanup all nodes, data objects created in previous test", ref); 66 } 67 68 73 public void testDataObjectsCreatedOncePerFile() throws Exception { 74 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 75 "folder/file.slow", 76 }); 77 FileObject folder = lfs.findResource("folder"); 78 DataLoader l = DataLoader.getLoader(SlowDataLoader.class); 79 log.info("Registering the slow loader"); 80 Pool.setExtra(l); 81 82 log.info("Clearing the counts"); 83 SlowDataLoader.createCount = 0; 84 SlowDataObject.createCount = 0; 85 log.info("Counts cleared"); 86 87 DataFolder f = DataFolder.findFolder(folder); 88 89 log.info("Folder created: " + f); 90 91 Node foldernode = f.getNodeDelegate(); 92 93 log.info("Node created: " + foldernode); 94 95 Children folderkids = foldernode.getChildren(); 96 97 log.info("Children are here"); 98 assertEquals("Getting a folder node does not start automatically scanning children", 0, SlowDataLoader.createCount); 99 assertEquals("Getting a folder node does not finish automatically scanning children", 0, SlowDataObject.createCount); 100 101 Node[] keep = folderkids.getNodes(true); 102 103 log.info("Nodes for children are computed: " + keep.length); 104 105 assertEquals("After getting folder node children, a data object is not started to be created >1 time", 1, SlowDataLoader.createCount); 106 assertEquals("After getting folder node children, a data object is not successfully created >1 time", 1, SlowDataObject.createCount); 107 } 108 109 private static final class ExpectingListener implements PropertyChangeListener { 110 private final Set changes = new HashSet (); public synchronized void propertyChange(PropertyChangeEvent ev) { 112 changes.add(ev.getPropertyName()); 113 notifyAll(); 115 } 116 public synchronized boolean gotSomething(String prop) throws InterruptedException { 117 if (changes.contains(prop)) return true; 118 wait(3000); 119 return changes.contains(prop); 120 } 121 } 122 123 public static final class SlowDataLoader extends UniFileLoader { 124 public static int createCount = 0; 125 private static Logger ERR = Logger.getLogger("SlowDataLoader"); 126 public SlowDataLoader() { 127 super(SlowDataObject.class.getName()); 128 } 129 protected void initialize() { 130 super.initialize(); 131 getExtensions().addExtension("slow"); 132 } 133 protected String displayName() { 134 return "Slow"; 135 } 136 protected MultiDataObject createMultiObject(FileObject pf) throws IOException { 137 ERR.info("in createMultiObject for: " + pf); 138 SlowDataObject o = new SlowDataObject(pf, this); 139 ERR.info("created object : " + o); 140 return o; 142 } 143 } 144 public static final class SlowDataObject extends MultiDataObject { 145 public Thread ok; 146 public static int createCount = 0; 147 public SlowDataObject(FileObject pf, MultiFileLoader loader) throws IOException { 148 super(pf, loader); 149 synchronized (loader) { 150 SlowDataLoader.ERR.info("Incrementing SlowDataObject count to " + ++createCount); 151 SlowDataLoader.ERR.info("Incrementing SlowDataLoader count to " + ++SlowDataLoader.createCount); 152 153 SlowDataLoader.ERR.info("Wake up sleepers"); 156 loader.notifyAll (); 157 } 158 159 int cnt = 1; 160 161 while (cnt-- > 0) { 162 try { 163 Thread.sleep(2000); 164 } catch (InterruptedException ie) { 165 throw new IOException (ie.toString()); 166 } 167 } 168 169 170 ok = Thread.currentThread(); 171 SlowDataLoader.ERR.info("End of constructor"); 172 } 173 protected Node createNodeDelegate() { 174 return new SlowDataNode(this); 175 } 176 177 protected DataObject handleCopy (DataFolder df) throws java.io.IOException { 178 FileObject fo = this.getPrimaryEntry().copy (df.getPrimaryFile(), "slow"); 179 return new SlowDataObject (fo, (MultiFileLoader)this.getLoader()); 180 } 181 protected DataObject handleCreateFromTemplate (DataFolder df, String s) throws java.io.IOException { 182 FileObject fo = this.getPrimaryEntry().createFromTemplate (df.getPrimaryFile(), null); 183 return new SlowDataObject (fo, (MultiFileLoader)this.getLoader()); 184 } 185 } 186 public static final class SlowDataNode extends DataNode { 187 public SlowDataNode(SlowDataObject o) { 188 super(o, Children.LEAF); 189 if (o.ok == null) throw new IllegalStateException ("getDataNode called too early"); 190 setShortDescription("slownode"); 193 } 194 } 195 196 private static final class Pool extends DataLoaderPool { 197 private static DataLoader extra; 198 199 200 protected java.util.Enumeration loaders () { 201 if (extra == null) { 202 return org.openide.util.Enumerations.empty (); 203 } else { 204 return org.openide.util.Enumerations.singleton (extra); 205 } 206 } 207 208 public static void setExtra(DataLoader aExtra) { 209 if (aExtra != null && extra != null) { 210 fail("Cannot set extra loader while one is already there. 1: " + extra + " 2: " + aExtra); 211 } 212 extra = aExtra; 213 Pool p = (Pool)DataLoaderPool.getDefault(); 214 p.fireChangeEvent(new javax.swing.event.ChangeEvent (p)); 215 } 216 } 217 } 218 | Popular Tags |