[Mercurial Tests] -HgCatTestCase (reason: testing 'hg') +HgHistoryTestCase
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Tue, 22 Jun 2010 09:34:38 +0000 (13:34 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Tue, 22 Jun 2010 09:34:38 +0000 (13:34 +0400)
plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgCatTestCase.java [deleted file]
plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgHistoryTestCase.java [new file with mode: 0644]

diff --git a/plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgCatTestCase.java b/plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgCatTestCase.java
deleted file mode 100644 (file)
index 4d459ec..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-package org.zmlx.hg4idea;
-
-import org.testng.annotations.Test;
-import org.zmlx.hg4idea.command.HgCatCommand;
-
-import java.nio.charset.Charset;
-
-import static org.testng.Assert.assertEquals;
-
-public class HgCatTestCase extends AbstractHgTestCase {
-  @Test
-  public void testCatCurrentRevision() throws Exception {
-    fillFile(myProjectRepo, new String[]{"file.txt"}, "initial contents");
-    runHgOnProjectRepo("add", ".");
-    runHgOnProjectRepo("commit", "-m", "initial contents");
-
-    HgCatCommand command = new HgCatCommand(myProject);
-    String content = command.execute(getHgFile("file.txt"), HgRevisionNumber.getLocalInstance("0"), Charset.defaultCharset());
-    assertEquals(content, "initial contents");
-  }
-
-
-  @Test
-  public void testCatPastRevision() throws Exception {
-    fillFile(myProjectRepo, new String[]{"file.txt"}, "initial contents");
-    runHgOnProjectRepo("add", ".");
-    runHgOnProjectRepo("commit", "-m", "initial contents");
-
-    runHgOnProjectRepo("rename", "file.txt", "file2.txt");
-    runHgOnProjectRepo("commit", "-m", "file renamed");
-
-    fillFile(myProjectRepo, new String[]{"file2.txt"}, "updated contents");
-    runHgOnProjectRepo("commit", "-m", "updated contents");
-
-    HgCatCommand command = new HgCatCommand(myProject);
-    String content = command.execute(getHgFile("file.txt"), HgRevisionNumber.getLocalInstance("0"), Charset.defaultCharset());
-    assertEquals(content, "initial contents");
-  }
-
-
-  @Test
-  public void testCatTrackFileNames() throws Exception {
-    fillFile(myProjectRepo, new String[]{"file.txt"}, "initial contents");
-    runHgOnProjectRepo("add", ".");
-    runHgOnProjectRepo("commit", "-m", "initial contents");
-
-    runHgOnProjectRepo("rename", "file.txt", "file2.txt");
-    runHgOnProjectRepo("commit", "-m", "file renamed");
-
-    fillFile(myProjectRepo, new String[]{"file2.txt"}, "updated contents");
-    runHgOnProjectRepo("commit", "-m", "updated contents");
-
-    HgCatCommand command = new HgCatCommand(myProject);
-    String content = command.execute(getHgFile("file2.txt"), HgRevisionNumber.getLocalInstance("0"), Charset.defaultCharset());
-    assertEquals(content, "initial contents");
-  }
-}
diff --git a/plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgHistoryTestCase.java b/plugins/hg4idea/testSrc/org/zmlx/hg4idea/HgHistoryTestCase.java
new file mode 100644 (file)
index 0000000..1cfc4ac
--- /dev/null
@@ -0,0 +1,93 @@
+package org.zmlx.hg4idea;
+
+import com.intellij.openapi.vcs.VcsException;
+import com.intellij.openapi.vcs.history.VcsFileRevision;
+import com.intellij.openapi.vcs.history.VcsHistorySession;
+import com.intellij.vcsUtil.VcsUtil;
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.List;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * HgHistoryTestCase tests retrieving file history and specific revisions.
+ */
+public class HgHistoryTestCase extends AbstractHgTestCase {
+
+  /**
+   * 1. Make two versions of a file (create, add, commit, modify, commit).
+   * 2. Get the revisions history.
+   * 3. Verify versions' contents and the current version.
+   */
+  @Test
+  public void testCurrentAndPreviousRevisions() throws Exception {
+    int versions = 0;
+    fillFile(myProjectRepo, new String[]{ AFILE }, FILE_CONTENT);
+    addAll();
+    commitAll("initial content");
+    versions++;
+    fillFile(myProjectRepo, new String[] { AFILE} , FILE_CONTENT_2);
+    commitAll("updated content");
+    versions++;
+
+    final VcsHistorySession session = getHistorySession(AFILE);
+    final List<VcsFileRevision> revisions = session.getRevisionList();
+    for (VcsFileRevision rev : revisions) {
+      rev.loadContent();
+    }
+
+    assertEquals(revisions.size(), versions);
+    assertTrue(session.isCurrentRevision(revisions.get(0).getRevisionNumber()));
+    assertEquals(revisions.get(0).getContent(), FILE_CONTENT_2.getBytes());
+    assertEquals(revisions.get(1).getContent(), FILE_CONTENT.getBytes());
+  }
+
+  /**
+   * 1. Make initial version of a file (create, add, commit).
+   * 2. Rename file (rename, commit).
+   * 3. Update file (modify, commit).
+   * 4. Get the file history.
+   * 5. Verify revision contents and the current revision.
+   */
+  @Test
+  public void renameShouldPreserveFileHistory() throws Exception {
+    int versions = 0;
+
+    fillFile(myProjectRepo, new String[]{ AFILE }, FILE_CONTENT);
+    addAll();
+    commitAll("initial content");
+    versions++;
+
+    runHgOnProjectRepo("rename", AFILE, BFILE);
+    commitAll("file renamed");
+    versions++;
+
+    fillFile(myProjectRepo, new String[]{ BFILE }, FILE_CONTENT_2);
+    commitAll("updated content");
+    versions++;
+
+    final VcsHistorySession session = getHistorySession(BFILE);
+    final List<VcsFileRevision> revisions = session.getRevisionList();
+    loadAllRevisions(revisions);
+
+    assertEquals(revisions.size(), versions);
+    assertTrue(session.isCurrentRevision(revisions.get(0).getRevisionNumber()));
+    assertEquals(revisions.get(0).getContent(), FILE_CONTENT_2.getBytes());
+    assertEquals(revisions.get(2).getContent(), FILE_CONTENT.getBytes());
+  }
+
+  private void loadAllRevisions(Collection<VcsFileRevision> revisions) throws Exception {
+    for (VcsFileRevision rev : revisions) {
+      rev.loadContent();
+    }
+  }
+
+  private VcsHistorySession getHistorySession(String relativePath) throws VcsException {
+    return HgVcs.getInstance(myProject).getVcsHistoryProvider().createSessionFor(VcsUtil.getFilePath(new File(myProjectRepo, relativePath), false));
+  }
+
+}