public void testCopyToPointDir() throws Exception {
    File top = createTempDirectory(false);
    File sub = IoTestUtil.createTestDir(top, "sub");
    File file = IoTestUtil.createTestFile(top, "file.txt", "hi there");

    LocalFileSystem lfs = LocalFileSystem.getInstance();
    VirtualFile topDir = lfs.refreshAndFindFileByIoFile(top);
    assertNotNull(topDir);
    VirtualFile sourceFile = lfs.refreshAndFindFileByIoFile(file);
    assertNotNull(sourceFile);
    VirtualFile parentDir = lfs.refreshAndFindFileByIoFile(sub);
    assertNotNull(parentDir);
    assertEquals(2, topDir.getChildren().length);

    try {
      sourceFile.copy(this, parentDir, ".");
      fail("Copying a file into a '.' path should have failed");
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }

    topDir.refresh(false, true);
    assertTrue(topDir.exists());
    assertEquals(2, topDir.getChildren().length);
  }
Ejemplo n.º 2
0
 private void paste(@NotNull CopyPasteFilesData data, @NotNull VirtualFile basedir) {
   for (VirtualFile f : data.getFiles()) {
     try {
       if (!FileTypeManager.getInstance().isFileIgnored(f.getName())) {
         if (!data.isCut()) {
           f.copy(this, basedir, f.getName());
         } else {
           f.move(this, basedir);
         }
       }
     } catch (IOException e) {
       LOG.error("Error while pasting " + f + "\n", e);
     }
   }
 }
  public void testCopyDir() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile dirToCopy = fromVDir.createChildDirectory(this, "dir");
    final VirtualFile file = dirToCopy.createChildData(this, "temp_file");
    file.setBinaryContent(new byte[] {0, 1, 2, 3});
    final String newName = "dir";
    final VirtualFile dirCopy = dirToCopy.copy(this, toVDir, newName);
    assertEquals(newName, dirCopy.getName());
    PlatformTestUtil.assertDirectoriesEqual(toVDir, fromVDir);
  }
  public void testCopyFile() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile fileToCopy = fromVDir.createChildData(this, "temp_file");
    final byte[] byteContent = {0, 1, 2, 3};
    fileToCopy.setBinaryContent(byteContent);
    final String newName = "new_temp_file";
    final VirtualFile copy = fileToCopy.copy(this, toVDir, newName);
    assertEquals(newName, copy.getName());
    assertTrue(Arrays.equals(byteContent, copy.contentsToByteArray()));
  }
  private static VirtualFile getCopyWithAnswers(
      @NotNull final VirtualFile taskDir,
      @NotNull final VirtualFile file,
      @NotNull final TaskFile source,
      @NotNull final TaskFile target) {
    VirtualFile copy = null;
    try {

      copy =
          file.copy(
              taskDir,
              taskDir,
              file.getNameWithoutExtension()
                  + EduNames.ANSWERS_POSTFIX
                  + "."
                  + file.getExtension());
      final FileDocumentManager documentManager = FileDocumentManager.getInstance();
      final Document document = documentManager.getDocument(copy);
      if (document != null) {
        TaskFile.copy(source, target);
        EduDocumentListener listener = new EduDocumentListener(target);
        document.addDocumentListener(listener);
        for (AnswerPlaceholder answerPlaceholder : target.getAnswerPlaceholders()) {
          final int start = answerPlaceholder.getOffset();
          final int end = start + answerPlaceholder.getRealLength();
          final String text = answerPlaceholder.getPossibleAnswer();
          document.replaceString(start, end, text);
        }
        ApplicationManager.getApplication()
            .runWriteAction(() -> documentManager.saveDocument(document));
      }
    } catch (IOException e) {
      LOG.error(e);
    }
    return copy;
  }