KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FileUtilTestHidden


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.openide.filesystems;
21
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import org.openide.util.Utilities;
28
29 public class FileUtilTestHidden extends TestBaseHid {
30     private FileObject root = null;
31
32     protected String JavaDoc[] getResources(String JavaDoc testName) {
33         return new String JavaDoc[]{
34             "fileutildir/tofile.txt",
35             "fileutildir/tofileobject.txt",
36             "fileutildir/isParentOf.txt",
37             "fileutildir/fileutildir2/fileutildir3",
38             "fileutildir/fileutildir2/folder/file"
39         };
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43         super.setUp();
44         Repository.getDefault().addFileSystem(testedFS);
45         root = testedFS.findResource(getResourcePrefix());
46     }
47
48     protected void tearDown() throws Exception JavaDoc {
49         Repository.getDefault().removeFileSystem(testedFS);
50         super.tearDown();
51     }
52
53     /** Creates new FileObjectTestHidden */
54     public FileUtilTestHidden(String JavaDoc name) {
55         super(name);
56     }
57
58     public void testCreateFolder () throws Exception JavaDoc {
59         if (this.testedFS instanceof JarFileSystem) return;
60         
61         assertNotNull(root);
62         FileObject folder = root.getFileObject("fileutildir");
63         FileObject result = FileUtil.createFolder(folder, "fileutildir2/folder/fileutildir4");
64         assertNotNull(result);
65         assertSame(result, root.getFileObject("fileutildir/fileutildir2/folder/fileutildir4"));
66     }
67     
68     public void testNormalizeFile () throws Exception JavaDoc {
69        File JavaDoc file = getWorkDir ();
70        file = FileUtil.normalizeFile(file);
71        
72        File JavaDoc file2 = FileUtil.normalizeFile(file);
73        assertSame(file, file2);
74        
75        file2 = new File JavaDoc (file, "test/..");
76        file2 = FileUtil.normalizeFile(file);
77        assertEquals(file2, file);
78     }
79     
80     public void testNormalizeFile2 () throws Exception JavaDoc {
81         if (!Utilities.isWindows()) return;
82         
83         File JavaDoc rootFile = FileUtil.toFile(root);
84         if (rootFile == null) return;
85         assertTrue(rootFile.exists());
86         
87         File JavaDoc testFile = new File JavaDoc (rootFile, "abc.txt");
88         assertTrue(testFile.createNewFile());
89         assertTrue(testFile.exists());
90         
91         File JavaDoc testFile2 = new File JavaDoc (rootFile, "ABC.TXT");
92         assertTrue(testFile2.exists());
93         
94         assertSame(testFile, FileUtil.normalizeFile(testFile));
95         assertNotSame(testFile2, FileUtil.normalizeFile(testFile2));
96     }
97     
98     public void testFindFreeFolderName () throws Exception JavaDoc {
99         if (this.testedFS.isReadOnly()) return;
100
101         String JavaDoc name = FileUtil.findFreeFolderName(root, "fileutildir".toUpperCase());
102         root.createFolder(name);
103     }
104     
105     public void testToFile() throws Exception JavaDoc {
106         if (this.testedFS instanceof JarFileSystem) return;
107         
108         assertNotNull(root);
109         FileObject testFo = root.getFileObject("fileutildir/tofile.txt");
110         assertNotNull(testFo);
111
112         File JavaDoc testFile = FileUtil.toFile(testFo);
113         assertNotNull(testFile);
114         assertTrue(testFile.exists());
115     }
116     
117     
118     public void testIsArchiveFile () throws Exception JavaDoc {
119         final String JavaDoc base = getWorkDir().toURI().toURL().toExternalForm();
120         URL JavaDoc url = new URL JavaDoc (base + "test.jar"); //NOI18N
121
assertTrue ("test.jar has to be an archive",FileUtil.isArchiveFile (url)); //NOI18N
122
url = new URL JavaDoc (base + ".hidden.jar"); //NOI18N
123
assertTrue (FileUtil.isArchiveFile (url)); //NOI18N
124
url = new URL JavaDoc (base + "folder"); //NOI18N
125
assertFalse ("folder cannot to be an archive", FileUtil.isArchiveFile (url)); //NOI18N
126
url = new URL JavaDoc (base + "folder/"); //NOI18N
127
assertFalse ("folder cannot to be an archive", FileUtil.isArchiveFile (url)); //NOI18N
128
url = new URL JavaDoc (base + ".hidden"); //NOI18N
129
assertFalse (".hidden cannot to be an archive", FileUtil.isArchiveFile (url)); //NOI18N
130
url = new URL JavaDoc (base + ".hidden/"); //NOI18N
131
assertFalse (".hidden cannot to be an archive", FileUtil.isArchiveFile (url)); //NOI18N
132
}
133
134
135     public void testToFileObject () throws Exception JavaDoc {
136         if (this.testedFS instanceof JarFileSystem) return;
137         
138         assertNotNull(root);
139         FileObject testFo = root.getFileObject("fileutildir/tofileobject.txt");
140         assertNotNull(testFo);
141
142         File JavaDoc rootFile = FileUtil.toFile(root);
143         assertNotNull(rootFile);
144         assertTrue(rootFile.exists());
145
146         File JavaDoc testFile = new File JavaDoc (rootFile, "fileutildir/tofileobject.txt");
147         assertNotNull(testFile);
148         assertTrue(testFile.exists());
149
150         FileObject testFo2 = FileUtil.toFileObject(testFile);
151         assertNotNull(testFo2);
152         assertEquals(testFo2, testFo);
153     }
154
155     public void testIsParentOf () throws Exception JavaDoc {
156         if (this.testedFS instanceof JarFileSystem) return;
157         
158         final List JavaDoc<FileEvent> events = new ArrayList JavaDoc<FileEvent>();
159         assertNotNull(root);
160         final FileObject parent = root.getFileObject("fileutildir");
161         assertNotNull(parent);
162         
163         final FileObject child = root.getFileObject("fileutildir/isParentOf.txt");
164         assertNotNull(child);
165         
166         assertTrue(FileUtil.isParentOf(parent, child));
167                 
168         child.addFileChangeListener(new FileChangeAdapter() {
169             public void fileDeleted(FileEvent fe) {
170                 FileObject file = fe.getFile();
171                 assertTrue(FileUtil.isParentOf(parent, file));
172                 assertEquals(parent, file.getParent());
173                 events.add(fe);
174             }
175         });
176         child.delete();
177         assertTrue (events.size() == 1);
178         assertNull(root.getFileObject("fileutildir/isParentOf.txt"));
179         
180     }
181
182
183     public void testIsParentOf3 () throws Exception JavaDoc {
184         if (this.testedFS instanceof JarFileSystem) return;
185         
186         final List JavaDoc<FileEvent> events = new ArrayList JavaDoc<FileEvent>();
187         assertNotNull(root);
188         final FileObject[] fileObjects = new FileObject[]{
189             root.getFileObject("fileutildir"),
190             root.getFileObject("fileutildir/fileutildir2"),
191             root.getFileObject("fileutildir/fileutildir2/fileutildir3")
192         };
193         
194         for (int i = 0; i < fileObjects.length; i++) {
195             FileObject fo = fileObjects[i];
196             assertNotNull(fo);
197         }
198                 
199         assertTrue(FileUtil.isParentOf(root, fileObjects[0]));
200         assertTrue(FileUtil.isParentOf(fileObjects[0], fileObjects[1]));
201         assertTrue(FileUtil.isParentOf(fileObjects[1], fileObjects[2]));
202                 
203         testedFS.addFileChangeListener(new FileChangeAdapter() {
204             public void fileDeleted(FileEvent fe) {
205                 FileObject file = fe.getFile();
206                 assertNotNull(file.getPath(),file.getParent());
207                 assertTrue(file.getPath(), FileUtil.isParentOf(root, file));
208                 events.add(fe);
209             }
210         });
211         fileObjects[1].delete();
212         assertTrue (events.size() > 0);
213     }
214     
215     public void testGetFileDisplayName () throws Exception JavaDoc {
216         final FileObject[] fileObjects = new FileObject[]{
217             root,
218             root.getFileObject("fileutildir"),
219             root.getFileObject("fileutildir/fileutildir2")
220         };
221
222         for (int i = 0; i < fileObjects.length; i++) {
223             FileObject fo = fileObjects[i];
224             assertNotNull(fo);
225             String JavaDoc displayName = FileUtil.getFileDisplayName(fo);
226             File JavaDoc f = FileUtil.toFile(fo);
227             if (f != null) {
228                 assertEquals(f.getAbsolutePath(), displayName);
229             } else {
230                 FileObject archivFo = FileUtil.getArchiveFile (fo);
231                 File JavaDoc archiv = (archivFo != null) ? FileUtil.toFile(archivFo) : null;
232                 if (archiv != null) {
233                     if (fo.isRoot()) {
234                         assertEquals(displayName, archiv.getAbsolutePath());
235                     } else {
236                         assertTrue(displayName.indexOf(archiv.getAbsolutePath()) != -1);
237                     }
238                 } else {
239                     if (fo.isRoot()) {
240                         assertEquals(displayName, fo.getFileSystem().getDisplayName());
241                     } else {
242                         assertTrue(displayName.indexOf(fo.getFileSystem().getDisplayName()) != -1);
243                     }
244                 }
245             }
246         }
247     }
248 }
249
Popular Tags