1 19 20 package org.openide.loaders; 21 22 23 import org.openide.filesystems.*; 24 import java.io.IOException ; 25 import java.util.*; 26 import org.netbeans.junit.*; 27 import java.beans.PropertyChangeListener ; 28 29 32 public class DataLoaderOrigTest extends NbTestCase { 33 34 public DataLoaderOrigTest(String name) { 35 super(name); 36 } 37 38 protected void setUp() throws IOException { 39 clearWorkDir(); 40 } 41 42 public void testSimpleLoader() throws Exception { 43 DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class); 44 DataLoaderPool pool; 45 AddLoaderManuallyHid.addRemoveLoader(l, true); 46 try { 47 pool = DataLoaderPool.getDefault (); 48 assertTrue(Arrays.asList(pool.toArray()).contains(l)); 49 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 50 "folder/file.simple", 51 }); 52 FileObject fo = lfs.findResource("folder/file.simple"); 53 assertNotNull(fo); 54 DataObject dob = DataObject.find(fo); 55 assertEquals(SimpleDataObject.class, dob.getClass()); 56 } finally { 57 AddLoaderManuallyHid.addRemoveLoader(l, false); 58 } 59 assertFalse(Arrays.asList(pool.toArray()).contains(l)); 60 TestUtilHid.destroyLocalFileSystem(getName()); 61 } 62 63 65 public void testDataObjectFind() throws Exception { 66 DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class); 67 DataLoaderPool pool; 68 AddLoaderManuallyHid.addRemoveLoader(l, true); 69 try { 70 pool = DataLoaderPool.getDefault (); 71 assertTrue(Arrays.asList(pool.toArray()).contains(l)); 72 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 73 "folder/file.simple", 74 }); 75 FileObject fo = lfs.findResource("folder/file.simple"); 76 assertNotNull(fo); 77 78 DataObject jdo = DataObject.find(fo); 79 for (int i = 0; i < 5000; i++) { 80 FileObject primary = jdo.getPrimaryFile(); 81 jdo.setValid(false); 82 jdo = DataObject.find(primary); 83 assertNotNull(jdo); 84 assertTrue(jdo.isValid()); 85 } 86 87 } finally { 88 AddLoaderManuallyHid.addRemoveLoader(l, false); 89 } 90 TestUtilHid.destroyLocalFileSystem(getName()); 91 } 92 93 public static final class SimpleUniFileLoader extends UniFileLoader { 94 public SimpleUniFileLoader() { 95 super(SimpleDataObject.class.getName()); 96 } 97 protected void initialize() { 98 super.initialize(); 99 getExtensions().addExtension("simple"); 100 } 101 protected String displayName() { 102 return "Simple"; 103 } 104 protected MultiDataObject createMultiObject(FileObject pf) throws IOException { 105 return new SimpleDataObject(pf, this); 106 } 107 } 108 public static final class SimpleDataObject extends MultiDataObject { 109 private ArrayList supp = new ArrayList (); 110 111 public SimpleDataObject(FileObject pf, MultiFileLoader loader) throws IOException { 112 super(pf, loader); 113 } 114 115 118 public final org.openide.nodes.CookieSet cookieSet () { 119 return getCookieSet (); 120 } 121 122 124 public final Enumeration listeners () { 125 return Collections.enumeration (supp); 126 } 127 128 public void addPropertyChangeListener (PropertyChangeListener l) { 129 super.addPropertyChangeListener (l); 130 supp.add (l); 131 } 132 133 public void removePropertyChangeListener (PropertyChangeListener l) { 134 super.removePropertyChangeListener (l); 135 supp.remove (l); 136 } 137 } 138 139 145 public void testFindTwoPart() throws Exception { 146 DataLoader l = DataLoader.getLoader(TwoPartLoader.class); 147 AddLoaderManuallyHid.addRemoveLoader(l, true); 148 try { 149 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 150 "folder/part/", 151 "folder/part.ext", 152 "folder/trash.txt", 153 }); 154 FileObject fo = lfs.findResource("folder/part"); 156 assertEquals(TwoPartObject.class, DataObject.find(fo).getClass()); 157 } finally { 158 AddLoaderManuallyHid.addRemoveLoader(l, false); 159 } 160 TestUtilHid.destroyLocalFileSystem(getName()); 161 } 162 163 184 public void testFindTwoPart6() throws Exception { 185 abstractTestFindTwoPart(true, false, true); 186 } 187 196 private static final boolean DEBUG = false; 197 private void abstractTestFindTwoPart(final boolean atomic, final boolean makePrimFirst, final boolean findPrim) throws Exception { 198 if (DEBUG) System.err.println("atomic=" + atomic + " makePrimFirst=" + makePrimFirst + " findPrim=" + findPrim); 199 DataLoader l = DataLoader.getLoader(TwoPartLoader.class); 200 AddLoaderManuallyHid.addRemoveLoader(l, true); 201 try { 202 TestUtilHid.destroyLocalFileSystem(getName()); 203 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 204 "folder/trash.txt", 205 }); 206 final FileObject filefolder = lfs.findResource("folder"); 207 final boolean[] stop = new boolean[] {false}; 208 Thread t = new Thread ("recognizing objects sometimes") { 209 public void run() { 210 try { 211 DataFolder folder = DataFolder.findFolder(filefolder); 212 for (int delay = 0; !stop[0]; delay++) { 213 Thread.sleep(delay * 10); 214 switch (delay % 3) { 215 case 0: 216 DataObject[] kids = folder.getChildren(); 217 if (DEBUG) System.err.println("got children; " + kids.length); 218 break; 219 case 1: 220 FileObject fo = filefolder.getFileObject("part"); 221 if (fo != null) { 222 DataObject o = DataObject.find(fo); 227 if (DEBUG) System.err.println("from folder found: " + o); 228 } else { 229 if (DEBUG) System.err.println("no folder folder/part"); 230 } 231 break; 232 case 2: 233 fo = filefolder.getFileObject("part", "ext"); 234 if (fo != null) { 235 DataObject o = DataObject.find(fo); 236 if (DEBUG) System.err.println("from file found: " + o); 237 } else { 238 if (DEBUG) System.err.println("no file folder/part.ext"); 239 } 240 break; 241 default: 242 throw new IllegalStateException (); 243 } 244 } 245 } catch (Exception e) { 246 e.printStackTrace(); 247 } 248 } 249 }; 250 try { 251 t.start(); 252 for (int i = 0; i < 35; i++) { 253 Thread.sleep((i * 17) % 100); 256 final FileObject[] primSec = new FileObject[2]; 258 final String name = "part"; 260 final int pause = (i * 23) % 100; 261 final int _i = i; 262 FileSystem.AtomicAction action = new FileSystem.AtomicAction() { 263 public void run() throws IOException { 264 try { 265 if (makePrimFirst) { 266 loadPrim(); 267 Thread.sleep(pause); 268 } 269 loadSec(); 270 if (! makePrimFirst) { 271 Thread.sleep(pause); 272 loadPrim(); 273 } 274 } catch (InterruptedException ie) { 275 throw new IOException (ie.toString()); 276 } 277 } 278 private void loadPrim() throws IOException { 279 primSec[0] = filefolder.createFolder(name); 280 } 281 private void loadSec() throws IOException { 282 primSec[1] = filefolder.createData(name, "ext"); 283 } 284 }; 285 if (atomic) { 286 lfs.runAtomicAction(action); 287 } else { 288 action.run(); 289 } 290 Thread.sleep((i * 19) % 100); 291 FileObject tofind = findPrim ? primSec[0] : primSec[1]; 292 DataObject dob = DataObject.find(tofind); 293 assertEquals("On iteration #" + i + ", found right object", TwoPartObject.class, dob.getClass()); 294 if (DEBUG) System.err.println("it worked, #" + i); 295 action = new FileSystem.AtomicAction() { 297 public void run() throws IOException { 298 if (! makePrimFirst) { 300 primSec[0].delete(); 301 } 302 primSec[1].delete(); 303 if (makePrimFirst) { 304 primSec[0].delete(); 305 } 306 } 307 }; 308 if (atomic) { 309 lfs.runAtomicAction(action); 310 } else { 311 action.run(); 312 } 313 } 314 } finally { 315 stop[0] = true; 317 t.join(5000); 318 } 319 } finally { 320 AddLoaderManuallyHid.addRemoveLoader(l, false); 321 } 322 TestUtilHid.destroyLocalFileSystem(getName()); 323 } 324 325 public static final class TwoPartLoader extends MultiFileLoader { 326 public TwoPartLoader() { 327 super(TwoPartObject.class); 328 } 329 protected String displayName() { 330 return "TwoPart"; 331 } 332 protected FileObject findPrimaryFile(FileObject fo) { 333 if (fo.isFolder() && ! fo.isRoot() && FileUtil.findBrother(fo, "ext") != null) { 334 return fo; 335 } else if (fo.isData() && fo.hasExt("ext")) { 336 FileObject fo2 = fo.getParent().getFileObject(fo.getName()); 337 if (fo2 != null && fo2.isFolder()) { 338 return fo2; 339 } 340 } 341 return null; 342 } 343 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 344 return new TwoPartObject(this, primaryFile); 345 } 346 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 347 return new FileEntry.Folder(obj, primaryFile); 348 } 349 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 350 return new FileEntry(obj, secondaryFile); 351 } 352 } 353 public static final class TwoPartObject extends DataFolder { 354 public TwoPartObject(TwoPartLoader l, FileObject folder) throws DataObjectExistsException { 355 super(folder, l); 356 } 357 } 358 359 360 } 361 | Popular Tags |