Java Code Examples for org.openide.filesystems.FileObject#rename()

The following examples show how to use org.openide.filesystems.FileObject#rename() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfilerStorageProvider.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void loadProjectProperties(Properties properties, Lookup.Provider project, String filename) throws IOException {
    FileObject folder = getProjectFolder(project, false);
    if (folder == null) return;

    FileObject fo = folder.getFileObject(filename, EXT);
    if (fo == null) {
        // Convert PP storage .pp to .xml
        fo = folder.getFileObject(filename, "pp"); // NOI18N
        if (fo != null) {
            FileLock fol = fo.lock();
            try { fo.rename(fol, filename, EXT); }
            finally { fol.releaseLock(); }
        }
    }
    if (fo == null) return;

    loadProperties(properties, fo);
}
 
Example 2
Source File: SourceTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMimeTypeChange() throws IOException {
    clearWorkDir();
    final FileObject file = createFileObject("empty.foo", "", "\n");
    final Source source = Source.create(file);
    assertNotNull("No Source for " + file, source);
    assertSame("Wrong FileObject", file, source.getFileObject());
    assertSame("Inconsistent Source.create(FileObject)", source, Source.create(file));
    final FileLock lock = file.lock();
    try {
        file.rename(lock, "empty", "txt");  //NOI18N
    } finally {
        lock.releaseLock();
    }
    final Source source2 = Source.create(file);
    assertNotNull("No Source for " + file, source2);
    assertSame("Wrong FileObject", file, source2.getFileObject());
    assertNotSame("Inconsistent Source.create(FileObject)", source, source2);
}
 
Example 3
Source File: VCSInterceptorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testFileCreatedLockedRenamedDeleted() throws IOException {
    File f = new File(dataRootDir, "workdir/root-test-versioned");
    FileObject fo = FileUtil.toFileObject(f);
    fo = fo.createData("deleteme.txt");
    File file = FileUtil.toFile(fo);
    FileLock lock = fo.lock();
    fo.rename(lock, "deleteme", "now");
    lock.releaseLock();
    File file2 = FileUtil.toFile(fo);
    fo.delete();
    assertTrue(inteceptor.getBeforeCreateFiles().contains(file));
    assertTrue(inteceptor.getDoCreateFiles().contains(file));
    assertTrue(inteceptor.getCreatedFiles().contains(file));
    assertTrue(inteceptor.getBeforeEditFiles().contains(file));
    assertTrue(inteceptor.getBeforeMoveFiles().contains(file));
    assertTrue(inteceptor.getAfterMoveFiles().contains(file));
    assertTrue(inteceptor.getBeforeDeleteFiles().contains(file2));
    assertTrue(inteceptor.getDoDeleteFiles().contains(file2));
    assertTrue(inteceptor.getDeletedFiles().contains(file2));
}
 
Example 4
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRenameWithAttributes() throws Exception {
    final FileObject workDirFo = FileBasedFileSystem.getFileObject(getWorkDir());
    FileObject folder = workDirFo.createFolder("a");
    folder.createData("non.empty");
    folder.setAttribute("name", "jmeno");
    assertEquals("jmeno", folder.getAttribute("name"));
    FileLock lock = folder.lock();
    folder.rename(lock, "b", null);
    lock.releaseLock();
    assertEquals("Name is not b", "b", folder.getNameExt());
    WeakReference<?> ref = new WeakReference<FileObject>(folder);
    folder = null;
    assertGC("Folder can disappear", ref);
    folder = workDirFo.getFileObject("b");
    assertNotNull("Folder b found", folder);
    assertEquals("The attribute remains even after rename", "jmeno", folder.getAttribute("name"));
    assertEquals("One children", 1, folder.getChildren().length);
}
 
Example 5
Source File: ProvidedExtensionsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testImplsRename() throws IOException {
    FileObject fo = FileUtil.toFileObject(getWorkDir());
    assertNotNull(fo);
    assertNotNull(iListener);
    FileObject toRename = fo.createData("aa");
    assertNotNull(toRename);
    
    iListener.clear();
    FileLock lock = toRename.lock();
    iListener.setLock(lock);
    try {
        assertEquals(0,iListener.implsRenameCalls);
        assertEquals(0,iListener.renameImplCalls);
        iListener.setImplsRenameRetVal(true);
        assertTrue(toRename.isValid());
        assertNull(toRename.getParent().getFileObject(toRename.getExt(), toRename.getName()));
        toRename.rename(lock,toRename.getExt(), toRename.getName());
        assertEquals(1,iListener.implsRenameCalls);
        assertEquals(1,iListener.renameImplCalls);
    } finally {
        if (lock != null) {
            iListener.setLock(null);
            lock.releaseLock();
        }
    }
}
 
Example 6
Source File: EGTaskDataObjectDataObject.java    From BART with MIT License 6 votes vote down vote up
@Override
protected FileObject handleRename(String name) throws IOException {
    StringBuilder path = new StringBuilder("statistics/");
    path.append(getPrimaryFile().getName());
    FileObject statFolder = FileUtil.getConfigFile(path.toString());
    if(statFolder != null)   {
        FileLock lockStatFolder = null;
        try{           
            lockStatFolder = statFolder.lock();
            statFolder.rename(lockStatFolder, name, null);            
        }catch(IOException ex)   {
            ErrorManager.getDefault().notify(ErrorManager.WARNING,ex);
        }finally{
            if(lockStatFolder != null)   {
                lockStatFolder.releaseLock();
            }
        }
    }
    return super.handleRename(name);
}
 
Example 7
Source File: FilesystemInterceptorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void renameFO(File from, File to) throws DataObjectNotFoundException, IOException {
    // ensure parent is known by filesystems
    // otherwise no event will be thrown
    FileObject parent = FileUtil.toFileObject(from.getParentFile());
    FileObject foFrom = FileUtil.toFileObject(from);
    FileLock lock = foFrom.lock();
    try {
        foFrom.rename(lock, to.getName(), null);
    } finally {
        lock.releaseLock();
    }
}
 
Example 8
Source File: WSITRefactoringPlugin.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void performChange() {
    FileLock lock = null;
    FileObject parentFile = getParentFile();
    try {
        lock = parentFile.lock();
        parentFile.rename(lock, newConfigName, getParentFile().getExt());
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (lock != null) lock.releaseLock();
    }
}
 
Example 9
Source File: RepositoryUpdaterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testIssue171719() throws Exception {
    final TestHandler handler = new TestHandler();
    final Logger logger = Logger.getLogger(RepositoryUpdater.class.getName()+".tests");
    logger.setLevel (Level.FINEST);
    logger.addHandler(handler);
    indexerFactory.indexer.setExpectedFile(customFiles, new URL[0], new URL[0]);
    eindexerFactory.indexer.setExpectedFile(embeddedFiles, new URL[0], new URL[0]);
    MutableClassPathImplementation mcpi1 = new MutableClassPathImplementation ();
    mcpi1.addResource(this.srcRootWithFiles1);
    ClassPath cp1 = ClassPathFactory.createClassPath(mcpi1);
    globalPathRegistry_register(SOURCES,new ClassPath[]{cp1});
    assertTrue (handler.await());
    assertEquals(0, handler.getBinaries().size());
    assertEquals(1, handler.getSources().size());
    assertEquals(this.srcRootWithFiles1.toURL(), handler.getSources().get(0));
    assertTrue(indexerFactory.indexer.awaitIndex(TIME));
    assertTrue(eindexerFactory.indexer.awaitIndex());

    File root = FileUtil.toFile(srcRootWithFiles1);
    File fdf = new File (root, "direct.emb");   //NOI18N
    eindexerFactory.indexer.setExpectedFile(new URL[]{org.openide.util.Utilities.toURI(fdf).toURL()}, new URL[0], new URL[0]);
    FileObject df = FileUtil.createData(fdf);
    assertNotNull(df);
    assertEquals(EMIME, df.getMIMEType());
    eindexerFactory.indexer.awaitIndex();

    File newfdf = new File (root, "new_direct.emb");   //NOI18N
    eindexerFactory.indexer.setExpectedFile(new URL[]{org.openide.util.Utilities.toURI(newfdf).toURL()}, new URL[]{org.openide.util.Utilities.toURI(fdf).toURL()}, new URL[0]);
    FileLock lock = df.lock();
    try {
        df.rename(lock, "new_direct", "emb");
    } finally {
        lock.releaseLock();
    }
    eindexerFactory.indexer.awaitIndex();
    eindexerFactory.indexer.awaitDeleted();
    assertFalse(eindexerFactory.indexer.broken);
}
 
Example 10
Source File: SingleModulePropertiesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSimulateIllLocalizedBundlePackageRefactoring_67961() throws Exception {
    NbModuleProject p = generateStandaloneModule("module1");
    SingleModuleProperties props = loadProperties(p);
    assertEquals("display name from ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name from LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
    
    // change manifest (will fire a change event before the package is actually renamed)
    FileObject pDir = p.getProjectDirectory();
    FileObject manifestFO = pDir.getFileObject("manifest.mf");
    EditableManifest mf = Util.loadManifest(manifestFO);
    mf.setAttribute(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, "org/example/module1Renamed/resources/Bundle.properties", null);
    Util.storeManifest(manifestFO, mf);
    
    // rename package
    FileObject pkg = pDir.getFileObject("src/org/example/module1");
    FileLock lock = pkg.lock();
    pkg.rename(lock, "module1Renamed", null);
    lock.releaseLock();
    
    simulatePropertiesOpening(props, p);
    
    // make sure that properties are not damaged
    assertEquals("display name was refreshed in ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name was refreshed in LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
}
 
Example 11
Source File: SingleModulePropertiesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSimulateLocalizedBundlePackageRefactoring() throws Exception {
    NbModuleProject p = generateStandaloneModule("module1");
    SingleModuleProperties props = loadProperties(p);
    assertEquals("display name from ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name from LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
    
    // rename package
    FileObject pDir = p.getProjectDirectory();
    FileObject pkg = pDir.getFileObject("src/org/example/module1");
    FileLock lock = pkg.lock();
    pkg.rename(lock, "module1Renamed", null);
    lock.releaseLock();
    System.gc();    // no more random
    FileObject manifestFO = pDir.getFileObject("manifest.mf");

    // change manifest
    EditableManifest mf = Util.loadManifest(manifestFO);
    mf.setAttribute(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, "org/example/module1Renamed/resources/Bundle.properties", null);
    Util.storeManifest(manifestFO, mf);
    simulatePropertiesOpening(props, p);
    
    // make sure that properties are not damaged
    assertEquals("display name was refreshed in ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name was refreshed in LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
}
 
Example 12
Source File: BaseFileObjectTestHid.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCaseSensitiveRenameEventForMasterFS() throws Exception {
    FileObject parent = root.getFileObject("testdir").createFolder("parent");
    FileObject file = parent.createData("origi.nal");
    file.addFileChangeListener(new FileChangeAdapter() {
        @Override
        public void fileRenamed(FileRenameEvent fe) {
            assertEquals("origi", fe.getName());
            assertEquals("nal", fe.getExt());
        }
    });
    FileLock lock = file.lock();
    file.rename(lock, "Origi", "nal");
    lock.releaseLock();
}
 
Example 13
Source File: RepositoryUpdaterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFilesScannedAfterRenameOfFolder193243() throws Exception {
    final TestHandler handler = new TestHandler();
    final Logger logger = Logger.getLogger(RepositoryUpdater.class.getName()+".tests");
    logger.setLevel (Level.FINEST);
    logger.addHandler(handler);
    final FileObject testFo = FileUtil.createData(this.srcRootWithFiles1, "rename/A.foo");
    final MutableClassPathImplementation mcpi1 = new MutableClassPathImplementation ();
    mcpi1.addResource(this.srcRootWithFiles1);
    final ClassPath cp1 = ClassPathFactory.createClassPath(mcpi1);
    globalPathRegistry_register(SOURCES,new ClassPath[]{cp1});
    assertTrue (handler.await());
    indexerFactory.indexer.setExpectedFile(new URL[]{new URL(this.srcRootWithFiles1.toURL()+"renamed/A.foo")}, new URL[]{testFo.toURL()}, new URL[0]);
    eindexerFactory.indexer.setExpectedFile(new URL[0], new URL[]{testFo.toURL()}, new URL[0]);
    final FileObject parent = testFo.getParent();
    final FileLock lock = parent.lock();
    try {
        parent.rename(lock, "renamed", null);
    } finally {
        lock.releaseLock();
    }
    assertTrue(indexerFactory.indexer.awaitDeleted(TIME));
    assertTrue(eindexerFactory.indexer.awaitDeleted());
    assertTrue(indexerFactory.indexer.awaitIndex(TIME));
    assertTrue(eindexerFactory.indexer.awaitIndex());
    assertEquals(0, eindexerFactory.indexer.getIndexCount());
    assertEquals(1, eindexerFactory.indexer.getDeletedCount());
    assertEquals(0, eindexerFactory.indexer.getDirtyCount());
    assertEquals(1, indexerFactory.indexer.getIndexCount());
    assertEquals(1, indexerFactory.indexer.getDeletedCount());
    assertEquals(0, indexerFactory.indexer.getDirtyCount());
}
 
Example 14
Source File: InterceptorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void renameFO (File from, String newName) throws DataObjectNotFoundException, IOException {
    // need to let FS know about it
    FileObject parent = FileUtil.toFileObject(from.getParentFile());
    FileObject foFrom = FileUtil.toFileObject(from);
    assertNotNull(foFrom);
    FileLock lock = foFrom.lock();
    try {
        foFrom.rename(lock, newName, null);
    } finally {
        lock.releaseLock();
    }
}
 
Example 15
Source File: LockForFileTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLockingAndReleasingLockAfterRename_82170() throws Exception {
    File folder = new File(getWorkDir(), "a/b/c/d");
    if (!folder.exists()) {
        assertTrue(folder.mkdirs());
    }
    File file = new File(folder, "file.txt");
    if (!file.exists()) {
        assertTrue(file.createNewFile());
    }
    FileObject fileFo = FileUtil.toFileObject(file);
    assertNotNull(fileFo);

    FileLock lockForFile = LockForFile.tryLock(file);
    assertTrue(lockForFile.isValid());
    FileObject parentOfFileFo = fileFo.getParent().getParent();
    FileLock lockForParentOfFileFo = parentOfFileFo.lock();
    try {
        parentOfFileFo.rename(lockForParentOfFileFo, "renamedFile", "");//NOI18N
    } finally {
        lockForParentOfFileFo.releaseLock();
    }

    assertTrue(lockForFile.isValid());//after rename is lock still valid
    file = FileUtil.toFile(fileFo);
    assertNotNull(file);
    try {
        LockForFile.tryLock(file);
        fail();
    } catch (IOException ex) {
    //after rename is still locked and there isn't possible to get other lock
    }
    File lockFile = LockForFile.getLockFile(file);
    lockForFile.releaseLock();
    assertFalse(lockFile.exists());//lock file is deleted after releasing lock
    LockForFile.tryLock(file);//there is possible to get lock after releasing lock
}
 
Example 16
Source File: J2SEDeployProperties.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static FileObject copyBuildNativeTemplate(@NonNull final Project project) throws IOException {
    Parameters.notNull("project", project); //NOI18N
    final FileObject buildExFoBack = project.getProjectDirectory().getFileObject(String.format(
        "%s~",  //NOI18N
        EXTENSION_BUILD_SCRIPT_PATH));
    if (buildExFoBack != null) {
        closeInEditor(buildExFoBack);
        buildExFoBack.delete();
    }
    FileObject buildExFo = project.getProjectDirectory().getFileObject(EXTENSION_BUILD_SCRIPT_PATH);
    FileLock lock;
    if (buildExFo != null) {
        closeInEditor(buildExFo);
        lock = buildExFo.lock();
        try {
            buildExFo.rename(
                lock,
                buildExFo.getName(),
                String.format(
                    "%s~",  //NOI18N
                    buildExFo.getExt()));
        } finally {
            lock.releaseLock();
        }
    }
    buildExFo = FileUtil.createData(project.getProjectDirectory(), EXTENSION_BUILD_SCRIPT_PATH);
    lock = buildExFo.lock();
    try (final InputStream in = J2SEDeployProperties.class.getClassLoader().getResourceAsStream(BUILD_SCRIPT_PROTOTYPE);
         final OutputStream out = buildExFo.getOutputStream(lock)) {
        FileUtil.copy(in, out);
    } finally {
        lock.releaseLock();
    }
    return buildExFo;
}
 
Example 17
Source File: AutomaticModuleNameCompilerOptionsQueryImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testModuleInfoChanges() throws IOException {
    updateManifest(Collections.singletonMap("Automatic-Module-Name", "org.me.foo"));    //NOI18N
    assertNotNull(getManifest());
    final CompilerOptionsQueryImplementation q = new AutomaticModuleNameCompilerOptionsQueryImpl(
            project.getUpdateHelper().getAntProjectHelper(),
            project.getEvaluator(),
            srcRoots,
            ProjectProperties.MANIFEST_FILE);
    final CompilerOptionsQueryImplementation.Result r = q.getOptions(srcRoots.getRoots()[0]);
    assertNotNull(r);
    assertEquals(
            Collections.singletonList("-XDautomatic-module-name:org.me.foo"),    //NOI18N
            r.getArguments());
    FileObject modInfo = srcRoots.getRoots()[0].createData("module-info", "java");       //NOI18N
    assertTrue(r.getArguments().isEmpty());
    try (FileLock lck = modInfo.lock()) {
        modInfo.rename(lck, "module-info", "bak"); //NOI18N
    }
    assertEquals(
            Collections.singletonList("-XDautomatic-module-name:org.me.foo"),    //NOI18N
            r.getArguments());
    try (FileLock lck = modInfo.lock()) {
        modInfo.rename(lck, "module-info", "java"); //NOI18N
    }
    assertTrue(r.getArguments().isEmpty());
    modInfo.delete();
    assertEquals(
            Collections.singletonList("-XDautomatic-module-name:org.me.foo"),    //NOI18N
            r.getArguments());
}
 
Example 18
Source File: FileUtilTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Tests FileChangeListener on folder. As declared in
 * {@link FileUtil#addFileChangeListener(org.openide.filesystems.FileChangeListener, java.io.File) }
 * - fileFolderCreated event is fired when the folder is created or a child folder created
 * - fileDataCreated event is fired when a child file is created
 * - fileDeleted event is fired when the folder is deleted or a child file/folder removed
 * - fileChanged event is fired when a child file is modified
 * - fileRenamed event is fired when the folder is renamed or a child file/folder is renamed
 * - fileAttributeChanged is fired when FileObject's attribute is changed
 */
public void testAddFileChangeListenerFolder() throws IOException {
    clearWorkDir();
    // test files: dir/file1, dir/subdir/subfile
    File rootF = getWorkDir();
    File dirF = new File(rootF, "dir");
    TestFileChangeListener fcl = new TestFileChangeListener();
    FileUtil.addFileChangeListener(fcl, dirF);
    // create dir
    FileObject dirFO = FileUtil.createFolder(dirF);
    assertEquals("Event not fired when folder created.", 1, fcl.check(EventType.FOLDER_CREATED));
    FileObject subdirFO = dirFO.createFolder("subdir");
    assertEquals("Event not fired when sub folder created.", 1, fcl.check(EventType.FOLDER_CREATED));

    // create file
    FileObject file1FO = dirFO.createData("file1");
    assertEquals("Event not fired when data created.", 1, fcl.check(EventType.DATA_CREATED));
    FileObject subfileFO = subdirFO.createData("subfile");
    assertEquals("Event fired when data in sub folder created.", 0, fcl.checkAll());

    // modify
    file1FO.getOutputStream().close();
    assertEquals("fileChanged event not fired.", 1, fcl.check(EventType.CHANGED));
    subfileFO.getOutputStream().close();
    assertEquals("Event fired when file sub folder modified.", 0, fcl.checkAll());

    // delete
    file1FO.delete();
    assertEquals("Event not fired when child file deleted.", 1, fcl.check(EventType.DELETED));
    subfileFO.delete();
    assertEquals("Event fired when child file in sub folder deleted.", 0, fcl.checkAll());
    subdirFO.delete();
    assertEquals("Event not fired when sub folder deleted.", 1, fcl.check(EventType.DELETED));
    dirFO.delete();
    assertEquals("Event not fired when folder deleted.", 1, fcl.check(EventType.DELETED));

    // rename
    dirFO = FileUtil.createFolder(dirF);
    file1FO = dirFO.createData("file1");
    subdirFO = dirFO.createFolder("subdir");
    subfileFO = subdirFO.createData("subfile");
    fcl.checkAll();
    FileLock lock = file1FO.lock();
    file1FO.rename(lock, "file1Renamed", null);
    lock.releaseLock();
    assertEquals("Event not fired when child file renamed.", 1, fcl.check(EventType.RENAMED));
    lock = subfileFO.lock();
    subfileFO.rename(lock, "subfileRenamed", null);
    lock.releaseLock();
    assertEquals("Event fired when child file in sub folder renamed.", 0, fcl.check(EventType.RENAMED));
    lock = subdirFO.lock();
    subdirFO.rename(lock, "subdirRenamed", null);
    lock.releaseLock();
    assertEquals("Event not fired when sub folder renamed.", 1, fcl.check(EventType.RENAMED));
    lock = dirFO.lock();
    dirFO.rename(lock, "dirRenamed", null);
    lock.releaseLock();
    assertEquals("Event not fired when sub folder renamed.", 1, fcl.check(EventType.RENAMED));
    fcl.printAll();
    assertEquals("No other events should be fired.", 0, fcl.checkAll());
}
 
Example 19
Source File: RepositoryUpdaterTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testVisibilityQueryInFileChangeListener() throws Exception {
    final FileObject workDir = FileUtil.toFileObject(getWorkDir());
    final FileObject root = FileUtil.createFolder(workDir,"visibilitySrc"); //NOI18N
    final FileObject folder1 = FileUtil.createFolder(root,"folder");               //NOI18N
    final FileObject a = FileUtil.createData(folder1,"a.foo");                    //NOI18N
    final FileObject folder2 = FileUtil.createFolder(folder1,"invisible");          //NOI18N
    final FileObject b = FileUtil.createData(folder2,"b.foo");                    //NOI18N
    final Visibility vis = Lookup.getDefault().lookup(Visibility.class);
    assertNotNull(vis);
    vis.registerInvisibles(Collections.<FileObject>singleton(folder2));
    final ClassPath cp = ClassPathSupport.createClassPath(root.toURL());
    indexerFactory.indexer.setExpectedFile(
            new URL[]{a.toURL()},
            new URL[0],
            new URL[0]);
    globalPathRegistry_register(SOURCES, new ClassPath[]{cp});
    assertTrue(indexerFactory.indexer.awaitIndex(TIME));
    assertEquals(1, indexerFactory.indexer.getIndexCount());

    //Modify a.foo - should trigger indexer
    indexerFactory.indexer.setExpectedFile(
        new URL[]{a.toURL()},
        new URL[0],
        new URL[0]);
    touch(a.toURL());
    assertTrue(indexerFactory.indexer.awaitIndex(TIME));
    assertEquals(1, indexerFactory.indexer.getIndexCount());

    //Modify b.foo - should NOT trigger indexer
    indexerFactory.indexer.setExpectedFile(
        new URL[]{b.toURL()},
        new URL[0],
        new URL[0]);
    touch(b.toURL());
    assertFalse(indexerFactory.indexer.awaitIndex(NEGATIVE_TIME));
    assertEquals(0, indexerFactory.indexer.getIndexCount());

    //Rename a.foo - should trigger indexer
    File af = FileUtil.toFile(a);
    File anf = new File (af.getParentFile(),"an.foo");  //NOI18N
    indexerFactory.indexer.setExpectedFile(
        new URL[]{org.openide.util.Utilities.toURI(anf).toURL()},
        new URL[]{org.openide.util.Utilities.toURI(af).toURL()},
        new URL[0]);
    FileLock l = a.lock();
    try {
        a.rename(l, "an", a.getExt());  //NOI18N
    } finally {
        l.releaseLock();
    }
    assertTrue(indexerFactory.indexer.awaitIndex(TIME));
    assertTrue(indexerFactory.indexer.awaitDeleted(TIME));
    assertEquals(1, indexerFactory.indexer.getIndexCount());

    //Rename b.foo - should trigger indexer
    File bf = FileUtil.toFile(b);
    File bnf = new File (bf.getParentFile(),"bn.foo");  //NOI18N
    indexerFactory.indexer.setExpectedFile(
        new URL[]{org.openide.util.Utilities.toURI(bnf).toURL()},
        new URL[]{org.openide.util.Utilities.toURI(bf).toURL()},
        new URL[0]);
    l = b.lock();
    try {
        b.rename(l, "bn", b.getExt());  //NOI18N
    } finally {
        l.releaseLock();
    }
    assertFalse(indexerFactory.indexer.awaitIndex(NEGATIVE_TIME));
    assertEquals(0, indexerFactory.indexer.getIndexCount());

    //Delete a.foo - should trigger indexed
    indexerFactory.indexer.setExpectedFile(
        new URL[0],
        new URL[]{a.toURL()},
        new URL[0]);
    a.delete();
    assertTrue(indexerFactory.indexer.awaitDeleted(TIME));

    //Delete b.foo - should NOT trigger indexed
    indexerFactory.indexer.setExpectedFile(
        new URL[0],
        new URL[]{b.toURL()},
        new URL[0]);
    b.delete();
    assertFalse(indexerFactory.indexer.awaitDeleted(NEGATIVE_TIME));
}
 
Example 20
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRename() throws Exception {
   File f = testFile;

   final List<FileEvent> l = new ArrayList<FileEvent>();
   FileChangeListener fcl = new FileChangeAdapter () {
       @Override
        public void fileRenamed(FileRenameEvent fe) {
            FileObject fold = fe.getFile();
            final FileObject[] ch = fold.getChildren();
            assertTrue("There should be some children in " + fold, ch.length > 0);
            l.add(fe);
        }
   };
   
   FileObject fo = FileBasedFileSystem.getFileObject(f);
   assertNotNull(fo);
   FileObject folder =fo.createFolder("testRename");
   assertNotNull(folder);
   
   FileObject file =folder.createData("test.txt");
   assertNotNull(file);
   folder.addFileChangeListener(fcl);
   assertTrue(folder.getChildren().length > 0);
   FileLock lock = folder.lock();
   try {
       folder.rename(lock,"renfolder","");            
       assertTrue(folder.getChildren().length > 0);
       assertTrue(!l.isEmpty());            
       
       l.clear();
       
       folder.rename(lock,"testRename","");            
       assertTrue(folder.getChildren().length > 0);
       assertTrue(!l.isEmpty());            
       
   } finally {
       lock.releaseLock();
   }
   
   
}