public void testParentAndChildRename() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {123}, -1, 4000);

    getVcs().beginChangeSet();
    dir.rename(this, "dir2");
    f.rename(this, "bar.txt");
    getVcs().endChangeSet(null);

    revertLastChange();

    assertNull(myRoot.findChild("dir2"));
    dir = myRoot.findChild("dir");

    assertNull(dir.findChild("bar.txt"));
    f = dir.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(123, f.contentsToByteArray()[0]);
    assertEquals(4000, f.getTimeStamp());
  }
  public void testRename() throws Exception {
    VirtualFile f = myRoot.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {123}, -1, 4000);
    f.rename(this, "bar.txt");

    revertLastChange();

    assertNull(myRoot.findChild("bar.txt"));
    f = myRoot.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(123, f.contentsToByteArray()[0]);
    assertEquals(4000, f.getTimeStamp());
  }
  public void testRevertingRenameFromOldRevisionsWhenDirDoesNotExists() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");

    f.rename(this, "bar.txt");

    dir.delete(this);

    revertChange(1);

    dir = myRoot.findChild("dir");
    assertNotNull(dir);
    assertNotNull(dir.findChild("foo.txt"));
    assertNull(dir.findChild("bar.txt"));
  }
  public void testContentChangeWhenDirectoryExists() throws Exception {
    VirtualFile f = myRoot.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {1}, -1, 1000);

    getVcs().beginChangeSet();
    f.rename(this, "bar.txt");
    f.setBinaryContent(new byte[] {2}, -1, 2000);
    getVcs().endChangeSet(null);

    myRoot.createChildDirectory(this, "foo.txt");

    revertChange(1, 0, 1);

    assertNull(myRoot.findChild("bar.txt"));
    f = myRoot.findChild("foo.txt");
    assertNotNull(f);
    assertFalse(f.isDirectory());
    assertEquals(1, f.contentsToByteArray()[0]);
    assertEquals(1000, f.getTimeStamp());
  }
 @Nullable
 public static VirtualFile findPatchTarget(
     final ApplyPatchContext context,
     final String beforeName,
     final String afterName,
     final boolean isNewFile)
     throws IOException {
   VirtualFile file = null;
   if (beforeName != null) {
     file = findFileToPatchByName(context, beforeName, isNewFile);
   }
   if (file == null) {
     file = findFileToPatchByName(context, afterName, isNewFile);
   } else if (context.isAllowRename() && afterName != null && !beforeName.equals(afterName)) {
     String[] beforeNameComponents = beforeName.split("/");
     String[] afterNameComponents = afterName.split("/");
     if (!beforeNameComponents[beforeNameComponents.length - 1].equals(
         afterNameComponents[afterNameComponents.length - 1])) {
       context.registerBeforeRename(file);
       file.rename(FilePatch.class, afterNameComponents[afterNameComponents.length - 1]);
       context.addAffectedFile(file);
     }
     boolean needMove = (beforeNameComponents.length != afterNameComponents.length);
     if (!needMove) {
       needMove = checkPackageRename(context, beforeNameComponents, afterNameComponents);
     }
     if (needMove) {
       VirtualFile moveTarget =
           findFileToPatchByComponents(
               context, afterNameComponents, afterNameComponents.length - 1);
       if (moveTarget == null) {
         return null;
       }
       context.registerBeforeRename(file);
       file.move(FilePatch.class, moveTarget);
       context.addAffectedFile(file);
     }
   }
   return file;
 }