KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > MasterFileObjectTestHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.*;
25 import org.netbeans.modules.masterfs.filebasedfs.FileBasedFileSystem;
26 import org.openide.filesystems.*;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import org.openide.util.RequestProcessor;
34
35 import org.openide.util.io.NbMarshalledObject;
36 import org.openide.util.Utilities;
37
38 import javax.swing.filechooser.FileSystemView JavaDoc;
39 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.WriteLockUtils;
40 import org.netbeans.modules.masterfs.providers.ProvidedExtensionsTest;
41
42 public class MasterFileObjectTestHid extends TestBaseHid{
43     private FileObject root;
44
45     public MasterFileObjectTestHid(String JavaDoc name) {
46         super(name);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         root = testedFS.findResource(getResourcePrefix());
52     }
53
54     protected String JavaDoc[] getResources(String JavaDoc testName) {
55         return new String JavaDoc[] {"testdir/ignoredir/nextdir/",
56                              "testdir/mountdir/nextdir/",
57                              "testdir/mountdir2/nextdir/",
58                              "testdir/mountdir2/fname/notestfile",
59                              "testdir/mountdir2/fname2/testfile",
60                              "testdir/mountdir4/file.ext",
61                              "testdir/mountdir5/file.ext",
62                              "testdir/mountdir6/file.ext",
63                              "testdir/mountdir6/file2.ext",
64                              "testdir/mountdir7/file2.ext",
65                              "testdir/mountdir8/",
66                              "testdir/mountdir9/",
67         };
68     }
69
70     public void testCreateNotExistingFolderOrDataFile() throws IOException JavaDoc {
71         final File JavaDoc wDir = getWorkDir();
72         final File JavaDoc fold = new File JavaDoc(wDir,"a/b/c");
73         final File JavaDoc data = new File JavaDoc(fold,"c.data");
74         implCreateFolderOrDataFile(fold, data);
75     }
76
77     public void testCreateExistingFolderOrDataFile() throws IOException JavaDoc {
78         final File JavaDoc wDir = getWorkDir();
79         final File JavaDoc fold = new File JavaDoc(wDir,"a/b/c");
80         final File JavaDoc data = new File JavaDoc(fold,"c.data");
81
82         assertTrue(fold.mkdirs());
83         assertTrue(data.createNewFile());
84                 
85         implCreateFolderOrDataFile(fold, data);
86     }
87
88     public void testCreateFolderOrDataFileExternalChange() throws IOException JavaDoc {
89         final File JavaDoc wDir = getWorkDir();
90         final File JavaDoc fold = new File JavaDoc(wDir,"a/b/c");
91         final File JavaDoc data = new File JavaDoc(fold,"c.data");
92         createFiles(data, fold);
93                 
94         implCreateFolderOrDataFile(fold, data);
95         
96         FileObject foldFo = FileUtil.toFileObject(fold);
97         FileObject dataFo = FileUtil.toFileObject(data);
98         assertNotNull(foldFo);
99         assertNotNull(dataFo);
100         deleteFiles(data, wDir);
101         
102         implCreateFolderOrDataFile(fold, data);
103
104         deleteFiles(data, wDir);
105         foldFo.getFileSystem().refresh(false);
106         foldFo = FileUtil.toFileObject(fold);
107         dataFo = FileUtil.toFileObject(data);
108         assertNull(foldFo);
109         assertNull(dataFo);
110         createFiles(data, fold);
111
112         implCreateFolderOrDataFile(fold, data);
113     }
114     //disabled because may cause that dialog is poped up on Windows promting user to put floppy disk in
115
/*public void testCreateFolderOrDataFileWithNotExistingRoot() throws Exception {
116         File wDir = null;
117         for (char d = 'A'; d < 'Z'; d++ ) {
118             wDir = new File(String.valueOf(d)+":/");
119             if (!wDir.exists()) {
120                 break;
121             }
122         }
123         final File fold = new File(wDir,"a/b/c");
124         final File data = new File(fold,"c.data");
125         try {
126             implCreateFolderOrDataFile(fold, data);
127             fail();
128         } catch (IOException ex) {
129         }
130     }*/

131
132     //just for JDK 1.6
133
/*public void testCreateFolderOrDataFile_ReadOnly() throws Exception {
134         final File wDir = getWorkDir();
135         final File fold = new File(wDir,"a/b/c");
136         final File data = new File(fold,"a/b/c.data");
137         assertTrue(getWorkDir().setReadOnly());
138         try {
139             implCreateFolderOrDataFile(wDir, fold, data);
140             fail();
141         } catch (IOException ex) {
142         } finally {
143             assertTrue(getWorkDir().setWritable(true));
144         }
145     }*/

146     
147     private static void createFiles(final File JavaDoc data, final File JavaDoc fold) throws IOException JavaDoc {
148         assertTrue(fold.mkdirs());
149         assertTrue(data.createNewFile());
150     }
151
152     private static void deleteFiles(final File JavaDoc data, final File JavaDoc wDir) {
153         File JavaDoc tmp = data;
154         while(!tmp.equals(wDir)) {
155             assertTrue(tmp.delete());
156             tmp = tmp.getParentFile();
157         }
158     }
159         
160     private void implCreateFolderOrDataFile(final File JavaDoc fold, final File JavaDoc data) throws IOException JavaDoc {
161         FileObject foldFo = FileUtil.createFolder(fold);
162         assertNotNull(foldFo);
163         assertTrue(foldFo.isValid());
164         assertNotNull(FileUtil.toFile(foldFo));
165         assertEquals(FileUtil.toFile(foldFo),fold);
166         assertTrue(foldFo.isFolder());
167         
168         FileObject dataFo = FileUtil.createData(data);
169         assertNotNull(dataFo);
170         assertTrue(dataFo.isValid());
171         assertNotNull(FileUtil.toFile(dataFo));
172         assertEquals(FileUtil.toFile(dataFo),data);
173         assertTrue(dataFo.isData());
174     }
175     
176     public void testGetNameExt2() throws IOException JavaDoc {
177         FileObject fold1 = FileUtil.createFolder(
178                 FileBasedFileSystem.getFileObject(getWorkDir()),getName());
179         assertNotNull(fold1);
180         testComposeNameImpl(fold1.createData("a.b"));
181         testComposeNameImpl(fold1.createData(".b"));
182         if (!Utilities.isWindows()) {
183             testComposeNameImpl(fold1.createData("a."));
184         }
185     }
186
187     private void testComposeNameImpl(FileObject fo) throws IOException JavaDoc {
188         assertTrue(fo.isValid() && fo.isData());
189         String JavaDoc fullName = fo.getNameExt();
190         String JavaDoc ext = fo.getExt();
191         String JavaDoc name = fo.getName();
192         FileObject parent = fo.getParent();
193         fo.delete();
194         FileObject fo2 = parent.createData(name, ext);
195         assertEquals(fullName, fo2.getNameExt());
196         assertEquals(name, fo2.getName());
197         assertEquals(ext, fo2.getExt());
198     }
199     
200     public void testFileUtilToFileObjectIsValid() throws Exception JavaDoc {
201         char SEP = File.separatorChar;
202         final File JavaDoc fileF = new File JavaDoc(FileUtil.toFile(root).getAbsolutePath() + SEP + "dir" + SEP + "file2");
203         File JavaDoc dirF = fileF.getParentFile();
204         
205         for (int cntr = 0; cntr < 10; cntr++) {
206             dirF.mkdir();
207             new FileOutputStream JavaDoc(fileF).close();
208             root.getFileSystem().refresh(false);
209             final List JavaDoc valid = new ArrayList JavaDoc();
210             FileUtil.toFileObject(fileF).addFileChangeListener(new FileChangeListener() {
211                 public void fileAttributeChanged(FileAttributeEvent fe) {
212                     update();
213                 }
214                 public void fileChanged(FileEvent fe) {
215                     update();
216                 }
217                 public void fileDataCreated(FileEvent fe) {
218                     update();
219                 }
220                 public void fileDeleted(FileEvent fe) {
221                     update();
222                 }
223                 public void fileFolderCreated(FileEvent fe) {
224                     update();
225                 }
226                 public void fileRenamed(FileRenameEvent fe) {
227                     update();
228                 }
229                 
230                 private void update() {
231                     FileObject fo;
232                     File JavaDoc f = fileF;
233                     
234                     while ((fo = FileUtil.toFileObject(f)) == null) {
235                         f = f.getParentFile();
236                     }
237                     
238                     valid.add(Boolean.valueOf(fo.isValid()));
239                 }
240             });
241             fileF.delete();
242             dirF.delete();
243             root.getFileSystem().refresh(false);
244             
245             assertTrue("at least one event", valid.size() > 0);
246             
247             for (Iterator i = valid.iterator(); i.hasNext(); ) {
248                 assertTrue("valid=" + valid + ", count=" + cntr, ((Boolean JavaDoc) i.next()).booleanValue());
249             }
250         }
251     }
252     
253     public void testRefresh69744() throws Exception JavaDoc {
254         File JavaDoc thisTest = new File JavaDoc(getWorkDir(),"thisFolder/thisFolder");
255         thisTest.mkdirs();
256         thisTest = new File JavaDoc(thisTest,"thisTest");
257         thisTest.createNewFile();
258         FileObject testf = FileUtil.toFileObject(thisTest);
259         assertNotNull(testf);
260         assertGC("",new WeakReference JavaDoc(testf.getParent()));
261         modifyFileObject(testf, "abc");
262         FileSystem fs = testf.getFileSystem();
263         final List JavaDoc l = new ArrayList JavaDoc();
264         FileChangeListener fcl = new FileChangeAdapter() {
265             public void fileChanged(FileEvent fe) {
266                 l.add(fe);
267             }
268         };
269         Thread.sleep(1500);
270         fs.addFileChangeListener(fcl);
271         try {
272             modifyFileObject(testf, "def");
273             assertFalse(l.isEmpty());
274         } finally {
275             fs.removeFileChangeListener(fcl);
276         }
277     }
278     
279     private void modifyFileObject(final FileObject testf, String JavaDoc content) throws IOException JavaDoc {
280         FileLock lock = null;
281         OutputStream JavaDoc os = null;
282         try {
283             lock = testf.lock();
284             os = testf.getOutputStream(lock);
285             os.write(content.getBytes());
286         } finally {
287             if (os != null) os.close();
288             if (lock != null) lock.releaseLock();
289         }
290     }
291     
292     public void testCaseInsensitivity() throws Exception JavaDoc {
293         if (!Utilities.isWindows()) return;
294         
295         File JavaDoc testa = new File JavaDoc(getWorkDir(), "a");
296         File JavaDoc testA = new File JavaDoc(getWorkDir(), "A");
297         
298         if (testA.exists()) {
299             assertTrue(testA.delete());
300         }
301         if (!testa.exists()) {
302             assertTrue(testa.createNewFile());
303         }
304
305         //FileBasedFileSystem's case sensitivity depends on platform. This is different behaviour
306
// than originally provided by AbstractFileSystem.
307
FileObject A = root.getFileObject("A");
308         assertNotNull(A);
309         assertNotNull(root.getFileObject("a"));
310         assertSame(root.getFileObject("A"), root.getFileObject("a"));
311         assertSame(URLMapper.findFileObject(testa.toURI().toURL()),
312                 URLMapper.findFileObject(testA.toURI().toURL()));
313         
314         //but
315
root.getChildren();
316         assertEquals("A",root.getFileObject("A").getName());
317         assertEquals("A",root.getFileObject("a").getName());
318     }
319
320     private class TestListener extends FileChangeAdapter {
321         private List JavaDoc fileObjects;
322         TestListener(List JavaDoc fileObjects) {
323             this.fileObjects = fileObjects;
324         }
325         public void fileFolderCreated(FileEvent fe) {
326             assertTrue(fileObjects.remove(fe.getFile()));
327         }
328
329         public void fileDeleted(FileEvent fe) {
330             assertTrue(fileObjects.remove(fe.getFile()));
331         }
332
333         public void fileDataCreated(FileEvent fe) {
334             assertTrue(fileObjects.remove(fe.getFile()));
335         }
336     }
337     
338     public void testSimulatesRefactoringRename() throws Exception JavaDoc {
339         assertNotNull(root);
340         FileSystem fs = root.getFileSystem();
341         assertNotNull(fs);
342         FileObject main = root.createData("Main.java");
343         FileUtil.createData(root,"subpackage/newclass.java");
344         final List JavaDoc fileObjects = new ArrayList JavaDoc();
345         final Set allSubPackages = new HashSet();
346         final TestListener tl = new TestListener(fileObjects);
347         fs.addFileChangeListener(tl);
348         try {
349             fs.runAtomicAction(new FileSystem.AtomicAction(){
350                 public void run() throws IOException JavaDoc {
351                     FileObject subpackage = root.getFileObject("subpackage");
352                     allSubPackages.add(subpackage);
353                     FileObject newclass = subpackage.getFileObject("newclass.java");
354                     FileObject subpackage1 = root.createFolder("subpackage1");
355                     fileObjects.add(subpackage1);
356                     allSubPackages.add(subpackage1);
357                     FileObject newclass1 = subpackage1.createData("newclass.java");
358                     fileObjects.add(newclass1);
359                     subpackage.delete();
360                     fileObjects.add(subpackage);
361                     fileObjects.add(newclass);
362                 }
363             });
364         } finally {
365             fs.removeFileChangeListener(tl);
366         }
367         assertTrue(fileObjects.isEmpty());
368         assertNotNull(root.getFileObject("Main.java"));
369         assertNotNull(root.getFileObject("subpackage1"));
370         assertNotNull(root.getFileObject("subpackage1/newclass.java"));
371         FileObjectTestHid.implOfTestGetFileObjectForSubversion(root, "subpackage");
372         final String JavaDoc subpackageName = (ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal() && Utilities.isWindows()) ?
373             "subpackage2" : "Subpackage";
374         fs.addFileChangeListener(tl);
375         try {
376              fs.runAtomicAction(new FileSystem.AtomicAction(){
377                 public void run() throws IOException JavaDoc {
378                     FileObject subpackage1 = root.getFileObject("subpackage1");
379                     FileObject newclass = root.getFileObject("subpackage1/newclass.java");
380                     FileObject Subpackage = root.createFolder(subpackageName);
381                     allSubPackages.add(Subpackage);
382                     assertEquals(3,allSubPackages.size());
383                     
384                     fileObjects.add(Subpackage);
385                     FileObject newclass1 = Subpackage.createData("newclass.java");
386                     fileObjects.add(newclass1);
387                     subpackage1.delete();
388                     fileObjects.add(subpackage1);
389                     fileObjects.add(newclass);
390                 }
391             });
392         } finally {
393             fs.removeFileChangeListener(tl);
394         }
395         assertTrue(fileObjects.isEmpty());
396         assertNotNull(root.getFileObject("Main.java"));
397         assertNotNull(root.getFileObject(subpackageName+"/newclass.java"));
398         FileObjectTestHid.implOfTestGetFileObjectForSubversion(root, "subpackage1");
399         assertEquals(3,allSubPackages.size());
400     }
401     
402     public void testRefresh60479 () throws Exception JavaDoc {
403         final List JavaDoc l = new ArrayList JavaDoc();
404         File JavaDoc rootFile = FileUtil.toFile(root);
405         assertTrue(rootFile.exists());
406         
407         File JavaDoc testFile = new File JavaDoc (rootFile, "testRefresh60479.txt");
408         assertTrue(testFile.createNewFile());
409         assertTrue(testFile.exists());
410         
411         FileObject testFo = FileUtil.toFileObject(testFile);
412         assertNotNull(testFo);
413         FileLock lock = testFo.lock();
414         OutputStream JavaDoc os = null;
415         
416         try {
417             os = testFo.getOutputStream(lock);
418             os.write("abcdefgh".getBytes());
419             lock.releaseLock();
420             os.close();
421             Thread.sleep(3000);
422             os = new FileOutputStream JavaDoc(testFile);
423             os.write("ijkl".getBytes());
424             os.close();
425         } finally {
426             if (lock != null && lock.isValid()) {
427                 lock.releaseLock();
428             }
429             if (os != null) {
430                 os.close();
431             }
432         }
433         
434         testFo.addFileChangeListener(new FileChangeAdapter(){
435             public void fileChanged(FileEvent fe) {
436                 l.add(fe);
437             }
438             
439         });
440         
441         testFo.refresh(true);
442         assertEquals(1,l.size());
443     }
444             
445     public void testNormalization51910 () throws Exception JavaDoc {
446         if (!Utilities.isWindows()) return;
447         
448         File JavaDoc rootFile = FileUtil.toFile(root);
449         assertTrue(rootFile.exists());
450         
451         File JavaDoc testFile = new File JavaDoc (rootFile, "abc.txt");
452         assertTrue(testFile.createNewFile());
453         assertTrue(testFile.exists());
454         
455         File JavaDoc testFile2 = new File JavaDoc (rootFile, "ABC.TXT");
456         assertTrue(testFile2.exists());
457         
458         
459         assertEquals(FileUtil.normalizeFile(testFile).toURI().toURL(), FileUtil.normalizeFile(testFile2).toURI().toURL());
460     }
461     
462     public void testEventsAfterCreatedFiles55550() throws Exception JavaDoc {
463         FileObject parent = root.getFileObject("testdir/mountdir8");
464         assertNotNull(parent);
465         assertTrue(parent.isFolder());
466         parent.getChildren();
467         
468         File JavaDoc parentFile = FileUtil.toFile(parent);
469         assertNotNull(parentFile);
470         assertTrue(parentFile.getAbsolutePath(),parentFile.exists());
471         File JavaDoc newFile = new File JavaDoc(parentFile, "sun-web.xml");
472         assertFalse(newFile.getAbsolutePath(),newFile.exists());
473                         
474         class FCLImpl extends FileChangeAdapter {
475             boolean created;
476             public void fileDataCreated(FileEvent e) {
477                 created = true;
478             }
479         }
480         FCLImpl fl = new FCLImpl();
481         parent.addFileChangeListener(fl);
482         
483         assertTrue(newFile.getAbsolutePath(), newFile.createNewFile());
484         assertTrue(newFile.exists());
485         
486         // !!!! This is the source of the problem !!!
487
// ask for the new file
488
// remove this line ans the test passes
489
FileUtil.toFileObject(newFile);
490         
491         
492         parent.refresh();
493         parent.removeFileChangeListener(fl);
494         assertTrue("Didn't receive a FileEvent on the parent.", fl.created);
495     }
496     
497     public void testIssue49037 () throws Exception JavaDoc {
498         assertNotNull(root);
499         FileObject fo = root.getFileObject("testdir/");
500         assertNotNull(fo);
501         
502         File JavaDoc f = FileUtil.toFile (fo);
503         assertNotNull(f);
504         
505         File JavaDoc newFile = new File JavaDoc (f, "absolutelyNewFile");
506         assertFalse(newFile.exists());
507         
508         new FileOutputStream JavaDoc (newFile).close();
509         assertTrue(newFile.exists());
510         assertNotNull(FileUtil.toFileObject(newFile));
511     }
512     
513     public void testDeadlock55547 () throws Exception JavaDoc {
514         assertNotNull(root);
515         File JavaDoc rootFile = new File JavaDoc (FileUtil.toFile(root), "testdir/mountdir9/");
516         assertNotNull(rootFile);
517         assertTrue(rootFile.exists());
518         
519         IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem();
520         
521         fs1.setRootDirectory(rootFile);
522         MountTable.getDefault().mount(rootFile.getAbsolutePath(), fs1);
523         
524         FileObject fo = root.getFileObject("testdir/mountdir9/");
525         assertNotNull(fo);
526
527         File JavaDoc f = FileUtil.toFile (fo);
528         assertNotNull(f);
529        
530         File JavaDoc nFile = new File JavaDoc (f, "newchild.txt");
531         assertFalse(nFile.exists());
532         assertTrue(nFile.createNewFile());
533         
534         final Object JavaDoc wait = new Object JavaDoc ();
535         
536         FileChangeListener fcl = null;
537         final List JavaDoc list = new ArrayList JavaDoc ();
538         fo.addFileChangeListener(fcl = new FileChangeAdapter () {
539             public void fileDataCreated(final FileEvent fe) {
540                 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
541                     public void run() {
542                         synchronized (wait) {
543                             assertNotNull(root.getFileObject("testdir/"));
544                             list.add(fe);
545                         }
546                     }
547                 });
548                 
549                 task.schedule(0);
550                 task.waitFinished();
551             }
552         });
553         
554         if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) {
555             assertNotNull(fo.getFileObject("newchild.txt"));
556             assertTrue(list.size() == 1);
557         }
558         fo.removeFileChangeListener(fcl);
559     }
560         
561     public void testToFileObject () throws Exception JavaDoc {
562         assertNotNull(root);
563
564         File JavaDoc rootFile = FileUtil.toFile (root);
565         File JavaDoc mnt1 = new File JavaDoc (rootFile, "testdir/mountdir2/fname");
566         
567         IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem();
568         
569         fs1.setRootDirectory(mnt1);
570         MountTable.getDefault().mount(mnt1.getAbsolutePath(), fs1);
571         
572         File JavaDoc test = new File JavaDoc (rootFile, "testdir/mountdir2/fname2/testfile");
573         assertNotNull (FileUtil.toFileObject(test));
574     }
575     
576     public void testFileUtilFromFile () throws Exception JavaDoc {
577         assertNotNull(root);
578         
579         File JavaDoc f = FileUtil.normalizeFile(getWorkDir());
580         IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
581         ifs.setRootDirectory(f);
582         
583         Repository.getDefault().addFileSystem(ifs);
584         Repository.getDefault().addFileSystem(testedFS);
585         
586         FileObject[] fos = FileUtil.fromFile(f);
587         assertTrue(fos.length > 0);
588         assertEquals(fos[0].getFileSystem(), testedFS );
589         
590     }
591
592     public void testFileSystemStatus () throws Exception JavaDoc {
593         assertNotNull(root);
594
595         File JavaDoc rootFile = FileUtil.toFile (root);
596         File JavaDoc mnt1 = new File JavaDoc (rootFile, "testdir/mountdir6");
597         
598         IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem();
599         
600         fs1.setRootDirectory(mnt1);
601         MountTable.getDefault().mount(mnt1.getAbsolutePath(), fs1);
602         
603         assertNotNull (FileUtil.toFileObject(mnt1));
604         
605         java.util.Set JavaDoc files = new java.util.HashSet JavaDoc ();
606         java.util.Enumeration JavaDoc en = FileUtil.toFileObject(mnt1).getChildren(true);
607         while (en.hasMoreElements()) {
608             files.add(en.nextElement());
609         }
610         FileSystem.Status status = root.getFileSystem().getStatus();
611         String JavaDoc name = status.annotateName("name", files);
612
613         Iterator it = files.iterator();
614         while (it.hasNext()) {
615             FileObject fo = (FileObject)it.next();
616             assertTrue (name.indexOf(fo.getNameExt()) != -1);
617         }
618         
619     }
620     
621     public void testIssue45485 () {
622         assertNotNull(root);
623         final FileObject testdir = root.getFileObject("testdir.");
624         assertNull(testdir);
625     }
626     
627     public void testMount1 () throws Exception JavaDoc {
628         final List JavaDoc events = new ArrayList JavaDoc ();
629         assertNotNull(root);
630         final FileObject ignore = root.getFileObject("testdir/ignoredir");
631         assertNotNull(ignore);
632         assertTrue(ignore.isFolder());
633         MountTable mt = MountTable.getDefault();
634         FileObject fo = root.getFileObject("testdir").getParent();
635         File JavaDoc f = FileUtil.normalizeFile(FileUtil.toFile(fo));
636         IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
637         ifs.setRootDirectory(f);
638         
639         mt.mount(f.getAbsolutePath(), ifs);
640         
641         assertNotNull(root.getFileObject("testdir/ignoredir"));
642         mt.unmount(ifs);
643         FileObject file = root.getFileObject("testdir/ignoredir");
644         assertNotNull(file);
645         assertTrue(file.isFolder());
646         assertNotNull(file.getParent());
647         assertTrue(FileUtil.isParentOf(root, file));
648     }
649
650     public void testDeleteMountPoint () throws Exception JavaDoc {
651         assertNotNull(root);
652         FileObject mountDir = root.getFileObject("testdir/mountdir");
653         assertNotNull(mountDir);
654         File JavaDoc mountFile = FileUtil.normalizeFile(FileUtil.toFile(mountDir));
655         IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
656         ifs.setRootDirectory(mountFile);
657         
658         MountTable mt = MountTable.getDefault();
659         mt.mount(mountFile.getAbsolutePath(), ifs);
660
661         if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) {
662             mountDir.delete();
663             assertTrue(!mountDir.isValid());
664             assertNull(root.getFileObject("testdir/mountdir"));
665         }
666     }
667
668     public void testDeleteCaptureExteralChanges () throws Exception JavaDoc {
669         String JavaDoc externalName = "newfile.external";
670         assertNotNull(root);
671         FileObject fileObject = root.getFileObject("testdir/mountdir5/file.ext");
672         assertNotNull(fileObject);
673         File JavaDoc f = FileUtil.toFile(fileObject);
674                         
675         assertNotNull(f);
676         f = FileUtil.normalizeFile(f);
677         assertTrue(f.exists());
678         assertTrue (f.delete());
679         
680         if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) {
681             assertFalse(fileObject.isValid());
682         }
683     }
684         
685     public void testFindResourceCaptureExteralChanges () throws Exception JavaDoc {
686         String JavaDoc externalName = "newfile.external";
687         assertNotNull(root);
688         FileObject fileObject = root.getFileObject("testdir");
689         assertNotNull(fileObject);
690         File JavaDoc f = FileUtil.toFile(fileObject);
691         
692         assertNull(testedFS.findResource(new File JavaDoc (f, externalName).getAbsolutePath().replace('\\',File.separatorChar)));
693         assertNull(fileObject.getFileObject(externalName));
694         
695         assertNotNull(f);
696         f = FileUtil.normalizeFile(f);
697         assertTrue(f.exists());
698         f = new File JavaDoc (f, externalName);
699         assertTrue(!f.exists());
700         assertTrue(f.getAbsolutePath(),f.createNewFile());
701         assertNotNull(testedFS.findResource(f.getAbsolutePath().replace('\\',File.separatorChar)));
702     }
703
704     public void testGetFileObjectCaptureExteralChanges () throws Exception JavaDoc {
705         String JavaDoc externalName = "newfile.external2";
706         assertNotNull(root);
707         FileObject fileObject = root.getFileObject("testdir");
708         assertNotNull(fileObject);
709         File JavaDoc f = FileUtil.toFile(fileObject);
710         
711         assertNull(testedFS.findResource(new File JavaDoc (f, externalName).getAbsolutePath().replace('\\',File.separatorChar)));
712         assertNull(fileObject.getFileObject(externalName));
713         
714         assertNotNull(f);
715         f = FileUtil.normalizeFile(f);
716         assertTrue(f.exists());
717         f = new File JavaDoc (f, externalName);
718         assertTrue(!f.exists());
719         assertTrue(f.getAbsolutePath(),f.createNewFile());
720         assertNotNull(fileObject.getFileObject(externalName));
721     }
722
723     public void testGetFileObject47885 () throws Exception JavaDoc {
724         assertNotNull(root);
725         
726         FileObject fileObject = root.getFileObject("testdir/mountdir4/file.ext");
727         assertNotNull(fileObject);
728         
729         fileObject = root.getFileObject("testdir/mountdir4/file", "ext");
730         assertNull(fileObject);
731         
732         fileObject = root.getFileObject("testdir\\mountdir4\\file.ext");
733         assertNull(fileObject);
734     }
735     
736     
737     public void testValidRoots () throws Exception JavaDoc {
738         FileSystemView JavaDoc fsv = FileSystemView.getFileSystemView();
739         
740         File JavaDoc[] roots = File.listRoots();
741         for (int i = 0; i < roots.length; i++) {
742             FileObject root = FileUtil.toFileObject(roots[i]);
743             if (fsv.isFloppyDrive(roots[i]) || !roots[i].exists()) {
744                assertNull(root);
745                continue;
746             }
747             
748             assertNotNull(roots[i].getAbsolutePath (),root);
749             assertEquals(testedFS, root.getFileSystem());
750             assertTrue(root.isValid());
751         }
752         assertNotNull(testedFS.getRoot());
753         assertTrue(testedFS.getRoot().isValid());
754     }
755     
756     public void testDeserializationOfMasterFSLeadsToTheSameFileSystem () throws Exception JavaDoc {
757         NbMarshalledObject stream = new NbMarshalledObject (testedFS);
758         Object JavaDoc obj = stream.get ();
759         assertSame ("After deserialization it is still the same", testedFS, obj);
760     }
761     
762
763     public void testNormalizeDrivesOnWindows48681 () {
764         if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) {
765             File JavaDoc[] roots = File.listRoots();
766             for (int i = 0; i < roots.length; i++) {
767                 File JavaDoc file = roots[i];
768                 if (FileSystemView.getFileSystemView().isFloppyDrive(file) || !file.exists()) {
769                     continue;
770                 }
771                 File JavaDoc normalizedFile = FileUtil.normalizeFile(file);
772                 File JavaDoc normalizedFile2 = FileUtil.normalizeFile(new File JavaDoc (file, "."));
773                 
774                 assertEquals (normalizedFile.getPath(), normalizedFile2.getPath());
775             }
776             
777         }
778     }
779     
780     public void testJarFileSystemDelete () throws Exception JavaDoc {
781         assertNotNull(root);
782         FileObject folderFo = root.getFileObject("testdir/mountdir7");
783         File JavaDoc folder = FileUtil.toFile(folderFo);
784         assertNotNull(folder);
785         
786         File JavaDoc f = new File JavaDoc (folder,"jfstest.jar");
787         if (!f.exists()) {
788             f.getParentFile().mkdirs();
789             f.createNewFile();
790         }
791         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f));
792         jos.putNextEntry(new ZipEntry JavaDoc ("a/b/c/c.txt"));
793         jos.putNextEntry(new ZipEntry JavaDoc ("a/b/d/d.txt"));
794                         
795        jos.close();
796
797         FileObject parent = FileUtil.toFileObject(f.getParentFile());
798         parent.getChildren();
799         JarFileSystem jfs = new JarFileSystem ();
800         try {
801             jfs.setJarFile(f);
802         } catch (Exception JavaDoc ex) {
803             fail ();
804         }
805         
806
807         ArrayList JavaDoc all = new ArrayList JavaDoc ();
808         FileObject jfsRoot = jfs.getRoot();
809         Enumeration en = jfsRoot.getChildren(true);
810         while (en.hasMoreElements()) {
811             all.add ((FileObject) en.nextElement());
812         }
813
814         assertTrue (all.size() > 0);
815         
816         final ArrayList JavaDoc deleted = new ArrayList JavaDoc ();
817         jfs.addFileChangeListener(new FileChangeAdapter() {
818             public void fileDeleted(FileEvent fe) {
819                 super.fileDeleted(fe);
820                 deleted.add (fe.getFile());
821             }
822         });
823         
824         Thread.sleep(1000);
825         assertTrue (f.getAbsolutePath(), f.delete());
826         parent.refresh();
827         assertEquals (deleted.size(), all.size());
828
829         for (int i = 0; i < all.size(); i++) {
830             FileObject fileObject = (FileObject) all.get(i);
831             assertFalse (fileObject.isValid());
832         }
833         
834         
835         assertFalse (jfsRoot.isValid());
836     }
837     
838     
839     public void testInitialization () throws Exception JavaDoc {
840         assertTrue (MasterFileSystemTest.TestFSProvider.DEFAULT.isInitailized());
841     }
842     
843
844     public void testLockFileAfterCrash() throws Exception JavaDoc {
845         FileObject testFo = FileUtil.createData(root,"/testAfterCrash/testfile.data");
846         File JavaDoc testFile = FileUtil.toFile(testFo);
847   
848         
849         File JavaDoc lockFile = WriteLockUtils.getAssociatedLockFile(testFile);
850         if (!lockFile.exists()) {
851             assertTrue(lockFile.createNewFile());
852         }
853                 
854         assertTrue(lockFile.exists());
855
856         FileObject lockFo = FileUtil.toFileObject(lockFile);
857         assertNull(lockFo);
858         testFo.delete();
859         
860         
861         lockFo = FileUtil.toFileObject(lockFile);
862         String JavaDoc msg = (lockFo != null) ? lockFo.toString() : "";
863         assertNull(msg,lockFo);
864     }
865     
866     private class IgnoreDirFileSystem extends LocalFileSystem {
867         org.openide.filesystems.FileSystem.Status status = new org.openide.filesystems.FileSystem.HtmlStatus() {
868             public String JavaDoc annotateName (String JavaDoc name, java.util.Set JavaDoc files) {
869                 java.lang.StringBuffer JavaDoc sb = new StringBuffer JavaDoc (name);
870                 Iterator it = files.iterator ();
871                 while (it.hasNext()) {
872                     FileObject fo = (FileObject)it.next();
873                     try {
874                         if (fo.getFileSystem() instanceof IgnoreDirFileSystem) {
875                             sb.append ("," +fo.getNameExt());//NOI18N
876
}
877                     } catch (Exception JavaDoc ex) {
878                         fail ();
879                     }
880                 }
881                                 
882                 return sb.toString () ;
883             }
884
885             public java.awt.Image JavaDoc annotateIcon (java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc files) {
886                 return icon;
887             }
888
889             public String JavaDoc annotateNameHtml(String JavaDoc name, Set files) {
890                 return annotateName (name, files);
891             }
892             
893         };
894         
895         public org.openide.filesystems.FileSystem.Status getStatus() {
896             return status;
897         }
898         
899         protected String JavaDoc[] children(String JavaDoc name) {
900             String JavaDoc[] strings = super.children(name);
901             return strings;
902         }
903     }
904 }
905
Popular Tags