1 19 20 package org.openide.filesystems; 21 22 23 import java.io.File ; 24 import java.net.URL ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import org.openide.util.Utilities; 28 29 public class FileUtilTestHidden extends TestBaseHid { 30 private FileObject root = null; 31 32 protected String [] getResources(String testName) { 33 return new String []{ 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 { 43 super.setUp(); 44 Repository.getDefault().addFileSystem(testedFS); 45 root = testedFS.findResource(getResourcePrefix()); 46 } 47 48 protected void tearDown() throws Exception { 49 Repository.getDefault().removeFileSystem(testedFS); 50 super.tearDown(); 51 } 52 53 54 public FileUtilTestHidden(String name) { 55 super(name); 56 } 57 58 public void testCreateFolder () throws Exception { 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 { 69 File file = getWorkDir (); 70 file = FileUtil.normalizeFile(file); 71 72 File file2 = FileUtil.normalizeFile(file); 73 assertSame(file, file2); 74 75 file2 = new File (file, "test/.."); 76 file2 = FileUtil.normalizeFile(file); 77 assertEquals(file2, file); 78 } 79 80 public void testNormalizeFile2 () throws Exception { 81 if (!Utilities.isWindows()) return; 82 83 File rootFile = FileUtil.toFile(root); 84 if (rootFile == null) return; 85 assertTrue(rootFile.exists()); 86 87 File testFile = new File (rootFile, "abc.txt"); 88 assertTrue(testFile.createNewFile()); 89 assertTrue(testFile.exists()); 90 91 File testFile2 = new File (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 { 99 if (this.testedFS.isReadOnly()) return; 100 101 String name = FileUtil.findFreeFolderName(root, "fileutildir".toUpperCase()); 102 root.createFolder(name); 103 } 104 105 public void testToFile() throws Exception { 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 testFile = FileUtil.toFile(testFo); 113 assertNotNull(testFile); 114 assertTrue(testFile.exists()); 115 } 116 117 118 public void testIsArchiveFile () throws Exception { 119 final String base = getWorkDir().toURI().toURL().toExternalForm(); 120 URL url = new URL (base + "test.jar"); assertTrue ("test.jar has to be an archive",FileUtil.isArchiveFile (url)); url = new URL (base + ".hidden.jar"); assertTrue (FileUtil.isArchiveFile (url)); url = new URL (base + "folder"); assertFalse ("folder cannot to be an archive", FileUtil.isArchiveFile (url)); url = new URL (base + "folder/"); assertFalse ("folder cannot to be an archive", FileUtil.isArchiveFile (url)); url = new URL (base + ".hidden"); assertFalse (".hidden cannot to be an archive", FileUtil.isArchiveFile (url)); url = new URL (base + ".hidden/"); assertFalse (".hidden cannot to be an archive", FileUtil.isArchiveFile (url)); } 133 134 135 public void testToFileObject () throws Exception { 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 rootFile = FileUtil.toFile(root); 143 assertNotNull(rootFile); 144 assertTrue(rootFile.exists()); 145 146 File testFile = new File (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 { 156 if (this.testedFS instanceof JarFileSystem) return; 157 158 final List <FileEvent> events = new ArrayList <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 { 184 if (this.testedFS instanceof JarFileSystem) return; 185 186 final List <FileEvent> events = new ArrayList <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 { 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 displayName = FileUtil.getFileDisplayName(fo); 226 File f = FileUtil.toFile(fo); 227 if (f != null) { 228 assertEquals(f.getAbsolutePath(), displayName); 229 } else { 230 FileObject archivFo = FileUtil.getArchiveFile (fo); 231 File 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 |