1 19 20 package org.netbeans.modules.masterfs.providers; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Set ; 35 import junit.framework.AssertionFailedError; 36 import org.netbeans.junit.NbTestCase; 37 import org.netbeans.modules.masterfs.filebasedfs.FileBasedFileSystem; 38 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 39 import org.openide.filesystems.FileChangeAdapter; 40 import org.openide.filesystems.FileChangeListener; 41 import org.openide.filesystems.FileEvent; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileRenameEvent; 45 import org.openide.filesystems.FileStateInvalidException; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.Lookup; 48 import org.openide.util.lookup.Lookups; 49 50 54 public class ProvidedExtensionsTest extends NbTestCase { 55 private ProvidedExtensionsImpl iListener; 56 protected void setUp() throws Exception { 57 super.setUp(); 58 AnnotationProvider provider = (AnnotationProvider)Lookups.metaInfServices( 59 Thread.currentThread().getContextClassLoader()).lookup(AnnotationProvider.class); 60 assertNotNull(provider); 61 iListener = lookupImpl(); 62 assertNotNull(iListener); 63 clearWorkDir(); 64 } 65 66 private ProvidedExtensionsImpl lookupImpl() { 67 Lookup.Result result = Lookups.metaInfServices(Thread.currentThread().getContextClassLoader()). 68 lookup(new Lookup.Template(AnnotationProvider.class)); 69 Collection all = result.allInstances(); 70 for (Iterator it = all.iterator(); it.hasNext();) { 71 AnnotationProvider ap = (AnnotationProvider) it.next(); 72 InterceptionListener iil = ap.getInterceptionListener(); 73 if (iil instanceof ProvidedExtensions) { 74 return (ProvidedExtensionsImpl)iil; 75 } 76 } 77 return null; 78 } 79 80 public ProvidedExtensionsTest(String testName) { 81 super(testName); 82 } 83 84 85 public void testImplsBeforeChange() throws IOException { 86 FileObject fo = FileUtil.toFileObject(getWorkDir()); 87 assertNotNull(fo); 88 assertNotNull(iListener); 89 FileObject toChange = fo.createData("aa"); 90 assertNotNull(toChange); 91 OutputStream os = toChange.getOutputStream(); 92 try { 93 assertEquals(1, iListener.implsBeforeChangeCalls); 94 } finally { 95 os.close(); 96 } 97 98 } 99 100 public void testImplsMove() throws IOException { 101 FileObject fo = FileUtil.toFileObject(getWorkDir()); 102 assertNotNull(fo); 103 assertNotNull(iListener); 104 FileObject toMove = fo.createData("aa"); 105 assertNotNull(toMove); 106 FileObject whereToMove = fo.createFolder("aafolder"); 107 assertNotNull(whereToMove); 108 109 iListener.clear(); 110 FileLock lock = toMove.lock(); 111 iListener.setLock(lock); 112 try { 113 assertEquals(0,iListener.implsMoveCalls); 114 assertEquals(0,iListener.moveImplCalls); 115 iListener.setImplsMoveRetVal(true); 116 assertNotNull(toMove.move(lock, whereToMove, toMove.getName(), toMove.getExt())); 117 assertEquals(1,iListener.implsMoveCalls); 118 assertEquals(1,iListener.moveImplCalls); 119 } finally { 120 if (lock != null) { 121 iListener.setLock(null); 122 lock.releaseLock(); 123 } 124 } 125 } 126 127 public void testImplsMove2() throws IOException { 128 FileObject fo = FileUtil.toFileObject(getWorkDir()); 129 assertNotNull(fo); 130 assertNotNull(iListener); 131 FileObject toMove = fo.createData("aa"); 132 assertNotNull(toMove); 133 FileObject whereToMove = fo.createFolder("aafolder"); 134 assertNotNull(whereToMove); 135 136 iListener.clear(); 137 FileLock lock = toMove.lock(); 138 iListener.setLock(lock); 139 try { 140 assertEquals(0,iListener.implsMoveCalls); 141 assertEquals(0,iListener.moveImplCalls); 142 iListener.setImplsMoveRetVal(false); 143 assertNotNull(toMove.move(lock, whereToMove, toMove.getName(), toMove.getExt())); 144 assertEquals(1,iListener.implsMoveCalls); 145 assertEquals(0,iListener.moveImplCalls); 146 } finally { 147 if (lock != null) { 148 iListener.setLock(null); 149 lock.releaseLock(); 150 } 151 } 152 } 153 154 public void testImplsRename() throws IOException { 155 FileObject fo = FileUtil.toFileObject(getWorkDir()); 156 assertNotNull(fo); 157 assertNotNull(iListener); 158 FileObject toRename = fo.createData("aa"); 159 assertNotNull(toRename); 160 161 iListener.clear(); 162 FileLock lock = toRename.lock(); 163 iListener.setLock(lock); 164 try { 165 assertEquals(0,iListener.implsRenameCalls); 166 assertEquals(0,iListener.renameImplCalls); 167 iListener.setImplsRenameRetVal(true); 168 assertTrue(toRename.isValid()); 169 assertNull(toRename.getParent().getFileObject(toRename.getExt(), toRename.getName())); 170 toRename.rename(lock,toRename.getExt(), toRename.getName()); 171 assertEquals(1,iListener.implsRenameCalls); 172 assertEquals(1,iListener.renameImplCalls); 173 } finally { 174 if (lock != null) { 175 iListener.setLock(null); 176 lock.releaseLock(); 177 } 178 } 179 } 180 181 public void testImplsRename2() throws IOException { 182 final List events = new ArrayList (); 183 FileObject fo = FileUtil.toFileObject(getWorkDir()); 184 assertNotNull(fo); 185 final FileObject toRename = fo.createData(getName()); 186 assertNotNull(toRename); 187 188 iListener.clear(); 189 FileLock lock = toRename.lock(); 190 iListener.setLock(lock); 191 FileChangeListener fcl = null; 192 try { 193 final String origNameExt = toRename.getNameExt(); 194 final String origPath = toRename.getPath(); 195 final File origFile = FileUtil.toFile(toRename); 196 197 iListener.setImplsRenameRetVal(true); 198 fcl = new FileChangeAdapter() { 199 public void fileRenamed(FileRenameEvent fe) { 200 events.add(fe); 201 assertFalse(fe.getFile().getNameExt().equals(origNameExt)); 202 assertNull(toRename.getParent().getFileObject(origNameExt)); 203 File f = FileUtil.toFile(toRename); 204 assertNotNull(f); 205 assertNotNull(FileUtil.toFileObject(f)); 206 assertSame(toRename, FileUtil.toFileObject(f)); 207 208 assertTrue(f.exists()); 209 FileObject delegate = FileBasedFileSystem.getFileObject(f); 210 assertNotNull(delegate); 211 assertTrue(delegate.isValid()); 212 assertNull(FileBasedFileSystem.getFileObject(origFile)); 213 assertTrue(toRename.isValid()); 214 215 assertSame(toRename, fe.getFile()); 216 assertSame(toRename.getParent(), fe.getFile().getParent()); 217 try { 218 assertNotNull(toRename.getFileSystem().findResource(toRename.getPath())); 219 assertNull(toRename.getFileSystem().findResource(origPath)); 220 221 } catch (FileStateInvalidException ex) { 222 fail(); 223 } 224 225 assertEquals("bb",toRename.getName()); 226 assertEquals("ext",toRename.getExt()); 227 assertFalse(origNameExt.equals(toRename.getNameExt())); 228 229 assertEquals("bb",fe.getFile().getName()); 230 assertEquals("ext",fe.getFile().getExt()); 231 assertFalse(origNameExt.equals(fe.getFile().getNameExt())); 232 233 assertFalse(fe.getName().equals(fe.getFile().getName())); 234 assertFalse(fe.getExt().equals(fe.getFile().getName())); 235 236 toRename.refresh(); 238 toRename.getParent().refresh(); 239 } 240 241 public void fileChanged(FileEvent fe) { 242 fail(); 243 } 244 245 public void fileDeleted(FileEvent fe) { 246 fail(); 247 } 248 249 public void fileDataCreated(FileEvent fe) { 250 fail(); 251 } 252 253 public void fileFolderCreated(FileEvent fe) { 254 fail(); 255 } 256 }; 257 toRename.getParent().addFileChangeListener(fcl); 258 toRename.addFileChangeListener(fcl); 259 toRename.getFileSystem().addFileChangeListener(fcl); 260 toRename.rename(lock,"bb", "ext"); 261 assertNull(toRename.getParent().getFileObject(origNameExt)); 262 263 } finally { 264 toRename.getParent().removeFileChangeListener(fcl); 265 toRename.removeFileChangeListener(fcl); 266 toRename.getFileSystem().removeFileChangeListener(fcl); 267 268 if (lock != null) { 269 iListener.setLock(null); 270 lock.releaseLock(); 271 } 272 } 273 assertEquals(3,events.size()); 274 } 275 276 public void testImplsRename3() throws IOException { 277 FileObject fo = FileUtil.toFileObject(getWorkDir()); 278 assertNotNull(fo); 279 final FileObject toRename = fo.createData("aa"); 280 assertNotNull(toRename); 281 282 iListener.clear(); 283 FileLock lock = toRename.lock(); 284 iListener.setLock(lock); 285 try { 286 final String origNameExt = toRename.getNameExt(); 287 final String origPath = toRename.getPath(); 288 final File origFile = FileUtil.toFile(toRename); 289 final File newFile = new File (origFile.getParentFile(),FileInfo.composeName("bb", "ext")); 290 291 292 iListener.setImplsRenameRetVal(true); 293 FileChangeListener fcl = new FileChangeAdapter() { 294 public void fileRenamed(FileRenameEvent fe) { 295 try { 296 File f = FileUtil.toFile(toRename); 297 assertNotNull(f); 298 assertNotNull(FileUtil.toFileObject(f)); 299 assertSame(toRename, FileUtil.toFileObject(f)); 300 301 assertTrue(f.exists()); 302 FileObject delegate = FileBasedFileSystem.getFileObject(f); 303 assertNotNull(delegate); 304 assertTrue(delegate.isValid()); 305 if (fe.getFile().getNameExt().equals(newFile.getName())) { 306 assertNull(FileBasedFileSystem.getFileObject(origFile)); 307 assertNotNull(FileBasedFileSystem.getFileObject(newFile)); 308 assertNotNull(FileUtil.toFileObject(newFile)); 309 assertNull(FileUtil.toFileObject(origFile)); 310 } else { 311 assertNotNull(FileBasedFileSystem.getFileObject(origFile)); 312 assertNull(FileBasedFileSystem.getFileObject(newFile)); 313 assertNull(FileUtil.toFileObject(newFile)); 314 assertNotNull(FileUtil.toFileObject(origFile)); 315 } 316 assertTrue(toRename.isValid()); 317 318 assertSame(toRename, fe.getFile()); 319 assertSame(toRename.getParent(), fe.getFile().getParent()); 320 try { 321 assertNotNull(toRename.getFileSystem().findResource(toRename.getPath())); 322 323 } catch (FileStateInvalidException ex) { 324 fail(); 325 } 326 327 assertFalse(fe.getName().equals(fe.getFile().getName())); 328 assertFalse(fe.getExt().equals(fe.getFile().getName())); 329 330 toRename.refresh(); 332 toRename.getParent().refresh(); 333 } catch(AssertionFailedError afe) { 334 afe.printStackTrace(); 335 throw afe; 336 } 337 } 338 339 public void fileChanged(FileEvent fe) { 340 fail(); 341 } 342 343 public void fileDeleted(FileEvent fe) { 344 fail(); 345 } 346 347 public void fileDataCreated(FileEvent fe) { 348 fail(); 349 } 350 351 public void fileFolderCreated(FileEvent fe) { 352 fail(); 353 } 354 }; 355 toRename.getParent().addFileChangeListener(fcl); 356 toRename.addFileChangeListener(fcl); 357 toRename.getFileSystem().addFileChangeListener(fcl); 358 toRename.rename(lock,"bb", "ext"); 359 assertNull(toRename.getParent().getFileObject(origNameExt)); 360 toRename.rename(lock,"aa", ""); 361 toRename.rename(lock,"bb", "ext"); 362 toRename.rename(lock,"aa", ""); 363 toRename.rename(lock,"bb", "ext"); 364 toRename.rename(lock,"aa", ""); 365 } finally { 366 if (lock != null) { 367 iListener.setLock(null); 368 lock.releaseLock(); 369 } 370 } 371 } 372 373 374 public static class AnnotationProviderImpl extends InterceptionListenerTest.AnnotationProviderImpl { 375 private ProvidedExtensionsImpl impl = new ProvidedExtensionsImpl(); 376 public InterceptionListener getInterceptionListener() { 377 return impl; 378 } 379 } 380 381 public static class ProvidedExtensionsImpl extends ProvidedExtensions { 382 private int implsMoveCalls; 383 private int moveImplCalls; 384 private int implsRenameCalls; 385 private int renameImplCalls; 386 private int implsBeforeChangeCalls; 387 388 private static boolean implsMoveRetVal = true; 389 private static boolean implsRenameRetVal = true; 390 private static boolean implsDeleteRetVal = true; 391 392 public static FileLock lock; 393 394 public void clear() { 395 implsMoveCalls = 0; 396 moveImplCalls = 0; 397 implsRenameCalls = 0; 398 renameImplCalls = 0; 399 implsBeforeChangeCalls = 0; 400 } 401 402 public void beforeChange(FileObject f) { 403 assertNotNull(FileUtil.toFile(f)); 404 implsBeforeChangeCalls++; 405 } 406 407 public ProvidedExtensions.DeleteHandler getDeleteHandler(File f) { 408 return (!isImplsDeleteRetVal()) ? null : new ProvidedExtensions.DeleteHandler(){ 409 final Set s = new HashSet (); 410 public boolean delete(File file) { 411 if (file.isDirectory()) { 412 File [] childs = file.listFiles(new FileFilter () { 413 public boolean accept(File pathname) { 414 boolean accepted = pathname.isFile(); 415 if (!accepted && pathname.isDirectory()) { 416 accepted = !s.contains(pathname); 417 if (!s.contains(pathname)) { 418 s.add(pathname); 419 } 420 } 421 return accepted; 422 } 423 }); 424 return childs.length == 0; 425 } 426 return file.delete(); 427 } 428 }; 429 } 430 431 public ProvidedExtensions.IOHandler getRenameHandler(final File from, final String newName) { 432 implsRenameCalls++; 433 final File f = new File (from.getParentFile(),newName); 434 435 return (!isImplsRenameRetVal()) ? null : new ProvidedExtensions.IOHandler(){ 436 public void handle() throws IOException { 437 renameImplCalls++; 438 assertTrue(from.renameTo(f)); 439 } 440 }; 441 } 442 443 public ProvidedExtensions.IOHandler getMoveHandler(final File from, final File to) { 444 implsMoveCalls++; 445 return (!isImplsMoveRetVal()) ? null : new ProvidedExtensions.IOHandler(){ 446 public void handle() throws IOException { 447 moveImplCalls++; 448 if (to.exists()) { 449 throw new IOException (); 450 } 451 assertTrue(from.exists()); 452 assertFalse(to.exists()); 453 454 assertFalse(from.equals(to)); 455 InputStream inputStream = new FileInputStream (from); 456 OutputStream outputStream = new FileOutputStream (to); 457 try { 458 FileUtil.copy(inputStream, outputStream); 459 } finally { 460 if (inputStream != null) inputStream.close(); 461 if (outputStream != null) outputStream.close(); 462 } 463 assertTrue(from.delete()); 464 465 assertFalse(from.exists()); 466 assertTrue(to.exists()); 467 } 468 }; 469 } 470 471 public static FileLock getLock() { 472 return lock; 473 } 474 475 public static void setLock(FileLock lock) { 476 ProvidedExtensionsImpl.lock = lock; 477 } 478 479 public static boolean isImplsMoveRetVal() { 480 return implsMoveRetVal; 481 } 482 483 public static void setImplsMoveRetVal(boolean implsMoveRetVal) { 484 ProvidedExtensionsImpl.implsMoveRetVal = implsMoveRetVal; 485 } 486 487 public static boolean isImplsRenameRetVal() { 488 return implsRenameRetVal; 489 } 490 491 public static void setImplsRenameRetVal(boolean implsRenameRetVal) { 492 ProvidedExtensionsImpl.implsRenameRetVal = implsRenameRetVal; 493 } 494 495 public static boolean isImplsDeleteRetVal() { 496 return implsDeleteRetVal; 497 } 498 499 public static void setImplsDeleteRetVal(boolean implsDeleteRetVal) { 500 ProvidedExtensionsImpl.implsDeleteRetVal = implsDeleteRetVal; 501 } 502 } 503 } 504 | Popular Tags |