view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java @ 16:7aa397165fa0

identify command added, test connection now uses identify command
author Pavel.Sher
date Wed, 16 Jul 2008 01:26:07 +0400
parents 26505742bae5
children c76d6a2b27f6
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.TempFiles;
import jetbrains.buildServer.serverSide.ServerPaths;
import jetbrains.buildServer.vcs.*;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import jetbrains.buildServer.vcs.patches.PatchBuilderImpl;
import jetbrains.buildServer.vcs.patches.PatchTestCase;
import org.jmock.Mock;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Test
public class MercurialVcsSupportTest extends PatchTestCase {
  private MercurialVcsSupport myVcs;
  private TempFiles myTempFiles;

  @BeforeMethod
  protected void setUp() throws Exception {
    super.setUp();
    Mock vcsManagerMock = new Mock(VcsManager.class);
    vcsManagerMock.stubs().method("registerVcsSupport");

    myTempFiles = new TempFiles();
    File systemDir = myTempFiles.createTempDir();
    ServerPaths sp = new ServerPaths(systemDir.getAbsolutePath(), systemDir.getAbsolutePath());
    assertTrue(new File(sp.getCachesDir()).mkdirs());
    myVcs = new MercurialVcsSupport((VcsManager)vcsManagerMock.proxy(), sp);
  }

  @AfterMethod
  protected void tearDown() {
    myTempFiles.cleanup();
  }

  protected String getTestDataPath() {
    return "mercurial-tests/testData";
  }

  public void testGetCurrentVersion() throws Exception {
    VcsRootImpl vcsRoot = createVcsRoot();

    assertEquals("5:1d2cc6f3bc29", myVcs.getCurrentVersion(vcsRoot));
  }

  public void testCollectChanges() throws Exception {
    VcsRootImpl vcsRoot = createVcsRoot();

    List<ModificationData> changes = myVcs.collectBuildChanges(vcsRoot, "0:9875b412a788", "3:9522278aa38d", new CheckoutRules(""));
    assertEquals(3, changes.size());

    ModificationData md1 = changes.get(0);
    ModificationData md2 = changes.get(1);
    ModificationData md3 = changes.get(2);
    assertEquals("1:1d446e82d356", md1.getVersion());
    assertEquals("new file added", md1.getDescription());
    List<VcsChange> files1 = md1.getChanges();
    assertEquals(1, files1.size());
    assertEquals(VcsChangeInfo.Type.ADDED, files1.get(0).getType());
    assertEquals("dir1/file3.txt", normalizePath(files1.get(0).getRelativeFileName()));

    assertEquals("2:7209b1f1d793", md2.getVersion());
    assertEquals("file4.txt added", md2.getDescription());
    List<VcsChange> files2 = md2.getChanges();
    assertEquals(1, files2.size());
    assertEquals(VcsChangeInfo.Type.ADDED, files2.get(0).getType());
    assertEquals("dir1/file4.txt", normalizePath(files2.get(0).getRelativeFileName()));

    assertEquals("3:9522278aa38d", md3.getVersion());
    assertEquals("file removed", md3.getDescription());
    List<VcsChange> files3 = md3.getChanges();
    assertEquals(1, files3.size());
    assertEquals(VcsChangeInfo.Type.REMOVED, files3.get(0).getType());
    assertEquals("dir1/file4.txt", normalizePath(files3.get(0).getRelativeFileName()));
  }

  public void testCollectChangesWithCheckoutRules() throws Exception {
    VcsRootImpl vcsRoot = createVcsRoot();

    List<ModificationData> changes = myVcs.collectBuildChanges(vcsRoot, "0:9875b412a788", "3:9522278aa38d", new CheckoutRules("-:.\n+:dir1/subdir"));
    assertEquals(0, changes.size());

    changes = myVcs.collectBuildChanges(vcsRoot, "0:9875b412a788", "5:1d2cc6f3bc29", new CheckoutRules("-:.\n+:dir1/subdir"));
    assertEquals(1, changes.size());
    ModificationData md = changes.get(0);
    assertEquals("modified in subdir", md.getDescription());
  }

  public void testBuildFullPatch() throws IOException, VcsException {
    setName("cleanPatch1");
    VcsRootImpl vcsRoot = createVcsRoot();

    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final PatchBuilderImpl builder = new PatchBuilderImpl(output);

    myVcs.buildPatch(vcsRoot, null, "4:b06a290a363b", builder, new CheckoutRules(""));
    builder.close();

    checkPatchResult(output.toByteArray());
  }

  public void testBuildIncrementalPatch() throws IOException, VcsException {
    setName("patch1");
    VcsRootImpl vcsRoot = createVcsRoot();

    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final PatchBuilderImpl builder = new PatchBuilderImpl(output);

    myVcs.buildPatch(vcsRoot, "3:9522278aa38d", "4:b06a290a363b", builder, new CheckoutRules(""));
    builder.close();

    checkPatchResult(output.toByteArray());
  }

  public void testGetContent() throws IOException, VcsException {
    VcsRootImpl vcsRoot = createVcsRoot();

    byte[] content = myVcs.getContent("dir1/subdir/file2.txt", vcsRoot, "4:b06a290a363b");
    assertEquals("bbb", new String(content));
    content = myVcs.getContent("dir1/subdir/file2.txt", vcsRoot, "5:1d2cc6f3bc29");
    assertEquals("modified\r\nbbb", new String(content));
  }

  public void testTestConnection() throws IOException, VcsException {
    VcsRootImpl vcsRoot = createVcsRoot();

    System.out.println(myVcs.testConnection(vcsRoot));

    vcsRoot.addProperty(Constants.REPOSITORY_PROP, "/some/non/existent/path");
    try {
      myVcs.testConnection(vcsRoot);
      fail("Exception expected");
    } catch (VcsException e) {
    }
  }

  private Object normalizePath(final String path) {
    return path.replace(File.separatorChar, '/');
  }

  private VcsRootImpl createVcsRoot() throws IOException {
    VcsRootImpl vcsRoot = new VcsRootImpl(1, myVcs.getName());
    vcsRoot.addProperty(Constants.HG_COMMAND_PATH_PROP, new File("mercurial-tests/testData/bin/hg.exe").getAbsolutePath());
    File repository = LocalRepositoryUtil.prepareRepository(new File("mercurial-tests/testData/rep1").getAbsolutePath());
    vcsRoot.addProperty(Constants.REPOSITORY_PROP, repository.getAbsolutePath());
    return vcsRoot;
  }
}