Search in sources :

Example 1 with Path

use of org.eclipse.che.api.vfs.Path in project che by eclipse.

the class LocalVirtualFileTest method failsDeleteLockedFileWhenLockTokenIsInvalid.

@Test
public void failsDeleteLockedFileWhenLockTokenIsInvalid() throws Exception {
    VirtualFile root = getRoot();
    VirtualFile folder = root.createFolder(generateFolderName());
    VirtualFile file = folder.createFile(generateFileName(), DEFAULT_CONTENT);
    file.setProperty("property1", "value1");
    Path filePath = file.getPath();
    String invalidLockToken = invalidateLockToken(file.lock(0));
    try {
        file.delete(invalidLockToken);
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException e) {
        assertionHelper.assertThatIoFileHasContent(filePath, DEFAULT_CONTENT_BYTES);
        assertionHelper.assertThatMetadataIoFileHasContent(file.getPath(), serializeVirtualFileMetadata(ImmutableMap.of("property1", "value1")));
        assertionHelper.assertThatLockIoFileExists(filePath);
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Test(org.junit.Test)

Example 2 with Path

use of org.eclipse.che.api.vfs.Path in project che by eclipse.

the class LocalVirtualFileTest method failsMoveLockedFileWithoutLockToken.

@Test
public void failsMoveLockedFileWithoutLockToken() throws Exception {
    VirtualFile file = getRoot().createFile(generateFileName(), DEFAULT_CONTENT);
    file.setProperty("property1", "value1");
    Path filePath = file.getPath();
    file.lock(0);
    VirtualFile targetFolder = getRoot().createFolder(generateFolderName());
    Path movedFilePath = targetFolder.getPath().newPath(file.getName());
    try {
        file.moveTo(targetFolder);
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException e) {
        assertionHelper.assertThatIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatLockIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatMetadataIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatIoFileHasContent(filePath, DEFAULT_CONTENT_BYTES);
        assertionHelper.assertThatMetadataIoFileHasContent(filePath, serializeVirtualFileMetadata(ImmutableMap.of("property1", "value1")));
        assertionHelper.assertThatLockIoFileExists(filePath);
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Test(org.junit.Test)

Example 3 with Path

use of org.eclipse.che.api.vfs.Path in project che by eclipse.

the class LocalVirtualFileTest method movesFolderUnderNewNameAndReplaceExistedItem.

@Test
public void movesFolderUnderNewNameAndReplaceExistedItem() throws Exception {
    VirtualFile folder = getRoot().createFolder(generateFolderName());
    VirtualFile file = folder.createFile(generateFileName(), DEFAULT_CONTENT);
    Path folderPath = folder.getPath();
    VirtualFile targetFolder = getRoot().createFolder(generateFolderName());
    targetFolder.createFolder("new_name");
    VirtualFile movedFolder = folder.moveTo(targetFolder, "new_name", true, null);
    VirtualFile movedFile = movedFolder.getChild(Path.of(file.getName()));
    assertionHelper.assertThatIoFileExists(movedFolder.getPath());
    assertionHelper.assertThatIoFileHasContent(movedFile.getPath(), DEFAULT_CONTENT_BYTES);
    assertionHelper.assertThatIoFileDoesNotExist(folderPath);
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) Test(org.junit.Test)

Example 4 with Path

use of org.eclipse.che.api.vfs.Path in project che by eclipse.

the class LocalVirtualFileTest method deletesFile.

@Test
public void deletesFile() throws Exception {
    VirtualFile folder = getRoot().createFolder(generateFolderName());
    VirtualFile file = folder.createFile(generateFileName(), DEFAULT_CONTENT);
    file.setProperty("property1", "value1");
    Path filePath = file.getPath();
    file.delete();
    assertionHelper.assertThatIoFileDoesNotExist(filePath);
    assertionHelper.assertThatMetadataIoFileDoesNotExist(filePath);
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) Test(org.junit.Test)

Example 5 with Path

use of org.eclipse.che.api.vfs.Path in project che by eclipse.

the class MemoryVirtualFile method moveTo.

@Override
public VirtualFile moveTo(VirtualFile parent, String newName, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    MemoryVirtualFile memoryParent = (MemoryVirtualFile) parent;
    memoryParent.checkExistence();
    if (isRoot()) {
        throw new ForbiddenException("Unable move root folder");
    }
    if (!parent.isFolder()) {
        throw new ForbiddenException("Unable move item. Item specified as parent is not a folder");
    }
    if (newName == null || newName.trim().isEmpty()) {
        newName = this.getName();
    }
    final boolean isFile = isFile();
    final Path myPath = getPath();
    final Path newParentPath = parent.getPath();
    final boolean folder = isFolder();
    if (folder) {
        if (newParentPath.isChild(myPath)) {
            throw new ForbiddenException(String.format("Unable move item %s to %s. Item may not have itself as parent", myPath, newParentPath));
        }
        final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable move item '%s'. Child items '%s' are locked", getName(), lockedFiles));
        }
    } else if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
        throw new ForbiddenException(String.format("Unable move item %s. Item is locked", myPath));
    }
    if (overwrite) {
        MemoryVirtualFile existedItem = memoryParent.children.get(newName);
        if (existedItem != null) {
            existedItem.delete();
        }
    }
    if (memoryParent.children.containsKey(newName)) {
        throw new ConflictException(String.format("Item '%s' already exists", parent.getPath().newPath(newName)));
    }
    this.parent.children.remove(name);
    memoryParent.children.put(newName, this);
    this.parent = memoryParent;
    this.name = newName;
    lock = null;
    deleteFromSearcher(myPath, isFile);
    addInSearcher(this);
    return this;
}
Also used : Path(org.eclipse.che.api.vfs.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ConflictException(org.eclipse.che.api.core.ConflictException)

Aggregations

Path (org.eclipse.che.api.vfs.Path)72 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)66 Test (org.junit.Test)50 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)30 ConflictException (org.eclipse.che.api.core.ConflictException)13 File (java.io.File)10 ServerException (org.eclipse.che.api.core.ServerException)6 LockedFileFinder (org.eclipse.che.api.vfs.LockedFileFinder)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 NewProjectConfig (org.eclipse.che.api.core.model.project.NewProjectConfig)2 ProjectConfig (org.eclipse.che.api.core.model.project.ProjectConfig)2 CreateProjectHandler (org.eclipse.che.api.project.server.handlers.CreateProjectHandler)2